1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-08 16:55:38 +02:00

created ANT build file to run LPG generator, moved grammar files to separate folder

This commit is contained in:
Mike Kucera 2008-01-30 21:48:38 +00:00
parent 0aa860ee97
commit bf2aafaf4b
8 changed files with 284 additions and 2909 deletions

View file

@ -0,0 +1,71 @@
<!--
Copyright (c) 2006, 2008 IBM Corporation and others.
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License v1.0
which accompanies this distribution, and is available at
http://www.eclipse.org/legal/epl-v10.html
Contributors:
IBM Corporation - initial API and implementation
-->
<project name="CDT Extensible LR Parser Framework" default="both" basedir=".">
<description>Generates LPG parsers from grammar files</description>
<fail unless="lpg_exe">
Property $${lpg_exe} not set.
This property must be set to the full path to the LPG generator executable.
</fail>
<fail unless="lpg_template">
Property $${lpg_template} not set.
This property must be set to the full path to the LPG templates folder.
</fail>
<target name="both" depends="cpp, c99">
<description>Generates the C99 and C++ parsers</description>
<echo message="Done"/>
</target>
<target name="cpp">
<description>Generate the C++ parser</description>
<antcall target="generate">
<param name="grammar_dir" value="cpp"/>
<param name="grammar_name" value="CPPParser"/>
<param name="output_dir" value="org/eclipse/cdt/internal/core/dom/lrparser/cpp"/>
</antcall>
</target>
<target name="c99">
<description>Generate the C99 parser</description>
<antcall target="generate">
<param name="grammar_dir" value="c99"/>
<param name="grammar_name" value="C99Parser"/>
<param name="output_dir" value="org/eclipse/cdt/internal/core/dom/lrparser/c99"/>
</antcall>
</target>
<target name="generate">
<property name="grammar_file" value="${grammar_dir}/${grammar_name}.g"/>
<echo message="lpg_exe=${lpg_exe}"/>
<echo message="lpg_template=${lpg_template}"/>
<echo message="grammar_file=${grammar_file}.g"/>
<echo message="output_dir=${output_dir}"/>
<exec executable="${lpg_exe}">
<arg value="${grammar_file}"/>
<env key="LPG_TEMPLATE" path="${lpg_template}"/>
</exec>
<move overwrite="true" toDir="../src/${output_dir}">
<fileset dir=".">
<include name="${grammar_name}*.*"/>
</fileset>
</move>
</target>
</project>

View file

@ -13,6 +13,9 @@
%options package=org.eclipse.cdt.internal.core.dom.lrparser.c99
%options template=TrialUndoParserTemplate.g
--$Include
--../common.g
--$End
$Notice
-- Copied into all files generated by LPG
@ -31,8 +34,6 @@ $Notice
./
$End
$Terminals
-- Keywords
@ -130,20 +131,37 @@ $Globals
./
$End
-- TODO move this code into a common template
$Define
-- These macros allow the template and header code to be customized by an extending parser.
$ast_class /.Object./
$additional_interfaces /. ./
$build_action_class /. C99BuildASTParserAction ./
$resolve_action_class /. C99TypedefTrackerParserAction ./
$node_factory_create_expression /. C99ASTNodeFactory.DEFAULT_INSTANCE ./
$sym_class /. C99Parsersym ./
$lexer_class /. C99Lexer ./
$action_class /. C99ParserAction ./
$End
$Define
-- These macros allow the template and header code to be customized by an extending parser.
$ast_class /.Object./
$data_class /. Object ./ -- allow anything to be passed between actions
--$additional_interfaces /. , IParserActionTokenProvider, IParser ./
$additional_interfaces /. ./
--$build_action_class /. ./
--$resolve_action_class /. ./
--$node_factory_create_expression /. ./
--$lexer_class /. ./
--$action_class /. ./
$UndoResolver /.$Undo action.resolver.undo(); $EndUndo./
$Resolve /. $BeginTrial $resolve.
@ -162,6 +180,15 @@ $Define
$resolve /. action.resolver./
$builder /. action.builder./
-- comment out when using trial/undo
--$Action /. $BeginAction ./
--$BeginFinal /. ./
--$EndFinal /. ./
--$BeginTrial /. ./
--$EndTrial /. ./
--$Undo /. ./
--$EndUndo /. ./
$End
@ -169,12 +196,15 @@ $Headers
/.
private $action_class action;
//public $action_type() { // constructor
//}
private void initActions(IASTTranslationUnit tu) {
// binding resolution actions need access to IASTName nodes, temporary
action = new $action_class();
action.resolver = new $resolve_action_class(this);
action.builder = new $build_action_class($node_factory_create_expression, this, tu);
action.builder.setTokenMap(C99Parsersym.orderedTerminalSymbols);
action.builder.setTokenMap($sym_class.orderedTerminalSymbols);
setParserAction(action);
}
@ -184,6 +214,12 @@ $Headers
super.addToken(token);
}
public void setTokens(List<IToken> tokens) {
resetTokenStream();
for(IToken token : tokens) {
addToken(token);
}
}
public IASTCompletionNode parse(IASTTranslationUnit tu) {
// this has to be done, or... kaboom!
@ -206,8 +242,8 @@ $Headers
public int getKind(int i) {
int kind = super.getKind(i);
// lexer feedback hack!
if(kind == C99Parsersym.TK_identifier && action.resolver.isTypedef(getTokenText(i))) {
kind = C99Parsersym.TK_TypedefName;
if(kind == $sym_class.TK_identifier && action.resolver.isTypedef(getTokenText(i))) {
kind = $sym_class.TK_TypedefName;
}
return kind;
}
@ -215,7 +251,6 @@ $Headers
./
$End
$Start
translation_unit
$End

View file

@ -0,0 +1,135 @@
-----------------------------------------------------------------------------------
-- Copyright (c) 2006, 2008 IBM Corporation and others.
-- All rights reserved. This program and the accompanying materials
-- are made available under the terms of the Eclipse Public License v1.0
-- which accompanies this distribution, and is available at
-- http://www.eclipse.org/legal/epl-v10.html
--
-- Contributors:
-- IBM Corporation - initial API and implementation
-----------------------------------------------------------------------------------
$Notice
-- Copied into all files generated by LPG
/./*******************************************************************************
* Copyright (c) 2006, 2008 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*********************************************************************************/
// This file was generated by LPG
./
$End
$Define
-- These macros allow the template and header code to be customized by an extending parser.
$ast_class /.Object./
$data_class /. Object ./ -- allow anything to be passed between actions
--$additional_interfaces /. , IParserActionTokenProvider, IParser ./
$additional_interfaces /. ./
$build_action_class /. ./
$resolve_action_class /. ./
$node_factory_create_expression /. ./
$lexer_class /. ./
$action_class /. ./
$UndoResolver /.$Undo action.resolver.undo(); $EndUndo./
$Resolve /. $BeginTrial $resolve.
./
$EndResolve /. $EndTrial
$UndoResolver
./ -- undo actions are automatically generated for binding resolution actions
$Builder /. $BeginFinal $builder.
./
$EndBuilder /. /*$builder.getASTStack().print();*/ $EndFinal ./
$Build /. $Action $Builder ./
$EndBuild /. $EndBuilder $EndAction ./
$resolve /. action.resolver./
$builder /. action.builder./
-- comment out when using trial/undo
--$Action /. $BeginAction ./
--$BeginFinal /. ./
--$EndFinal /. ./
--$BeginTrial /. ./
--$EndTrial /. ./
--$Undo /. ./
--$EndUndo /. ./
$End
$Headers
/.
private $action_class action;
//public $action_type() { // constructor
//}
private void initActions(IASTTranslationUnit tu) {
// binding resolution actions need access to IASTName nodes, temporary
action = new $action_class();
action.resolver = new $resolve_action_class(this);
action.builder = new $build_action_class($node_factory_create_expression, this, tu);
action.builder.setTokenMap($sym_class.orderedTerminalSymbols);
//setParserAction(action);
}
public void addToken(IToken token) {
token.setKind(mapKind(token.getKind()));
super.addToken(token);
}
public void setTokens(List<IToken> tokens) {
resetTokenStream();
for(IToken token : tokens) {
addToken(token);
}
}
public IASTCompletionNode parse(IASTTranslationUnit tu) {
// this has to be done, or... kaboom!
setStreamLength(getSize());
initActions(tu);
final int errorRepairCount = -1; // -1 means full error handling
parser(null, errorRepairCount); // do the actual parse
super.resetTokenStream(); // allow tokens to be garbage collected
// the completion node may be null
IASTCompletionNode compNode = action.builder.getASTCompletionNode();
action = null;
parserAction = null;
return compNode;
}
public int getKind(int i) {
int kind = super.getKind(i);
// lexer feedback hack!
//if(kind == $sym_class.TK_identifier && action.resolver.isTypedef(getTokenText(i))) {
// kind = $sym_class.TK_TypedefName;
//}
return kind;
}
./
$End

View file

@ -188,7 +188,8 @@ $Headers
private $action_class action;
// uncomment to use with backtracking parser
public CPPParser() {}
public $action_type() { // constructor
}
private void initActions(IASTTranslationUnit tu) {
// binding resolution actions need access to IASTName nodes, temporary
@ -208,6 +209,13 @@ $Headers
}
public void setTokens(List<IToken> tokens) {
resetTokenStream();
for(IToken token : tokens) {
addToken(token);
}
}
public IASTCompletionNode parse(IASTTranslationUnit tu) {
// this has to be done, or... kaboom!
setStreamLength(getSize());

View file

@ -29,7 +29,7 @@ import org.eclipse.cdt.core.dom.lrparser.action.c99.C99BuildASTParserAction;
import org.eclipse.cdt.core.dom.lrparser.action.c99.C99TypedefTrackerParserAction;
import org.eclipse.cdt.core.dom.lrparser.util.DebugUtil;
public class C99Parser extends AbstractTrialUndoActionProvider< C99ParserAction , Object > implements IParserActionTokenProvider, IParser {
public class C99Parser extends AbstractTrialUndoActionProvider< C99ParserAction , Object > implements IParserActionTokenProvider, IParser {
private static ParseTable prs = new C99Parserprs();
protected static final Action< C99ParserAction , Object >[] RULE_ACTIONS;
@ -131,12 +131,15 @@ public class C99Parser extends AbstractTrialUndoActionProvider< C99ParserAction
private C99ParserAction action;
//public C99Parser() { // constructor
//}
private void initActions(IASTTranslationUnit tu) {
// binding resolution actions need access to IASTName nodes, temporary
action = new C99ParserAction ();
action.resolver = new C99TypedefTrackerParserAction (this);
action.builder = new C99BuildASTParserAction ( C99ASTNodeFactory.DEFAULT_INSTANCE , this, tu);
action.builder.setTokenMap(C99Parsersym.orderedTerminalSymbols);
action.builder.setTokenMap( C99Parsersym .orderedTerminalSymbols);
setParserAction(action);
}
@ -146,6 +149,12 @@ public void addToken(IToken token) {
super.addToken(token);
}
public void setTokens(List<IToken> tokens) {
resetTokenStream();
for(IToken token : tokens) {
addToken(token);
}
}
public IASTCompletionNode parse(IASTTranslationUnit tu) {
// this has to be done, or... kaboom!
@ -168,8 +177,8 @@ public IASTCompletionNode parse(IASTTranslationUnit tu) {
public int getKind(int i) {
int kind = super.getKind(i);
// lexer feedback hack!
if(kind == C99Parsersym.TK_identifier && action.resolver.isTypedef(getTokenText(i))) {
kind = C99Parsersym.TK_TypedefName;
if(kind == C99Parsersym .TK_identifier && action.resolver.isTypedef(getTokenText(i))) {
kind = C99Parsersym .TK_TypedefName;
}
return kind;
}

View file

@ -165,7 +165,8 @@ public class CPPParser extends PrsStream implements RuleAction , IParserActionTo
private CPPParserAction action;
// uncomment to use with backtracking parser
public CPPParser() {}
public CPPParser() { // constructor
}
private void initActions(IASTTranslationUnit tu) {
// binding resolution actions need access to IASTName nodes, temporary
@ -185,6 +186,13 @@ public void addToken(IToken token) {
}
public void setTokens(List<IToken> tokens) {
resetTokenStream();
for(IToken token : tokens) {
addToken(token);
}
}
public IASTCompletionNode parse(IASTTranslationUnit tu) {
// this has to be done, or... kaboom!
setStreamLength(getSize());