[e-cvs] cvs commit: e/src/jsrc/antlr/preprocessor Grammar.java GrammarFile.java Hierarchy.java Option.java Preprocessor.java PreprocessorLexer.java PreprocessorTokenTypes.java Rule.java Tool.java preproc.g
markm@eros.cs.jhu.edu
markm@eros.cs.jhu.edu
Wed, 3 Oct 2001 21:09:24 -0400
markm 01/10/03 21:09:22
Modified: src/jsrc/antlr ANTLRError.java ANTLRException.java
ANTLRGrammarParseBehavior.java ANTLRHashString.java
ANTLRLexer.java ANTLRParser.java
ANTLRStringBuffer.java ANTLRTokdefLexer.java
ANTLRTokdefParser.java
ANTLRTokdefParserTokenTypes.java
ANTLRTokenTypes.java ASTFactory.java
ASTIterator.java ASTNULLType.java ASTPair.java
ASTVisitor.java ActionElement.java
ActionTransInfo.java Alternative.java
AlternativeBlock.java AlternativeElement.java
BaseAST.java BlockContext.java BlockEndElement.java
BlockWithImpliedExitPath.java ByteBuffer.java
CharBuffer.java CharFormatter.java
CharLiteralElement.java CharQueue.java
CharRangeElement.java CharScanner.java
CharStreamException.java CharStreamIOException.java
CodeGenerator.java CommonAST.java
CommonASTWithHiddenTokens.java
CommonHiddenStreamToken.java CommonToken.java
CppBlockFinishingInfo.java CppCharFormatter.java
CppCodeGenerator.java DefaultFileLineFormatter.java
DefaultToolErrorHandler.java
DefineGrammarSymbols.java
DiagnosticCodeGenerator.java DumpASTVisitor.java
ExceptionHandler.java ExceptionSpec.java
FileCopyException.java FileLineFormatter.java
Grammar.java GrammarAnalyzer.java GrammarAtom.java
GrammarElement.java GrammarSymbol.java
HTMLCodeGenerator.java ImportVocabTokenManager.java
InputBuffer.java JavaBlockFinishingInfo.java
JavaCharFormatter.java JavaCodeGenerator.java
LLkAnalyzer.java LLkGrammarAnalyzer.java
LLkParser.java LexerGrammar.java
LexerSharedInputState.java Lookahead.java
MakeGrammar.java MismatchedCharException.java
MismatchedTokenException.java NameSpace.java
NoViableAltException.java
NoViableAltForCharException.java
OneOrMoreBlock.java Parser.java ParserGrammar.java
ParserSharedInputState.java
RecognitionException.java RuleBlock.java
RuleEndElement.java RuleRefElement.java
RuleSymbol.java SatherBlockFinishingInfo.java
SatherCharFormatter.java SatherCodeGenerator.java
SemanticException.java SimpleTokenManager.java
StringLiteralElement.java StringLiteralSymbol.java
SynPredBlock.java Token.java TokenBuffer.java
TokenManager.java TokenQueue.java
TokenRangeElement.java TokenRefElement.java
TokenStream.java TokenStreamBasicFilter.java
TokenStreamException.java
TokenStreamHiddenTokenFilter.java
TokenStreamIOException.java
TokenStreamRecognitionException.java
TokenStreamRetryException.java
TokenStreamSelector.java TokenSymbol.java Tool.java
ToolErrorHandler.java TreeBlockContext.java
TreeElement.java TreeParser.java
TreeParserSharedInputState.java
TreeSpecifierNode.java TreeWalkerGrammar.java
WildcardElement.java ZeroOrMoreBlock.java antlr.g
tokdef.g
src/jsrc/antlr/actions/cpp ActionLexer.java
ActionLexerTokenTypes.java action.g
src/jsrc/antlr/actions/java ActionLexer.java
ActionLexerTokenTypes.java action.g
src/jsrc/antlr/actions/sather ActionLexer.java
ActionLexerTokenTypes.java action.g
src/jsrc/antlr/collections AST.java ASTEnumeration.java
Enumerator.java List.java Stack.java
src/jsrc/antlr/collections/impl ASTArray.java
ASTEnumerator.java BitSet.java IndexedVector.java
IntRange.java LLCell.java LLEnumeration.java
LList.java Vector.java VectorEnumeration.java
VectorEnumerator.java
src/jsrc/antlr/debug DebuggingCharScanner.java
DebuggingInputBuffer.java DebuggingParser.java
Event.java GuessingEvent.java
InputBufferAdapter.java InputBufferEvent.java
InputBufferEventSupport.java
InputBufferListener.java InputBufferReporter.java
LLkDebuggingParser.java ListenerBase.java
MessageAdapter.java MessageEvent.java
MessageListener.java NewLineEvent.java
NewLineListener.java ParserAdapter.java
ParserController.java ParserEventSupport.java
ParserListener.java ParserMatchAdapter.java
ParserMatchEvent.java ParserMatchListener.java
ParserReporter.java ParserTokenAdapter.java
ParserTokenEvent.java ParserTokenListener.java
SemanticPredicateAdapter.java
SemanticPredicateEvent.java
SemanticPredicateListener.java
SyntacticPredicateAdapter.java
SyntacticPredicateEvent.java
SyntacticPredicateListener.java TraceAdapter.java
TraceEvent.java TraceListener.java Tracer.java
src/jsrc/antlr/debug/misc ASTFrame.java JTreeASTModel.java
JTreeASTPanel.java
src/jsrc/antlr/preprocessor Grammar.java GrammarFile.java
Hierarchy.java Option.java Preprocessor.java
PreprocessorLexer.java PreprocessorTokenTypes.java
Rule.java Tool.java preproc.g
Added: src/jsrc/antlr AbsASTFactory.java BaseMuAST.java
CommonMuAST.java CommonMuASTWithHiddenTokens.java
MuASTFactory.java MuASTNULLType.java
src/jsrc/antlr/collections MuAST.java
Log:
forked mutable from immutable ASTs
Revision Changes Path
1.2 +4 -4 e/src/jsrc/antlr/ANTLRError.java
Index: ANTLRError.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/ANTLRError.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ANTLRError.java 2001/10/03 21:04:13 1.1
+++ ANTLRError.java 2001/10/04 01:09:13 1.2
@@ -4,23 +4,23 @@
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
- * $Id: ANTLRError.java,v 1.1 2001/10/03 21:04:13 markm Exp $
+ * $Id: ANTLRError.java,v 1.2 2001/10/04 01:09:13 markm Exp $
*/
public class ANTLRError extends Error {
-
+
/**
* ANTLRError constructor comment.
*/
public ANTLRError() {
- super();
+ super();
}
-
+
/**
* ANTLRError constructor comment.
* @param s java.lang.String
*/
public ANTLRError(String s) {
- super(s);
+ super(s);
}
}
1.2 +7 -6 e/src/jsrc/antlr/ANTLRException.java
Index: ANTLRException.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/ANTLRException.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ANTLRException.java 2001/10/03 21:04:13 1.1
+++ ANTLRException.java 2001/10/04 01:09:13 1.2
@@ -4,15 +4,16 @@
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
- * $Id: ANTLRException.java,v 1.1 2001/10/03 21:04:13 markm Exp $
+ * $Id: ANTLRException.java,v 1.2 2001/10/04 01:09:13 markm Exp $
*/
public class ANTLRException extends Exception {
- public ANTLRException() {
- super();
- }
- public ANTLRException(String s) {
- super(s);
- }
+ public ANTLRException() {
+ super();
+ }
+
+ public ANTLRException(String s) {
+ super(s);
+ }
}
1.2 +107 -59 e/src/jsrc/antlr/ANTLRGrammarParseBehavior.java
Index: ANTLRGrammarParseBehavior.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/ANTLRGrammarParseBehavior.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ANTLRGrammarParseBehavior.java 2001/10/03 21:04:13 1.1
+++ ANTLRGrammarParseBehavior.java 2001/10/04 01:09:13 1.2
@@ -4,69 +4,117 @@
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
- * $Id: ANTLRGrammarParseBehavior.java,v 1.1 2001/10/03 21:04:13 markm Exp $
+ * $Id: ANTLRGrammarParseBehavior.java,v 1.2 2001/10/04 01:09:13 markm Exp $
*/
import antlr.collections.impl.BitSet;
public interface ANTLRGrammarParseBehavior {
- public void abortGrammar();
- public void beginAlt(boolean doAST_);
- public void beginChildList();
- // Exception handling
- public void beginExceptionGroup();
- public void beginExceptionSpec(Token label);
- public void beginSubRule(Token label, int line, boolean not);
- // Trees
- public void beginTree(int line) throws SemanticException ;
- public void defineRuleName(Token r, String access, boolean ruleAST, String docComment) throws SemanticException;
- public void defineToken(Token tokname, Token tokliteral);
- public void endAlt();
- public void endChildList();
- public void endExceptionGroup();
- public void endExceptionSpec();
- public void endGrammar();
- public void endOptions();
- public void endRule(String r);
- public void endSubRule();
- public void endTree();
- public void hasError();
- public void noASTSubRule();
- public void oneOrMoreSubRule();
- public void optionalSubRule();
- public void refAction(Token action);
- public void refArgAction(Token action);
- public void setUserExceptions(String thr);
- public void refCharLiteral(Token lit, Token label, boolean inverted, int autoGenType, boolean lastInRule);
- public void refCharRange(Token t1, Token t2, Token label, int autoGenType, boolean lastInRule);
- public void refElementOption(Token option, Token value);
- public void refTokensSpecElementOption(Token tok, Token option, Token value);
- public void refExceptionHandler(Token exTypeAndName, Token action);
- public void refHeaderAction(Token name,Token act);
- public void refInitAction(Token action);
- public void refMemberAction(Token act);
- public void refPreambleAction(Token act);
- public void refReturnAction(Token returnAction);
- public void refRule(Token idAssign, Token r, Token label, Token arg, int autoGenType);
- public void refSemPred(Token pred);
- public void refStringLiteral(Token lit, Token label, int autoGenType, boolean lastInRule);
- public void refToken(Token assignId, Token t, Token label, Token args,
- boolean inverted, int autoGenType, boolean lastInRule);
- public void refTokenRange(Token t1, Token t2, Token label, int autoGenType, boolean lastInRule);
- // Tree specifiers
- public void refTreeSpecifier(Token treeSpec);
- public void refWildcard(Token t, Token label, int autoGenType);
- public void setArgOfRuleRef(Token argaction);
- public void setCharVocabulary(BitSet b);
- // Options
- public void setFileOption(Token key, Token value, String filename);
- public void setGrammarOption(Token key, Token value);
- public void setRuleOption(Token key, Token value);
- public void setSubruleOption(Token key, Token value);
- public void startLexer(String file, Token name, String superClass, String doc);
- // Flow control for grammars
- public void startParser(String file, Token name, String superClass, String doc);
- public void startTreeWalker(String file, Token name, String superClass, String doc);
- public void synPred();
- public void zeroOrMoreSubRule();
+
+ public void abortGrammar();
+
+ public void beginAlt(boolean doAST_);
+
+ public void beginChildList();
+ // Exception handling
+ public void beginExceptionGroup();
+
+ public void beginExceptionSpec(Token label);
+
+ public void beginSubRule(Token label, int line, boolean not);
+ // Trees
+ public void beginTree(int line) throws SemanticException;
+
+ public void defineRuleName(Token r, String access, boolean ruleAST, String docComment) throws SemanticException;
+
+ public void defineToken(Token tokname, Token tokliteral);
+
+ public void endAlt();
+
+ public void endChildList();
+
+ public void endExceptionGroup();
+
+ public void endExceptionSpec();
+
+ public void endGrammar();
+
+ public void endOptions();
+
+ public void endRule(String r);
+
+ public void endSubRule();
+
+ public void endTree();
+
+ public void hasError();
+
+ public void noASTSubRule();
+
+ public void oneOrMoreSubRule();
+
+ public void optionalSubRule();
+
+ public void refAction(Token action);
+
+ public void refArgAction(Token action);
+
+ public void setUserExceptions(String thr);
+
+ public void refCharLiteral(Token lit, Token label, boolean inverted, int autoGenType, boolean lastInRule);
+
+ public void refCharRange(Token t1, Token t2, Token label, int autoGenType, boolean lastInRule);
+
+ public void refElementOption(Token option, Token value);
+
+ public void refTokensSpecElementOption(Token tok, Token option, Token value);
+
+ public void refExceptionHandler(Token exTypeAndName, Token action);
+
+ public void refHeaderAction(Token name, Token act);
+
+ public void refInitAction(Token action);
+
+ public void refMemberAction(Token act);
+
+ public void refPreambleAction(Token act);
+
+ public void refReturnAction(Token returnAction);
+
+ public void refRule(Token idAssign, Token r, Token label, Token arg, int autoGenType);
+
+ public void refSemPred(Token pred);
+
+ public void refStringLiteral(Token lit, Token label, int autoGenType, boolean lastInRule);
+
+ public void refToken(Token assignId, Token t, Token label, Token args,
+ boolean inverted, int autoGenType, boolean lastInRule);
+
+ public void refTokenRange(Token t1, Token t2, Token label, int autoGenType, boolean lastInRule);
+ // Tree specifiers
+ public void refTreeSpecifier(Token treeSpec);
+
+ public void refWildcard(Token t, Token label, int autoGenType);
+
+ public void setArgOfRuleRef(Token argaction);
+
+ public void setCharVocabulary(BitSet b);
+ // Options
+ public void setFileOption(Token key, Token value, String filename);
+
+ public void setGrammarOption(Token key, Token value);
+
+ public void setRuleOption(Token key, Token value);
+
+ public void setSubruleOption(Token key, Token value);
+
+ public void startLexer(String file, Token name, String superClass, String doc);
+ // Flow control for grammars
+ public void startParser(String file, Token name, String superClass, String doc);
+
+ public void startTreeWalker(String file, Token name, String superClass, String doc);
+
+ public void synPred();
+
+ public void zeroOrMoreSubRule();
}
1.2 +92 -82 e/src/jsrc/antlr/ANTLRHashString.java
Index: ANTLRHashString.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/ANTLRHashString.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ANTLRHashString.java 2001/10/03 21:04:13 1.1
+++ ANTLRHashString.java 2001/10/04 01:09:13 1.2
@@ -4,7 +4,7 @@
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
- * $Id: ANTLRHashString.java,v 1.1 2001/10/03 21:04:13 markm Exp $
+ * $Id: ANTLRHashString.java,v 1.2 2001/10/04 01:09:13 markm Exp $
*/
// class implements a String-like object whose sole purpose is to be
@@ -12,86 +12,96 @@
// information about case sensitivity.
public class ANTLRHashString {
- // only one of s or buf is non-null
- private String s;
- private char[] buf;
- private int len;
- private CharScanner lexer;
- private static final int prime = 151;
-
-
- public ANTLRHashString(char[] buf, int length, CharScanner lexer) {
- this.lexer = lexer;
- setBuffer(buf, length);
- }
- // Hash strings constructed this way are unusable until setBuffer or setString are called.
- public ANTLRHashString(CharScanner lexer) {
- this.lexer = lexer;
- }
- public ANTLRHashString(String s, CharScanner lexer) {
- this.lexer = lexer;
- setString(s);
- }
- private final char charAt(int index) {
- return (s!=null) ? s.charAt(index) : buf[index];
- }
- // Return true if o is an ANTLRHashString equal to this.
- public boolean equals(Object o) {
- if (!(o instanceof ANTLRHashString) && !(o instanceof String)) {
- return false;
- }
-
- ANTLRHashString s;
- if ( o instanceof String ) {
- s = new ANTLRHashString((String)o,lexer);
- }
- else {
- s = (ANTLRHashString)o;
- }
- int l = length();
- if (s.length() != l) {
- return false;
- }
- if (lexer.getCaseSensitiveLiterals()) {
- for (int i = 0; i < l; i++) {
- if (charAt(i) != s.charAt(i)) {
- return false;
- }
- }
- } else {
- for (int i = 0; i < l; i++) {
- if (lexer.toLower(charAt(i)) != lexer.toLower(s.charAt(i))) {
- return false;
- }
- }
- }
- return true;
- }
- public int hashCode() {
- int hashval = 0;
- int l = length();
-
- if (lexer.getCaseSensitiveLiterals()) {
- for (int i = 0; i < l; i++) {
- hashval = hashval * prime + charAt(i);
- }
- } else {
- for (int i = 0; i < l; i++) {
- hashval = hashval * prime + lexer.toLower(charAt(i));
- }
- }
- return hashval;
- }
- private final int length() {
- return (s!=null) ? s.length() : len;
- }
- public void setBuffer(char[] buf, int length) {
- this.buf = buf;
- this.len = length;
- s = null;
- }
- public void setString(String s) {
- this.s = s;
- buf = null;
- }
+
+ // only one of s or buf is non-null
+ private String s;
+
+ private char[] buf;
+
+ private int len;
+
+ private CharScanner lexer;
+
+ private static final int prime = 151;
+
+
+ public ANTLRHashString(char[] buf, int length, CharScanner lexer) {
+ this.lexer = lexer;
+ setBuffer(buf, length);
+ }
+ // Hash strings constructed this way are unusable until setBuffer or setString are called.
+ public ANTLRHashString(CharScanner lexer) {
+ this.lexer = lexer;
+ }
+
+ public ANTLRHashString(String s, CharScanner lexer) {
+ this.lexer = lexer;
+ setString(s);
+ }
+
+ private final char charAt(int index) {
+ return (s != null) ? s.charAt(index) : buf[index];
+ }
+ // Return true if o is an ANTLRHashString equal to this.
+ public boolean equals(Object o) {
+ if (!(o instanceof ANTLRHashString) && !(o instanceof String)) {
+ return false;
+ }
+
+ ANTLRHashString s;
+ if (o instanceof String) {
+ s = new ANTLRHashString((String)o, lexer);
+ } else {
+ s = (ANTLRHashString)o;
+ }
+ int l = length();
+ if (s.length() != l) {
+ return false;
+ }
+ if (lexer.getCaseSensitiveLiterals()) {
+ for (int i = 0; i < l; i++) {
+ if (charAt(i) != s.charAt(i)) {
+ return false;
+ }
+ }
+ } else {
+ for (int i = 0; i < l; i++) {
+ if (lexer.toLower(charAt(i)) != lexer.toLower(s.charAt(i))) {
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+
+ public int hashCode() {
+ int hashval = 0;
+ int l = length();
+
+ if (lexer.getCaseSensitiveLiterals()) {
+ for (int i = 0; i < l; i++) {
+ hashval = hashval * prime + charAt(i);
+ }
+ } else {
+ for (int i = 0; i < l; i++) {
+ hashval = hashval * prime + lexer.toLower(charAt(i));
+ }
+ }
+ return hashval;
+ }
+
+ private final int length() {
+ return (s != null) ? s.length() : len;
+ }
+
+ public void setBuffer(char[] buf, int length) {
+ this.buf = buf;
+ this.len = length;
+ s = null;
+ }
+
+ public void setString(String s) {
+ this.s = s;
+ buf = null;
+ }
}
1.2 +2097 -1625e/src/jsrc/antlr/ANTLRLexer.java
Index: ANTLRLexer.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/ANTLRLexer.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ANTLRLexer.java 2001/10/03 21:04:13 1.1
+++ ANTLRLexer.java 2001/10/04 01:09:13 1.2
@@ -2,1635 +2,2107 @@
package antlr;
+import antlr.collections.impl.BitSet;
+
import java.io.InputStream;
-import antlr.TokenStreamException;
-import antlr.TokenStreamIOException;
-import antlr.TokenStreamRecognitionException;
-import antlr.CharStreamException;
-import antlr.CharStreamIOException;
-import antlr.ANTLRException;
import java.io.Reader;
import java.util.Hashtable;
-import antlr.CharScanner;
-import antlr.InputBuffer;
-import antlr.ByteBuffer;
-import antlr.CharBuffer;
-import antlr.Token;
-import antlr.CommonToken;
-import antlr.RecognitionException;
-import antlr.NoViableAltForCharException;
-import antlr.MismatchedCharException;
-import antlr.TokenStream;
-import antlr.ANTLRHashString;
-import antlr.LexerSharedInputState;
-import antlr.collections.impl.BitSet;
-import antlr.SemanticException;
-public class ANTLRLexer extends antlr.CharScanner implements ANTLRTokenTypes, TokenStream
- {
+public class ANTLRLexer extends antlr.CharScanner implements ANTLRTokenTypes, TokenStream {
- /**Convert 'c' to an integer char value. */
- public static int escapeCharValue(String cs) {
- //System.out.println("escapeCharValue("+cs+")");
- if ( cs.charAt(1)!='\\' ) return 0;
- switch ( cs.charAt(2) ) {
- case 'b' : return '\b';
- case 'r' : return '\r';
- case 't' : return '\t';
- case 'n' : return '\n';
- case 'f' : return '\f';
- case '"' : return '\"';
- case '\'' :return '\'';
- case '\\' :return '\\';
-
- case 'u' :
- // Unicode char
- if (cs.length() != 8) {
- return 0;
- }
- else {
- return
- Character.digit(cs.charAt(3), 16) * 16 * 16 * 16 +
- Character.digit(cs.charAt(4), 16) * 16 * 16 +
- Character.digit(cs.charAt(5), 16) * 16 +
- Character.digit(cs.charAt(6), 16);
- }
-
- case '0' :
- case '1' :
- case '2' :
- case '3' :
- if ( cs.length()>5 && Character.isDigit(cs.charAt(4)) ) {
- return (cs.charAt(2)-'0')*8*8 + (cs.charAt(3)-'0')*8 + (cs.charAt(4)-'0');
- }
- if ( cs.length()>4 && Character.isDigit(cs.charAt(3)) ) {
- return (cs.charAt(2)-'0')*8 + (cs.charAt(3)-'0');
- }
- return cs.charAt(2)-'0';
-
- case '4' :
- case '5' :
- case '6' :
- case '7' :
- if ( cs.length()>4 && Character.isDigit(cs.charAt(3)) ) {
- return (cs.charAt(2)-'0')*8 + (cs.charAt(3)-'0');
- }
- return cs.charAt(2)-'0';
-
- default :
- return 0;
- }
- }
-
- public static int tokenTypeForCharLiteral(String lit) {
- if ( lit.length()>3 ) { // does char contain escape?
- return escapeCharValue(lit);
- }
- else {
- return lit.charAt(1);
- }
- }
-public ANTLRLexer(InputStream in) {
- this(new ByteBuffer(in));
-}
-public ANTLRLexer(Reader in) {
- this(new CharBuffer(in));
-}
-public ANTLRLexer(InputBuffer ib) {
- this(new LexerSharedInputState(ib));
-}
-public ANTLRLexer(LexerSharedInputState state) {
- super(state);
- literals = new Hashtable();
- literals.put(new ANTLRHashString("Parser", this), new Integer(30));
- literals.put(new ANTLRHashString("catch", this), new Integer(41));
- literals.put(new ANTLRHashString("Lexer", this), new Integer(12));
- literals.put(new ANTLRHashString("exception", this), new Integer(40));
- literals.put(new ANTLRHashString("class", this), new Integer(10));
- literals.put(new ANTLRHashString("lexclass", this), new Integer(9));
- literals.put(new ANTLRHashString("public", this), new Integer(32));
- literals.put(new ANTLRHashString("header", this), new Integer(5));
- literals.put(new ANTLRHashString("options", this), new Integer(51));
- literals.put(new ANTLRHashString("charVocabulary", this), new Integer(18));
- literals.put(new ANTLRHashString("tokens", this), new Integer(4));
- literals.put(new ANTLRHashString("returns", this), new Integer(36));
- literals.put(new ANTLRHashString("TreeParser", this), new Integer(13));
- literals.put(new ANTLRHashString("private", this), new Integer(33));
- literals.put(new ANTLRHashString("protected", this), new Integer(31));
- literals.put(new ANTLRHashString("throws", this), new Integer(38));
- literals.put(new ANTLRHashString("extends", this), new Integer(11));
-caseSensitiveLiterals = true;
-setCaseSensitive(true);
-}
+ /**Convert 'c' to an integer char value. */
+ public static int escapeCharValue(String cs) {
+ //System.out.println("escapeCharValue("+cs+")");
+ if (cs.charAt(1) != '\\') return 0;
+ switch (cs.charAt(2)) {
+ case 'b':
+ return '\b';
+ case 'r':
+ return '\r';
+ case 't':
+ return '\t';
+ case 'n':
+ return '\n';
+ case 'f':
+ return '\f';
+ case '"':
+ return '\"';
+ case '\'':
+ return '\'';
+ case '\\':
+ return '\\';
-public Token nextToken() throws TokenStreamException {
- Token theRetToken=null;
-tryAgain:
- for (;;) {
- Token _token = null;
- int _ttype = Token.INVALID_TYPE;
- resetText();
- try { // for char stream error handling
- try { // for lexical error handling
- switch ( LA(1)) {
- case '\t': case '\n': case '\r': case ' ':
- {
- mWS(true);
- theRetToken=_returnToken;
- break;
- }
- case '/':
- {
- mCOMMENT(true);
- theRetToken=_returnToken;
- break;
- }
- case '<':
- {
- mOPEN_ELEMENT_OPTION(true);
- theRetToken=_returnToken;
- break;
- }
- case '>':
- {
- mCLOSE_ELEMENT_OPTION(true);
- theRetToken=_returnToken;
- break;
- }
- case ',':
- {
- mCOMMA(true);
- theRetToken=_returnToken;
- break;
- }
- case '?':
- {
- mQUESTION(true);
- theRetToken=_returnToken;
- break;
- }
- case '#':
- {
- mTREE_BEGIN(true);
- theRetToken=_returnToken;
- break;
- }
- case '(':
- {
- mLPAREN(true);
- theRetToken=_returnToken;
- break;
- }
- case ')':
- {
- mRPAREN(true);
- theRetToken=_returnToken;
- break;
- }
- case ':':
- {
- mCOLON(true);
- theRetToken=_returnToken;
- break;
- }
- case '*':
- {
- mSTAR(true);
- theRetToken=_returnToken;
- break;
- }
- case '+':
- {
- mPLUS(true);
- theRetToken=_returnToken;
- break;
- }
- case ';':
- {
- mSEMI(true);
- theRetToken=_returnToken;
- break;
- }
- case '^':
- {
- mCARET(true);
- theRetToken=_returnToken;
- break;
- }
- case '!':
- {
- mBANG(true);
- theRetToken=_returnToken;
- break;
- }
- case '|':
- {
- mOR(true);
- theRetToken=_returnToken;
- break;
- }
- case '~':
- {
- mNOT_OP(true);
- theRetToken=_returnToken;
- break;
- }
- case '}':
- {
- mRCURLY(true);
- theRetToken=_returnToken;
- break;
- }
- case '\'':
- {
- mCHAR_LITERAL(true);
- theRetToken=_returnToken;
- break;
- }
- case '"':
- {
- mSTRING_LITERAL(true);
- theRetToken=_returnToken;
- break;
- }
- case '0': case '1': case '2': case '3':
- case '4': case '5': case '6': case '7':
- case '8': case '9':
- {
- mINT(true);
- theRetToken=_returnToken;
- break;
- }
- case '[':
- {
- mARG_ACTION(true);
- theRetToken=_returnToken;
- break;
- }
- case '{':
- {
- mACTION(true);
- theRetToken=_returnToken;
- break;
- }
- case 'A': case 'B': case 'C': case 'D':
- case 'E': case 'F': case 'G': case 'H':
- case 'I': case 'J': case 'K': case 'L':
- case 'M': case 'N': case 'O': case 'P':
- case 'Q': case 'R': case 'S': case 'T':
- case 'U': case 'V': case 'W': case 'X':
- case 'Y': case 'Z':
- {
- mTOKEN_REF(true);
- theRetToken=_returnToken;
- break;
- }
- case 'a': case 'b': case 'c': case 'd':
- case 'e': case 'f': case 'g': case 'h':
- case 'i': case 'j': case 'k': case 'l':
- case 'm': case 'n': case 'o': case 'p':
- case 'q': case 'r': case 's': case 't':
- case 'u': case 'v': case 'w': case 'x':
- case 'y': case 'z':
- {
- mRULE_REF(true);
- theRetToken=_returnToken;
- break;
- }
- default:
- if ((LA(1)=='=') && (LA(2)=='>')) {
- mIMPLIES(true);
- theRetToken=_returnToken;
- }
- else if ((LA(1)=='.') && (LA(2)=='.')) {
- mRANGE(true);
- theRetToken=_returnToken;
- }
- else if ((LA(1)=='=') && (true)) {
- mASSIGN(true);
- theRetToken=_returnToken;
- }
- else if ((LA(1)=='.') && (true)) {
- mWILDCARD(true);
- theRetToken=_returnToken;
- }
- else {
- if (LA(1)==EOF_CHAR) {uponEOF(); _returnToken = makeToken(Token.EOF_TYPE);}
- else {throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());}
- }
- }
- if ( _returnToken==null ) continue tryAgain; // found SKIP token
- _ttype = _returnToken.getType();
- _returnToken.setType(_ttype);
- return _returnToken;
- }
- catch (RecognitionException e) {
- throw new TokenStreamRecognitionException(e);
- }
- }
- catch (CharStreamException cse) {
- if ( cse instanceof CharStreamIOException ) {
- throw new TokenStreamIOException(((CharStreamIOException)cse).io);
- }
- else {
- throw new TokenStreamException(cse.getMessage());
- }
- }
- }
-}
+ case 'u':
+ // Unicode char
+ if (cs.length() != 8) {
+ return 0;
+ } else {
+ return
+ Character.digit(cs.charAt(3), 16) * 16 * 16 * 16 +
+ Character.digit(cs.charAt(4), 16) * 16 * 16 +
+ Character.digit(cs.charAt(5), 16) * 16 +
+ Character.digit(cs.charAt(6), 16);
+ }
+
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ if (cs.length() > 5 && Character.isDigit(cs.charAt(4))) {
+ return (cs.charAt(2) - '0') * 8 * 8 + (cs.charAt(3) - '0') * 8 + (cs.charAt(4) - '0');
+ }
+ if (cs.length() > 4 && Character.isDigit(cs.charAt(3))) {
+ return (cs.charAt(2) - '0') * 8 + (cs.charAt(3) - '0');
+ }
+ return cs.charAt(2) - '0';
+
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ if (cs.length() > 4 && Character.isDigit(cs.charAt(3))) {
+ return (cs.charAt(2) - '0') * 8 + (cs.charAt(3) - '0');
+ }
+ return cs.charAt(2) - '0';
+
+ default :
+ return 0;
+ }
+ }
+
+ public static int tokenTypeForCharLiteral(String lit) {
+ if (lit.length() > 3) {
+ // does char contain escape?
+ return escapeCharValue(lit);
+ } else {
+ return lit.charAt(1);
+ }
+ }
+
+ public ANTLRLexer(InputStream in) {
+ this(new ByteBuffer(in));
+ }
+
+ public ANTLRLexer(Reader in) {
+ this(new CharBuffer(in));
+ }
+
+ public ANTLRLexer(InputBuffer ib) {
+ this(new LexerSharedInputState(ib));
+ }
+
+ public ANTLRLexer(LexerSharedInputState state) {
+ super(state);
+ literals = new Hashtable();
+ literals.put(new ANTLRHashString("Parser", this), new Integer(30));
+ literals.put(new ANTLRHashString("catch", this), new Integer(41));
+ literals.put(new ANTLRHashString("Lexer", this), new Integer(12));
+ literals.put(new ANTLRHashString("exception", this), new Integer(40));
+ literals.put(new ANTLRHashString("class", this), new Integer(10));
+ literals.put(new ANTLRHashString("lexclass", this), new Integer(9));
+ literals.put(new ANTLRHashString("public", this), new Integer(32));
+ literals.put(new ANTLRHashString("header", this), new Integer(5));
+ literals.put(new ANTLRHashString("options", this), new Integer(51));
+ literals.put(new ANTLRHashString("charVocabulary", this), new Integer(18));
+ literals.put(new ANTLRHashString("tokens", this), new Integer(4));
+ literals.put(new ANTLRHashString("returns", this), new Integer(36));
+ literals.put(new ANTLRHashString("TreeParser", this), new Integer(13));
+ literals.put(new ANTLRHashString("private", this), new Integer(33));
+ literals.put(new ANTLRHashString("protected", this), new Integer(31));
+ literals.put(new ANTLRHashString("throws", this), new Integer(38));
+ literals.put(new ANTLRHashString("extends", this), new Integer(11));
+ caseSensitiveLiterals = true;
+ setCaseSensitive(true);
+ }
+
+ public Token nextToken() throws TokenStreamException {
+ Token theRetToken = null;
+ tryAgain:
+ for (; ;) {
+ Token _token = null;
+ int _ttype = Token.INVALID_TYPE;
+ resetText();
+ try {
+ // for char stream error handling
+ try {
+ // for lexical error handling
+ switch (LA(1)) {
+ case '\t':
+ case '\n':
+ case '\r':
+ case ' ':
+ {
+ mWS(true);
+ theRetToken = _returnToken;
+ break;
+ }
+ case '/':
+ {
+ mCOMMENT(true);
+ theRetToken = _returnToken;
+ break;
+ }
+ case '<':
+ {
+ mOPEN_ELEMENT_OPTION(true);
+ theRetToken = _returnToken;
+ break;
+ }
+ case '>':
+ {
+ mCLOSE_ELEMENT_OPTION(true);
+ theRetToken = _returnToken;
+ break;
+ }
+ case ',':
+ {
+ mCOMMA(true);
+ theRetToken = _returnToken;
+ break;
+ }
+ case '?':
+ {
+ mQUESTION(true);
+ theRetToken = _returnToken;
+ break;
+ }
+ case '#':
+ {
+ mTREE_BEGIN(true);
+ theRetToken = _returnToken;
+ break;
+ }
+ case '(':
+ {
+ mLPAREN(true);
+ theRetToken = _returnToken;
+ break;
+ }
+ case ')':
+ {
+ mRPAREN(true);
+ theRetToken = _returnToken;
+ break;
+ }
+ case ':':
+ {
+ mCOLON(true);
+ theRetToken = _returnToken;
+ break;
+ }
+ case '*':
+ {
+ mSTAR(true);
+ theRetToken = _returnToken;
+ break;
+ }
+ case '+':
+ {
+ mPLUS(true);
+ theRetToken = _returnToken;
+ break;
+ }
+ case ';':
+ {
+ mSEMI(true);
+ theRetToken = _returnToken;
+ break;
+ }
+ case '^':
+ {
+ mCARET(true);
+ theRetToken = _returnToken;
+ break;
+ }
+ case '!':
+ {
+ mBANG(true);
+ theRetToken = _returnToken;
+ break;
+ }
+ case '|':
+ {
+ mOR(true);
+ theRetToken = _returnToken;
+ break;
+ }
+ case '~':
+ {
+ mNOT_OP(true);
+ theRetToken = _returnToken;
+ break;
+ }
+ case '}':
+ {
+ mRCURLY(true);
+ theRetToken = _returnToken;
+ break;
+ }
+ case '\'':
+ {
+ mCHAR_LITERAL(true);
+ theRetToken = _returnToken;
+ break;
+ }
+ case '"':
+ {
+ mSTRING_LITERAL(true);
+ theRetToken = _returnToken;
+ break;
+ }
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ case '9':
+ {
+ mINT(true);
+ theRetToken = _returnToken;
+ break;
+ }
+ case '[':
+ {
+ mARG_ACTION(true);
+ theRetToken = _returnToken;
+ break;
+ }
+ case '{':
+ {
+ mACTION(true);
+ theRetToken = _returnToken;
+ break;
+ }
+ case 'A':
+ case 'B':
+ case 'C':
+ case 'D':
+ case 'E':
+ case 'F':
+ case 'G':
+ case 'H':
+ case 'I':
+ case 'J':
+ case 'K':
+ case 'L':
+ case 'M':
+ case 'N':
+ case 'O':
+ case 'P':
+ case 'Q':
+ case 'R':
+ case 'S':
+ case 'T':
+ case 'U':
+ case 'V':
+ case 'W':
+ case 'X':
+ case 'Y':
+ case 'Z':
+ {
+ mTOKEN_REF(true);
+ theRetToken = _returnToken;
+ break;
+ }
+ case 'a':
+ case 'b':
+ case 'c':
+ case 'd':
+ case 'e':
+ case 'f':
+ case 'g':
+ case 'h':
+ case 'i':
+ case 'j':
+ case 'k':
+ case 'l':
+ case 'm':
+ case 'n':
+ case 'o':
+ case 'p':
+ case 'q':
+ case 'r':
+ case 's':
+ case 't':
+ case 'u':
+ case 'v':
+ case 'w':
+ case 'x':
+ case 'y':
+ case 'z':
+ {
+ mRULE_REF(true);
+ theRetToken = _returnToken;
+ break;
+ }
+ default:
+ if ((LA(1) == '=') && (LA(2) == '>')) {
+ mIMPLIES(true);
+ theRetToken = _returnToken;
+ } else if ((LA(1) == '.') && (LA(2) == '.')) {
+ mRANGE(true);
+ theRetToken = _returnToken;
+ } else if ((LA(1) == '=') && (true)) {
+ mASSIGN(true);
+ theRetToken = _returnToken;
+ } else if ((LA(1) == '.') && (true)) {
+ mWILDCARD(true);
+ theRetToken = _returnToken;
+ } else {
+ if (LA(1) == EOF_CHAR) {
+ uponEOF();
+ _returnToken = makeToken(Token.EOF_TYPE);
+ } else {
+ throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());
+ }
+ }
+ }
+ if (_returnToken == null) continue tryAgain; // found SKIP token
+ _ttype = _returnToken.getType();
+ _returnToken.setType(_ttype);
+ return _returnToken;
+ } catch (RecognitionException e) {
+ throw new TokenStreamRecognitionException(e);
+ }
+ } catch (CharStreamException cse) {
+ if (cse instanceof CharStreamIOException) {
+ throw new TokenStreamIOException(((CharStreamIOException)cse).io);
+ } else {
+ throw new TokenStreamException(cse.getMessage());
+ }
+ }
+ }
+ }
+
+ public final void mWS(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = WS;
+ int _saveIndex;
+
+ {
+ switch (LA(1)) {
+ case ' ':
+ {
+ match(' ');
+ break;
+ }
+ case '\t':
+ {
+ match('\t');
+ break;
+ }
+ case '\n':
+ {
+ match('\n');
+ if (inputState.guessing == 0) {
+ newline();
+ }
+ break;
+ }
+ default:
+ if ((LA(1) == '\r') && (LA(2) == '\n')) {
+ match('\r');
+ match('\n');
+ if (inputState.guessing == 0) {
+ newline();
+ }
+ } else if ((LA(1) == '\r') && (true)) {
+ match('\r');
+ if (inputState.guessing == 0) {
+ newline();
+ }
+ } else {
+ throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());
+ }
+ }
+ }
+ if (inputState.guessing == 0) {
+ _ttype = Token.SKIP;
+ }
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ public final void mCOMMENT(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = COMMENT;
+ int _saveIndex;
+ Token t = null;
+
+ {
+ if ((LA(1) == '/') && (LA(2) == '/')) {
+ mSL_COMMENT(false);
+ } else if ((LA(1) == '/') && (LA(2) == '*')) {
+ mML_COMMENT(true);
+ t = _returnToken;
+ if (inputState.guessing == 0) {
+ _ttype = t.getType();
+ }
+ } else {
+ throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());
+ }
+
+ }
+ if (inputState.guessing == 0) {
+ if (_ttype != DOC_COMMENT) _ttype = Token.SKIP;
+ }
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ protected final void mSL_COMMENT(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = SL_COMMENT;
+ int _saveIndex;
+
+ match("//");
+ {
+ _loop153:
+ do {
+ if ((_tokenSet_0.member(LA(1)))) {
+ {
+ match(_tokenSet_0);
+ }
+ } else {
+ break _loop153;
+ }
+
+ } while (true);
+ }
+ {
+ if ((LA(1) == '\r') && (LA(2) == '\n')) {
+ match('\r');
+ match('\n');
+ } else if ((LA(1) == '\r') && (true)) {
+ match('\r');
+ } else if ((LA(1) == '\n')) {
+ match('\n');
+ } else {
+ throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());
+ }
+
+ }
+ if (inputState.guessing == 0) {
+ newline();
+ }
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ protected final void mML_COMMENT(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = ML_COMMENT;
+ int _saveIndex;
+
+ match("/*");
+ {
+ if (((LA(1) == '*') && ((LA(2) >= '\u0003' && LA(2) <= '~'))) && (LA(2) != '/')) {
+ match('*');
+ if (inputState.guessing == 0) {
+ _ttype = DOC_COMMENT;
+ }
+ } else if (((LA(1) >= '\u0003' && LA(1) <= '~')) && ((LA(2) >= '\u0003' && LA(2) <= '~'))) {
+ } else {
+ throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());
+ }
+
+ }
+ {
+ _loop159:
+ do {
+ // nongreedy exit test
+ if ((LA(1) == '*') && (LA(2) == '/')) break _loop159;
+ if ((LA(1) == '\r') && (LA(2) == '\n')) {
+ match('\r');
+ match('\n');
+ if (inputState.guessing == 0) {
+ newline();
+ }
+ } else if ((LA(1) == '\r') && ((LA(2) >= '\u0003' && LA(2) <= '~'))) {
+ match('\r');
+ if (inputState.guessing == 0) {
+ newline();
+ }
+ } else if ((_tokenSet_0.member(LA(1))) && ((LA(2) >= '\u0003' && LA(2) <= '~'))) {
+ {
+ match(_tokenSet_0);
+ }
+ } else if ((LA(1) == '\n')) {
+ match('\n');
+ if (inputState.guessing == 0) {
+ newline();
+ }
+ } else {
+ break _loop159;
+ }
+
+ } while (true);
+ }
+ match("*/");
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ public final void mOPEN_ELEMENT_OPTION(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = OPEN_ELEMENT_OPTION;
+ int _saveIndex;
+
+ match('<');
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ public final void mCLOSE_ELEMENT_OPTION(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = CLOSE_ELEMENT_OPTION;
+ int _saveIndex;
+
+ match('>');
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ public final void mCOMMA(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = COMMA;
+ int _saveIndex;
+
+ match(',');
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ public final void mQUESTION(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = QUESTION;
+ int _saveIndex;
+
+ match('?');
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ public final void mTREE_BEGIN(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = TREE_BEGIN;
+ int _saveIndex;
+
+ match("#(");
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ public final void mLPAREN(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = LPAREN;
+ int _saveIndex;
+
+ match('(');
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ public final void mRPAREN(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = RPAREN;
+ int _saveIndex;
+
+ match(')');
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ public final void mCOLON(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = COLON;
+ int _saveIndex;
+
+ match(':');
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ public final void mSTAR(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = STAR;
+ int _saveIndex;
+
+ match('*');
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ public final void mPLUS(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = PLUS;
+ int _saveIndex;
+
+ match('+');
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ public final void mASSIGN(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = ASSIGN;
+ int _saveIndex;
+
+ match('=');
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ public final void mIMPLIES(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = IMPLIES;
+ int _saveIndex;
+
+ match("=>");
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ public final void mSEMI(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = SEMI;
+ int _saveIndex;
+
+ match(';');
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ public final void mCARET(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = CARET;
+ int _saveIndex;
+
+ match('^');
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ public final void mBANG(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = BANG;
+ int _saveIndex;
+
+ match('!');
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ public final void mOR(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = OR;
+ int _saveIndex;
+
+ match('|');
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ public final void mWILDCARD(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = WILDCARD;
+ int _saveIndex;
+
+ match('.');
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ public final void mRANGE(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = RANGE;
+ int _saveIndex;
- public final void mWS(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = WS;
- int _saveIndex;
-
- {
- switch ( LA(1)) {
- case ' ':
- {
- match(' ');
- break;
- }
- case '\t':
- {
- match('\t');
- break;
- }
- case '\n':
- {
- match('\n');
- if ( inputState.guessing==0 ) {
- newline();
- }
- break;
- }
- default:
- if ((LA(1)=='\r') && (LA(2)=='\n')) {
- match('\r');
- match('\n');
- if ( inputState.guessing==0 ) {
- newline();
- }
- }
- else if ((LA(1)=='\r') && (true)) {
- match('\r');
- if ( inputState.guessing==0 ) {
- newline();
- }
- }
- else {
- throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());
- }
- }
- }
- if ( inputState.guessing==0 ) {
- _ttype = Token.SKIP;
- }
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
-
- public final void mCOMMENT(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = COMMENT;
- int _saveIndex;
- Token t=null;
-
- {
- if ((LA(1)=='/') && (LA(2)=='/')) {
- mSL_COMMENT(false);
- }
- else if ((LA(1)=='/') && (LA(2)=='*')) {
- mML_COMMENT(true);
- t=_returnToken;
- if ( inputState.guessing==0 ) {
- _ttype = t.getType();
- }
- }
- else {
- throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());
- }
-
- }
- if ( inputState.guessing==0 ) {
- if ( _ttype != DOC_COMMENT ) _ttype = Token.SKIP;
- }
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
-
- protected final void mSL_COMMENT(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = SL_COMMENT;
- int _saveIndex;
-
- match("//");
- {
- _loop153:
- do {
- if ((_tokenSet_0.member(LA(1)))) {
- {
- match(_tokenSet_0);
- }
- }
- else {
- break _loop153;
- }
-
- } while (true);
- }
- {
- if ((LA(1)=='\r') && (LA(2)=='\n')) {
- match('\r');
- match('\n');
- }
- else if ((LA(1)=='\r') && (true)) {
- match('\r');
- }
- else if ((LA(1)=='\n')) {
- match('\n');
- }
- else {
- throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());
- }
-
- }
- if ( inputState.guessing==0 ) {
- newline();
- }
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
-
- protected final void mML_COMMENT(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = ML_COMMENT;
- int _saveIndex;
-
- match("/*");
- {
- if (((LA(1)=='*') && ((LA(2) >= '\u0003' && LA(2) <= '~')))&&( LA(2)!='/' )) {
- match('*');
- if ( inputState.guessing==0 ) {
- _ttype = DOC_COMMENT;
- }
- }
- else if (((LA(1) >= '\u0003' && LA(1) <= '~')) && ((LA(2) >= '\u0003' && LA(2) <= '~'))) {
- }
- else {
- throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());
- }
-
- }
- {
- _loop159:
- do {
- // nongreedy exit test
- if ((LA(1)=='*') && (LA(2)=='/')) break _loop159;
- if ((LA(1)=='\r') && (LA(2)=='\n')) {
- match('\r');
- match('\n');
- if ( inputState.guessing==0 ) {
- newline();
- }
- }
- else if ((LA(1)=='\r') && ((LA(2) >= '\u0003' && LA(2) <= '~'))) {
- match('\r');
- if ( inputState.guessing==0 ) {
- newline();
- }
- }
- else if ((_tokenSet_0.member(LA(1))) && ((LA(2) >= '\u0003' && LA(2) <= '~'))) {
- {
- match(_tokenSet_0);
- }
- }
- else if ((LA(1)=='\n')) {
- match('\n');
- if ( inputState.guessing==0 ) {
- newline();
- }
- }
- else {
- break _loop159;
- }
-
- } while (true);
- }
- match("*/");
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
-
- public final void mOPEN_ELEMENT_OPTION(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = OPEN_ELEMENT_OPTION;
- int _saveIndex;
-
- match('<');
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
-
- public final void mCLOSE_ELEMENT_OPTION(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = CLOSE_ELEMENT_OPTION;
- int _saveIndex;
-
- match('>');
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
-
- public final void mCOMMA(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = COMMA;
- int _saveIndex;
-
- match(',');
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
-
- public final void mQUESTION(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = QUESTION;
- int _saveIndex;
-
- match('?');
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
-
- public final void mTREE_BEGIN(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = TREE_BEGIN;
- int _saveIndex;
-
- match("#(");
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
-
- public final void mLPAREN(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = LPAREN;
- int _saveIndex;
-
- match('(');
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
-
- public final void mRPAREN(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = RPAREN;
- int _saveIndex;
-
- match(')');
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
-
- public final void mCOLON(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = COLON;
- int _saveIndex;
-
- match(':');
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
-
- public final void mSTAR(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = STAR;
- int _saveIndex;
-
- match('*');
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
-
- public final void mPLUS(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = PLUS;
- int _saveIndex;
-
- match('+');
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
-
- public final void mASSIGN(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = ASSIGN;
- int _saveIndex;
-
- match('=');
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
-
- public final void mIMPLIES(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = IMPLIES;
- int _saveIndex;
-
- match("=>");
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
-
- public final void mSEMI(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = SEMI;
- int _saveIndex;
-
- match(';');
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
-
- public final void mCARET(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = CARET;
- int _saveIndex;
-
- match('^');
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
-
- public final void mBANG(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = BANG;
- int _saveIndex;
-
- match('!');
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
-
- public final void mOR(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = OR;
- int _saveIndex;
-
- match('|');
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
-
- public final void mWILDCARD(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = WILDCARD;
- int _saveIndex;
-
- match('.');
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
-
- public final void mRANGE(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = RANGE;
- int _saveIndex;
-
- match("..");
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
-
- public final void mNOT_OP(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = NOT_OP;
- int _saveIndex;
-
- match('~');
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
-
- public final void mRCURLY(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = RCURLY;
- int _saveIndex;
-
- match('}');
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
-
- public final void mCHAR_LITERAL(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = CHAR_LITERAL;
- int _saveIndex;
-
- match('\'');
- {
- switch ( LA(1)) {
- case '\\':
- {
- mESC(false);
- break;
- }
- case '\u0003': case '\u0004': case '\u0005': case '\u0006':
- case '\u0007': case '\u0008': case '\t': case '\n':
- case '\u000b': case '\u000c': case '\r': case '\u000e':
- case '\u000f': case '\u0010': case '\u0011': case '\u0012':
- case '\u0013': case '\u0014': case '\u0015': case '\u0016':
- case '\u0017': case '\u0018': case '\u0019': case '\u001a':
- case '\u001b': case '\u001c': case '\u001d': case '\u001e':
- case '\u001f': case ' ': case '!': case '"':
- case '#': case '$': case '%': case '&':
- case '(': case ')': case '*': case '+':
- case ',': case '-': case '.': case '/':
- case '0': case '1': case '2': case '3':
- case '4': case '5': case '6': case '7':
- case '8': case '9': case ':': case ';':
- case '<': case '=': case '>': case '?':
- case '@': case 'A': case 'B': case 'C':
- case 'D': case 'E': case 'F': case 'G':
- case 'H': case 'I': case 'J': case 'K':
- case 'L': case 'M': case 'N': case 'O':
- case 'P': case 'Q': case 'R': case 'S':
- case 'T': case 'U': case 'V': case 'W':
- case 'X': case 'Y': case 'Z': case '[':
- case ']': case '^': case '_': case '`':
- case 'a': case 'b': case 'c': case 'd':
- case 'e': case 'f': case 'g': case 'h':
- case 'i': case 'j': case 'k': case 'l':
- case 'm': case 'n': case 'o': case 'p':
- case 'q': case 'r': case 's': case 't':
- case 'u': case 'v': case 'w': case 'x':
- case 'y': case 'z': case '{': case '|':
- case '}': case '~':
- {
- matchNot('\'');
- break;
- }
- default:
- {
- throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());
- }
- }
- }
- match('\'');
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
-
- protected final void mESC(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = ESC;
- int _saveIndex;
-
- match('\\');
- {
- switch ( LA(1)) {
- case 'n':
- {
- match('n');
- break;
- }
- case 'r':
- {
- match('r');
- break;
- }
- case 't':
- {
- match('t');
- break;
- }
- case 'b':
- {
- match('b');
- break;
- }
- case 'f':
- {
- match('f');
- break;
- }
- case 'w':
- {
- match('w');
- break;
- }
- case 'a':
- {
- match('a');
- break;
- }
- case '"':
- {
- match('"');
- break;
- }
- case '\'':
- {
- match('\'');
- break;
- }
- case '\\':
- {
- match('\\');
- break;
- }
- case '0': case '1': case '2': case '3':
- {
- {
- matchRange('0','3');
- }
- {
- if (((LA(1) >= '0' && LA(1) <= '9')) && ((LA(2) >= '\u0003' && LA(2) <= '~'))) {
- {
- matchRange('0','9');
- }
- {
- if (((LA(1) >= '0' && LA(1) <= '9')) && ((LA(2) >= '\u0003' && LA(2) <= '~'))) {
- matchRange('0','9');
- }
- else if (((LA(1) >= '\u0003' && LA(1) <= '~')) && (true)) {
- }
- else {
- throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());
- }
-
- }
- }
- else if (((LA(1) >= '\u0003' && LA(1) <= '~')) && (true)) {
- }
- else {
- throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());
- }
-
- }
- break;
- }
- case '4': case '5': case '6': case '7':
- {
- {
- matchRange('4','7');
- }
- {
- if (((LA(1) >= '0' && LA(1) <= '9')) && ((LA(2) >= '\u0003' && LA(2) <= '~'))) {
- {
- matchRange('0','9');
- }
- }
- else if (((LA(1) >= '\u0003' && LA(1) <= '~')) && (true)) {
- }
- else {
- throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());
- }
-
- }
- break;
- }
- case 'u':
- {
- match('u');
- mXDIGIT(false);
- mXDIGIT(false);
- mXDIGIT(false);
- mXDIGIT(false);
- break;
- }
- default:
- {
- throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());
- }
- }
- }
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
-
- public final void mSTRING_LITERAL(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = STRING_LITERAL;
- int _saveIndex;
-
- match('"');
- {
- _loop184:
- do {
- switch ( LA(1)) {
- case '\\':
- {
- mESC(false);
- break;
- }
- case '\u0003': case '\u0004': case '\u0005': case '\u0006':
- case '\u0007': case '\u0008': case '\t': case '\n':
- case '\u000b': case '\u000c': case '\r': case '\u000e':
- case '\u000f': case '\u0010': case '\u0011': case '\u0012':
- case '\u0013': case '\u0014': case '\u0015': case '\u0016':
- case '\u0017': case '\u0018': case '\u0019': case '\u001a':
- case '\u001b': case '\u001c': case '\u001d': case '\u001e':
- case '\u001f': case ' ': case '!': case '#':
- case '$': case '%': case '&': case '\'':
- case '(': case ')': case '*': case '+':
- case ',': case '-': case '.': case '/':
- case '0': case '1': case '2': case '3':
- case '4': case '5': case '6': case '7':
- case '8': case '9': case ':': case ';':
- case '<': case '=': case '>': case '?':
- case '@': case 'A': case 'B': case 'C':
- case 'D': case 'E': case 'F': case 'G':
- case 'H': case 'I': case 'J': case 'K':
- case 'L': case 'M': case 'N': case 'O':
- case 'P': case 'Q': case 'R': case 'S':
- case 'T': case 'U': case 'V': case 'W':
- case 'X': case 'Y': case 'Z': case '[':
- case ']': case '^': case '_': case '`':
- case 'a': case 'b': case 'c': case 'd':
- case 'e': case 'f': case 'g': case 'h':
- case 'i': case 'j': case 'k': case 'l':
- case 'm': case 'n': case 'o': case 'p':
- case 'q': case 'r': case 's': case 't':
- case 'u': case 'v': case 'w': case 'x':
- case 'y': case 'z': case '{': case '|':
- case '}': case '~':
- {
- matchNot('"');
- break;
- }
- default:
- {
- break _loop184;
- }
- }
- } while (true);
- }
- match('"');
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
-
- protected final void mXDIGIT(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = XDIGIT;
- int _saveIndex;
-
- switch ( LA(1)) {
- case '0': case '1': case '2': case '3':
- case '4': case '5': case '6': case '7':
- case '8': case '9':
- {
- matchRange('0','9');
- break;
- }
- case 'a': case 'b': case 'c': case 'd':
- case 'e': case 'f':
- {
- matchRange('a','f');
- break;
- }
- case 'A': case 'B': case 'C': case 'D':
- case 'E': case 'F':
- {
- matchRange('A','F');
- break;
- }
- default:
- {
- throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());
- }
- }
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
-
- protected final void mDIGIT(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = DIGIT;
- int _saveIndex;
-
- matchRange('0','9');
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
-
- protected final void mVOCAB(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = VOCAB;
- int _saveIndex;
-
- matchRange('\3','\176');
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
-
- public final void mINT(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = INT;
- int _saveIndex;
-
- {
- int _cnt199=0;
- _loop199:
- do {
- if (((LA(1) >= '0' && LA(1) <= '9'))) {
- matchRange('0','9');
- }
- else {
- if ( _cnt199>=1 ) { break _loop199; } else {throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());}
- }
-
- _cnt199++;
- } while (true);
- }
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
-
- public final void mARG_ACTION(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = ARG_ACTION;
- int _saveIndex;
-
- mNESTED_ARG_ACTION(false);
- if ( inputState.guessing==0 ) {
- setText(Tool.stripFrontBack(getText(), "[", "]"));
- }
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
-
- protected final void mNESTED_ARG_ACTION(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = NESTED_ARG_ACTION;
- int _saveIndex;
-
- match('[');
- {
- _loop203:
- do {
- switch ( LA(1)) {
- case '[':
- {
- mNESTED_ARG_ACTION(false);
- break;
- }
- case '\n':
- {
- match('\n');
- if ( inputState.guessing==0 ) {
- newline();
- }
- break;
- }
- case '\'':
- {
- mCHAR_LITERAL(false);
- break;
- }
- case '"':
- {
- mSTRING_LITERAL(false);
- break;
- }
- case '\u0003': case '\u0004': case '\u0005': case '\u0006':
- case '\u0007': case '\u0008': case '\t': case '\u000b':
- case '\u000c': case '\u000e': case '\u000f': case '\u0010':
- case '\u0011': case '\u0012': case '\u0013': case '\u0014':
- case '\u0015': case '\u0016': case '\u0017': case '\u0018':
- case '\u0019': case '\u001a': case '\u001b': case '\u001c':
- case '\u001d': case '\u001e': case '\u001f': case ' ':
- case '!': case '#': case '$': case '%':
- case '&': case '(': case ')': case '*':
- case '+': case ',': case '-': case '.':
- case '/': case '0': case '1': case '2':
- case '3': case '4': case '5': case '6':
- case '7': case '8': case '9': case ':':
- case ';': case '<': case '=': case '>':
- case '?': case '@': case 'A': case 'B':
- case 'C': case 'D': case 'E': case 'F':
- case 'G': case 'H': case 'I': case 'J':
- case 'K': case 'L': case 'M': case 'N':
- case 'O': case 'P': case 'Q': case 'R':
- case 'S': case 'T': case 'U': case 'V':
- case 'W': case 'X': case 'Y': case 'Z':
- case '\\': case '^': case '_': case '`':
- case 'a': case 'b': case 'c': case 'd':
- case 'e': case 'f': case 'g': case 'h':
- case 'i': case 'j': case 'k': case 'l':
- case 'm': case 'n': case 'o': case 'p':
- case 'q': case 'r': case 's': case 't':
- case 'u': case 'v': case 'w': case 'x':
- case 'y': case 'z': case '{': case '|':
- case '}': case '~':
- {
- matchNot(']');
- break;
- }
- default:
- if ((LA(1)=='\r') && (LA(2)=='\n')) {
- match('\r');
- match('\n');
- if ( inputState.guessing==0 ) {
- newline();
- }
- }
- else if ((LA(1)=='\r') && ((LA(2) >= '\u0003' && LA(2) <= '~'))) {
- match('\r');
- if ( inputState.guessing==0 ) {
- newline();
- }
- }
- else {
- break _loop203;
- }
- }
- } while (true);
- }
- match(']');
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
-
- public final void mACTION(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = ACTION;
- int _saveIndex;
- int actionLine=getLine();
-
- mNESTED_ACTION(false);
- {
- if ((LA(1)=='?')) {
- match('?');
- if ( inputState.guessing==0 ) {
- _ttype = SEMPRED;
- }
- }
- else {
- }
-
- }
- if ( inputState.guessing==0 ) {
-
- if ( _ttype==ACTION ) {
- setText(Tool.stripFrontBack(getText(), "{", "}"));
- }
- else {
- setText(Tool.stripFrontBack(getText(), "{", "}?"));
- }
- CommonToken t = new CommonToken(_ttype,new String(text.getBuffer(),_begin,text.length()-_begin));
- t.setLine(actionLine); // set action line to start
- _token = t;
-
- }
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
-
- protected final void mNESTED_ACTION(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = NESTED_ACTION;
- int _saveIndex;
-
- match('{');
- {
- _loop209:
- do {
- // nongreedy exit test
- if ((LA(1)=='}') && (true)) break _loop209;
- if ((LA(1)=='\n'||LA(1)=='\r') && ((LA(2) >= '\u0003' && LA(2) <= '~'))) {
- {
- if ((LA(1)=='\r') && (LA(2)=='\n')) {
- match('\r');
- match('\n');
- if ( inputState.guessing==0 ) {
- newline();
- }
- }
- else if ((LA(1)=='\r') && ((LA(2) >= '\u0003' && LA(2) <= '~'))) {
- match('\r');
- if ( inputState.guessing==0 ) {
- newline();
- }
- }
- else if ((LA(1)=='\n')) {
- match('\n');
- if ( inputState.guessing==0 ) {
- newline();
- }
- }
- else {
- throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());
- }
-
- }
- }
- else if ((LA(1)=='{') && ((LA(2) >= '\u0003' && LA(2) <= '~'))) {
- mNESTED_ACTION(false);
- }
- else if ((LA(1)=='\'') && (_tokenSet_1.member(LA(2)))) {
- mCHAR_LITERAL(false);
- }
- else if ((LA(1)=='/') && (LA(2)=='*'||LA(2)=='/')) {
- mCOMMENT(false);
- }
- else if ((LA(1)=='"') && ((LA(2) >= '\u0003' && LA(2) <= '~'))) {
- mSTRING_LITERAL(false);
- }
- else if (((LA(1) >= '\u0003' && LA(1) <= '~')) && ((LA(2) >= '\u0003' && LA(2) <= '~'))) {
- matchNot(EOF_CHAR);
- }
- else {
- break _loop209;
- }
-
- } while (true);
- }
- match('}');
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
-
- public final void mTOKEN_REF(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = TOKEN_REF;
- int _saveIndex;
-
- matchRange('A','Z');
- {
- _loop212:
- do {
- switch ( LA(1)) {
- case 'a': case 'b': case 'c': case 'd':
- case 'e': case 'f': case 'g': case 'h':
- case 'i': case 'j': case 'k': case 'l':
- case 'm': case 'n': case 'o': case 'p':
- case 'q': case 'r': case 's': case 't':
- case 'u': case 'v': case 'w': case 'x':
- case 'y': case 'z':
- {
- matchRange('a','z');
- break;
- }
- case 'A': case 'B': case 'C': case 'D':
- case 'E': case 'F': case 'G': case 'H':
- case 'I': case 'J': case 'K': case 'L':
- case 'M': case 'N': case 'O': case 'P':
- case 'Q': case 'R': case 'S': case 'T':
- case 'U': case 'V': case 'W': case 'X':
- case 'Y': case 'Z':
- {
- matchRange('A','Z');
- break;
- }
- case '_':
- {
- match('_');
- break;
- }
- case '0': case '1': case '2': case '3':
- case '4': case '5': case '6': case '7':
- case '8': case '9':
- {
- matchRange('0','9');
- break;
- }
- default:
- {
- break _loop212;
- }
- }
- } while (true);
- }
- _ttype = testLiteralsTable(_ttype);
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
-
- public final void mRULE_REF(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = RULE_REF;
- int _saveIndex;
-
- int t=0;
-
-
- t=mINTERNAL_RULE_REF(false);
- if ( inputState.guessing==0 ) {
- _ttype=t;
- }
- {
- if (true&&(t==LITERAL_options)) {
- mWS_LOOP(false);
- {
- if ((LA(1)=='{')) {
- match('{');
- if ( inputState.guessing==0 ) {
- _ttype = OPTIONS;
- }
- }
- else {
- }
-
- }
- }
- else if (true&&(t==LITERAL_tokens)) {
- mWS_LOOP(false);
- {
- if ((LA(1)=='{')) {
- match('{');
- if ( inputState.guessing==0 ) {
- _ttype = TOKENS;
- }
- }
- else {
- }
-
- }
- }
- else {
- }
-
- }
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
-
- protected final int mINTERNAL_RULE_REF(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
- int t;
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = INTERNAL_RULE_REF;
- int _saveIndex;
-
- t = RULE_REF;
-
-
- matchRange('a','z');
- {
- _loop222:
- do {
- switch ( LA(1)) {
- case 'a': case 'b': case 'c': case 'd':
- case 'e': case 'f': case 'g': case 'h':
- case 'i': case 'j': case 'k': case 'l':
- case 'm': case 'n': case 'o': case 'p':
- case 'q': case 'r': case 's': case 't':
- case 'u': case 'v': case 'w': case 'x':
- case 'y': case 'z':
- {
- matchRange('a','z');
- break;
- }
- case 'A': case 'B': case 'C': case 'D':
- case 'E': case 'F': case 'G': case 'H':
- case 'I': case 'J': case 'K': case 'L':
- case 'M': case 'N': case 'O': case 'P':
- case 'Q': case 'R': case 'S': case 'T':
- case 'U': case 'V': case 'W': case 'X':
- case 'Y': case 'Z':
- {
- matchRange('A','Z');
- break;
- }
- case '_':
- {
- match('_');
- break;
- }
- case '0': case '1': case '2': case '3':
- case '4': case '5': case '6': case '7':
- case '8': case '9':
- {
- matchRange('0','9');
- break;
- }
- default:
- {
- break _loop222;
- }
- }
- } while (true);
- }
- if ( inputState.guessing==0 ) {
- t = testLiteralsTable(t);
- }
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- return t;
- }
-
- protected final void mWS_LOOP(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = WS_LOOP;
- int _saveIndex;
-
- {
- _loop219:
- do {
- switch ( LA(1)) {
- case '\t': case '\n': case '\r': case ' ':
- {
- mWS(false);
- break;
- }
- case '/':
- {
- mCOMMENT(false);
- break;
- }
- default:
- {
- break _loop219;
- }
- }
- } while (true);
- }
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
-
- protected final void mWS_OPT(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = WS_OPT;
- int _saveIndex;
-
- {
- if ((_tokenSet_2.member(LA(1)))) {
- mWS(false);
- }
- else {
- }
-
- }
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
-
- protected final void mNOT_USEFUL(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = NOT_USEFUL;
- int _saveIndex;
-
- boolean synPredMatched227 = false;
- if (((LA(1)=='a') && (true))) {
- int _m227 = mark();
- synPredMatched227 = true;
- inputState.guessing++;
- try {
- {
- match('a');
- }
- }
- catch (RecognitionException pe) {
- synPredMatched227 = false;
- }
- rewind(_m227);
- inputState.guessing--;
- }
- if ( synPredMatched227 ) {
- match('a');
- }
- else if ((LA(1)=='a') && (true)) {
- match('a');
- }
- else {
- throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());
- }
-
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
-
-
- private static final long _tokenSet_0_data_[] = { -9224L, 9223372036854775807L, 0L, 0L };
- public static final BitSet _tokenSet_0 = new BitSet(_tokenSet_0_data_);
- private static final long _tokenSet_1_data_[] = { -549755813896L, 9223372036854775807L, 0L, 0L };
- public static final BitSet _tokenSet_1 = new BitSet(_tokenSet_1_data_);
- private static final long _tokenSet_2_data_[] = { 4294977024L, 0L, 0L };
- public static final BitSet _tokenSet_2 = new BitSet(_tokenSet_2_data_);
-
- }
+ match("..");
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ public final void mNOT_OP(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = NOT_OP;
+ int _saveIndex;
+
+ match('~');
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ public final void mRCURLY(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = RCURLY;
+ int _saveIndex;
+
+ match('}');
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ public final void mCHAR_LITERAL(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = CHAR_LITERAL;
+ int _saveIndex;
+
+ match('\'');
+ {
+ switch (LA(1)) {
+ case '\\':
+ {
+ mESC(false);
+ break;
+ }
+ case '\u0003':
+ case '\u0004':
+ case '\u0005':
+ case '\u0006':
+ case '\u0007':
+ case '\u0008':
+ case '\t':
+ case '\n':
+ case '\u000b':
+ case '\u000c':
+ case '\r':
+ case '\u000e':
+ case '\u000f':
+ case '\u0010':
+ case '\u0011':
+ case '\u0012':
+ case '\u0013':
+ case '\u0014':
+ case '\u0015':
+ case '\u0016':
+ case '\u0017':
+ case '\u0018':
+ case '\u0019':
+ case '\u001a':
+ case '\u001b':
+ case '\u001c':
+ case '\u001d':
+ case '\u001e':
+ case '\u001f':
+ case ' ':
+ case '!':
+ case '"':
+ case '#':
+ case '$':
+ case '%':
+ case '&':
+ case '(':
+ case ')':
+ case '*':
+ case '+':
+ case ',':
+ case '-':
+ case '.':
+ case '/':
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ case '9':
+ case ':':
+ case ';':
+ case '<':
+ case '=':
+ case '>':
+ case '?':
+ case '@':
+ case 'A':
+ case 'B':
+ case 'C':
+ case 'D':
+ case 'E':
+ case 'F':
+ case 'G':
+ case 'H':
+ case 'I':
+ case 'J':
+ case 'K':
+ case 'L':
+ case 'M':
+ case 'N':
+ case 'O':
+ case 'P':
+ case 'Q':
+ case 'R':
+ case 'S':
+ case 'T':
+ case 'U':
+ case 'V':
+ case 'W':
+ case 'X':
+ case 'Y':
+ case 'Z':
+ case '[':
+ case ']':
+ case '^':
+ case '_':
+ case '`':
+ case 'a':
+ case 'b':
+ case 'c':
+ case 'd':
+ case 'e':
+ case 'f':
+ case 'g':
+ case 'h':
+ case 'i':
+ case 'j':
+ case 'k':
+ case 'l':
+ case 'm':
+ case 'n':
+ case 'o':
+ case 'p':
+ case 'q':
+ case 'r':
+ case 's':
+ case 't':
+ case 'u':
+ case 'v':
+ case 'w':
+ case 'x':
+ case 'y':
+ case 'z':
+ case '{':
+ case '|':
+ case '}':
+ case '~':
+ {
+ matchNot('\'');
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());
+ }
+ }
+ }
+ match('\'');
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ protected final void mESC(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = ESC;
+ int _saveIndex;
+
+ match('\\');
+ {
+ switch (LA(1)) {
+ case 'n':
+ {
+ match('n');
+ break;
+ }
+ case 'r':
+ {
+ match('r');
+ break;
+ }
+ case 't':
+ {
+ match('t');
+ break;
+ }
+ case 'b':
+ {
+ match('b');
+ break;
+ }
+ case 'f':
+ {
+ match('f');
+ break;
+ }
+ case 'w':
+ {
+ match('w');
+ break;
+ }
+ case 'a':
+ {
+ match('a');
+ break;
+ }
+ case '"':
+ {
+ match('"');
+ break;
+ }
+ case '\'':
+ {
+ match('\'');
+ break;
+ }
+ case '\\':
+ {
+ match('\\');
+ break;
+ }
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ {
+ {
+ matchRange('0', '3');
+ }
+ {
+ if (((LA(1) >= '0' && LA(1) <= '9')) && ((LA(2) >= '\u0003' && LA(2) <= '~'))) {
+ {
+ matchRange('0', '9');
+ }
+ {
+ if (((LA(1) >= '0' && LA(1) <= '9')) && ((LA(2) >= '\u0003' && LA(2) <= '~'))) {
+ matchRange('0', '9');
+ } else if (((LA(1) >= '\u0003' && LA(1) <= '~')) && (true)) {
+ } else {
+ throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());
+ }
+
+ }
+ } else if (((LA(1) >= '\u0003' && LA(1) <= '~')) && (true)) {
+ } else {
+ throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());
+ }
+
+ }
+ break;
+ }
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ {
+ {
+ matchRange('4', '7');
+ }
+ {
+ if (((LA(1) >= '0' && LA(1) <= '9')) && ((LA(2) >= '\u0003' && LA(2) <= '~'))) {
+ {
+ matchRange('0', '9');
+ }
+ } else if (((LA(1) >= '\u0003' && LA(1) <= '~')) && (true)) {
+ } else {
+ throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());
+ }
+
+ }
+ break;
+ }
+ case 'u':
+ {
+ match('u');
+ mXDIGIT(false);
+ mXDIGIT(false);
+ mXDIGIT(false);
+ mXDIGIT(false);
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());
+ }
+ }
+ }
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ public final void mSTRING_LITERAL(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = STRING_LITERAL;
+ int _saveIndex;
+
+ match('"');
+ {
+ _loop184:
+ do {
+ switch (LA(1)) {
+ case '\\':
+ {
+ mESC(false);
+ break;
+ }
+ case '\u0003':
+ case '\u0004':
+ case '\u0005':
+ case '\u0006':
+ case '\u0007':
+ case '\u0008':
+ case '\t':
+ case '\n':
+ case '\u000b':
+ case '\u000c':
+ case '\r':
+ case '\u000e':
+ case '\u000f':
+ case '\u0010':
+ case '\u0011':
+ case '\u0012':
+ case '\u0013':
+ case '\u0014':
+ case '\u0015':
+ case '\u0016':
+ case '\u0017':
+ case '\u0018':
+ case '\u0019':
+ case '\u001a':
+ case '\u001b':
+ case '\u001c':
+ case '\u001d':
+ case '\u001e':
+ case '\u001f':
+ case ' ':
+ case '!':
+ case '#':
+ case '$':
+ case '%':
+ case '&':
+ case '\'':
+ case '(':
+ case ')':
+ case '*':
+ case '+':
+ case ',':
+ case '-':
+ case '.':
+ case '/':
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ case '9':
+ case ':':
+ case ';':
+ case '<':
+ case '=':
+ case '>':
+ case '?':
+ case '@':
+ case 'A':
+ case 'B':
+ case 'C':
+ case 'D':
+ case 'E':
+ case 'F':
+ case 'G':
+ case 'H':
+ case 'I':
+ case 'J':
+ case 'K':
+ case 'L':
+ case 'M':
+ case 'N':
+ case 'O':
+ case 'P':
+ case 'Q':
+ case 'R':
+ case 'S':
+ case 'T':
+ case 'U':
+ case 'V':
+ case 'W':
+ case 'X':
+ case 'Y':
+ case 'Z':
+ case '[':
+ case ']':
+ case '^':
+ case '_':
+ case '`':
+ case 'a':
+ case 'b':
+ case 'c':
+ case 'd':
+ case 'e':
+ case 'f':
+ case 'g':
+ case 'h':
+ case 'i':
+ case 'j':
+ case 'k':
+ case 'l':
+ case 'm':
+ case 'n':
+ case 'o':
+ case 'p':
+ case 'q':
+ case 'r':
+ case 's':
+ case 't':
+ case 'u':
+ case 'v':
+ case 'w':
+ case 'x':
+ case 'y':
+ case 'z':
+ case '{':
+ case '|':
+ case '}':
+ case '~':
+ {
+ matchNot('"');
+ break;
+ }
+ default:
+ {
+ break _loop184;
+ }
+ }
+ } while (true);
+ }
+ match('"');
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ protected final void mXDIGIT(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = XDIGIT;
+ int _saveIndex;
+
+ switch (LA(1)) {
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ case '9':
+ {
+ matchRange('0', '9');
+ break;
+ }
+ case 'a':
+ case 'b':
+ case 'c':
+ case 'd':
+ case 'e':
+ case 'f':
+ {
+ matchRange('a', 'f');
+ break;
+ }
+ case 'A':
+ case 'B':
+ case 'C':
+ case 'D':
+ case 'E':
+ case 'F':
+ {
+ matchRange('A', 'F');
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());
+ }
+ }
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ protected final void mDIGIT(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = DIGIT;
+ int _saveIndex;
+
+ matchRange('0', '9');
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ protected final void mVOCAB(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = VOCAB;
+ int _saveIndex;
+
+ matchRange('\3', '\176');
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ public final void mINT(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = INT;
+ int _saveIndex;
+
+ {
+ int _cnt199 = 0;
+ _loop199:
+ do {
+ if (((LA(1) >= '0' && LA(1) <= '9'))) {
+ matchRange('0', '9');
+ } else {
+ if (_cnt199 >= 1) {
+ break _loop199;
+ } else {
+ throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());
+ }
+ }
+
+ _cnt199++;
+ } while (true);
+ }
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ public final void mARG_ACTION(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = ARG_ACTION;
+ int _saveIndex;
+
+ mNESTED_ARG_ACTION(false);
+ if (inputState.guessing == 0) {
+ setText(Tool.stripFrontBack(getText(), "[", "]"));
+ }
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ protected final void mNESTED_ARG_ACTION(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = NESTED_ARG_ACTION;
+ int _saveIndex;
+
+ match('[');
+ {
+ _loop203:
+ do {
+ switch (LA(1)) {
+ case '[':
+ {
+ mNESTED_ARG_ACTION(false);
+ break;
+ }
+ case '\n':
+ {
+ match('\n');
+ if (inputState.guessing == 0) {
+ newline();
+ }
+ break;
+ }
+ case '\'':
+ {
+ mCHAR_LITERAL(false);
+ break;
+ }
+ case '"':
+ {
+ mSTRING_LITERAL(false);
+ break;
+ }
+ case '\u0003':
+ case '\u0004':
+ case '\u0005':
+ case '\u0006':
+ case '\u0007':
+ case '\u0008':
+ case '\t':
+ case '\u000b':
+ case '\u000c':
+ case '\u000e':
+ case '\u000f':
+ case '\u0010':
+ case '\u0011':
+ case '\u0012':
+ case '\u0013':
+ case '\u0014':
+ case '\u0015':
+ case '\u0016':
+ case '\u0017':
+ case '\u0018':
+ case '\u0019':
+ case '\u001a':
+ case '\u001b':
+ case '\u001c':
+ case '\u001d':
+ case '\u001e':
+ case '\u001f':
+ case ' ':
+ case '!':
+ case '#':
+ case '$':
+ case '%':
+ case '&':
+ case '(':
+ case ')':
+ case '*':
+ case '+':
+ case ',':
+ case '-':
+ case '.':
+ case '/':
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ case '9':
+ case ':':
+ case ';':
+ case '<':
+ case '=':
+ case '>':
+ case '?':
+ case '@':
+ case 'A':
+ case 'B':
+ case 'C':
+ case 'D':
+ case 'E':
+ case 'F':
+ case 'G':
+ case 'H':
+ case 'I':
+ case 'J':
+ case 'K':
+ case 'L':
+ case 'M':
+ case 'N':
+ case 'O':
+ case 'P':
+ case 'Q':
+ case 'R':
+ case 'S':
+ case 'T':
+ case 'U':
+ case 'V':
+ case 'W':
+ case 'X':
+ case 'Y':
+ case 'Z':
+ case '\\':
+ case '^':
+ case '_':
+ case '`':
+ case 'a':
+ case 'b':
+ case 'c':
+ case 'd':
+ case 'e':
+ case 'f':
+ case 'g':
+ case 'h':
+ case 'i':
+ case 'j':
+ case 'k':
+ case 'l':
+ case 'm':
+ case 'n':
+ case 'o':
+ case 'p':
+ case 'q':
+ case 'r':
+ case 's':
+ case 't':
+ case 'u':
+ case 'v':
+ case 'w':
+ case 'x':
+ case 'y':
+ case 'z':
+ case '{':
+ case '|':
+ case '}':
+ case '~':
+ {
+ matchNot(']');
+ break;
+ }
+ default:
+ if ((LA(1) == '\r') && (LA(2) == '\n')) {
+ match('\r');
+ match('\n');
+ if (inputState.guessing == 0) {
+ newline();
+ }
+ } else if ((LA(1) == '\r') && ((LA(2) >= '\u0003' && LA(2) <= '~'))) {
+ match('\r');
+ if (inputState.guessing == 0) {
+ newline();
+ }
+ } else {
+ break _loop203;
+ }
+ }
+ } while (true);
+ }
+ match(']');
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ public final void mACTION(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = ACTION;
+ int _saveIndex;
+ int actionLine = getLine();
+
+ mNESTED_ACTION(false);
+ {
+ if ((LA(1) == '?')) {
+ match('?');
+ if (inputState.guessing == 0) {
+ _ttype = SEMPRED;
+ }
+ } else {
+ }
+
+ }
+ if (inputState.guessing == 0) {
+
+ if (_ttype == ACTION) {
+ setText(Tool.stripFrontBack(getText(), "{", "}"));
+ } else {
+ setText(Tool.stripFrontBack(getText(), "{", "}?"));
+ }
+ CommonToken t = new CommonToken(_ttype, new String(text.getBuffer(), _begin, text.length() - _begin));
+ t.setLine(actionLine); // set action line to start
+ _token = t;
+
+ }
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ protected final void mNESTED_ACTION(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = NESTED_ACTION;
+ int _saveIndex;
+
+ match('{');
+ {
+ _loop209:
+ do {
+ // nongreedy exit test
+ if ((LA(1) == '}') && (true)) break _loop209;
+ if ((LA(1) == '\n' || LA(1) == '\r') && ((LA(2) >= '\u0003' && LA(2) <= '~'))) {
+ {
+ if ((LA(1) == '\r') && (LA(2) == '\n')) {
+ match('\r');
+ match('\n');
+ if (inputState.guessing == 0) {
+ newline();
+ }
+ } else if ((LA(1) == '\r') && ((LA(2) >= '\u0003' && LA(2) <= '~'))) {
+ match('\r');
+ if (inputState.guessing == 0) {
+ newline();
+ }
+ } else if ((LA(1) == '\n')) {
+ match('\n');
+ if (inputState.guessing == 0) {
+ newline();
+ }
+ } else {
+ throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());
+ }
+
+ }
+ } else if ((LA(1) == '{') && ((LA(2) >= '\u0003' && LA(2) <= '~'))) {
+ mNESTED_ACTION(false);
+ } else if ((LA(1) == '\'') && (_tokenSet_1.member(LA(2)))) {
+ mCHAR_LITERAL(false);
+ } else if ((LA(1) == '/') && (LA(2) == '*' || LA(2) == '/')) {
+ mCOMMENT(false);
+ } else if ((LA(1) == '"') && ((LA(2) >= '\u0003' && LA(2) <= '~'))) {
+ mSTRING_LITERAL(false);
+ } else if (((LA(1) >= '\u0003' && LA(1) <= '~')) && ((LA(2) >= '\u0003' && LA(2) <= '~'))) {
+ matchNot(EOF_CHAR);
+ } else {
+ break _loop209;
+ }
+
+ } while (true);
+ }
+ match('}');
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ public final void mTOKEN_REF(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = TOKEN_REF;
+ int _saveIndex;
+
+ matchRange('A', 'Z');
+ {
+ _loop212:
+ do {
+ switch (LA(1)) {
+ case 'a':
+ case 'b':
+ case 'c':
+ case 'd':
+ case 'e':
+ case 'f':
+ case 'g':
+ case 'h':
+ case 'i':
+ case 'j':
+ case 'k':
+ case 'l':
+ case 'm':
+ case 'n':
+ case 'o':
+ case 'p':
+ case 'q':
+ case 'r':
+ case 's':
+ case 't':
+ case 'u':
+ case 'v':
+ case 'w':
+ case 'x':
+ case 'y':
+ case 'z':
+ {
+ matchRange('a', 'z');
+ break;
+ }
+ case 'A':
+ case 'B':
+ case 'C':
+ case 'D':
+ case 'E':
+ case 'F':
+ case 'G':
+ case 'H':
+ case 'I':
+ case 'J':
+ case 'K':
+ case 'L':
+ case 'M':
+ case 'N':
+ case 'O':
+ case 'P':
+ case 'Q':
+ case 'R':
+ case 'S':
+ case 'T':
+ case 'U':
+ case 'V':
+ case 'W':
+ case 'X':
+ case 'Y':
+ case 'Z':
+ {
+ matchRange('A', 'Z');
+ break;
+ }
+ case '_':
+ {
+ match('_');
+ break;
+ }
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ case '9':
+ {
+ matchRange('0', '9');
+ break;
+ }
+ default:
+ {
+ break _loop212;
+ }
+ }
+ } while (true);
+ }
+ _ttype = testLiteralsTable(_ttype);
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ public final void mRULE_REF(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = RULE_REF;
+ int _saveIndex;
+
+ int t = 0;
+
+
+ t = mINTERNAL_RULE_REF(false);
+ if (inputState.guessing == 0) {
+ _ttype = t;
+ }
+ {
+ if (true && (t == LITERAL_options)) {
+ mWS_LOOP(false);
+ {
+ if ((LA(1) == '{')) {
+ match('{');
+ if (inputState.guessing == 0) {
+ _ttype = OPTIONS;
+ }
+ } else {
+ }
+
+ }
+ } else if (true && (t == LITERAL_tokens)) {
+ mWS_LOOP(false);
+ {
+ if ((LA(1) == '{')) {
+ match('{');
+ if (inputState.guessing == 0) {
+ _ttype = TOKENS;
+ }
+ } else {
+ }
+
+ }
+ } else {
+ }
+
+ }
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ protected final int mINTERNAL_RULE_REF(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
+ int t;
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = INTERNAL_RULE_REF;
+ int _saveIndex;
+
+ t = RULE_REF;
+
+
+ matchRange('a', 'z');
+ {
+ _loop222:
+ do {
+ switch (LA(1)) {
+ case 'a':
+ case 'b':
+ case 'c':
+ case 'd':
+ case 'e':
+ case 'f':
+ case 'g':
+ case 'h':
+ case 'i':
+ case 'j':
+ case 'k':
+ case 'l':
+ case 'm':
+ case 'n':
+ case 'o':
+ case 'p':
+ case 'q':
+ case 'r':
+ case 's':
+ case 't':
+ case 'u':
+ case 'v':
+ case 'w':
+ case 'x':
+ case 'y':
+ case 'z':
+ {
+ matchRange('a', 'z');
+ break;
+ }
+ case 'A':
+ case 'B':
+ case 'C':
+ case 'D':
+ case 'E':
+ case 'F':
+ case 'G':
+ case 'H':
+ case 'I':
+ case 'J':
+ case 'K':
+ case 'L':
+ case 'M':
+ case 'N':
+ case 'O':
+ case 'P':
+ case 'Q':
+ case 'R':
+ case 'S':
+ case 'T':
+ case 'U':
+ case 'V':
+ case 'W':
+ case 'X':
+ case 'Y':
+ case 'Z':
+ {
+ matchRange('A', 'Z');
+ break;
+ }
+ case '_':
+ {
+ match('_');
+ break;
+ }
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ case '9':
+ {
+ matchRange('0', '9');
+ break;
+ }
+ default:
+ {
+ break _loop222;
+ }
+ }
+ } while (true);
+ }
+ if (inputState.guessing == 0) {
+ t = testLiteralsTable(t);
+ }
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ return t;
+ }
+
+ protected final void mWS_LOOP(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = WS_LOOP;
+ int _saveIndex;
+
+ {
+ _loop219:
+ do {
+ switch (LA(1)) {
+ case '\t':
+ case '\n':
+ case '\r':
+ case ' ':
+ {
+ mWS(false);
+ break;
+ }
+ case '/':
+ {
+ mCOMMENT(false);
+ break;
+ }
+ default:
+ {
+ break _loop219;
+ }
+ }
+ } while (true);
+ }
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ protected final void mWS_OPT(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = WS_OPT;
+ int _saveIndex;
+
+ {
+ if ((_tokenSet_2.member(LA(1)))) {
+ mWS(false);
+ } else {
+ }
+
+ }
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ protected final void mNOT_USEFUL(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = NOT_USEFUL;
+ int _saveIndex;
+
+ boolean synPredMatched227 = false;
+ if (((LA(1) == 'a') && (true))) {
+ int _m227 = mark();
+ synPredMatched227 = true;
+ inputState.guessing++;
+ try {
+ {
+ match('a');
+ }
+ } catch (RecognitionException pe) {
+ synPredMatched227 = false;
+ }
+ rewind(_m227);
+ inputState.guessing--;
+ }
+ if (synPredMatched227) {
+ match('a');
+ } else if ((LA(1) == 'a') && (true)) {
+ match('a');
+ } else {
+ throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());
+ }
+
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+
+ private static final long _tokenSet_0_data_[] = { -9224L, 9223372036854775807L, 0L, 0L };
+
+ public static final BitSet _tokenSet_0 = new BitSet(_tokenSet_0_data_);
+
+ private static final long _tokenSet_1_data_[] = { -549755813896L, 9223372036854775807L, 0L, 0L };
+
+ public static final BitSet _tokenSet_1 = new BitSet(_tokenSet_1_data_);
+
+ private static final long _tokenSet_2_data_[] = { 4294977024L, 0L, 0L };
+
+ public static final BitSet _tokenSet_2 = new BitSet(_tokenSet_2_data_);
+
+}
1.2 +2902 -2913e/src/jsrc/antlr/ANTLRParser.java
Index: ANTLRParser.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/ANTLRParser.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ANTLRParser.java 2001/10/03 21:04:13 1.1
+++ ANTLRParser.java 2001/10/04 01:09:13 1.2
@@ -2,2926 +2,2915 @@
package antlr;
-import antlr.TokenBuffer;
-import antlr.TokenStreamException;
-import antlr.TokenStreamIOException;
-import antlr.ANTLRException;
-import antlr.LLkParser;
-import antlr.Token;
-import antlr.TokenStream;
-import antlr.RecognitionException;
-import antlr.NoViableAltException;
-import antlr.MismatchedTokenException;
-import antlr.SemanticException;
-import antlr.ParserSharedInputState;
import antlr.collections.impl.BitSet;
-import antlr.collections.AST;
-import antlr.ASTPair;
-import antlr.collections.impl.ASTArray;
-
-import java.util.Enumeration;
-import java.io.DataInputStream;
-import java.io.InputStream;
-import java.io.FileInputStream;
-import java.io.IOException;
public class ANTLRParser extends antlr.LLkParser
- implements ANTLRTokenTypes
- {
+ implements ANTLRTokenTypes {
- private static final boolean DEBUG_PARSER = false;
+ private static final boolean DEBUG_PARSER = false;
- ANTLRGrammarParseBehavior behavior;
- Tool tool;
- protected int blockNesting= -1;
-
- public ANTLRParser(
- TokenBuffer tokenBuf,
- ANTLRGrammarParseBehavior behavior_,
- Tool tool_
- ) {
- super(tokenBuf, 1);
- tokenNames = _tokenNames;
- behavior = behavior_;
- tool = tool_;
- }
-
- private boolean lastInRule() throws TokenStreamException {
- if ( blockNesting==0 && (LA(1)==SEMI || LA(1)==LITERAL_exception || LA(1)==OR) ) {
- return true;
- }
- return false;
- }
-
- private void checkForMissingEndRule(Token label) {
- if ( label.getColumn()==1 ) {
- Tool.warning("did you forget to terminate previous rule?", getFilename(), label.getLine());
- }
- }
-
-protected ANTLRParser(TokenBuffer tokenBuf, int k) {
- super(tokenBuf,k);
- tokenNames = _tokenNames;
-}
+ ANTLRGrammarParseBehavior behavior;
-public ANTLRParser(TokenBuffer tokenBuf) {
- this(tokenBuf,2);
-}
+ Tool tool;
-protected ANTLRParser(TokenStream lexer, int k) {
- super(lexer,k);
- tokenNames = _tokenNames;
-}
+ protected int blockNesting = -1;
-public ANTLRParser(TokenStream lexer) {
- this(lexer,2);
-}
+ public ANTLRParser(
+ TokenBuffer tokenBuf,
+ ANTLRGrammarParseBehavior behavior_,
+ Tool tool_
+ ) {
+ super(tokenBuf, 1);
+ tokenNames = _tokenNames;
+ behavior = behavior_;
+ tool = tool_;
+ }
-public ANTLRParser(ParserSharedInputState state) {
- super(state,2);
- tokenNames = _tokenNames;
-}
+ private boolean lastInRule() throws TokenStreamException {
+ if (blockNesting == 0 && (LA(1) == SEMI || LA(1) == LITERAL_exception || LA(1) == OR)) {
+ return true;
+ }
+ return false;
+ }
+
+ private void checkForMissingEndRule(Token label) {
+ if (label.getColumn() == 1) {
+ Tool.warning("did you forget to terminate previous rule?", getFilename(), label.getLine());
+ }
+ }
+
+ protected ANTLRParser(TokenBuffer tokenBuf, int k) {
+ super(tokenBuf, k);
+ tokenNames = _tokenNames;
+ }
+
+ public ANTLRParser(TokenBuffer tokenBuf) {
+ this(tokenBuf, 2);
+ }
+
+ protected ANTLRParser(TokenStream lexer, int k) {
+ super(lexer, k);
+ tokenNames = _tokenNames;
+ }
+
+ public ANTLRParser(TokenStream lexer) {
+ this(lexer, 2);
+ }
+
+ public ANTLRParser(ParserSharedInputState state) {
+ super(state, 2);
+ tokenNames = _tokenNames;
+ }
+
+ public final void grammar() throws RecognitionException, TokenStreamException {
+
+ Token n = null;
+ Token h = null;
+
+ try {
+ // for error handling
+ {
+ _loop4:
+ do {
+ if ((LA(1) == LITERAL_header)) {
+ match(LITERAL_header);
+ {
+ switch (LA(1)) {
+ case STRING_LITERAL:
+ {
+ n = LT(1);
+ match(STRING_LITERAL);
+ break;
+ }
+ case ACTION:
+ {
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ h = LT(1);
+ match(ACTION);
+ if (inputState.guessing == 0) {
+ behavior.refHeaderAction(n, h);
+ }
+ } else {
+ break _loop4;
+ }
+
+ } while (true);
+ }
+ {
+ switch (LA(1)) {
+ case OPTIONS:
+ {
+ fileOptionsSpec();
+ break;
+ }
+ case EOF:
+ case ACTION:
+ case DOC_COMMENT:
+ case LITERAL_lexclass:
+ case LITERAL_class:
+ {
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ {
+ _loop7:
+ do {
+ if (((LA(1) >= ACTION && LA(1) <= LITERAL_class))) {
+ classDef();
+ } else {
+ break _loop7;
+ }
+
+ } while (true);
+ }
+ match(Token.EOF_TYPE);
+ } catch (RecognitionException ex) {
+ if (inputState.guessing == 0) {
+
+ reportError("rule grammar trapped: " + ex.toString());
+ consumeUntil(EOF);
+
+ } else {
+ throw ex;
+ }
+ }
+ }
+
+ public final void fileOptionsSpec() throws RecognitionException, TokenStreamException {
+
+ Token idTok;
+ Token value;
+
+ match(OPTIONS);
+ {
+ _loop18:
+ do {
+ if ((LA(1) == TOKEN_REF || LA(1) == RULE_REF)) {
+ idTok = id();
+ match(ASSIGN);
+ value = optionValue();
+ if (inputState.guessing == 0) {
+ behavior.setFileOption(idTok, value, getInputState().filename);
+ }
+ match(SEMI);
+ } else {
+ break _loop18;
+ }
+
+ } while (true);
+ }
+ match(RCURLY);
+ }
+
+ public final void classDef() throws RecognitionException, TokenStreamException {
+
+ Token a = null;
+ Token d = null;
+ String doc = null;
+
+ try {
+ // for error handling
+ {
+ switch (LA(1)) {
+ case ACTION:
+ {
+ a = LT(1);
+ match(ACTION);
+ if (inputState.guessing == 0) {
+ behavior.refPreambleAction(a);
+ }
+ break;
+ }
+ case DOC_COMMENT:
+ case LITERAL_lexclass:
+ case LITERAL_class:
+ {
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ {
+ switch (LA(1)) {
+ case DOC_COMMENT:
+ {
+ d = LT(1);
+ match(DOC_COMMENT);
+ if (inputState.guessing == 0) {
+ doc = d.getText();
+ }
+ break;
+ }
+ case LITERAL_lexclass:
+ case LITERAL_class:
+ {
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ {
+ boolean synPredMatched13 = false;
+ if (((LA(1) == LITERAL_lexclass || LA(1) == LITERAL_class) && (LA(2) == TOKEN_REF || LA(2) == RULE_REF))) {
+ int _m13 = mark();
+ synPredMatched13 = true;
+ inputState.guessing++;
+ try {
+ {
+ switch (LA(1)) {
+ case LITERAL_lexclass:
+ {
+ match(LITERAL_lexclass);
+ break;
+ }
+ case LITERAL_class:
+ {
+ match(LITERAL_class);
+ id();
+ match(LITERAL_extends);
+ match(LITERAL_Lexer);
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ } catch (RecognitionException pe) {
+ synPredMatched13 = false;
+ }
+ rewind(_m13);
+ inputState.guessing--;
+ }
+ if (synPredMatched13) {
+ lexerSpec(doc);
+ } else {
+ boolean synPredMatched15 = false;
+ if (((LA(1) == LITERAL_class) && (LA(2) == TOKEN_REF || LA(2) == RULE_REF))) {
+ int _m15 = mark();
+ synPredMatched15 = true;
+ inputState.guessing++;
+ try {
+ {
+ match(LITERAL_class);
+ id();
+ match(LITERAL_extends);
+ match(LITERAL_TreeParser);
+ }
+ } catch (RecognitionException pe) {
+ synPredMatched15 = false;
+ }
+ rewind(_m15);
+ inputState.guessing--;
+ }
+ if (synPredMatched15) {
+ treeParserSpec(doc);
+ } else if ((LA(1) == LITERAL_class) && (LA(2) == TOKEN_REF || LA(2) == RULE_REF)) {
+ parserSpec(doc);
+ } else {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ rules();
+ if (inputState.guessing == 0) {
+ behavior.endGrammar();
+ }
+ } catch (RecognitionException ex) {
+ if (inputState.guessing == 0) {
+
+ if (ex instanceof NoViableAltException) {
+ NoViableAltException e = (NoViableAltException)ex;
+ if (e.token.getType() == DOC_COMMENT) {
+ reportError("line " + ex.line + ": JAVADOC comments may only prefix rules and grammars");
+ } else {
+ reportError("rule classDef trapped: " + ex.toString());
+ }
+ } else {
+ reportError("rule classDef trapped: " + ex.toString());
+ }
+ behavior.abortGrammar();
+ boolean consuming = true;
+ // consume everything until the next class definition or EOF
+ while (consuming) {
+ consume();
+ switch (LA(1)) {
+ case LITERAL_class:
+ case LITERAL_lexclass:
+ case EOF:
+ consuming = false;
+ break;
+ }
+ }
+
+ } else {
+ throw ex;
+ }
+ }
+ }
+
+ public final Token id() throws RecognitionException, TokenStreamException {
+ Token idTok;
+
+ Token a = null;
+ Token b = null;
+ idTok = null;
+
+ switch (LA(1)) {
+ case TOKEN_REF:
+ {
+ a = LT(1);
+ match(TOKEN_REF);
+ if (inputState.guessing == 0) {
+ idTok = a;
+ }
+ break;
+ }
+ case RULE_REF:
+ {
+ b = LT(1);
+ match(RULE_REF);
+ if (inputState.guessing == 0) {
+ idTok = b;
+ }
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ return idTok;
+ }
+
+ public final void lexerSpec(
+ String doc
+ ) throws RecognitionException, TokenStreamException {
+
+ Token lc = null;
+ Token a = null;
+
+ Token idTok;
+ String sup = null;
+
+
+ {
+ switch (LA(1)) {
+ case LITERAL_lexclass:
+ {
+ lc = LT(1);
+ match(LITERAL_lexclass);
+ idTok = id();
+ if (inputState.guessing == 0) {
+ System.out.println("warning: line " + lc.getLine() + ": 'lexclass' is deprecated; use 'class X extends Lexer'");
+ }
+ break;
+ }
+ case LITERAL_class:
+ {
+ match(LITERAL_class);
+ idTok = id();
+ match(LITERAL_extends);
+ match(LITERAL_Lexer);
+ {
+ switch (LA(1)) {
+ case LPAREN:
+ {
+ sup = superClass();
+ break;
+ }
+ case SEMI:
+ {
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ if (inputState.guessing == 0) {
+ behavior.startLexer(getFilename(), idTok, sup, doc);
+ }
+ match(SEMI);
+ {
+ switch (LA(1)) {
+ case OPTIONS:
+ {
+ lexerOptionsSpec();
+ break;
+ }
+ case ACTION:
+ case DOC_COMMENT:
+ case TOKENS:
+ case TOKEN_REF:
+ case RULE_REF:
+ case LITERAL_protected:
+ case LITERAL_public:
+ case LITERAL_private:
+ {
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ if (inputState.guessing == 0) {
+ behavior.endOptions();
+ }
+ {
+ switch (LA(1)) {
+ case TOKENS:
+ {
+ tokensSpec();
+ break;
+ }
+ case ACTION:
+ case DOC_COMMENT:
+ case TOKEN_REF:
+ case RULE_REF:
+ case LITERAL_protected:
+ case LITERAL_public:
+ case LITERAL_private:
+ {
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ {
+ switch (LA(1)) {
+ case ACTION:
+ {
+ a = LT(1);
+ match(ACTION);
+ if (inputState.guessing == 0) {
+ behavior.refMemberAction(a);
+ }
+ break;
+ }
+ case DOC_COMMENT:
+ case TOKEN_REF:
+ case RULE_REF:
+ case LITERAL_protected:
+ case LITERAL_public:
+ case LITERAL_private:
+ {
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ }
+
+ public final void treeParserSpec(
+ String doc
+ ) throws RecognitionException, TokenStreamException {
+
+ Token a = null;
+
+ Token idTok;
+ String sup = null;
+
+
+ match(LITERAL_class);
+ idTok = id();
+ match(LITERAL_extends);
+ match(LITERAL_TreeParser);
+ {
+ switch (LA(1)) {
+ case LPAREN:
+ {
+ sup = superClass();
+ break;
+ }
+ case SEMI:
+ {
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ if (inputState.guessing == 0) {
+ behavior.startTreeWalker(getFilename(), idTok, sup, doc);
+ }
+ match(SEMI);
+ {
+ switch (LA(1)) {
+ case OPTIONS:
+ {
+ treeParserOptionsSpec();
+ break;
+ }
+ case ACTION:
+ case DOC_COMMENT:
+ case TOKENS:
+ case TOKEN_REF:
+ case RULE_REF:
+ case LITERAL_protected:
+ case LITERAL_public:
+ case LITERAL_private:
+ {
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ if (inputState.guessing == 0) {
+ behavior.endOptions();
+ }
+ {
+ switch (LA(1)) {
+ case TOKENS:
+ {
+ tokensSpec();
+ break;
+ }
+ case ACTION:
+ case DOC_COMMENT:
+ case TOKEN_REF:
+ case RULE_REF:
+ case LITERAL_protected:
+ case LITERAL_public:
+ case LITERAL_private:
+ {
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ {
+ switch (LA(1)) {
+ case ACTION:
+ {
+ a = LT(1);
+ match(ACTION);
+ if (inputState.guessing == 0) {
+ behavior.refMemberAction(a);
+ }
+ break;
+ }
+ case DOC_COMMENT:
+ case TOKEN_REF:
+ case RULE_REF:
+ case LITERAL_protected:
+ case LITERAL_public:
+ case LITERAL_private:
+ {
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ }
+
+ public final void parserSpec(
+ String doc
+ ) throws RecognitionException, TokenStreamException {
+
+ Token a = null;
+
+ Token idTok;
+ String sup = null;
+
+
+ match(LITERAL_class);
+ idTok = id();
+ {
+ switch (LA(1)) {
+ case LITERAL_extends:
+ {
+ match(LITERAL_extends);
+ match(LITERAL_Parser);
+ {
+ switch (LA(1)) {
+ case LPAREN:
+ {
+ sup = superClass();
+ break;
+ }
+ case SEMI:
+ {
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ break;
+ }
+ case SEMI:
+ {
+ if (inputState.guessing == 0) {
+
+ System.out.println("warning: line " +
+ idTok.getLine() + ": use 'class X extends Parser'");
+
+ }
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ if (inputState.guessing == 0) {
+ behavior.startParser(getFilename(), idTok, sup, doc);
+ }
+ match(SEMI);
+ {
+ switch (LA(1)) {
+ case OPTIONS:
+ {
+ parserOptionsSpec();
+ break;
+ }
+ case ACTION:
+ case DOC_COMMENT:
+ case TOKENS:
+ case TOKEN_REF:
+ case RULE_REF:
+ case LITERAL_protected:
+ case LITERAL_public:
+ case LITERAL_private:
+ {
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ if (inputState.guessing == 0) {
+ behavior.endOptions();
+ }
+ {
+ switch (LA(1)) {
+ case TOKENS:
+ {
+ tokensSpec();
+ break;
+ }
+ case ACTION:
+ case DOC_COMMENT:
+ case TOKEN_REF:
+ case RULE_REF:
+ case LITERAL_protected:
+ case LITERAL_public:
+ case LITERAL_private:
+ {
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ {
+ switch (LA(1)) {
+ case ACTION:
+ {
+ a = LT(1);
+ match(ACTION);
+ if (inputState.guessing == 0) {
+ behavior.refMemberAction(a);
+ }
+ break;
+ }
+ case DOC_COMMENT:
+ case TOKEN_REF:
+ case RULE_REF:
+ case LITERAL_protected:
+ case LITERAL_public:
+ case LITERAL_private:
+ {
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ }
+
+ public final void rules() throws RecognitionException, TokenStreamException {
+
+
+ {
+ int _cnt68 = 0;
+ _loop68:
+ do {
+ if ((_tokenSet_0.member(LA(1))) && (_tokenSet_1.member(LA(2)))) {
+ rule();
+ } else {
+ if (_cnt68 >= 1) {
+ break _loop68;
+ } else {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+
+ _cnt68++;
+ } while (true);
+ }
+ }
+
+ public final Token optionValue() throws RecognitionException, TokenStreamException {
+ Token retval;
+
+ Token sl = null;
+ Token cl = null;
+ Token il = null;
+ retval = null;
+
+ switch (LA(1)) {
+ case TOKEN_REF:
+ case RULE_REF:
+ {
+ retval = qualifiedID();
+ break;
+ }
+ case STRING_LITERAL:
+ {
+ sl = LT(1);
+ match(STRING_LITERAL);
+ if (inputState.guessing == 0) {
+ retval = sl;
+ }
+ break;
+ }
+ case CHAR_LITERAL:
+ {
+ cl = LT(1);
+ match(CHAR_LITERAL);
+ if (inputState.guessing == 0) {
+ retval = cl;
+ }
+ break;
+ }
+ case INT:
+ {
+ il = LT(1);
+ match(INT);
+ if (inputState.guessing == 0) {
+ retval = il;
+ }
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ return retval;
+ }
+
+ public final void parserOptionsSpec() throws RecognitionException, TokenStreamException {
+
+ Token idTok;
+ Token value;
+
+ match(OPTIONS);
+ {
+ _loop21:
+ do {
+ if ((LA(1) == TOKEN_REF || LA(1) == RULE_REF)) {
+ idTok = id();
+ match(ASSIGN);
+ value = optionValue();
+ if (inputState.guessing == 0) {
+ behavior.setGrammarOption(idTok, value);
+ }
+ match(SEMI);
+ } else {
+ break _loop21;
+ }
+
+ } while (true);
+ }
+ match(RCURLY);
+ }
+
+ public final void treeParserOptionsSpec() throws RecognitionException, TokenStreamException {
+
+ Token idTok;
+ Token value;
+
+ match(OPTIONS);
+ {
+ _loop24:
+ do {
+ if ((LA(1) == TOKEN_REF || LA(1) == RULE_REF)) {
+ idTok = id();
+ match(ASSIGN);
+ value = optionValue();
+ if (inputState.guessing == 0) {
+ behavior.setGrammarOption(idTok, value);
+ }
+ match(SEMI);
+ } else {
+ break _loop24;
+ }
+
+ } while (true);
+ }
+ match(RCURLY);
+ }
+
+ public final void lexerOptionsSpec() throws RecognitionException, TokenStreamException {
+
+ Token idTok;
+ Token value;
+ BitSet b;
+
+ match(OPTIONS);
+ {
+ _loop27:
+ do {
+ switch (LA(1)) {
+ case LITERAL_charVocabulary:
+ {
+ match(LITERAL_charVocabulary);
+ match(ASSIGN);
+ b = charSet();
+ match(SEMI);
+ if (inputState.guessing == 0) {
+ behavior.setCharVocabulary(b);
+ }
+ break;
+ }
+ case TOKEN_REF:
+ case RULE_REF:
+ {
+ idTok = id();
+ match(ASSIGN);
+ value = optionValue();
+ if (inputState.guessing == 0) {
+ behavior.setGrammarOption(idTok, value);
+ }
+ match(SEMI);
+ break;
+ }
+ default:
+ {
+ break _loop27;
+ }
+ }
+ } while (true);
+ }
+ match(RCURLY);
+ }
+
+ public final BitSet charSet() throws RecognitionException, TokenStreamException {
+ BitSet b;
+
+
+ b = null;
+ BitSet tmpSet = null;
+
+
+ b = setBlockElement();
+ {
+ _loop34:
+ do {
+ if ((LA(1) == OR)) {
+ match(OR);
+ tmpSet = setBlockElement();
+ if (inputState.guessing == 0) {
+ b.orInPlace(tmpSet);
+ }
+ } else {
+ break _loop34;
+ }
+
+ } while (true);
+ }
+ return b;
+ }
+
+ public final void subruleOptionsSpec() throws RecognitionException, TokenStreamException {
+
+ Token idTok;
+ Token value;
+
+ match(OPTIONS);
+ {
+ _loop30:
+ do {
+ if ((LA(1) == TOKEN_REF || LA(1) == RULE_REF)) {
+ idTok = id();
+ match(ASSIGN);
+ value = optionValue();
+ if (inputState.guessing == 0) {
+ behavior.setSubruleOption(idTok, value);
+ }
+ match(SEMI);
+ } else {
+ break _loop30;
+ }
+
+ } while (true);
+ }
+ match(RCURLY);
+ }
+
+ /** Match a.b.c.d qualified ids; WILDCARD here is overloaded as
+ * id separator; that is, I need a reference to the '.' token.
+ */
+ public final Token qualifiedID() throws RecognitionException, TokenStreamException {
+ Token qidTok = null;
+
+
+ StringBuffer buf = new StringBuffer(30);
+ Token a;
+
+
+ a = id();
+ if (inputState.guessing == 0) {
+ buf.append(a.getText());
+ }
+ {
+ _loop144:
+ do {
+ if ((LA(1) == WILDCARD)) {
+ match(WILDCARD);
+ a = id();
+ if (inputState.guessing == 0) {
+ buf.append('.');
+ buf.append(a.getText());
+ }
+ } else {
+ break _loop144;
+ }
+
+ } while (true);
+ }
+ if (inputState.guessing == 0) {
+
+ // can use either TOKEN_REF or RULE_REF; should
+ // really create a QID or something instead.
+ qidTok = new CommonToken(TOKEN_REF, buf.toString());
+ qidTok.setLine(a.getLine());
+
+ }
+ return qidTok;
+ }
+
+ public final BitSet setBlockElement() throws RecognitionException, TokenStreamException {
+ BitSet b;
+
+ Token c1 = null;
+ Token c2 = null;
+
+ b = null;
+ int rangeMin = 0;
+
+
+ c1 = LT(1);
+ match(CHAR_LITERAL);
+ if (inputState.guessing == 0) {
+
+ rangeMin = ANTLRLexer.tokenTypeForCharLiteral(c1.getText());
+ b = BitSet.of(rangeMin);
+
+ }
+ {
+ switch (LA(1)) {
+ case RANGE:
+ {
+ match(RANGE);
+ c2 = LT(1);
+ match(CHAR_LITERAL);
+ if (inputState.guessing == 0) {
+
+ int rangeMax = ANTLRLexer.tokenTypeForCharLiteral(c2.getText());
+ if (rangeMax < rangeMin) {
+ tool.error("Malformed range line " + c1.getLine());
+ }
+ for (int i = rangeMin + 1; i <= rangeMax; i++) {
+ b.add(i);
+ }
+
+ }
+ break;
+ }
+ case SEMI:
+ case OR:
+ {
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ return b;
+ }
+
+ public final void tokensSpec() throws RecognitionException, TokenStreamException {
+
+ Token t1 = null;
+ Token s1 = null;
+ Token s3 = null;
+
+ match(TOKENS);
+ {
+ int _cnt43 = 0;
+ _loop43:
+ do {
+ if ((LA(1) == STRING_LITERAL || LA(1) == TOKEN_REF)) {
+ {
+ switch (LA(1)) {
+ case TOKEN_REF:
+ {
+ if (inputState.guessing == 0) {
+ s1 = null;
+ }
+ t1 = LT(1);
+ match(TOKEN_REF);
+ {
+ switch (LA(1)) {
+ case ASSIGN:
+ {
+ match(ASSIGN);
+ s1 = LT(1);
+ match(STRING_LITERAL);
+ break;
+ }
+ case SEMI:
+ case OPEN_ELEMENT_OPTION:
+ {
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ if (inputState.guessing == 0) {
+ behavior.defineToken(t1, s1);
+ }
+ {
+ switch (LA(1)) {
+ case OPEN_ELEMENT_OPTION:
+ {
+ tokensSpecOptions(t1);
+ break;
+ }
+ case SEMI:
+ {
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ break;
+ }
+ case STRING_LITERAL:
+ {
+ s3 = LT(1);
+ match(STRING_LITERAL);
+ if (inputState.guessing == 0) {
+ behavior.defineToken(null, s3);
+ }
+ {
+ switch (LA(1)) {
+ case OPEN_ELEMENT_OPTION:
+ {
+ tokensSpecOptions(s3);
+ break;
+ }
+ case SEMI:
+ {
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ match(SEMI);
+ } else {
+ if (_cnt43 >= 1) {
+ break _loop43;
+ } else {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+
+ _cnt43++;
+ } while (true);
+ }
+ match(RCURLY);
+ }
+
+ public final void tokensSpecOptions(
+ Token t
+ ) throws RecognitionException, TokenStreamException {
+
+
+ Token o = null, v = null;
+
+
+ match(OPEN_ELEMENT_OPTION);
+ o = id();
+ match(ASSIGN);
+ v = optionValue();
+ if (inputState.guessing == 0) {
+ behavior.refTokensSpecElementOption(t, o, v);
+ }
+ {
+ _loop46:
+ do {
+ if ((LA(1) == SEMI)) {
+ match(SEMI);
+ o = id();
+ match(ASSIGN);
+ v = optionValue();
+ if (inputState.guessing == 0) {
+ behavior.refTokensSpecElementOption(t, o, v);
+ }
+ } else {
+ break _loop46;
+ }
+
+ } while (true);
+ }
+ match(CLOSE_ELEMENT_OPTION);
+ }
+
+ public final String superClass() throws RecognitionException, TokenStreamException {
+ String sup;
+
+ sup = null;
+
+ match(LPAREN);
+ if (inputState.guessing == 0) {
+ sup = LT(1).getText();
+ }
+ {
+ switch (LA(1)) {
+ case TOKEN_REF:
+ {
+ match(TOKEN_REF);
+ break;
+ }
+ case RULE_REF:
+ {
+ match(RULE_REF);
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ match(RPAREN);
+ return sup;
+ }
+
+ public final void rule() throws RecognitionException, TokenStreamException {
+
+ Token d = null;
+ Token p1 = null;
+ Token p2 = null;
+ Token p3 = null;
+ Token aa = null;
+ Token rt = null;
+ Token a = null;
+
+ String access = "public";
+ Token idTok;
+ String doc = null;
+ boolean ruleAutoGen = true;
+ blockNesting = -1; // block increments, so -1 to make rule at level 0
- public final void grammar() throws RecognitionException, TokenStreamException {
-
- Token n = null;
- Token h = null;
-
- try { // for error handling
- {
- _loop4:
- do {
- if ((LA(1)==LITERAL_header)) {
- match(LITERAL_header);
- {
- switch ( LA(1)) {
- case STRING_LITERAL:
- {
- n = LT(1);
- match(STRING_LITERAL);
- break;
- }
- case ACTION:
- {
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- h = LT(1);
- match(ACTION);
- if ( inputState.guessing==0 ) {
- behavior.refHeaderAction(n,h);
- }
- }
- else {
- break _loop4;
- }
-
- } while (true);
- }
- {
- switch ( LA(1)) {
- case OPTIONS:
- {
- fileOptionsSpec();
- break;
- }
- case EOF:
- case ACTION:
- case DOC_COMMENT:
- case LITERAL_lexclass:
- case LITERAL_class:
- {
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- {
- _loop7:
- do {
- if (((LA(1) >= ACTION && LA(1) <= LITERAL_class))) {
- classDef();
- }
- else {
- break _loop7;
- }
-
- } while (true);
- }
- match(Token.EOF_TYPE);
- }
- catch (RecognitionException ex) {
- if (inputState.guessing==0) {
-
- reportError("rule grammar trapped: "+ex.toString());
- consumeUntil(EOF);
-
- } else {
- throw ex;
- }
- }
- }
-
- public final void fileOptionsSpec() throws RecognitionException, TokenStreamException {
-
- Token idTok; Token value;
-
- match(OPTIONS);
- {
- _loop18:
- do {
- if ((LA(1)==TOKEN_REF||LA(1)==RULE_REF)) {
- idTok=id();
- match(ASSIGN);
- value=optionValue();
- if ( inputState.guessing==0 ) {
- behavior.setFileOption(idTok, value,getInputState().filename);
- }
- match(SEMI);
- }
- else {
- break _loop18;
- }
-
- } while (true);
- }
- match(RCURLY);
- }
-
- public final void classDef() throws RecognitionException, TokenStreamException {
-
- Token a = null;
- Token d = null;
- String doc=null;
-
- try { // for error handling
- {
- switch ( LA(1)) {
- case ACTION:
- {
- a = LT(1);
- match(ACTION);
- if ( inputState.guessing==0 ) {
- behavior.refPreambleAction(a);
- }
- break;
- }
- case DOC_COMMENT:
- case LITERAL_lexclass:
- case LITERAL_class:
- {
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- {
- switch ( LA(1)) {
- case DOC_COMMENT:
- {
- d = LT(1);
- match(DOC_COMMENT);
- if ( inputState.guessing==0 ) {
- doc=d.getText();
- }
- break;
- }
- case LITERAL_lexclass:
- case LITERAL_class:
- {
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- {
- boolean synPredMatched13 = false;
- if (((LA(1)==LITERAL_lexclass||LA(1)==LITERAL_class) && (LA(2)==TOKEN_REF||LA(2)==RULE_REF))) {
- int _m13 = mark();
- synPredMatched13 = true;
- inputState.guessing++;
- try {
- {
- switch ( LA(1)) {
- case LITERAL_lexclass:
- {
- match(LITERAL_lexclass);
- break;
- }
- case LITERAL_class:
- {
- match(LITERAL_class);
- id();
- match(LITERAL_extends);
- match(LITERAL_Lexer);
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- }
- catch (RecognitionException pe) {
- synPredMatched13 = false;
- }
- rewind(_m13);
- inputState.guessing--;
- }
- if ( synPredMatched13 ) {
- lexerSpec(doc);
- }
- else {
- boolean synPredMatched15 = false;
- if (((LA(1)==LITERAL_class) && (LA(2)==TOKEN_REF||LA(2)==RULE_REF))) {
- int _m15 = mark();
- synPredMatched15 = true;
- inputState.guessing++;
- try {
- {
- match(LITERAL_class);
- id();
- match(LITERAL_extends);
- match(LITERAL_TreeParser);
- }
- }
- catch (RecognitionException pe) {
- synPredMatched15 = false;
- }
- rewind(_m15);
- inputState.guessing--;
- }
- if ( synPredMatched15 ) {
- treeParserSpec(doc);
- }
- else if ((LA(1)==LITERAL_class) && (LA(2)==TOKEN_REF||LA(2)==RULE_REF)) {
- parserSpec(doc);
- }
- else {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- rules();
- if ( inputState.guessing==0 ) {
- behavior.endGrammar();
- }
- }
- catch (RecognitionException ex) {
- if (inputState.guessing==0) {
-
- if ( ex instanceof NoViableAltException ) {
- NoViableAltException e = (NoViableAltException)ex;
- if ( e.token.getType()==DOC_COMMENT ) {
- reportError("line "+ex.line+": JAVADOC comments may only prefix rules and grammars");
- }
- else {
- reportError("rule classDef trapped: "+ex.toString());
- }
- }
- else {
- reportError("rule classDef trapped: "+ex.toString());
- }
- behavior.abortGrammar();
- boolean consuming = true;
- // consume everything until the next class definition or EOF
- while (consuming) {
- consume();
- switch(LA(1)) {
- case LITERAL_class:
- case LITERAL_lexclass:
- case EOF:
- consuming = false;
- break;
- }
- }
-
- } else {
- throw ex;
- }
- }
- }
-
- public final Token id() throws RecognitionException, TokenStreamException {
- Token idTok ;
-
- Token a = null;
- Token b = null;
- idTok = null;
-
- switch ( LA(1)) {
- case TOKEN_REF:
- {
- a = LT(1);
- match(TOKEN_REF);
- if ( inputState.guessing==0 ) {
- idTok = a;
- }
- break;
- }
- case RULE_REF:
- {
- b = LT(1);
- match(RULE_REF);
- if ( inputState.guessing==0 ) {
- idTok = b;
- }
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- return idTok ;
- }
-
- public final void lexerSpec(
- String doc
- ) throws RecognitionException, TokenStreamException {
-
- Token lc = null;
- Token a = null;
-
- Token idTok;
- String sup=null;
-
-
- {
- switch ( LA(1)) {
- case LITERAL_lexclass:
- {
- lc = LT(1);
- match(LITERAL_lexclass);
- idTok=id();
- if ( inputState.guessing==0 ) {
- System.out.println("warning: line " + lc.getLine() + ": 'lexclass' is deprecated; use 'class X extends Lexer'");
- }
- break;
- }
- case LITERAL_class:
- {
- match(LITERAL_class);
- idTok=id();
- match(LITERAL_extends);
- match(LITERAL_Lexer);
- {
- switch ( LA(1)) {
- case LPAREN:
- {
- sup=superClass();
- break;
- }
- case SEMI:
- {
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- if ( inputState.guessing==0 ) {
- behavior.startLexer(getFilename(), idTok,sup,doc);
- }
- match(SEMI);
- {
- switch ( LA(1)) {
- case OPTIONS:
- {
- lexerOptionsSpec();
- break;
- }
- case ACTION:
- case DOC_COMMENT:
- case TOKENS:
- case TOKEN_REF:
- case RULE_REF:
- case LITERAL_protected:
- case LITERAL_public:
- case LITERAL_private:
- {
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- if ( inputState.guessing==0 ) {
- behavior.endOptions();
- }
- {
- switch ( LA(1)) {
- case TOKENS:
- {
- tokensSpec();
- break;
- }
- case ACTION:
- case DOC_COMMENT:
- case TOKEN_REF:
- case RULE_REF:
- case LITERAL_protected:
- case LITERAL_public:
- case LITERAL_private:
- {
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- {
- switch ( LA(1)) {
- case ACTION:
- {
- a = LT(1);
- match(ACTION);
- if ( inputState.guessing==0 ) {
- behavior.refMemberAction(a);
- }
- break;
- }
- case DOC_COMMENT:
- case TOKEN_REF:
- case RULE_REF:
- case LITERAL_protected:
- case LITERAL_public:
- case LITERAL_private:
- {
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- }
-
- public final void treeParserSpec(
- String doc
- ) throws RecognitionException, TokenStreamException {
-
- Token a = null;
-
- Token idTok;
- String sup=null;
-
-
- match(LITERAL_class);
- idTok=id();
- match(LITERAL_extends);
- match(LITERAL_TreeParser);
- {
- switch ( LA(1)) {
- case LPAREN:
- {
- sup=superClass();
- break;
- }
- case SEMI:
- {
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- if ( inputState.guessing==0 ) {
- behavior.startTreeWalker(getFilename(), idTok,sup,doc);
- }
- match(SEMI);
- {
- switch ( LA(1)) {
- case OPTIONS:
- {
- treeParserOptionsSpec();
- break;
- }
- case ACTION:
- case DOC_COMMENT:
- case TOKENS:
- case TOKEN_REF:
- case RULE_REF:
- case LITERAL_protected:
- case LITERAL_public:
- case LITERAL_private:
- {
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- if ( inputState.guessing==0 ) {
- behavior.endOptions();
- }
- {
- switch ( LA(1)) {
- case TOKENS:
- {
- tokensSpec();
- break;
- }
- case ACTION:
- case DOC_COMMENT:
- case TOKEN_REF:
- case RULE_REF:
- case LITERAL_protected:
- case LITERAL_public:
- case LITERAL_private:
- {
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- {
- switch ( LA(1)) {
- case ACTION:
- {
- a = LT(1);
- match(ACTION);
- if ( inputState.guessing==0 ) {
- behavior.refMemberAction(a);
- }
- break;
- }
- case DOC_COMMENT:
- case TOKEN_REF:
- case RULE_REF:
- case LITERAL_protected:
- case LITERAL_public:
- case LITERAL_private:
- {
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- }
-
- public final void parserSpec(
- String doc
- ) throws RecognitionException, TokenStreamException {
-
- Token a = null;
-
- Token idTok;
- String sup=null;
-
-
- match(LITERAL_class);
- idTok=id();
- {
- switch ( LA(1)) {
- case LITERAL_extends:
- {
- match(LITERAL_extends);
- match(LITERAL_Parser);
- {
- switch ( LA(1)) {
- case LPAREN:
- {
- sup=superClass();
- break;
- }
- case SEMI:
- {
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- break;
- }
- case SEMI:
- {
- if ( inputState.guessing==0 ) {
-
- System.out.println("warning: line " +
- idTok.getLine() + ": use 'class X extends Parser'");
-
- }
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- if ( inputState.guessing==0 ) {
- behavior.startParser(getFilename(), idTok, sup, doc);
- }
- match(SEMI);
- {
- switch ( LA(1)) {
- case OPTIONS:
- {
- parserOptionsSpec();
- break;
- }
- case ACTION:
- case DOC_COMMENT:
- case TOKENS:
- case TOKEN_REF:
- case RULE_REF:
- case LITERAL_protected:
- case LITERAL_public:
- case LITERAL_private:
- {
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- if ( inputState.guessing==0 ) {
- behavior.endOptions();
- }
- {
- switch ( LA(1)) {
- case TOKENS:
- {
- tokensSpec();
- break;
- }
- case ACTION:
- case DOC_COMMENT:
- case TOKEN_REF:
- case RULE_REF:
- case LITERAL_protected:
- case LITERAL_public:
- case LITERAL_private:
- {
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- {
- switch ( LA(1)) {
- case ACTION:
- {
- a = LT(1);
- match(ACTION);
- if ( inputState.guessing==0 ) {
- behavior.refMemberAction(a);
- }
- break;
- }
- case DOC_COMMENT:
- case TOKEN_REF:
- case RULE_REF:
- case LITERAL_protected:
- case LITERAL_public:
- case LITERAL_private:
- {
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- }
-
- public final void rules() throws RecognitionException, TokenStreamException {
-
-
- {
- int _cnt68=0;
- _loop68:
- do {
- if ((_tokenSet_0.member(LA(1))) && (_tokenSet_1.member(LA(2)))) {
- rule();
- }
- else {
- if ( _cnt68>=1 ) { break _loop68; } else {throw new NoViableAltException(LT(1), getFilename());}
- }
-
- _cnt68++;
- } while (true);
- }
- }
-
- public final Token optionValue() throws RecognitionException, TokenStreamException {
- Token retval ;
-
- Token sl = null;
- Token cl = null;
- Token il = null;
- retval = null;
-
- switch ( LA(1)) {
- case TOKEN_REF:
- case RULE_REF:
- {
- retval=qualifiedID();
- break;
- }
- case STRING_LITERAL:
- {
- sl = LT(1);
- match(STRING_LITERAL);
- if ( inputState.guessing==0 ) {
- retval = sl;
- }
- break;
- }
- case CHAR_LITERAL:
- {
- cl = LT(1);
- match(CHAR_LITERAL);
- if ( inputState.guessing==0 ) {
- retval = cl;
- }
- break;
- }
- case INT:
- {
- il = LT(1);
- match(INT);
- if ( inputState.guessing==0 ) {
- retval = il;
- }
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- return retval ;
- }
-
- public final void parserOptionsSpec() throws RecognitionException, TokenStreamException {
-
- Token idTok; Token value;
-
- match(OPTIONS);
- {
- _loop21:
- do {
- if ((LA(1)==TOKEN_REF||LA(1)==RULE_REF)) {
- idTok=id();
- match(ASSIGN);
- value=optionValue();
- if ( inputState.guessing==0 ) {
- behavior.setGrammarOption(idTok, value);
- }
- match(SEMI);
- }
- else {
- break _loop21;
- }
-
- } while (true);
- }
- match(RCURLY);
- }
-
- public final void treeParserOptionsSpec() throws RecognitionException, TokenStreamException {
-
- Token idTok; Token value;
-
- match(OPTIONS);
- {
- _loop24:
- do {
- if ((LA(1)==TOKEN_REF||LA(1)==RULE_REF)) {
- idTok=id();
- match(ASSIGN);
- value=optionValue();
- if ( inputState.guessing==0 ) {
- behavior.setGrammarOption(idTok, value);
- }
- match(SEMI);
- }
- else {
- break _loop24;
- }
-
- } while (true);
- }
- match(RCURLY);
- }
-
- public final void lexerOptionsSpec() throws RecognitionException, TokenStreamException {
-
- Token idTok; Token value; BitSet b;
-
- match(OPTIONS);
- {
- _loop27:
- do {
- switch ( LA(1)) {
- case LITERAL_charVocabulary:
- {
- match(LITERAL_charVocabulary);
- match(ASSIGN);
- b=charSet();
- match(SEMI);
- if ( inputState.guessing==0 ) {
- behavior.setCharVocabulary(b);
- }
- break;
- }
- case TOKEN_REF:
- case RULE_REF:
- {
- idTok=id();
- match(ASSIGN);
- value=optionValue();
- if ( inputState.guessing==0 ) {
- behavior.setGrammarOption(idTok, value);
- }
- match(SEMI);
- break;
- }
- default:
- {
- break _loop27;
- }
- }
- } while (true);
- }
- match(RCURLY);
- }
-
- public final BitSet charSet() throws RecognitionException, TokenStreamException {
- BitSet b ;
-
-
- b = null;
- BitSet tmpSet = null;
-
-
- b=setBlockElement();
- {
- _loop34:
- do {
- if ((LA(1)==OR)) {
- match(OR);
- tmpSet=setBlockElement();
- if ( inputState.guessing==0 ) {
- b.orInPlace(tmpSet);
- }
- }
- else {
- break _loop34;
- }
-
- } while (true);
- }
- return b ;
- }
-
- public final void subruleOptionsSpec() throws RecognitionException, TokenStreamException {
-
- Token idTok; Token value;
-
- match(OPTIONS);
- {
- _loop30:
- do {
- if ((LA(1)==TOKEN_REF||LA(1)==RULE_REF)) {
- idTok=id();
- match(ASSIGN);
- value=optionValue();
- if ( inputState.guessing==0 ) {
- behavior.setSubruleOption(idTok, value);
- }
- match(SEMI);
- }
- else {
- break _loop30;
- }
-
- } while (true);
- }
- match(RCURLY);
- }
-
-/** Match a.b.c.d qualified ids; WILDCARD here is overloaded as
- * id separator; that is, I need a reference to the '.' token.
- */
- public final Token qualifiedID() throws RecognitionException, TokenStreamException {
- Token qidTok=null;
-
-
- StringBuffer buf = new StringBuffer(30);
- Token a;
-
-
- a=id();
- if ( inputState.guessing==0 ) {
- buf.append(a.getText());
- }
- {
- _loop144:
- do {
- if ((LA(1)==WILDCARD)) {
- match(WILDCARD);
- a=id();
- if ( inputState.guessing==0 ) {
- buf.append('.'); buf.append(a.getText());
- }
- }
- else {
- break _loop144;
- }
-
- } while (true);
- }
- if ( inputState.guessing==0 ) {
-
- // can use either TOKEN_REF or RULE_REF; should
- // really create a QID or something instead.
- qidTok = new CommonToken(TOKEN_REF, buf.toString());
- qidTok.setLine(a.getLine());
-
- }
- return qidTok;
- }
-
- public final BitSet setBlockElement() throws RecognitionException, TokenStreamException {
- BitSet b ;
-
- Token c1 = null;
- Token c2 = null;
-
- b = null;
- int rangeMin = 0;
-
-
- c1 = LT(1);
- match(CHAR_LITERAL);
- if ( inputState.guessing==0 ) {
-
- rangeMin = ANTLRLexer.tokenTypeForCharLiteral(c1.getText());
- b = BitSet.of(rangeMin);
-
- }
- {
- switch ( LA(1)) {
- case RANGE:
- {
- match(RANGE);
- c2 = LT(1);
- match(CHAR_LITERAL);
- if ( inputState.guessing==0 ) {
-
- int rangeMax = ANTLRLexer.tokenTypeForCharLiteral(c2.getText());
- if (rangeMax < rangeMin) {
- tool.error("Malformed range line "+c1.getLine());
- }
- for (int i = rangeMin+1; i <= rangeMax; i++) {
- b.add(i);
- }
-
- }
- break;
- }
- case SEMI:
- case OR:
- {
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- return b ;
- }
-
- public final void tokensSpec() throws RecognitionException, TokenStreamException {
-
- Token t1 = null;
- Token s1 = null;
- Token s3 = null;
-
- match(TOKENS);
- {
- int _cnt43=0;
- _loop43:
- do {
- if ((LA(1)==STRING_LITERAL||LA(1)==TOKEN_REF)) {
- {
- switch ( LA(1)) {
- case TOKEN_REF:
- {
- if ( inputState.guessing==0 ) {
- s1=null;
- }
- t1 = LT(1);
- match(TOKEN_REF);
- {
- switch ( LA(1)) {
- case ASSIGN:
- {
- match(ASSIGN);
- s1 = LT(1);
- match(STRING_LITERAL);
- break;
- }
- case SEMI:
- case OPEN_ELEMENT_OPTION:
- {
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- if ( inputState.guessing==0 ) {
- behavior.defineToken(t1, s1);
- }
- {
- switch ( LA(1)) {
- case OPEN_ELEMENT_OPTION:
- {
- tokensSpecOptions(t1);
- break;
- }
- case SEMI:
- {
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- break;
- }
- case STRING_LITERAL:
- {
- s3 = LT(1);
- match(STRING_LITERAL);
- if ( inputState.guessing==0 ) {
- behavior.defineToken(null, s3);
- }
- {
- switch ( LA(1)) {
- case OPEN_ELEMENT_OPTION:
- {
- tokensSpecOptions(s3);
- break;
- }
- case SEMI:
- {
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- match(SEMI);
- }
- else {
- if ( _cnt43>=1 ) { break _loop43; } else {throw new NoViableAltException(LT(1), getFilename());}
- }
-
- _cnt43++;
- } while (true);
- }
- match(RCURLY);
- }
-
- public final void tokensSpecOptions(
- Token t
- ) throws RecognitionException, TokenStreamException {
-
-
- Token o=null, v=null;
-
-
- match(OPEN_ELEMENT_OPTION);
- o=id();
- match(ASSIGN);
- v=optionValue();
- if ( inputState.guessing==0 ) {
- behavior.refTokensSpecElementOption(t,o,v);
- }
- {
- _loop46:
- do {
- if ((LA(1)==SEMI)) {
- match(SEMI);
- o=id();
- match(ASSIGN);
- v=optionValue();
- if ( inputState.guessing==0 ) {
- behavior.refTokensSpecElementOption(t,o,v);
- }
- }
- else {
- break _loop46;
- }
-
- } while (true);
- }
- match(CLOSE_ELEMENT_OPTION);
- }
-
- public final String superClass() throws RecognitionException, TokenStreamException {
- String sup;
-
- sup=null;
-
- match(LPAREN);
- if ( inputState.guessing==0 ) {
- sup = LT(1).getText();
- }
- {
- switch ( LA(1)) {
- case TOKEN_REF:
- {
- match(TOKEN_REF);
- break;
- }
- case RULE_REF:
- {
- match(RULE_REF);
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- match(RPAREN);
- return sup;
- }
-
- public final void rule() throws RecognitionException, TokenStreamException {
-
- Token d = null;
- Token p1 = null;
- Token p2 = null;
- Token p3 = null;
- Token aa = null;
- Token rt = null;
- Token a = null;
-
- String access="public";
- Token idTok;
- String doc=null;
- boolean ruleAutoGen = true;
- blockNesting = -1; // block increments, so -1 to make rule at level 0
-
-
- {
- switch ( LA(1)) {
- case DOC_COMMENT:
- {
- d = LT(1);
- match(DOC_COMMENT);
- if ( inputState.guessing==0 ) {
- doc=d.getText();
- }
- break;
- }
- case TOKEN_REF:
- case RULE_REF:
- case LITERAL_protected:
- case LITERAL_public:
- case LITERAL_private:
- {
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- {
- switch ( LA(1)) {
- case LITERAL_protected:
- {
- p1 = LT(1);
- match(LITERAL_protected);
- if ( inputState.guessing==0 ) {
- access=p1.getText();
- }
- break;
- }
- case LITERAL_public:
- {
- p2 = LT(1);
- match(LITERAL_public);
- if ( inputState.guessing==0 ) {
- access=p2.getText();
- }
- break;
- }
- case LITERAL_private:
- {
- p3 = LT(1);
- match(LITERAL_private);
- if ( inputState.guessing==0 ) {
- access=p3.getText();
- }
- break;
- }
- case TOKEN_REF:
- case RULE_REF:
- {
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- idTok=id();
- {
- switch ( LA(1)) {
- case BANG:
- {
- match(BANG);
- if ( inputState.guessing==0 ) {
- ruleAutoGen = false;
- }
- break;
- }
- case ACTION:
- case OPTIONS:
- case ARG_ACTION:
- case LITERAL_returns:
- case COLON:
- case LITERAL_throws:
- {
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- if ( inputState.guessing==0 ) {
-
- behavior.defineRuleName(idTok, access, ruleAutoGen, doc);
-
- }
- {
- switch ( LA(1)) {
- case ARG_ACTION:
- {
- aa = LT(1);
- match(ARG_ACTION);
- if ( inputState.guessing==0 ) {
- behavior.refArgAction(aa);
- }
- break;
- }
- case ACTION:
- case OPTIONS:
- case LITERAL_returns:
- case COLON:
- case LITERAL_throws:
- {
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- {
- switch ( LA(1)) {
- case LITERAL_returns:
- {
- match(LITERAL_returns);
- rt = LT(1);
- match(ARG_ACTION);
- if ( inputState.guessing==0 ) {
- behavior.refReturnAction(rt);
- }
- break;
- }
- case ACTION:
- case OPTIONS:
- case COLON:
- case LITERAL_throws:
- {
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- {
- switch ( LA(1)) {
- case LITERAL_throws:
- {
- throwsSpec();
- break;
- }
- case ACTION:
- case OPTIONS:
- case COLON:
- {
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- {
- switch ( LA(1)) {
- case OPTIONS:
- {
- ruleOptionsSpec();
- break;
- }
- case ACTION:
- case COLON:
- {
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- {
- switch ( LA(1)) {
- case ACTION:
- {
- a = LT(1);
- match(ACTION);
- if ( inputState.guessing==0 ) {
- behavior.refInitAction(a);
- }
- break;
- }
- case COLON:
- {
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- match(COLON);
- block();
- match(SEMI);
- {
- switch ( LA(1)) {
- case LITERAL_exception:
- {
- exceptionGroup();
- break;
- }
- case EOF:
- case ACTION:
- case DOC_COMMENT:
- case LITERAL_lexclass:
- case LITERAL_class:
- case TOKEN_REF:
- case RULE_REF:
- case LITERAL_protected:
- case LITERAL_public:
- case LITERAL_private:
- {
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- if ( inputState.guessing==0 ) {
- behavior.endRule(idTok.getText());
- }
- }
-
- public final void throwsSpec() throws RecognitionException, TokenStreamException {
-
-
- String t=null;
- Token a,b;
-
-
- match(LITERAL_throws);
- a=id();
- if ( inputState.guessing==0 ) {
- t=a.getText();
- }
- {
- _loop84:
- do {
- if ((LA(1)==COMMA)) {
- match(COMMA);
- b=id();
- if ( inputState.guessing==0 ) {
- t+=","+b.getText();
- }
- }
- else {
- break _loop84;
- }
-
- } while (true);
- }
- if ( inputState.guessing==0 ) {
- behavior.setUserExceptions(t);
- }
- }
-
- public final void ruleOptionsSpec() throws RecognitionException, TokenStreamException {
-
- Token idTok; Token value;
-
- match(OPTIONS);
- {
- _loop81:
- do {
- if ((LA(1)==TOKEN_REF||LA(1)==RULE_REF)) {
- idTok=id();
- match(ASSIGN);
- value=optionValue();
- if ( inputState.guessing==0 ) {
- behavior.setRuleOption(idTok, value);
- }
- match(SEMI);
- }
- else {
- break _loop81;
- }
-
- } while (true);
- }
- match(RCURLY);
- }
-
- public final void block() throws RecognitionException, TokenStreamException {
-
-
- if ( inputState.guessing==0 ) {
- blockNesting++;
- }
- alternative();
- {
- _loop87:
- do {
- if ((LA(1)==OR)) {
- match(OR);
- alternative();
- }
- else {
- break _loop87;
- }
-
- } while (true);
- }
- if ( inputState.guessing==0 ) {
- blockNesting--;
- }
- }
-
- public final void exceptionGroup() throws RecognitionException, TokenStreamException {
-
-
- if ( inputState.guessing==0 ) {
- behavior.beginExceptionGroup();
- }
- {
- int _cnt95=0;
- _loop95:
- do {
- if ((LA(1)==LITERAL_exception)) {
- exceptionSpec();
- }
- else {
- if ( _cnt95>=1 ) { break _loop95; } else {throw new NoViableAltException(LT(1), getFilename());}
- }
-
- _cnt95++;
- } while (true);
- }
- if ( inputState.guessing==0 ) {
- behavior.endExceptionGroup();
- }
- }
-
- public final void alternative() throws RecognitionException, TokenStreamException {
-
- boolean altAutoGen = true;
-
- {
- switch ( LA(1)) {
- case BANG:
- {
- match(BANG);
- if ( inputState.guessing==0 ) {
- altAutoGen=false;
- }
- break;
- }
- case STRING_LITERAL:
- case ACTION:
- case SEMI:
- case CHAR_LITERAL:
- case OR:
- case TOKEN_REF:
- case LPAREN:
- case RULE_REF:
- case RPAREN:
- case LITERAL_exception:
- case NOT_OP:
- case SEMPRED:
- case TREE_BEGIN:
- case WILDCARD:
- {
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- if ( inputState.guessing==0 ) {
- behavior.beginAlt(altAutoGen);
- }
- {
- _loop91:
- do {
- if ((_tokenSet_2.member(LA(1)))) {
- element();
- }
- else {
- break _loop91;
- }
-
- } while (true);
- }
- {
- switch ( LA(1)) {
- case LITERAL_exception:
- {
- exceptionSpecNoLabel();
- break;
- }
- case SEMI:
- case OR:
- case RPAREN:
- {
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- if ( inputState.guessing==0 ) {
- behavior.endAlt();
- }
- }
-
- public final void element() throws RecognitionException, TokenStreamException {
-
-
- elementNoOptionSpec();
- {
- switch ( LA(1)) {
- case OPEN_ELEMENT_OPTION:
- {
- elementOptionSpec();
- break;
- }
- case STRING_LITERAL:
- case ACTION:
- case SEMI:
- case CHAR_LITERAL:
- case OR:
- case TOKEN_REF:
- case LPAREN:
- case RULE_REF:
- case RPAREN:
- case LITERAL_exception:
- case NOT_OP:
- case SEMPRED:
- case TREE_BEGIN:
- case WILDCARD:
- {
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- }
-
- public final void exceptionSpecNoLabel() throws RecognitionException, TokenStreamException {
-
-
- match(LITERAL_exception);
- if ( inputState.guessing==0 ) {
- behavior.beginExceptionSpec(null);
- }
- {
- _loop102:
- do {
- if ((LA(1)==LITERAL_catch)) {
- exceptionHandler();
- }
- else {
- break _loop102;
- }
-
- } while (true);
- }
- if ( inputState.guessing==0 ) {
- behavior.endExceptionSpec();
- }
- }
-
- public final void exceptionSpec() throws RecognitionException, TokenStreamException {
-
- Token aa = null;
- Token labelAction = null;
-
- match(LITERAL_exception);
- {
- switch ( LA(1)) {
- case ARG_ACTION:
- {
- aa = LT(1);
- match(ARG_ACTION);
- if ( inputState.guessing==0 ) {
- labelAction = aa;
- }
- break;
- }
- case EOF:
- case ACTION:
- case DOC_COMMENT:
- case LITERAL_lexclass:
- case LITERAL_class:
- case TOKEN_REF:
- case RULE_REF:
- case LITERAL_protected:
- case LITERAL_public:
- case LITERAL_private:
- case LITERAL_exception:
- case LITERAL_catch:
- {
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- if ( inputState.guessing==0 ) {
- behavior.beginExceptionSpec(labelAction);
- }
- {
- _loop99:
- do {
- if ((LA(1)==LITERAL_catch)) {
- exceptionHandler();
- }
- else {
- break _loop99;
- }
-
- } while (true);
- }
- if ( inputState.guessing==0 ) {
- behavior.endExceptionSpec();
- }
- }
-
- public final void exceptionHandler() throws RecognitionException, TokenStreamException {
-
- Token a1 = null;
- Token a2 = null;
- Token exType; Token exName;
-
- match(LITERAL_catch);
- a1 = LT(1);
- match(ARG_ACTION);
- a2 = LT(1);
- match(ACTION);
- if ( inputState.guessing==0 ) {
- behavior.refExceptionHandler(a1, a2);
- }
- }
-
- public final void elementNoOptionSpec() throws RecognitionException, TokenStreamException {
-
- Token rr = null;
- Token aa = null;
- Token tr = null;
- Token aa2 = null;
- Token r2 = null;
- Token aa3 = null;
- Token a = null;
- Token p = null;
-
- Token label = null;
- Token assignId = null;
- Token args = null;
- int autoGen = GrammarElement.AUTO_GEN_NONE;
-
-
- switch ( LA(1)) {
- case ACTION:
- {
- a = LT(1);
- match(ACTION);
- if ( inputState.guessing==0 ) {
- behavior.refAction(a);
- }
- break;
- }
- case SEMPRED:
- {
- p = LT(1);
- match(SEMPRED);
- if ( inputState.guessing==0 ) {
- behavior.refSemPred(p);
- }
- break;
- }
- case TREE_BEGIN:
- {
- tree();
- break;
- }
- default:
- if ((LA(1)==TOKEN_REF||LA(1)==RULE_REF) && (LA(2)==ASSIGN)) {
- assignId=id();
- match(ASSIGN);
- {
- if ((LA(1)==TOKEN_REF||LA(1)==RULE_REF) && (LA(2)==COLON)) {
- label=id();
- match(COLON);
- if ( inputState.guessing==0 ) {
- checkForMissingEndRule(label);
- }
- }
- else if ((LA(1)==TOKEN_REF||LA(1)==RULE_REF) && (_tokenSet_3.member(LA(2)))) {
- }
- else {
- throw new NoViableAltException(LT(1), getFilename());
- }
-
- }
- {
- switch ( LA(1)) {
- case RULE_REF:
- {
- rr = LT(1);
- match(RULE_REF);
- {
- switch ( LA(1)) {
- case ARG_ACTION:
- {
- aa = LT(1);
- match(ARG_ACTION);
- if ( inputState.guessing==0 ) {
- args=aa;
- }
- break;
- }
- case STRING_LITERAL:
- case ACTION:
- case SEMI:
- case CHAR_LITERAL:
- case OR:
- case TOKEN_REF:
- case OPEN_ELEMENT_OPTION:
- case LPAREN:
- case RULE_REF:
- case RPAREN:
- case BANG:
- case LITERAL_exception:
- case NOT_OP:
- case SEMPRED:
- case TREE_BEGIN:
- case WILDCARD:
- {
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- {
- switch ( LA(1)) {
- case BANG:
- {
- match(BANG);
- if ( inputState.guessing==0 ) {
- autoGen = GrammarElement.AUTO_GEN_BANG;
- }
- break;
- }
- case STRING_LITERAL:
- case ACTION:
- case SEMI:
- case CHAR_LITERAL:
- case OR:
- case TOKEN_REF:
- case OPEN_ELEMENT_OPTION:
- case LPAREN:
- case RULE_REF:
- case RPAREN:
- case LITERAL_exception:
- case NOT_OP:
- case SEMPRED:
- case TREE_BEGIN:
- case WILDCARD:
- {
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- if ( inputState.guessing==0 ) {
- behavior.refRule(assignId, rr, label, args, autoGen);
- }
- break;
- }
- case TOKEN_REF:
- {
- tr = LT(1);
- match(TOKEN_REF);
- {
- switch ( LA(1)) {
- case ARG_ACTION:
- {
- aa2 = LT(1);
- match(ARG_ACTION);
- if ( inputState.guessing==0 ) {
- args=aa2;
- }
- break;
- }
- case STRING_LITERAL:
- case ACTION:
- case SEMI:
- case CHAR_LITERAL:
- case OR:
- case TOKEN_REF:
- case OPEN_ELEMENT_OPTION:
- case LPAREN:
- case RULE_REF:
- case RPAREN:
- case LITERAL_exception:
- case NOT_OP:
- case SEMPRED:
- case TREE_BEGIN:
- case WILDCARD:
- {
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- if ( inputState.guessing==0 ) {
- behavior.refToken(assignId, tr, label, args, false, autoGen, lastInRule());
- }
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- }
- else if ((_tokenSet_4.member(LA(1))) && (_tokenSet_5.member(LA(2)))) {
- {
- if ((LA(1)==TOKEN_REF||LA(1)==RULE_REF) && (LA(2)==COLON)) {
- label=id();
- match(COLON);
- if ( inputState.guessing==0 ) {
- checkForMissingEndRule(label);
- }
- }
- else if ((_tokenSet_4.member(LA(1))) && (_tokenSet_6.member(LA(2)))) {
- }
- else {
- throw new NoViableAltException(LT(1), getFilename());
- }
-
- }
- {
- switch ( LA(1)) {
- case RULE_REF:
- {
- r2 = LT(1);
- match(RULE_REF);
- {
- switch ( LA(1)) {
- case ARG_ACTION:
- {
- aa3 = LT(1);
- match(ARG_ACTION);
- if ( inputState.guessing==0 ) {
- args=aa3;
- }
- break;
- }
- case STRING_LITERAL:
- case ACTION:
- case SEMI:
- case CHAR_LITERAL:
- case OR:
- case TOKEN_REF:
- case OPEN_ELEMENT_OPTION:
- case LPAREN:
- case RULE_REF:
- case RPAREN:
- case BANG:
- case LITERAL_exception:
- case NOT_OP:
- case SEMPRED:
- case TREE_BEGIN:
- case WILDCARD:
- {
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- {
- switch ( LA(1)) {
- case BANG:
- {
- match(BANG);
- if ( inputState.guessing==0 ) {
- autoGen = GrammarElement.AUTO_GEN_BANG;
- }
- break;
- }
- case STRING_LITERAL:
- case ACTION:
- case SEMI:
- case CHAR_LITERAL:
- case OR:
- case TOKEN_REF:
- case OPEN_ELEMENT_OPTION:
- case LPAREN:
- case RULE_REF:
- case RPAREN:
- case LITERAL_exception:
- case NOT_OP:
- case SEMPRED:
- case TREE_BEGIN:
- case WILDCARD:
- {
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- if ( inputState.guessing==0 ) {
- behavior.refRule(assignId, r2, label, args, autoGen);
- }
- break;
- }
- case NOT_OP:
- {
- match(NOT_OP);
- {
- switch ( LA(1)) {
- case CHAR_LITERAL:
- case TOKEN_REF:
- {
- notTerminal(label);
- break;
- }
- case LPAREN:
- {
- ebnf(label,true);
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- break;
- }
- case LPAREN:
- {
- ebnf(label,false);
- break;
- }
- default:
- if ((LA(1)==STRING_LITERAL||LA(1)==CHAR_LITERAL||LA(1)==TOKEN_REF) && (LA(2)==RANGE)) {
- range(label);
- }
- else if ((_tokenSet_7.member(LA(1))) && (_tokenSet_8.member(LA(2)))) {
- terminal(label);
- }
- else {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- }
- else {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
-
- public final void elementOptionSpec() throws RecognitionException, TokenStreamException {
-
-
- Token o=null, v=null;
-
-
- match(OPEN_ELEMENT_OPTION);
- o=id();
- match(ASSIGN);
- v=optionValue();
- if ( inputState.guessing==0 ) {
- behavior.refElementOption(o,v);
- }
- {
- _loop108:
- do {
- if ((LA(1)==SEMI)) {
- match(SEMI);
- o=id();
- match(ASSIGN);
- v=optionValue();
- if ( inputState.guessing==0 ) {
- behavior.refElementOption(o,v);
- }
- }
- else {
- break _loop108;
- }
-
- } while (true);
- }
- match(CLOSE_ELEMENT_OPTION);
- }
-
- public final void range(
- Token label
- ) throws RecognitionException, TokenStreamException {
-
- Token crLeft = null;
- Token crRight = null;
- Token t = null;
- Token u = null;
- Token v = null;
- Token w = null;
-
- Token trLeft=null;
- Token trRight=null;
- int autoGen=GrammarElement.AUTO_GEN_NONE;
-
-
- switch ( LA(1)) {
- case CHAR_LITERAL:
- {
- crLeft = LT(1);
- match(CHAR_LITERAL);
- match(RANGE);
- crRight = LT(1);
- match(CHAR_LITERAL);
- {
- switch ( LA(1)) {
- case BANG:
- {
- match(BANG);
- if ( inputState.guessing==0 ) {
- autoGen = GrammarElement.AUTO_GEN_BANG;
- }
- break;
- }
- case STRING_LITERAL:
- case ACTION:
- case SEMI:
- case CHAR_LITERAL:
- case OR:
- case TOKEN_REF:
- case OPEN_ELEMENT_OPTION:
- case LPAREN:
- case RULE_REF:
- case RPAREN:
- case LITERAL_exception:
- case NOT_OP:
- case SEMPRED:
- case TREE_BEGIN:
- case WILDCARD:
- {
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- if ( inputState.guessing==0 ) {
- behavior.refCharRange(crLeft, crRight, label, autoGen, lastInRule());
- }
- break;
- }
- case STRING_LITERAL:
- case TOKEN_REF:
- {
- {
- switch ( LA(1)) {
- case TOKEN_REF:
- {
- t = LT(1);
- match(TOKEN_REF);
- if ( inputState.guessing==0 ) {
- trLeft=t;
- }
- break;
- }
- case STRING_LITERAL:
- {
- u = LT(1);
- match(STRING_LITERAL);
- if ( inputState.guessing==0 ) {
- trLeft=u;
- }
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- match(RANGE);
- {
- switch ( LA(1)) {
- case TOKEN_REF:
- {
- v = LT(1);
- match(TOKEN_REF);
- if ( inputState.guessing==0 ) {
- trRight=v;
- }
- break;
- }
- case STRING_LITERAL:
- {
- w = LT(1);
- match(STRING_LITERAL);
- if ( inputState.guessing==0 ) {
- trRight=w;
- }
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- autoGen=ast_type_spec();
- if ( inputState.guessing==0 ) {
- behavior.refTokenRange(trLeft, trRight, label, autoGen, lastInRule());
- }
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
-
- public final void terminal(
- Token label
- ) throws RecognitionException, TokenStreamException {
-
- Token cl = null;
- Token tr = null;
- Token aa = null;
- Token sl = null;
- Token wi = null;
-
- int autoGen=GrammarElement.AUTO_GEN_NONE;
- Token args=null;
-
-
- switch ( LA(1)) {
- case CHAR_LITERAL:
- {
- cl = LT(1);
- match(CHAR_LITERAL);
- {
- switch ( LA(1)) {
- case BANG:
- {
- match(BANG);
- if ( inputState.guessing==0 ) {
- autoGen = GrammarElement.AUTO_GEN_BANG;
- }
- break;
- }
- case STRING_LITERAL:
- case ACTION:
- case SEMI:
- case CHAR_LITERAL:
- case OR:
- case TOKEN_REF:
- case OPEN_ELEMENT_OPTION:
- case LPAREN:
- case RULE_REF:
- case RPAREN:
- case LITERAL_exception:
- case NOT_OP:
- case SEMPRED:
- case TREE_BEGIN:
- case WILDCARD:
- {
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- if ( inputState.guessing==0 ) {
- behavior.refCharLiteral(cl, label, false, autoGen, lastInRule());
- }
- break;
- }
- case TOKEN_REF:
- {
- tr = LT(1);
- match(TOKEN_REF);
- autoGen=ast_type_spec();
- {
- switch ( LA(1)) {
- case ARG_ACTION:
- {
- aa = LT(1);
- match(ARG_ACTION);
- if ( inputState.guessing==0 ) {
- args=aa;
- }
- break;
- }
- case STRING_LITERAL:
- case ACTION:
- case SEMI:
- case CHAR_LITERAL:
- case OR:
- case TOKEN_REF:
- case OPEN_ELEMENT_OPTION:
- case LPAREN:
- case RULE_REF:
- case RPAREN:
- case LITERAL_exception:
- case NOT_OP:
- case SEMPRED:
- case TREE_BEGIN:
- case WILDCARD:
- {
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- if ( inputState.guessing==0 ) {
- behavior.refToken(null, tr, label, args, false, autoGen, lastInRule());
- }
- break;
- }
- case STRING_LITERAL:
- {
- sl = LT(1);
- match(STRING_LITERAL);
- autoGen=ast_type_spec();
- if ( inputState.guessing==0 ) {
- behavior.refStringLiteral(sl, label, autoGen, lastInRule());
- }
- break;
- }
- case WILDCARD:
- {
- wi = LT(1);
- match(WILDCARD);
- autoGen=ast_type_spec();
- if ( inputState.guessing==0 ) {
- behavior.refWildcard(wi, label, autoGen);
- }
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
-
- public final void notTerminal(
- Token label
- ) throws RecognitionException, TokenStreamException {
-
- Token cl = null;
- Token tr = null;
- int autoGen=GrammarElement.AUTO_GEN_NONE;
-
- switch ( LA(1)) {
- case CHAR_LITERAL:
- {
- cl = LT(1);
- match(CHAR_LITERAL);
- {
- switch ( LA(1)) {
- case BANG:
- {
- match(BANG);
- if ( inputState.guessing==0 ) {
- autoGen = GrammarElement.AUTO_GEN_BANG;
- }
- break;
- }
- case STRING_LITERAL:
- case ACTION:
- case SEMI:
- case CHAR_LITERAL:
- case OR:
- case TOKEN_REF:
- case OPEN_ELEMENT_OPTION:
- case LPAREN:
- case RULE_REF:
- case RPAREN:
- case LITERAL_exception:
- case NOT_OP:
- case SEMPRED:
- case TREE_BEGIN:
- case WILDCARD:
- {
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- if ( inputState.guessing==0 ) {
- behavior.refCharLiteral(cl, label, true, autoGen, lastInRule());
- }
- break;
- }
- case TOKEN_REF:
- {
- tr = LT(1);
- match(TOKEN_REF);
- autoGen=ast_type_spec();
- if ( inputState.guessing==0 ) {
- behavior.refToken(null, tr, label, null, true, autoGen, lastInRule());
- }
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
-
- public final void ebnf(
- Token label, boolean not
- ) throws RecognitionException, TokenStreamException {
-
- Token lp = null;
- Token aa = null;
- Token ab = null;
-
- lp = LT(1);
- match(LPAREN);
- if ( inputState.guessing==0 ) {
- behavior.beginSubRule(label, lp.getLine(), not);
- }
- {
- if ((LA(1)==OPTIONS)) {
- subruleOptionsSpec();
- {
- switch ( LA(1)) {
- case ACTION:
- {
- aa = LT(1);
- match(ACTION);
- if ( inputState.guessing==0 ) {
- behavior.refInitAction(aa);
- }
- break;
- }
- case COLON:
- {
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- match(COLON);
- }
- else if ((LA(1)==ACTION) && (LA(2)==COLON)) {
- ab = LT(1);
- match(ACTION);
- if ( inputState.guessing==0 ) {
- behavior.refInitAction(ab);
- }
- match(COLON);
- }
- else if ((_tokenSet_9.member(LA(1))) && (_tokenSet_10.member(LA(2)))) {
- }
- else {
- throw new NoViableAltException(LT(1), getFilename());
- }
-
- }
- block();
- match(RPAREN);
- {
- switch ( LA(1)) {
- case STRING_LITERAL:
- case ACTION:
- case SEMI:
- case CHAR_LITERAL:
- case OR:
- case TOKEN_REF:
- case OPEN_ELEMENT_OPTION:
- case LPAREN:
- case RULE_REF:
- case RPAREN:
- case BANG:
- case LITERAL_exception:
- case NOT_OP:
- case SEMPRED:
- case TREE_BEGIN:
- case QUESTION:
- case STAR:
- case PLUS:
- case WILDCARD:
- {
- {
- switch ( LA(1)) {
- case QUESTION:
- {
- match(QUESTION);
- if ( inputState.guessing==0 ) {
- behavior.optionalSubRule();
- }
- break;
- }
- case STAR:
- {
- match(STAR);
- if ( inputState.guessing==0 ) {
- behavior.zeroOrMoreSubRule();;
- }
- break;
- }
- case PLUS:
- {
- match(PLUS);
- if ( inputState.guessing==0 ) {
- behavior.oneOrMoreSubRule();
- }
- break;
- }
- case STRING_LITERAL:
- case ACTION:
- case SEMI:
- case CHAR_LITERAL:
- case OR:
- case TOKEN_REF:
- case OPEN_ELEMENT_OPTION:
- case LPAREN:
- case RULE_REF:
- case RPAREN:
- case BANG:
- case LITERAL_exception:
- case NOT_OP:
- case SEMPRED:
- case TREE_BEGIN:
- case WILDCARD:
- {
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- {
- switch ( LA(1)) {
- case BANG:
- {
- match(BANG);
- if ( inputState.guessing==0 ) {
- behavior.noASTSubRule();
- }
- break;
- }
- case STRING_LITERAL:
- case ACTION:
- case SEMI:
- case CHAR_LITERAL:
- case OR:
- case TOKEN_REF:
- case OPEN_ELEMENT_OPTION:
- case LPAREN:
- case RULE_REF:
- case RPAREN:
- case LITERAL_exception:
- case NOT_OP:
- case SEMPRED:
- case TREE_BEGIN:
- case WILDCARD:
- {
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- break;
- }
- case IMPLIES:
- {
- match(IMPLIES);
- if ( inputState.guessing==0 ) {
- behavior.synPred();
- }
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- if ( inputState.guessing==0 ) {
- behavior.endSubRule();
- }
- }
-
- public final void tree() throws RecognitionException, TokenStreamException {
-
- Token lp = null;
-
- lp = LT(1);
- match(TREE_BEGIN);
- if ( inputState.guessing==0 ) {
- behavior.beginTree(lp.getLine());
- }
- rootNode();
- if ( inputState.guessing==0 ) {
- behavior.beginChildList();
- }
- {
- int _cnt122=0;
- _loop122:
- do {
- if ((_tokenSet_2.member(LA(1)))) {
- element();
- }
- else {
- if ( _cnt122>=1 ) { break _loop122; } else {throw new NoViableAltException(LT(1), getFilename());}
- }
-
- _cnt122++;
- } while (true);
- }
- if ( inputState.guessing==0 ) {
- behavior.endChildList();
- }
- match(RPAREN);
- if ( inputState.guessing==0 ) {
- behavior.endTree();
- }
- }
-
- public final void rootNode() throws RecognitionException, TokenStreamException {
-
- Token label = null;
-
- {
- if ((LA(1)==TOKEN_REF||LA(1)==RULE_REF) && (LA(2)==COLON)) {
- label=id();
- match(COLON);
- if ( inputState.guessing==0 ) {
- checkForMissingEndRule(label);
- }
- }
- else if ((_tokenSet_7.member(LA(1))) && (_tokenSet_11.member(LA(2)))) {
- }
- else {
- throw new NoViableAltException(LT(1), getFilename());
- }
-
- }
- terminal(label);
- }
-
- public final int ast_type_spec() throws RecognitionException, TokenStreamException {
- int autoGen ;
-
- autoGen = GrammarElement.AUTO_GEN_NONE;
-
- {
- switch ( LA(1)) {
- case CARET:
- {
- match(CARET);
- if ( inputState.guessing==0 ) {
- autoGen = GrammarElement.AUTO_GEN_CARET;
- }
- break;
- }
- case BANG:
- {
- match(BANG);
- if ( inputState.guessing==0 ) {
- autoGen = GrammarElement.AUTO_GEN_BANG;
- }
- break;
- }
- case STRING_LITERAL:
- case ACTION:
- case SEMI:
- case CHAR_LITERAL:
- case OR:
- case TOKEN_REF:
- case OPEN_ELEMENT_OPTION:
- case LPAREN:
- case RULE_REF:
- case RPAREN:
- case ARG_ACTION:
- case LITERAL_exception:
- case NOT_OP:
- case SEMPRED:
- case TREE_BEGIN:
- case WILDCARD:
- {
- break;
- }
- default:
- {
- throw new NoViableAltException(LT(1), getFilename());
- }
- }
- }
- return autoGen ;
- }
-
-
- public static final String[] _tokenNames = {
- "<0>",
- "EOF",
- "<2>",
- "NULL_TREE_LOOKAHEAD",
- "\"tokens\"",
- "\"header\"",
- "STRING_LITERAL",
- "ACTION",
- "DOC_COMMENT",
- "\"lexclass\"",
- "\"class\"",
- "\"extends\"",
- "\"Lexer\"",
- "\"TreeParser\"",
- "OPTIONS",
- "ASSIGN",
- "SEMI",
- "RCURLY",
- "\"charVocabulary\"",
- "CHAR_LITERAL",
- "INT",
- "OR",
- "RANGE",
- "TOKENS",
- "TOKEN_REF",
- "OPEN_ELEMENT_OPTION",
- "CLOSE_ELEMENT_OPTION",
- "LPAREN",
- "RULE_REF",
- "RPAREN",
- "\"Parser\"",
- "\"protected\"",
- "\"public\"",
- "\"private\"",
- "BANG",
- "ARG_ACTION",
- "\"returns\"",
- "COLON",
- "\"throws\"",
- "COMMA",
- "\"exception\"",
- "\"catch\"",
- "NOT_OP",
- "SEMPRED",
- "TREE_BEGIN",
- "QUESTION",
- "STAR",
- "PLUS",
- "IMPLIES",
- "CARET",
- "WILDCARD",
- "\"options\"",
- "WS",
- "COMMENT",
- "SL_COMMENT",
- "ML_COMMENT",
- "ESC",
- "DIGIT",
- "XDIGIT",
- "VOCAB",
- "NESTED_ARG_ACTION",
- "NESTED_ACTION",
- "WS_LOOP",
- "INTERNAL_RULE_REF",
- "WS_OPT",
- "NOT_USEFUL"
- };
-
- private static final long _tokenSet_0_data_[] = { 15317598464L, 0L };
- public static final BitSet _tokenSet_0 = new BitSet(_tokenSet_0_data_);
- private static final long _tokenSet_1_data_[] = { 547893559424L, 0L };
- public static final BitSet _tokenSet_1 = new BitSet(_tokenSet_1_data_);
- private static final long _tokenSet_2_data_[] = { 1156686652375232L, 0L };
- public static final BitSet _tokenSet_2 = new BitSet(_tokenSet_2_data_);
- private static final long _tokenSet_3_data_[] = { 1157838276198592L, 0L };
- public static final BitSet _tokenSet_3 = new BitSet(_tokenSet_3_data_);
- private static final long _tokenSet_4_data_[] = { 1130298373308480L, 0L };
- public static final BitSet _tokenSet_4 = new BitSet(_tokenSet_4_data_);
- private static final long _tokenSet_5_data_[] = { 1720925672784064L, 0L };
- public static final BitSet _tokenSet_5 = new BitSet(_tokenSet_5_data_);
- private static final long _tokenSet_6_data_[] = { 1720788233830592L, 0L };
- public static final BitSet _tokenSet_6 = new BitSet(_tokenSet_6_data_);
- private static final long _tokenSet_7_data_[] = { 1125899924144192L, 0L };
- public static final BitSet _tokenSet_7 = new BitSet(_tokenSet_7_data_);
- private static final long _tokenSet_8_data_[] = { 1720788229619904L, 0L };
- public static final BitSet _tokenSet_8 = new BitSet(_tokenSet_8_data_);
- private static final long _tokenSet_9_data_[] = { 1157803882840256L, 0L };
- public static final BitSet _tokenSet_9 = new BitSet(_tokenSet_9_data_);
- private static final long _tokenSet_10_data_[] = { 2250890277404864L, 0L };
- public static final BitSet _tokenSet_10 = new BitSet(_tokenSet_10_data_);
- private static final long _tokenSet_11_data_[] = { 1719688145404096L, 0L };
- public static final BitSet _tokenSet_11 = new BitSet(_tokenSet_11_data_);
-
- }
+
+ {
+ switch (LA(1)) {
+ case DOC_COMMENT:
+ {
+ d = LT(1);
+ match(DOC_COMMENT);
+ if (inputState.guessing == 0) {
+ doc = d.getText();
+ }
+ break;
+ }
+ case TOKEN_REF:
+ case RULE_REF:
+ case LITERAL_protected:
+ case LITERAL_public:
+ case LITERAL_private:
+ {
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ {
+ switch (LA(1)) {
+ case LITERAL_protected:
+ {
+ p1 = LT(1);
+ match(LITERAL_protected);
+ if (inputState.guessing == 0) {
+ access = p1.getText();
+ }
+ break;
+ }
+ case LITERAL_public:
+ {
+ p2 = LT(1);
+ match(LITERAL_public);
+ if (inputState.guessing == 0) {
+ access = p2.getText();
+ }
+ break;
+ }
+ case LITERAL_private:
+ {
+ p3 = LT(1);
+ match(LITERAL_private);
+ if (inputState.guessing == 0) {
+ access = p3.getText();
+ }
+ break;
+ }
+ case TOKEN_REF:
+ case RULE_REF:
+ {
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ idTok = id();
+ {
+ switch (LA(1)) {
+ case BANG:
+ {
+ match(BANG);
+ if (inputState.guessing == 0) {
+ ruleAutoGen = false;
+ }
+ break;
+ }
+ case ACTION:
+ case OPTIONS:
+ case ARG_ACTION:
+ case LITERAL_returns:
+ case COLON:
+ case LITERAL_throws:
+ {
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ if (inputState.guessing == 0) {
+
+ behavior.defineRuleName(idTok, access, ruleAutoGen, doc);
+
+ }
+ {
+ switch (LA(1)) {
+ case ARG_ACTION:
+ {
+ aa = LT(1);
+ match(ARG_ACTION);
+ if (inputState.guessing == 0) {
+ behavior.refArgAction(aa);
+ }
+ break;
+ }
+ case ACTION:
+ case OPTIONS:
+ case LITERAL_returns:
+ case COLON:
+ case LITERAL_throws:
+ {
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ {
+ switch (LA(1)) {
+ case LITERAL_returns:
+ {
+ match(LITERAL_returns);
+ rt = LT(1);
+ match(ARG_ACTION);
+ if (inputState.guessing == 0) {
+ behavior.refReturnAction(rt);
+ }
+ break;
+ }
+ case ACTION:
+ case OPTIONS:
+ case COLON:
+ case LITERAL_throws:
+ {
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ {
+ switch (LA(1)) {
+ case LITERAL_throws:
+ {
+ throwsSpec();
+ break;
+ }
+ case ACTION:
+ case OPTIONS:
+ case COLON:
+ {
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ {
+ switch (LA(1)) {
+ case OPTIONS:
+ {
+ ruleOptionsSpec();
+ break;
+ }
+ case ACTION:
+ case COLON:
+ {
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ {
+ switch (LA(1)) {
+ case ACTION:
+ {
+ a = LT(1);
+ match(ACTION);
+ if (inputState.guessing == 0) {
+ behavior.refInitAction(a);
+ }
+ break;
+ }
+ case COLON:
+ {
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ match(COLON);
+ block();
+ match(SEMI);
+ {
+ switch (LA(1)) {
+ case LITERAL_exception:
+ {
+ exceptionGroup();
+ break;
+ }
+ case EOF:
+ case ACTION:
+ case DOC_COMMENT:
+ case LITERAL_lexclass:
+ case LITERAL_class:
+ case TOKEN_REF:
+ case RULE_REF:
+ case LITERAL_protected:
+ case LITERAL_public:
+ case LITERAL_private:
+ {
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ if (inputState.guessing == 0) {
+ behavior.endRule(idTok.getText());
+ }
+ }
+
+ public final void throwsSpec() throws RecognitionException, TokenStreamException {
+
+
+ String t = null;
+ Token a,b;
+
+
+ match(LITERAL_throws);
+ a = id();
+ if (inputState.guessing == 0) {
+ t = a.getText();
+ }
+ {
+ _loop84:
+ do {
+ if ((LA(1) == COMMA)) {
+ match(COMMA);
+ b = id();
+ if (inputState.guessing == 0) {
+ t += "," + b.getText();
+ }
+ } else {
+ break _loop84;
+ }
+
+ } while (true);
+ }
+ if (inputState.guessing == 0) {
+ behavior.setUserExceptions(t);
+ }
+ }
+
+ public final void ruleOptionsSpec() throws RecognitionException, TokenStreamException {
+
+ Token idTok;
+ Token value;
+
+ match(OPTIONS);
+ {
+ _loop81:
+ do {
+ if ((LA(1) == TOKEN_REF || LA(1) == RULE_REF)) {
+ idTok = id();
+ match(ASSIGN);
+ value = optionValue();
+ if (inputState.guessing == 0) {
+ behavior.setRuleOption(idTok, value);
+ }
+ match(SEMI);
+ } else {
+ break _loop81;
+ }
+
+ } while (true);
+ }
+ match(RCURLY);
+ }
+
+ public final void block() throws RecognitionException, TokenStreamException {
+
+
+ if (inputState.guessing == 0) {
+ blockNesting++;
+ }
+ alternative();
+ {
+ _loop87:
+ do {
+ if ((LA(1) == OR)) {
+ match(OR);
+ alternative();
+ } else {
+ break _loop87;
+ }
+
+ } while (true);
+ }
+ if (inputState.guessing == 0) {
+ blockNesting--;
+ }
+ }
+
+ public final void exceptionGroup() throws RecognitionException, TokenStreamException {
+
+
+ if (inputState.guessing == 0) {
+ behavior.beginExceptionGroup();
+ }
+ {
+ int _cnt95 = 0;
+ _loop95:
+ do {
+ if ((LA(1) == LITERAL_exception)) {
+ exceptionSpec();
+ } else {
+ if (_cnt95 >= 1) {
+ break _loop95;
+ } else {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+
+ _cnt95++;
+ } while (true);
+ }
+ if (inputState.guessing == 0) {
+ behavior.endExceptionGroup();
+ }
+ }
+
+ public final void alternative() throws RecognitionException, TokenStreamException {
+
+ boolean altAutoGen = true;
+
+ {
+ switch (LA(1)) {
+ case BANG:
+ {
+ match(BANG);
+ if (inputState.guessing == 0) {
+ altAutoGen = false;
+ }
+ break;
+ }
+ case STRING_LITERAL:
+ case ACTION:
+ case SEMI:
+ case CHAR_LITERAL:
+ case OR:
+ case TOKEN_REF:
+ case LPAREN:
+ case RULE_REF:
+ case RPAREN:
+ case LITERAL_exception:
+ case NOT_OP:
+ case SEMPRED:
+ case TREE_BEGIN:
+ case WILDCARD:
+ {
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ if (inputState.guessing == 0) {
+ behavior.beginAlt(altAutoGen);
+ }
+ {
+ _loop91:
+ do {
+ if ((_tokenSet_2.member(LA(1)))) {
+ element();
+ } else {
+ break _loop91;
+ }
+
+ } while (true);
+ }
+ {
+ switch (LA(1)) {
+ case LITERAL_exception:
+ {
+ exceptionSpecNoLabel();
+ break;
+ }
+ case SEMI:
+ case OR:
+ case RPAREN:
+ {
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ if (inputState.guessing == 0) {
+ behavior.endAlt();
+ }
+ }
+
+ public final void element() throws RecognitionException, TokenStreamException {
+
+
+ elementNoOptionSpec();
+ {
+ switch (LA(1)) {
+ case OPEN_ELEMENT_OPTION:
+ {
+ elementOptionSpec();
+ break;
+ }
+ case STRING_LITERAL:
+ case ACTION:
+ case SEMI:
+ case CHAR_LITERAL:
+ case OR:
+ case TOKEN_REF:
+ case LPAREN:
+ case RULE_REF:
+ case RPAREN:
+ case LITERAL_exception:
+ case NOT_OP:
+ case SEMPRED:
+ case TREE_BEGIN:
+ case WILDCARD:
+ {
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ }
+
+ public final void exceptionSpecNoLabel() throws RecognitionException, TokenStreamException {
+
+
+ match(LITERAL_exception);
+ if (inputState.guessing == 0) {
+ behavior.beginExceptionSpec(null);
+ }
+ {
+ _loop102:
+ do {
+ if ((LA(1) == LITERAL_catch)) {
+ exceptionHandler();
+ } else {
+ break _loop102;
+ }
+
+ } while (true);
+ }
+ if (inputState.guessing == 0) {
+ behavior.endExceptionSpec();
+ }
+ }
+
+ public final void exceptionSpec() throws RecognitionException, TokenStreamException {
+
+ Token aa = null;
+ Token labelAction = null;
+
+ match(LITERAL_exception);
+ {
+ switch (LA(1)) {
+ case ARG_ACTION:
+ {
+ aa = LT(1);
+ match(ARG_ACTION);
+ if (inputState.guessing == 0) {
+ labelAction = aa;
+ }
+ break;
+ }
+ case EOF:
+ case ACTION:
+ case DOC_COMMENT:
+ case LITERAL_lexclass:
+ case LITERAL_class:
+ case TOKEN_REF:
+ case RULE_REF:
+ case LITERAL_protected:
+ case LITERAL_public:
+ case LITERAL_private:
+ case LITERAL_exception:
+ case LITERAL_catch:
+ {
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ if (inputState.guessing == 0) {
+ behavior.beginExceptionSpec(labelAction);
+ }
+ {
+ _loop99:
+ do {
+ if ((LA(1) == LITERAL_catch)) {
+ exceptionHandler();
+ } else {
+ break _loop99;
+ }
+
+ } while (true);
+ }
+ if (inputState.guessing == 0) {
+ behavior.endExceptionSpec();
+ }
+ }
+
+ public final void exceptionHandler() throws RecognitionException, TokenStreamException {
+
+ Token a1 = null;
+ Token a2 = null;
+ Token exType;
+ Token exName;
+
+ match(LITERAL_catch);
+ a1 = LT(1);
+ match(ARG_ACTION);
+ a2 = LT(1);
+ match(ACTION);
+ if (inputState.guessing == 0) {
+ behavior.refExceptionHandler(a1, a2);
+ }
+ }
+
+ public final void elementNoOptionSpec() throws RecognitionException, TokenStreamException {
+
+ Token rr = null;
+ Token aa = null;
+ Token tr = null;
+ Token aa2 = null;
+ Token r2 = null;
+ Token aa3 = null;
+ Token a = null;
+ Token p = null;
+
+ Token label = null;
+ Token assignId = null;
+ Token args = null;
+ int autoGen = GrammarElement.AUTO_GEN_NONE;
+
+
+ switch (LA(1)) {
+ case ACTION:
+ {
+ a = LT(1);
+ match(ACTION);
+ if (inputState.guessing == 0) {
+ behavior.refAction(a);
+ }
+ break;
+ }
+ case SEMPRED:
+ {
+ p = LT(1);
+ match(SEMPRED);
+ if (inputState.guessing == 0) {
+ behavior.refSemPred(p);
+ }
+ break;
+ }
+ case TREE_BEGIN:
+ {
+ tree();
+ break;
+ }
+ default:
+ if ((LA(1) == TOKEN_REF || LA(1) == RULE_REF) && (LA(2) == ASSIGN)) {
+ assignId = id();
+ match(ASSIGN);
+ {
+ if ((LA(1) == TOKEN_REF || LA(1) == RULE_REF) && (LA(2) == COLON)) {
+ label = id();
+ match(COLON);
+ if (inputState.guessing == 0) {
+ checkForMissingEndRule(label);
+ }
+ } else if ((LA(1) == TOKEN_REF || LA(1) == RULE_REF) && (_tokenSet_3.member(LA(2)))) {
+ } else {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+
+ }
+ {
+ switch (LA(1)) {
+ case RULE_REF:
+ {
+ rr = LT(1);
+ match(RULE_REF);
+ {
+ switch (LA(1)) {
+ case ARG_ACTION:
+ {
+ aa = LT(1);
+ match(ARG_ACTION);
+ if (inputState.guessing == 0) {
+ args = aa;
+ }
+ break;
+ }
+ case STRING_LITERAL:
+ case ACTION:
+ case SEMI:
+ case CHAR_LITERAL:
+ case OR:
+ case TOKEN_REF:
+ case OPEN_ELEMENT_OPTION:
+ case LPAREN:
+ case RULE_REF:
+ case RPAREN:
+ case BANG:
+ case LITERAL_exception:
+ case NOT_OP:
+ case SEMPRED:
+ case TREE_BEGIN:
+ case WILDCARD:
+ {
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ {
+ switch (LA(1)) {
+ case BANG:
+ {
+ match(BANG);
+ if (inputState.guessing == 0) {
+ autoGen = GrammarElement.AUTO_GEN_BANG;
+ }
+ break;
+ }
+ case STRING_LITERAL:
+ case ACTION:
+ case SEMI:
+ case CHAR_LITERAL:
+ case OR:
+ case TOKEN_REF:
+ case OPEN_ELEMENT_OPTION:
+ case LPAREN:
+ case RULE_REF:
+ case RPAREN:
+ case LITERAL_exception:
+ case NOT_OP:
+ case SEMPRED:
+ case TREE_BEGIN:
+ case WILDCARD:
+ {
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ if (inputState.guessing == 0) {
+ behavior.refRule(assignId, rr, label, args, autoGen);
+ }
+ break;
+ }
+ case TOKEN_REF:
+ {
+ tr = LT(1);
+ match(TOKEN_REF);
+ {
+ switch (LA(1)) {
+ case ARG_ACTION:
+ {
+ aa2 = LT(1);
+ match(ARG_ACTION);
+ if (inputState.guessing == 0) {
+ args = aa2;
+ }
+ break;
+ }
+ case STRING_LITERAL:
+ case ACTION:
+ case SEMI:
+ case CHAR_LITERAL:
+ case OR:
+ case TOKEN_REF:
+ case OPEN_ELEMENT_OPTION:
+ case LPAREN:
+ case RULE_REF:
+ case RPAREN:
+ case LITERAL_exception:
+ case NOT_OP:
+ case SEMPRED:
+ case TREE_BEGIN:
+ case WILDCARD:
+ {
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ if (inputState.guessing == 0) {
+ behavior.refToken(assignId, tr, label, args, false, autoGen, lastInRule());
+ }
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ } else if ((_tokenSet_4.member(LA(1))) && (_tokenSet_5.member(LA(2)))) {
+ {
+ if ((LA(1) == TOKEN_REF || LA(1) == RULE_REF) && (LA(2) == COLON)) {
+ label = id();
+ match(COLON);
+ if (inputState.guessing == 0) {
+ checkForMissingEndRule(label);
+ }
+ } else if ((_tokenSet_4.member(LA(1))) && (_tokenSet_6.member(LA(2)))) {
+ } else {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+
+ }
+ {
+ switch (LA(1)) {
+ case RULE_REF:
+ {
+ r2 = LT(1);
+ match(RULE_REF);
+ {
+ switch (LA(1)) {
+ case ARG_ACTION:
+ {
+ aa3 = LT(1);
+ match(ARG_ACTION);
+ if (inputState.guessing == 0) {
+ args = aa3;
+ }
+ break;
+ }
+ case STRING_LITERAL:
+ case ACTION:
+ case SEMI:
+ case CHAR_LITERAL:
+ case OR:
+ case TOKEN_REF:
+ case OPEN_ELEMENT_OPTION:
+ case LPAREN:
+ case RULE_REF:
+ case RPAREN:
+ case BANG:
+ case LITERAL_exception:
+ case NOT_OP:
+ case SEMPRED:
+ case TREE_BEGIN:
+ case WILDCARD:
+ {
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ {
+ switch (LA(1)) {
+ case BANG:
+ {
+ match(BANG);
+ if (inputState.guessing == 0) {
+ autoGen = GrammarElement.AUTO_GEN_BANG;
+ }
+ break;
+ }
+ case STRING_LITERAL:
+ case ACTION:
+ case SEMI:
+ case CHAR_LITERAL:
+ case OR:
+ case TOKEN_REF:
+ case OPEN_ELEMENT_OPTION:
+ case LPAREN:
+ case RULE_REF:
+ case RPAREN:
+ case LITERAL_exception:
+ case NOT_OP:
+ case SEMPRED:
+ case TREE_BEGIN:
+ case WILDCARD:
+ {
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ if (inputState.guessing == 0) {
+ behavior.refRule(assignId, r2, label, args, autoGen);
+ }
+ break;
+ }
+ case NOT_OP:
+ {
+ match(NOT_OP);
+ {
+ switch (LA(1)) {
+ case CHAR_LITERAL:
+ case TOKEN_REF:
+ {
+ notTerminal(label);
+ break;
+ }
+ case LPAREN:
+ {
+ ebnf(label, true);
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ break;
+ }
+ case LPAREN:
+ {
+ ebnf(label, false);
+ break;
+ }
+ default:
+ if ((LA(1) == STRING_LITERAL || LA(1) == CHAR_LITERAL || LA(1) == TOKEN_REF) && (LA(2) == RANGE)) {
+ range(label);
+ } else if ((_tokenSet_7.member(LA(1))) && (_tokenSet_8.member(LA(2)))) {
+ terminal(label);
+ } else {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ } else {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+
+ public final void elementOptionSpec() throws RecognitionException, TokenStreamException {
+
+
+ Token o = null, v = null;
+
+
+ match(OPEN_ELEMENT_OPTION);
+ o = id();
+ match(ASSIGN);
+ v = optionValue();
+ if (inputState.guessing == 0) {
+ behavior.refElementOption(o, v);
+ }
+ {
+ _loop108:
+ do {
+ if ((LA(1) == SEMI)) {
+ match(SEMI);
+ o = id();
+ match(ASSIGN);
+ v = optionValue();
+ if (inputState.guessing == 0) {
+ behavior.refElementOption(o, v);
+ }
+ } else {
+ break _loop108;
+ }
+
+ } while (true);
+ }
+ match(CLOSE_ELEMENT_OPTION);
+ }
+
+ public final void range(
+ Token label
+ ) throws RecognitionException, TokenStreamException {
+
+ Token crLeft = null;
+ Token crRight = null;
+ Token t = null;
+ Token u = null;
+ Token v = null;
+ Token w = null;
+
+ Token trLeft = null;
+ Token trRight = null;
+ int autoGen = GrammarElement.AUTO_GEN_NONE;
+
+
+ switch (LA(1)) {
+ case CHAR_LITERAL:
+ {
+ crLeft = LT(1);
+ match(CHAR_LITERAL);
+ match(RANGE);
+ crRight = LT(1);
+ match(CHAR_LITERAL);
+ {
+ switch (LA(1)) {
+ case BANG:
+ {
+ match(BANG);
+ if (inputState.guessing == 0) {
+ autoGen = GrammarElement.AUTO_GEN_BANG;
+ }
+ break;
+ }
+ case STRING_LITERAL:
+ case ACTION:
+ case SEMI:
+ case CHAR_LITERAL:
+ case OR:
+ case TOKEN_REF:
+ case OPEN_ELEMENT_OPTION:
+ case LPAREN:
+ case RULE_REF:
+ case RPAREN:
+ case LITERAL_exception:
+ case NOT_OP:
+ case SEMPRED:
+ case TREE_BEGIN:
+ case WILDCARD:
+ {
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ if (inputState.guessing == 0) {
+ behavior.refCharRange(crLeft, crRight, label, autoGen, lastInRule());
+ }
+ break;
+ }
+ case STRING_LITERAL:
+ case TOKEN_REF:
+ {
+ {
+ switch (LA(1)) {
+ case TOKEN_REF:
+ {
+ t = LT(1);
+ match(TOKEN_REF);
+ if (inputState.guessing == 0) {
+ trLeft = t;
+ }
+ break;
+ }
+ case STRING_LITERAL:
+ {
+ u = LT(1);
+ match(STRING_LITERAL);
+ if (inputState.guessing == 0) {
+ trLeft = u;
+ }
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ match(RANGE);
+ {
+ switch (LA(1)) {
+ case TOKEN_REF:
+ {
+ v = LT(1);
+ match(TOKEN_REF);
+ if (inputState.guessing == 0) {
+ trRight = v;
+ }
+ break;
+ }
+ case STRING_LITERAL:
+ {
+ w = LT(1);
+ match(STRING_LITERAL);
+ if (inputState.guessing == 0) {
+ trRight = w;
+ }
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ autoGen = ast_type_spec();
+ if (inputState.guessing == 0) {
+ behavior.refTokenRange(trLeft, trRight, label, autoGen, lastInRule());
+ }
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+
+ public final void terminal(
+ Token label
+ ) throws RecognitionException, TokenStreamException {
+
+ Token cl = null;
+ Token tr = null;
+ Token aa = null;
+ Token sl = null;
+ Token wi = null;
+
+ int autoGen = GrammarElement.AUTO_GEN_NONE;
+ Token args = null;
+
+
+ switch (LA(1)) {
+ case CHAR_LITERAL:
+ {
+ cl = LT(1);
+ match(CHAR_LITERAL);
+ {
+ switch (LA(1)) {
+ case BANG:
+ {
+ match(BANG);
+ if (inputState.guessing == 0) {
+ autoGen = GrammarElement.AUTO_GEN_BANG;
+ }
+ break;
+ }
+ case STRING_LITERAL:
+ case ACTION:
+ case SEMI:
+ case CHAR_LITERAL:
+ case OR:
+ case TOKEN_REF:
+ case OPEN_ELEMENT_OPTION:
+ case LPAREN:
+ case RULE_REF:
+ case RPAREN:
+ case LITERAL_exception:
+ case NOT_OP:
+ case SEMPRED:
+ case TREE_BEGIN:
+ case WILDCARD:
+ {
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ if (inputState.guessing == 0) {
+ behavior.refCharLiteral(cl, label, false, autoGen, lastInRule());
+ }
+ break;
+ }
+ case TOKEN_REF:
+ {
+ tr = LT(1);
+ match(TOKEN_REF);
+ autoGen = ast_type_spec();
+ {
+ switch (LA(1)) {
+ case ARG_ACTION:
+ {
+ aa = LT(1);
+ match(ARG_ACTION);
+ if (inputState.guessing == 0) {
+ args = aa;
+ }
+ break;
+ }
+ case STRING_LITERAL:
+ case ACTION:
+ case SEMI:
+ case CHAR_LITERAL:
+ case OR:
+ case TOKEN_REF:
+ case OPEN_ELEMENT_OPTION:
+ case LPAREN:
+ case RULE_REF:
+ case RPAREN:
+ case LITERAL_exception:
+ case NOT_OP:
+ case SEMPRED:
+ case TREE_BEGIN:
+ case WILDCARD:
+ {
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ if (inputState.guessing == 0) {
+ behavior.refToken(null, tr, label, args, false, autoGen, lastInRule());
+ }
+ break;
+ }
+ case STRING_LITERAL:
+ {
+ sl = LT(1);
+ match(STRING_LITERAL);
+ autoGen = ast_type_spec();
+ if (inputState.guessing == 0) {
+ behavior.refStringLiteral(sl, label, autoGen, lastInRule());
+ }
+ break;
+ }
+ case WILDCARD:
+ {
+ wi = LT(1);
+ match(WILDCARD);
+ autoGen = ast_type_spec();
+ if (inputState.guessing == 0) {
+ behavior.refWildcard(wi, label, autoGen);
+ }
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+
+ public final void notTerminal(
+ Token label
+ ) throws RecognitionException, TokenStreamException {
+
+ Token cl = null;
+ Token tr = null;
+ int autoGen = GrammarElement.AUTO_GEN_NONE;
+
+ switch (LA(1)) {
+ case CHAR_LITERAL:
+ {
+ cl = LT(1);
+ match(CHAR_LITERAL);
+ {
+ switch (LA(1)) {
+ case BANG:
+ {
+ match(BANG);
+ if (inputState.guessing == 0) {
+ autoGen = GrammarElement.AUTO_GEN_BANG;
+ }
+ break;
+ }
+ case STRING_LITERAL:
+ case ACTION:
+ case SEMI:
+ case CHAR_LITERAL:
+ case OR:
+ case TOKEN_REF:
+ case OPEN_ELEMENT_OPTION:
+ case LPAREN:
+ case RULE_REF:
+ case RPAREN:
+ case LITERAL_exception:
+ case NOT_OP:
+ case SEMPRED:
+ case TREE_BEGIN:
+ case WILDCARD:
+ {
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ if (inputState.guessing == 0) {
+ behavior.refCharLiteral(cl, label, true, autoGen, lastInRule());
+ }
+ break;
+ }
+ case TOKEN_REF:
+ {
+ tr = LT(1);
+ match(TOKEN_REF);
+ autoGen = ast_type_spec();
+ if (inputState.guessing == 0) {
+ behavior.refToken(null, tr, label, null, true, autoGen, lastInRule());
+ }
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+
+ public final void ebnf(
+ Token label, boolean not
+ ) throws RecognitionException, TokenStreamException {
+
+ Token lp = null;
+ Token aa = null;
+ Token ab = null;
+
+ lp = LT(1);
+ match(LPAREN);
+ if (inputState.guessing == 0) {
+ behavior.beginSubRule(label, lp.getLine(), not);
+ }
+ {
+ if ((LA(1) == OPTIONS)) {
+ subruleOptionsSpec();
+ {
+ switch (LA(1)) {
+ case ACTION:
+ {
+ aa = LT(1);
+ match(ACTION);
+ if (inputState.guessing == 0) {
+ behavior.refInitAction(aa);
+ }
+ break;
+ }
+ case COLON:
+ {
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ match(COLON);
+ } else if ((LA(1) == ACTION) && (LA(2) == COLON)) {
+ ab = LT(1);
+ match(ACTION);
+ if (inputState.guessing == 0) {
+ behavior.refInitAction(ab);
+ }
+ match(COLON);
+ } else if ((_tokenSet_9.member(LA(1))) && (_tokenSet_10.member(LA(2)))) {
+ } else {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+
+ }
+ block();
+ match(RPAREN);
+ {
+ switch (LA(1)) {
+ case STRING_LITERAL:
+ case ACTION:
+ case SEMI:
+ case CHAR_LITERAL:
+ case OR:
+ case TOKEN_REF:
+ case OPEN_ELEMENT_OPTION:
+ case LPAREN:
+ case RULE_REF:
+ case RPAREN:
+ case BANG:
+ case LITERAL_exception:
+ case NOT_OP:
+ case SEMPRED:
+ case TREE_BEGIN:
+ case QUESTION:
+ case STAR:
+ case PLUS:
+ case WILDCARD:
+ {
+ {
+ switch (LA(1)) {
+ case QUESTION:
+ {
+ match(QUESTION);
+ if (inputState.guessing == 0) {
+ behavior.optionalSubRule();
+ }
+ break;
+ }
+ case STAR:
+ {
+ match(STAR);
+ if (inputState.guessing == 0) {
+ behavior.zeroOrMoreSubRule();
+ ;
+ }
+ break;
+ }
+ case PLUS:
+ {
+ match(PLUS);
+ if (inputState.guessing == 0) {
+ behavior.oneOrMoreSubRule();
+ }
+ break;
+ }
+ case STRING_LITERAL:
+ case ACTION:
+ case SEMI:
+ case CHAR_LITERAL:
+ case OR:
+ case TOKEN_REF:
+ case OPEN_ELEMENT_OPTION:
+ case LPAREN:
+ case RULE_REF:
+ case RPAREN:
+ case BANG:
+ case LITERAL_exception:
+ case NOT_OP:
+ case SEMPRED:
+ case TREE_BEGIN:
+ case WILDCARD:
+ {
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ {
+ switch (LA(1)) {
+ case BANG:
+ {
+ match(BANG);
+ if (inputState.guessing == 0) {
+ behavior.noASTSubRule();
+ }
+ break;
+ }
+ case STRING_LITERAL:
+ case ACTION:
+ case SEMI:
+ case CHAR_LITERAL:
+ case OR:
+ case TOKEN_REF:
+ case OPEN_ELEMENT_OPTION:
+ case LPAREN:
+ case RULE_REF:
+ case RPAREN:
+ case LITERAL_exception:
+ case NOT_OP:
+ case SEMPRED:
+ case TREE_BEGIN:
+ case WILDCARD:
+ {
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ break;
+ }
+ case IMPLIES:
+ {
+ match(IMPLIES);
+ if (inputState.guessing == 0) {
+ behavior.synPred();
+ }
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ if (inputState.guessing == 0) {
+ behavior.endSubRule();
+ }
+ }
+
+ public final void tree() throws RecognitionException, TokenStreamException {
+
+ Token lp = null;
+
+ lp = LT(1);
+ match(TREE_BEGIN);
+ if (inputState.guessing == 0) {
+ behavior.beginTree(lp.getLine());
+ }
+ rootNode();
+ if (inputState.guessing == 0) {
+ behavior.beginChildList();
+ }
+ {
+ int _cnt122 = 0;
+ _loop122:
+ do {
+ if ((_tokenSet_2.member(LA(1)))) {
+ element();
+ } else {
+ if (_cnt122 >= 1) {
+ break _loop122;
+ } else {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+
+ _cnt122++;
+ } while (true);
+ }
+ if (inputState.guessing == 0) {
+ behavior.endChildList();
+ }
+ match(RPAREN);
+ if (inputState.guessing == 0) {
+ behavior.endTree();
+ }
+ }
+
+ public final void rootNode() throws RecognitionException, TokenStreamException {
+
+ Token label = null;
+
+ {
+ if ((LA(1) == TOKEN_REF || LA(1) == RULE_REF) && (LA(2) == COLON)) {
+ label = id();
+ match(COLON);
+ if (inputState.guessing == 0) {
+ checkForMissingEndRule(label);
+ }
+ } else if ((_tokenSet_7.member(LA(1))) && (_tokenSet_11.member(LA(2)))) {
+ } else {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+
+ }
+ terminal(label);
+ }
+
+ public final int ast_type_spec() throws RecognitionException, TokenStreamException {
+ int autoGen;
+
+ autoGen = GrammarElement.AUTO_GEN_NONE;
+
+ {
+ switch (LA(1)) {
+ case CARET:
+ {
+ match(CARET);
+ if (inputState.guessing == 0) {
+ autoGen = GrammarElement.AUTO_GEN_CARET;
+ }
+ break;
+ }
+ case BANG:
+ {
+ match(BANG);
+ if (inputState.guessing == 0) {
+ autoGen = GrammarElement.AUTO_GEN_BANG;
+ }
+ break;
+ }
+ case STRING_LITERAL:
+ case ACTION:
+ case SEMI:
+ case CHAR_LITERAL:
+ case OR:
+ case TOKEN_REF:
+ case OPEN_ELEMENT_OPTION:
+ case LPAREN:
+ case RULE_REF:
+ case RPAREN:
+ case ARG_ACTION:
+ case LITERAL_exception:
+ case NOT_OP:
+ case SEMPRED:
+ case TREE_BEGIN:
+ case WILDCARD:
+ {
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+ }
+ }
+ return autoGen;
+ }
+
+
+ public static final String[] _tokenNames = {
+ "<0>",
+ "EOF",
+ "<2>",
+ "NULL_TREE_LOOKAHEAD",
+ "\"tokens\"",
+ "\"header\"",
+ "STRING_LITERAL",
+ "ACTION",
+ "DOC_COMMENT",
+ "\"lexclass\"",
+ "\"class\"",
+ "\"extends\"",
+ "\"Lexer\"",
+ "\"TreeParser\"",
+ "OPTIONS",
+ "ASSIGN",
+ "SEMI",
+ "RCURLY",
+ "\"charVocabulary\"",
+ "CHAR_LITERAL",
+ "INT",
+ "OR",
+ "RANGE",
+ "TOKENS",
+ "TOKEN_REF",
+ "OPEN_ELEMENT_OPTION",
+ "CLOSE_ELEMENT_OPTION",
+ "LPAREN",
+ "RULE_REF",
+ "RPAREN",
+ "\"Parser\"",
+ "\"protected\"",
+ "\"public\"",
+ "\"private\"",
+ "BANG",
+ "ARG_ACTION",
+ "\"returns\"",
+ "COLON",
+ "\"throws\"",
+ "COMMA",
+ "\"exception\"",
+ "\"catch\"",
+ "NOT_OP",
+ "SEMPRED",
+ "TREE_BEGIN",
+ "QUESTION",
+ "STAR",
+ "PLUS",
+ "IMPLIES",
+ "CARET",
+ "WILDCARD",
+ "\"options\"",
+ "WS",
+ "COMMENT",
+ "SL_COMMENT",
+ "ML_COMMENT",
+ "ESC",
+ "DIGIT",
+ "XDIGIT",
+ "VOCAB",
+ "NESTED_ARG_ACTION",
+ "NESTED_ACTION",
+ "WS_LOOP",
+ "INTERNAL_RULE_REF",
+ "WS_OPT",
+ "NOT_USEFUL"
+ };
+
+ private static final long _tokenSet_0_data_[] = { 15317598464L, 0L };
+
+ public static final BitSet _tokenSet_0 = new BitSet(_tokenSet_0_data_);
+
+ private static final long _tokenSet_1_data_[] = { 547893559424L, 0L };
+
+ public static final BitSet _tokenSet_1 = new BitSet(_tokenSet_1_data_);
+
+ private static final long _tokenSet_2_data_[] = { 1156686652375232L, 0L };
+
+ public static final BitSet _tokenSet_2 = new BitSet(_tokenSet_2_data_);
+
+ private static final long _tokenSet_3_data_[] = { 1157838276198592L, 0L };
+
+ public static final BitSet _tokenSet_3 = new BitSet(_tokenSet_3_data_);
+
+ private static final long _tokenSet_4_data_[] = { 1130298373308480L, 0L };
+
+ public static final BitSet _tokenSet_4 = new BitSet(_tokenSet_4_data_);
+
+ private static final long _tokenSet_5_data_[] = { 1720925672784064L, 0L };
+
+ public static final BitSet _tokenSet_5 = new BitSet(_tokenSet_5_data_);
+
+ private static final long _tokenSet_6_data_[] = { 1720788233830592L, 0L };
+
+ public static final BitSet _tokenSet_6 = new BitSet(_tokenSet_6_data_);
+
+ private static final long _tokenSet_7_data_[] = { 1125899924144192L, 0L };
+
+ public static final BitSet _tokenSet_7 = new BitSet(_tokenSet_7_data_);
+
+ private static final long _tokenSet_8_data_[] = { 1720788229619904L, 0L };
+
+ public static final BitSet _tokenSet_8 = new BitSet(_tokenSet_8_data_);
+
+ private static final long _tokenSet_9_data_[] = { 1157803882840256L, 0L };
+
+ public static final BitSet _tokenSet_9 = new BitSet(_tokenSet_9_data_);
+
+ private static final long _tokenSet_10_data_[] = { 2250890277404864L, 0L };
+
+ public static final BitSet _tokenSet_10 = new BitSet(_tokenSet_10_data_);
+
+ private static final long _tokenSet_11_data_[] = { 1719688145404096L, 0L };
+
+ public static final BitSet _tokenSet_11 = new BitSet(_tokenSet_11_data_);
+
+}
1.2 +53 -34 e/src/jsrc/antlr/ANTLRStringBuffer.java
Index: ANTLRStringBuffer.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/ANTLRStringBuffer.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ANTLRStringBuffer.java 2001/10/03 21:04:13 1.1
+++ ANTLRStringBuffer.java 2001/10/04 01:09:13 1.2
@@ -4,56 +4,75 @@
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
- * $Id: ANTLRStringBuffer.java,v 1.1 2001/10/03 21:04:13 markm Exp $
+ * $Id: ANTLRStringBuffer.java,v 1.2 2001/10/04 01:09:13 markm Exp $
*/
// Implementation of a StringBuffer-like object that does not have the
// unfortunate side-effect of creating Strings with very large buffers.
public class ANTLRStringBuffer {
+
protected char[] buffer = new char[8];
+
protected int length = 0; // length and also where to store next char
+
+ public ANTLRStringBuffer() {
+ }
- public ANTLRStringBuffer() {}
public final void append(char c) {
- // This would normally be an "ensureCapacity" method, but inlined
- // here for speed.
- if (length >= buffer.length) {
- // Compute a new length that is at least double old length
- int newSize = buffer.length;
- while (length >= newSize) {
- newSize *= 2;
- }
- // Allocate new array and copy buffer
- char[] newBuffer = new char[newSize];
- for (int i = 0; i < length; i++) {
- newBuffer[i] = buffer[i];
- }
- buffer = newBuffer;
- }
- buffer[length] = c;
- length++;
+ // This would normally be an "ensureCapacity" method, but inlined
+ // here for speed.
+ if (length >= buffer.length) {
+ // Compute a new length that is at least double old length
+ int newSize = buffer.length;
+ while (length >= newSize) {
+ newSize *= 2;
+ }
+ // Allocate new array and copy buffer
+ char[] newBuffer = new char[newSize];
+ for (int i = 0; i < length; i++) {
+ newBuffer[i] = buffer[i];
+ }
+ buffer = newBuffer;
+ }
+ buffer[length] = c;
+ length++;
}
+
public final void append(String s) {
- for (int i = 0; i < s.length(); i++) {
- append(s.charAt(i));
- }
- }
- public final char charAt(int index) { return buffer[index]; }
- final public char[] getBuffer() { return buffer; }
- public final int length() { return length; }
- public final void setCharAt(int index, char ch) { buffer[index] = ch; }
+ for (int i = 0; i < s.length(); i++) {
+ append(s.charAt(i));
+ }
+ }
+
+ public final char charAt(int index) {
+ return buffer[index];
+ }
+
+ final public char[] getBuffer() {
+ return buffer;
+ }
+
+ public final int length() {
+ return length;
+ }
+
+ public final void setCharAt(int index, char ch) {
+ buffer[index] = ch;
+ }
+
public final void setLength(int newLength) {
- if (newLength < length) {
- length = newLength;
- } else {
- while (newLength > length) {
- append('\0');
- }
- }
+ if (newLength < length) {
+ length = newLength;
+ } else {
+ while (newLength > length) {
+ append('\0');
+ }
+ }
}
+
public final String toString() {
- return new String(buffer, 0, length);
+ return new String(buffer, 0, length);
}
}
1.2 +1063 -704 e/src/jsrc/antlr/ANTLRTokdefLexer.java
Index: ANTLRTokdefLexer.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/ANTLRTokdefLexer.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ANTLRTokdefLexer.java 2001/10/03 21:04:13 1.1
+++ ANTLRTokdefLexer.java 2001/10/04 01:09:13 1.2
@@ -2,715 +2,1074 @@
/*
* ANTLR-generated file resulting from grammar tokdef.g
- *
+ *
* Terence Parr, MageLang Institute
* ANTLR Version 2.7.0a2; 1989-1999
*/
- import java.io.InputStream;
+
+import antlr.collections.impl.BitSet;
+
+import java.io.InputStream;
import java.io.Reader;
-import antlr.TokenStreamException;
-import antlr.TokenStreamIOException;
-import antlr.CharStreamException;
-import antlr.CharStreamIOException;
import java.util.Hashtable;
-import antlr.CharScanner;
-import antlr.InputBuffer;
-import antlr.ByteBuffer;
-import antlr.CharBuffer;
-import antlr.Token;
-import antlr.CommonToken;
-import antlr.RecognitionException;
-import antlr.NoViableAltForCharException;
-import antlr.MismatchedCharException;
-import antlr.TokenStream;
-import antlr.ANTLRHashString;
-import antlr.LexerSharedInputState;
-import antlr.collections.impl.BitSet;
-public class ANTLRTokdefLexer extends antlr.CharScanner implements ANTLRTokdefParserTokenTypes, TokenStream
- {
- private static final long _tokenSet_0_data_[] = { -9224L, 9223372036854775807L, 0L, 0L };
- public static final BitSet _tokenSet_0 = new BitSet(_tokenSet_0_data_);
- private static final long _tokenSet_1_data_[] = { -140737488355336L, 9223372036854775807L, 0L, 0L };
- public static final BitSet _tokenSet_1 = new BitSet(_tokenSet_1_data_);
-
-
-public ANTLRTokdefLexer(InputBuffer ib) {
- this(new LexerSharedInputState(ib));
-}
-public ANTLRTokdefLexer(LexerSharedInputState state) {
- super(state);
- literals = new Hashtable();
-caseSensitiveLiterals = true;
-setCaseSensitive(true);
-}
-public ANTLRTokdefLexer(InputStream in) {
- this(new ByteBuffer(in));
-}
-public ANTLRTokdefLexer(Reader in) {
- this(new CharBuffer(in));
-}
- public final void mASSIGN(boolean _createToken) throws RecognitionException, CharStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = ASSIGN;
- int _saveIndex;
-
- match('=');
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
- protected final void mDIGIT(boolean _createToken) throws RecognitionException, CharStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = DIGIT;
- int _saveIndex;
-
- matchRange('0','9');
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
- protected final void mESC(boolean _createToken) throws RecognitionException, CharStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = ESC;
- int _saveIndex;
-
- match('\\');
- {
- switch ( LA(1)) {
- case 'n':
- {
- match('n');
- break;
- }
- case 'r':
- {
- match('r');
- break;
- }
- case 't':
- {
- match('t');
- break;
- }
- case 'b':
- {
- match('b');
- break;
- }
- case 'f':
- {
- match('f');
- break;
- }
- case '"':
- {
- match('"');
- break;
- }
- case '\'':
- {
- match('\'');
- break;
- }
- case '\\':
- {
- match('\\');
- break;
- }
- case '0': case '1': case '2': case '3':
- {
- {
- matchRange('0','3');
- }
- {
- if (((LA(1) >= '0' && LA(1) <= '9')) && ((LA(2) >= '\3' && LA(2) <= '~'))) {
- mDIGIT(false);
- {
- if (((LA(1) >= '0' && LA(1) <= '9')) && ((LA(2) >= '\3' && LA(2) <= '~'))) {
- mDIGIT(false);
- }
- else if (((LA(1) >= '\3' && LA(1) <= '~')) && (true)) {
- }
- else {
- throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());
- }
-
- }
- }
- else if (((LA(1) >= '\3' && LA(1) <= '~')) && (true)) {
- }
- else {
- throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());
- }
-
- }
- break;
- }
- case '4': case '5': case '6': case '7':
- {
- {
- matchRange('4','7');
- }
- {
- if (((LA(1) >= '0' && LA(1) <= '9')) && ((LA(2) >= '\3' && LA(2) <= '~'))) {
- mDIGIT(false);
- }
- else if (((LA(1) >= '\3' && LA(1) <= '~')) && (true)) {
- }
- else {
- throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());
- }
-
- }
- break;
- }
- case 'u':
- {
- match('u');
- mXDIGIT(false);
- mXDIGIT(false);
- mXDIGIT(false);
- mXDIGIT(false);
- break;
- }
- default:
- {
- throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());
- }
- }
- }
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
- public final void mID(boolean _createToken) throws RecognitionException, CharStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = ID;
- int _saveIndex;
-
- {
- switch ( LA(1)) {
- case 'a': case 'b': case 'c': case 'd':
- case 'e': case 'f': case 'g': case 'h':
- case 'i': case 'j': case 'k': case 'l':
- case 'm': case 'n': case 'o': case 'p':
- case 'q': case 'r': case 's': case 't':
- case 'u': case 'v': case 'w': case 'x':
- case 'y': case 'z':
- {
- matchRange('a','z');
- break;
- }
- case 'A': case 'B': case 'C': case 'D':
- case 'E': case 'F': case 'G': case 'H':
- case 'I': case 'J': case 'K': case 'L':
- case 'M': case 'N': case 'O': case 'P':
- case 'Q': case 'R': case 'S': case 'T':
- case 'U': case 'V': case 'W': case 'X':
- case 'Y': case 'Z':
- {
- matchRange('A','Z');
- break;
- }
- default:
- {
- throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());
- }
- }
- }
- {
- _loop37:
- do {
- switch ( LA(1)) {
- case 'a': case 'b': case 'c': case 'd':
- case 'e': case 'f': case 'g': case 'h':
- case 'i': case 'j': case 'k': case 'l':
- case 'm': case 'n': case 'o': case 'p':
- case 'q': case 'r': case 's': case 't':
- case 'u': case 'v': case 'w': case 'x':
- case 'y': case 'z':
- {
- matchRange('a','z');
- break;
- }
- case 'A': case 'B': case 'C': case 'D':
- case 'E': case 'F': case 'G': case 'H':
- case 'I': case 'J': case 'K': case 'L':
- case 'M': case 'N': case 'O': case 'P':
- case 'Q': case 'R': case 'S': case 'T':
- case 'U': case 'V': case 'W': case 'X':
- case 'Y': case 'Z':
- {
- matchRange('A','Z');
- break;
- }
- case '_':
- {
- match('_');
- break;
- }
- case '0': case '1': case '2': case '3':
- case '4': case '5': case '6': case '7':
- case '8': case '9':
- {
- matchRange('0','9');
- break;
- }
- default:
- {
- break _loop37;
- }
- }
- } while (true);
- }
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
- public final void mINT(boolean _createToken) throws RecognitionException, CharStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = INT;
- int _saveIndex;
-
- {
- int _cnt40=0;
- _loop40:
- do {
- if (((LA(1) >= '0' && LA(1) <= '9'))) {
- mDIGIT(false);
- }
- else {
- if ( _cnt40>=1 ) { break _loop40; } else {throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());}
- }
-
- _cnt40++;
- } while (true);
- }
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
- public final void mLPAREN(boolean _createToken) throws RecognitionException, CharStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = LPAREN;
- int _saveIndex;
-
- match('(');
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
- public final void mML_COMMENT(boolean _createToken) throws RecognitionException, CharStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = ML_COMMENT;
- int _saveIndex;
-
- match("/*");
- {
- _loop17:
- do {
- switch ( LA(1)) {
- case '\n':
- {
- match('\n');
- newline();
- break;
- }
- case '\3': case '\4': case '\5': case '\6':
- case '\7': case '\10': case '\t': case '\13':
- case '\14': case '\r': case '\16': case '\17':
- case '\20': case '\21': case '\22': case '\23':
- case '\24': case '\25': case '\26': case '\27':
- case '\30': case '\31': case '\32': case '\33':
- case '\34': case '\35': case '\36': case '\37':
- case ' ': case '!': case '"': case '#':
- case '$': case '%': case '&': case '\'':
- case '(': case ')': case '+': case ',':
- case '-': case '.': case '/': case '0':
- case '1': case '2': case '3': case '4':
- case '5': case '6': case '7': case '8':
- case '9': case ':': case ';': case '<':
- case '=': case '>': case '?': case '@':
- case 'A': case 'B': case 'C': case 'D':
- case 'E': case 'F': case 'G': case 'H':
- case 'I': case 'J': case 'K': case 'L':
- case 'M': case 'N': case 'O': case 'P':
- case 'Q': case 'R': case 'S': case 'T':
- case 'U': case 'V': case 'W': case 'X':
- case 'Y': case 'Z': case '[': case '\\':
- case ']': case '^': case '_': case '`':
- case 'a': case 'b': case 'c': case 'd':
- case 'e': case 'f': case 'g': case 'h':
- case 'i': case 'j': case 'k': case 'l':
- case 'm': case 'n': case 'o': case 'p':
- case 'q': case 'r': case 's': case 't':
- case 'u': case 'v': case 'w': case 'x':
- case 'y': case 'z': case '{': case '|':
- case '}': case '~':
- {
- matchNot('*');
- break;
- }
- default:
- if ((LA(1)=='*') && (_tokenSet_1.member(LA(2)))) {
- match('*');
- matchNot('/');
- }
- else {
- break _loop17;
- }
- }
- } while (true);
- }
- match("*/");
- _ttype = Token.SKIP;
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
- public final void mRPAREN(boolean _createToken) throws RecognitionException, CharStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = RPAREN;
- int _saveIndex;
-
- match(')');
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
- public final void mSL_COMMENT(boolean _createToken) throws RecognitionException, CharStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = SL_COMMENT;
- int _saveIndex;
-
- match("//");
- {
- _loop12:
- do {
- if ((_tokenSet_0.member(LA(1)))) {
- {
- match(_tokenSet_0);
- }
- }
- else {
- break _loop12;
- }
-
- } while (true);
- }
- {
- switch ( LA(1)) {
- case '\n':
- {
- match('\n');
- break;
- }
- case '\r':
- {
- match('\r');
- {
- if ((LA(1)=='\n')) {
- match('\n');
- }
- else {
- }
-
- }
- break;
- }
- default:
- {
- throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());
- }
- }
- }
- _ttype = Token.SKIP; newline();
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
- public final void mSTRING(boolean _createToken) throws RecognitionException, CharStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = STRING;
- int _saveIndex;
-
- match('"');
- {
- _loop23:
- do {
- switch ( LA(1)) {
- case '\\':
- {
- mESC(false);
- break;
- }
- case '\3': case '\4': case '\5': case '\6':
- case '\7': case '\10': case '\t': case '\n':
- case '\13': case '\14': case '\r': case '\16':
- case '\17': case '\20': case '\21': case '\22':
- case '\23': case '\24': case '\25': case '\26':
- case '\27': case '\30': case '\31': case '\32':
- case '\33': case '\34': case '\35': case '\36':
- case '\37': case ' ': case '!': case '#':
- case '$': case '%': case '&': case '\'':
- case '(': case ')': case '*': case '+':
- case ',': case '-': case '.': case '/':
- case '0': case '1': case '2': case '3':
- case '4': case '5': case '6': case '7':
- case '8': case '9': case ':': case ';':
- case '<': case '=': case '>': case '?':
- case '@': case 'A': case 'B': case 'C':
- case 'D': case 'E': case 'F': case 'G':
- case 'H': case 'I': case 'J': case 'K':
- case 'L': case 'M': case 'N': case 'O':
- case 'P': case 'Q': case 'R': case 'S':
- case 'T': case 'U': case 'V': case 'W':
- case 'X': case 'Y': case 'Z': case '[':
- case ']': case '^': case '_': case '`':
- case 'a': case 'b': case 'c': case 'd':
- case 'e': case 'f': case 'g': case 'h':
- case 'i': case 'j': case 'k': case 'l':
- case 'm': case 'n': case 'o': case 'p':
- case 'q': case 'r': case 's': case 't':
- case 'u': case 'v': case 'w': case 'x':
- case 'y': case 'z': case '{': case '|':
- case '}': case '~':
- {
- matchNot('"');
- break;
- }
- default:
- {
- break _loop23;
- }
- }
- } while (true);
- }
- match('"');
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
- protected final void mVOCAB(boolean _createToken) throws RecognitionException, CharStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = VOCAB;
- int _saveIndex;
-
- matchRange('\3','\176');
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
- public final void mWS(boolean _createToken) throws RecognitionException, CharStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = WS;
- int _saveIndex;
-
- {
- switch ( LA(1)) {
- case ' ':
- {
- match(' ');
- break;
- }
- case '\t':
- {
- match('\t');
- break;
- }
- case '\r':
- {
- match('\r');
- {
- if ((LA(1)=='\n')) {
- match('\n');
- }
- else {
- }
-
- }
- newline();
- break;
- }
- case '\n':
- {
- match('\n');
- newline();
- break;
- }
- default:
- {
- throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());
- }
- }
- }
- _ttype = Token.SKIP;
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
- protected final void mXDIGIT(boolean _createToken) throws RecognitionException, CharStreamException {
- int _ttype; Token _token=null; int _begin=text.length();
- _ttype = XDIGIT;
- int _saveIndex;
-
- switch ( LA(1)) {
- case '0': case '1': case '2': case '3':
- case '4': case '5': case '6': case '7':
- case '8': case '9':
- {
- matchRange('0','9');
- break;
- }
- case 'a': case 'b': case 'c': case 'd':
- case 'e': case 'f':
- {
- matchRange('a','f');
- break;
- }
- case 'A': case 'B': case 'C': case 'D':
- case 'E': case 'F':
- {
- matchRange('A','F');
- break;
- }
- default:
- {
- throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());
- }
- }
- if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
- _token = makeToken(_ttype);
- _token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
- }
- _returnToken = _token;
- }
-public Token nextToken() throws TokenStreamException {
- Token _rettoken=null;
-tryAgain:
- for (;;) {
- Token _token = null;
- int _ttype = Token.INVALID_TYPE;
- resetText();
- try {
- try { // for error handling
- switch ( LA(1)) {
- case '\t': case '\n': case '\r': case ' ':
- {
- mWS(true);
- _rettoken=_returnToken;
- break;
- }
- case '(':
- {
- mLPAREN(true);
- _rettoken=_returnToken;
- break;
- }
- case ')':
- {
- mRPAREN(true);
- _rettoken=_returnToken;
- break;
- }
- case '=':
- {
- mASSIGN(true);
- _rettoken=_returnToken;
- break;
- }
- case '"':
- {
- mSTRING(true);
- _rettoken=_returnToken;
- break;
- }
- case 'A': case 'B': case 'C': case 'D':
- case 'E': case 'F': case 'G': case 'H':
- case 'I': case 'J': case 'K': case 'L':
- case 'M': case 'N': case 'O': case 'P':
- case 'Q': case 'R': case 'S': case 'T':
- case 'U': case 'V': case 'W': case 'X':
- case 'Y': case 'Z': case 'a': case 'b':
- case 'c': case 'd': case 'e': case 'f':
- case 'g': case 'h': case 'i': case 'j':
- case 'k': case 'l': case 'm': case 'n':
- case 'o': case 'p': case 'q': case 'r':
- case 's': case 't': case 'u': case 'v':
- case 'w': case 'x': case 'y': case 'z':
- {
- mID(true);
- _rettoken=_returnToken;
- break;
- }
- case '0': case '1': case '2': case '3':
- case '4': case '5': case '6': case '7':
- case '8': case '9':
- {
- mINT(true);
- _rettoken=_returnToken;
- break;
- }
- default:
- if ((LA(1)=='/') && (LA(2)=='/')) {
- mSL_COMMENT(true);
- _rettoken=_returnToken;
- }
- else if ((LA(1)=='/') && (LA(2)=='*')) {
- mML_COMMENT(true);
- _rettoken=_returnToken;
- }
- else {
- if (LA(1)==EOF_CHAR) {_returnToken = makeToken(Token.EOF_TYPE);}
- else {throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());}
- }
- }
- if ( _returnToken==null ) continue tryAgain; // found SKIP token
- _ttype = _returnToken.getType();
- _returnToken.setType(_ttype);
- return _returnToken;
- }
- catch (RecognitionException e) {
- reportError(e);
- consume();
- }
- }
-
-
- catch (CharStreamException cse) {
- if ( cse instanceof CharStreamIOException ) {
- throw new TokenStreamIOException(((CharStreamIOException)cse).io);
- }
- else {
- throw new TokenStreamException(cse.getMessage());
- }
- }
+
+public class ANTLRTokdefLexer extends antlr.CharScanner implements ANTLRTokdefParserTokenTypes, TokenStream {
+
+ private static final long _tokenSet_0_data_[] = { -9224L, 9223372036854775807L, 0L, 0L };
+
+ public static final BitSet _tokenSet_0 = new BitSet(_tokenSet_0_data_);
+
+ private static final long _tokenSet_1_data_[] = { -140737488355336L, 9223372036854775807L, 0L, 0L };
+
+ public static final BitSet _tokenSet_1 = new BitSet(_tokenSet_1_data_);
+
+
+ public ANTLRTokdefLexer(InputBuffer ib) {
+ this(new LexerSharedInputState(ib));
+ }
+
+ public ANTLRTokdefLexer(LexerSharedInputState state) {
+ super(state);
+ literals = new Hashtable();
+ caseSensitiveLiterals = true;
+ setCaseSensitive(true);
+ }
+
+ public ANTLRTokdefLexer(InputStream in) {
+ this(new ByteBuffer(in));
+ }
+
+ public ANTLRTokdefLexer(Reader in) {
+ this(new CharBuffer(in));
+ }
+
+ public final void mASSIGN(boolean _createToken) throws RecognitionException, CharStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = ASSIGN;
+ int _saveIndex;
+
+ match('=');
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ protected final void mDIGIT(boolean _createToken) throws RecognitionException, CharStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = DIGIT;
+ int _saveIndex;
+
+ matchRange('0', '9');
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ protected final void mESC(boolean _createToken) throws RecognitionException, CharStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = ESC;
+ int _saveIndex;
+
+ match('\\');
+ {
+ switch (LA(1)) {
+ case 'n':
+ {
+ match('n');
+ break;
+ }
+ case 'r':
+ {
+ match('r');
+ break;
+ }
+ case 't':
+ {
+ match('t');
+ break;
+ }
+ case 'b':
+ {
+ match('b');
+ break;
+ }
+ case 'f':
+ {
+ match('f');
+ break;
+ }
+ case '"':
+ {
+ match('"');
+ break;
+ }
+ case '\'':
+ {
+ match('\'');
+ break;
+ }
+ case '\\':
+ {
+ match('\\');
+ break;
+ }
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ {
+ {
+ matchRange('0', '3');
+ }
+ {
+ if (((LA(1) >= '0' && LA(1) <= '9')) && ((LA(2) >= '\3' && LA(2) <= '~'))) {
+ mDIGIT(false);
+ {
+ if (((LA(1) >= '0' && LA(1) <= '9')) && ((LA(2) >= '\3' && LA(2) <= '~'))) {
+ mDIGIT(false);
+ } else if (((LA(1) >= '\3' && LA(1) <= '~')) && (true)) {
+ } else {
+ throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());
+ }
+
+ }
+ } else if (((LA(1) >= '\3' && LA(1) <= '~')) && (true)) {
+ } else {
+ throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());
+ }
+
+ }
+ break;
+ }
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ {
+ {
+ matchRange('4', '7');
+ }
+ {
+ if (((LA(1) >= '0' && LA(1) <= '9')) && ((LA(2) >= '\3' && LA(2) <= '~'))) {
+ mDIGIT(false);
+ } else if (((LA(1) >= '\3' && LA(1) <= '~')) && (true)) {
+ } else {
+ throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());
+ }
+
+ }
+ break;
+ }
+ case 'u':
+ {
+ match('u');
+ mXDIGIT(false);
+ mXDIGIT(false);
+ mXDIGIT(false);
+ mXDIGIT(false);
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());
+ }
+ }
+ }
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ public final void mID(boolean _createToken) throws RecognitionException, CharStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = ID;
+ int _saveIndex;
+
+ {
+ switch (LA(1)) {
+ case 'a':
+ case 'b':
+ case 'c':
+ case 'd':
+ case 'e':
+ case 'f':
+ case 'g':
+ case 'h':
+ case 'i':
+ case 'j':
+ case 'k':
+ case 'l':
+ case 'm':
+ case 'n':
+ case 'o':
+ case 'p':
+ case 'q':
+ case 'r':
+ case 's':
+ case 't':
+ case 'u':
+ case 'v':
+ case 'w':
+ case 'x':
+ case 'y':
+ case 'z':
+ {
+ matchRange('a', 'z');
+ break;
+ }
+ case 'A':
+ case 'B':
+ case 'C':
+ case 'D':
+ case 'E':
+ case 'F':
+ case 'G':
+ case 'H':
+ case 'I':
+ case 'J':
+ case 'K':
+ case 'L':
+ case 'M':
+ case 'N':
+ case 'O':
+ case 'P':
+ case 'Q':
+ case 'R':
+ case 'S':
+ case 'T':
+ case 'U':
+ case 'V':
+ case 'W':
+ case 'X':
+ case 'Y':
+ case 'Z':
+ {
+ matchRange('A', 'Z');
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());
+ }
+ }
+ }
+ {
+ _loop37:
+ do {
+ switch (LA(1)) {
+ case 'a':
+ case 'b':
+ case 'c':
+ case 'd':
+ case 'e':
+ case 'f':
+ case 'g':
+ case 'h':
+ case 'i':
+ case 'j':
+ case 'k':
+ case 'l':
+ case 'm':
+ case 'n':
+ case 'o':
+ case 'p':
+ case 'q':
+ case 'r':
+ case 's':
+ case 't':
+ case 'u':
+ case 'v':
+ case 'w':
+ case 'x':
+ case 'y':
+ case 'z':
+ {
+ matchRange('a', 'z');
+ break;
+ }
+ case 'A':
+ case 'B':
+ case 'C':
+ case 'D':
+ case 'E':
+ case 'F':
+ case 'G':
+ case 'H':
+ case 'I':
+ case 'J':
+ case 'K':
+ case 'L':
+ case 'M':
+ case 'N':
+ case 'O':
+ case 'P':
+ case 'Q':
+ case 'R':
+ case 'S':
+ case 'T':
+ case 'U':
+ case 'V':
+ case 'W':
+ case 'X':
+ case 'Y':
+ case 'Z':
+ {
+ matchRange('A', 'Z');
+ break;
+ }
+ case '_':
+ {
+ match('_');
+ break;
+ }
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ case '9':
+ {
+ matchRange('0', '9');
+ break;
+ }
+ default:
+ {
+ break _loop37;
+ }
+ }
+ } while (true);
+ }
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ public final void mINT(boolean _createToken) throws RecognitionException, CharStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = INT;
+ int _saveIndex;
+
+ {
+ int _cnt40 = 0;
+ _loop40:
+ do {
+ if (((LA(1) >= '0' && LA(1) <= '9'))) {
+ mDIGIT(false);
+ } else {
+ if (_cnt40 >= 1) {
+ break _loop40;
+ } else {
+ throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());
+ }
+ }
+
+ _cnt40++;
+ } while (true);
+ }
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ public final void mLPAREN(boolean _createToken) throws RecognitionException, CharStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = LPAREN;
+ int _saveIndex;
+
+ match('(');
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ public final void mML_COMMENT(boolean _createToken) throws RecognitionException, CharStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = ML_COMMENT;
+ int _saveIndex;
+
+ match("/*");
+ {
+ _loop17:
+ do {
+ switch (LA(1)) {
+ case '\n':
+ {
+ match('\n');
+ newline();
+ break;
+ }
+ case '\3':
+ case '\4':
+ case '\5':
+ case '\6':
+ case '\7':
+ case '\10':
+ case '\t':
+ case '\13':
+ case '\14':
+ case '\r':
+ case '\16':
+ case '\17':
+ case '\20':
+ case '\21':
+ case '\22':
+ case '\23':
+ case '\24':
+ case '\25':
+ case '\26':
+ case '\27':
+ case '\30':
+ case '\31':
+ case '\32':
+ case '\33':
+ case '\34':
+ case '\35':
+ case '\36':
+ case '\37':
+ case ' ':
+ case '!':
+ case '"':
+ case '#':
+ case '$':
+ case '%':
+ case '&':
+ case '\'':
+ case '(':
+ case ')':
+ case '+':
+ case ',':
+ case '-':
+ case '.':
+ case '/':
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ case '9':
+ case ':':
+ case ';':
+ case '<':
+ case '=':
+ case '>':
+ case '?':
+ case '@':
+ case 'A':
+ case 'B':
+ case 'C':
+ case 'D':
+ case 'E':
+ case 'F':
+ case 'G':
+ case 'H':
+ case 'I':
+ case 'J':
+ case 'K':
+ case 'L':
+ case 'M':
+ case 'N':
+ case 'O':
+ case 'P':
+ case 'Q':
+ case 'R':
+ case 'S':
+ case 'T':
+ case 'U':
+ case 'V':
+ case 'W':
+ case 'X':
+ case 'Y':
+ case 'Z':
+ case '[':
+ case '\\':
+ case ']':
+ case '^':
+ case '_':
+ case '`':
+ case 'a':
+ case 'b':
+ case 'c':
+ case 'd':
+ case 'e':
+ case 'f':
+ case 'g':
+ case 'h':
+ case 'i':
+ case 'j':
+ case 'k':
+ case 'l':
+ case 'm':
+ case 'n':
+ case 'o':
+ case 'p':
+ case 'q':
+ case 'r':
+ case 's':
+ case 't':
+ case 'u':
+ case 'v':
+ case 'w':
+ case 'x':
+ case 'y':
+ case 'z':
+ case '{':
+ case '|':
+ case '}':
+ case '~':
+ {
+ matchNot('*');
+ break;
+ }
+ default:
+ if ((LA(1) == '*') && (_tokenSet_1.member(LA(2)))) {
+ match('*');
+ matchNot('/');
+ } else {
+ break _loop17;
+ }
+ }
+ } while (true);
+ }
+ match("*/");
+ _ttype = Token.SKIP;
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ public final void mRPAREN(boolean _createToken) throws RecognitionException, CharStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = RPAREN;
+ int _saveIndex;
+
+ match(')');
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ public final void mSL_COMMENT(boolean _createToken) throws RecognitionException, CharStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = SL_COMMENT;
+ int _saveIndex;
+
+ match("//");
+ {
+ _loop12:
+ do {
+ if ((_tokenSet_0.member(LA(1)))) {
+ {
+ match(_tokenSet_0);
+ }
+ } else {
+ break _loop12;
+ }
+
+ } while (true);
+ }
+ {
+ switch (LA(1)) {
+ case '\n':
+ {
+ match('\n');
+ break;
+ }
+ case '\r':
+ {
+ match('\r');
+ {
+ if ((LA(1) == '\n')) {
+ match('\n');
+ } else {
+ }
+
+ }
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());
+ }
+ }
+ }
+ _ttype = Token.SKIP;
+ newline();
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ public final void mSTRING(boolean _createToken) throws RecognitionException, CharStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = STRING;
+ int _saveIndex;
+
+ match('"');
+ {
+ _loop23:
+ do {
+ switch (LA(1)) {
+ case '\\':
+ {
+ mESC(false);
+ break;
+ }
+ case '\3':
+ case '\4':
+ case '\5':
+ case '\6':
+ case '\7':
+ case '\10':
+ case '\t':
+ case '\n':
+ case '\13':
+ case '\14':
+ case '\r':
+ case '\16':
+ case '\17':
+ case '\20':
+ case '\21':
+ case '\22':
+ case '\23':
+ case '\24':
+ case '\25':
+ case '\26':
+ case '\27':
+ case '\30':
+ case '\31':
+ case '\32':
+ case '\33':
+ case '\34':
+ case '\35':
+ case '\36':
+ case '\37':
+ case ' ':
+ case '!':
+ case '#':
+ case '$':
+ case '%':
+ case '&':
+ case '\'':
+ case '(':
+ case ')':
+ case '*':
+ case '+':
+ case ',':
+ case '-':
+ case '.':
+ case '/':
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ case '9':
+ case ':':
+ case ';':
+ case '<':
+ case '=':
+ case '>':
+ case '?':
+ case '@':
+ case 'A':
+ case 'B':
+ case 'C':
+ case 'D':
+ case 'E':
+ case 'F':
+ case 'G':
+ case 'H':
+ case 'I':
+ case 'J':
+ case 'K':
+ case 'L':
+ case 'M':
+ case 'N':
+ case 'O':
+ case 'P':
+ case 'Q':
+ case 'R':
+ case 'S':
+ case 'T':
+ case 'U':
+ case 'V':
+ case 'W':
+ case 'X':
+ case 'Y':
+ case 'Z':
+ case '[':
+ case ']':
+ case '^':
+ case '_':
+ case '`':
+ case 'a':
+ case 'b':
+ case 'c':
+ case 'd':
+ case 'e':
+ case 'f':
+ case 'g':
+ case 'h':
+ case 'i':
+ case 'j':
+ case 'k':
+ case 'l':
+ case 'm':
+ case 'n':
+ case 'o':
+ case 'p':
+ case 'q':
+ case 'r':
+ case 's':
+ case 't':
+ case 'u':
+ case 'v':
+ case 'w':
+ case 'x':
+ case 'y':
+ case 'z':
+ case '{':
+ case '|':
+ case '}':
+ case '~':
+ {
+ matchNot('"');
+ break;
+ }
+ default:
+ {
+ break _loop23;
+ }
+ }
+ } while (true);
+ }
+ match('"');
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ protected final void mVOCAB(boolean _createToken) throws RecognitionException, CharStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = VOCAB;
+ int _saveIndex;
+
+ matchRange('\3', '\176');
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ public final void mWS(boolean _createToken) throws RecognitionException, CharStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = WS;
+ int _saveIndex;
+
+ {
+ switch (LA(1)) {
+ case ' ':
+ {
+ match(' ');
+ break;
+ }
+ case '\t':
+ {
+ match('\t');
+ break;
+ }
+ case '\r':
+ {
+ match('\r');
+ {
+ if ((LA(1) == '\n')) {
+ match('\n');
+ } else {
+ }
+
+ }
+ newline();
+ break;
+ }
+ case '\n':
+ {
+ match('\n');
+ newline();
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());
+ }
+ }
+ }
+ _ttype = Token.SKIP;
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ protected final void mXDIGIT(boolean _createToken) throws RecognitionException, CharStreamException {
+ int _ttype;
+ Token _token = null;
+ int _begin = text.length();
+ _ttype = XDIGIT;
+ int _saveIndex;
+
+ switch (LA(1)) {
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ case '9':
+ {
+ matchRange('0', '9');
+ break;
+ }
+ case 'a':
+ case 'b':
+ case 'c':
+ case 'd':
+ case 'e':
+ case 'f':
+ {
+ matchRange('a', 'f');
+ break;
+ }
+ case 'A':
+ case 'B':
+ case 'C':
+ case 'D':
+ case 'E':
+ case 'F':
+ {
+ matchRange('A', 'F');
+ break;
+ }
+ default:
+ {
+ throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());
+ }
+ }
+ if (_createToken && _token == null && _ttype != Token.SKIP) {
+ _token = makeToken(_ttype);
+ _token.setText(new String(text.getBuffer(), _begin, text.length() - _begin));
+ }
+ _returnToken = _token;
+ }
+
+ public Token nextToken() throws TokenStreamException {
+ Token _rettoken = null;
+ tryAgain:
+ for (; ;) {
+ Token _token = null;
+ int _ttype = Token.INVALID_TYPE;
+ resetText();
+ try {
+ try {
+ // for error handling
+ switch (LA(1)) {
+ case '\t':
+ case '\n':
+ case '\r':
+ case ' ':
+ {
+ mWS(true);
+ _rettoken = _returnToken;
+ break;
+ }
+ case '(':
+ {
+ mLPAREN(true);
+ _rettoken = _returnToken;
+ break;
+ }
+ case ')':
+ {
+ mRPAREN(true);
+ _rettoken = _returnToken;
+ break;
+ }
+ case '=':
+ {
+ mASSIGN(true);
+ _rettoken = _returnToken;
+ break;
+ }
+ case '"':
+ {
+ mSTRING(true);
+ _rettoken = _returnToken;
+ break;
+ }
+ case 'A':
+ case 'B':
+ case 'C':
+ case 'D':
+ case 'E':
+ case 'F':
+ case 'G':
+ case 'H':
+ case 'I':
+ case 'J':
+ case 'K':
+ case 'L':
+ case 'M':
+ case 'N':
+ case 'O':
+ case 'P':
+ case 'Q':
+ case 'R':
+ case 'S':
+ case 'T':
+ case 'U':
+ case 'V':
+ case 'W':
+ case 'X':
+ case 'Y':
+ case 'Z':
+ case 'a':
+ case 'b':
+ case 'c':
+ case 'd':
+ case 'e':
+ case 'f':
+ case 'g':
+ case 'h':
+ case 'i':
+ case 'j':
+ case 'k':
+ case 'l':
+ case 'm':
+ case 'n':
+ case 'o':
+ case 'p':
+ case 'q':
+ case 'r':
+ case 's':
+ case 't':
+ case 'u':
+ case 'v':
+ case 'w':
+ case 'x':
+ case 'y':
+ case 'z':
+ {
+ mID(true);
+ _rettoken = _returnToken;
+ break;
+ }
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ case '9':
+ {
+ mINT(true);
+ _rettoken = _returnToken;
+ break;
+ }
+ default:
+ if ((LA(1) == '/') && (LA(2) == '/')) {
+ mSL_COMMENT(true);
+ _rettoken = _returnToken;
+ } else if ((LA(1) == '/') && (LA(2) == '*')) {
+ mML_COMMENT(true);
+ _rettoken = _returnToken;
+ } else {
+ if (LA(1) == EOF_CHAR) {
+ _returnToken = makeToken(Token.EOF_TYPE);
+ } else {
+ throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());
+ }
+ }
+ }
+ if (_returnToken == null) continue tryAgain; // found SKIP token
+ _ttype = _returnToken.getType();
+ _returnToken.setType(_ttype);
+ return _returnToken;
+ } catch (RecognitionException e) {
+ reportError(e);
+ consume();
+ }
+ } catch (CharStreamException cse) {
+ if (cse instanceof CharStreamIOException) {
+ throw new TokenStreamIOException(((CharStreamIOException)cse).io);
+ } else {
+ throw new TokenStreamException(cse.getMessage());
+ }
+ }
- }
-}
+ }
+ }
}
1.2 +163 -167 e/src/jsrc/antlr/ANTLRTokdefParser.java
Index: ANTLRTokdefParser.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/ANTLRTokdefParser.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ANTLRTokdefParser.java 2001/10/03 21:04:13 1.1
+++ ANTLRTokdefParser.java 2001/10/04 01:09:13 1.2
@@ -2,177 +2,173 @@
/*
* ANTLR-generated file resulting from grammar tokdef.g
- *
+ *
* Terence Parr, MageLang Institute
* ANTLR Version 2.7.0a2; 1989-1999
*/
-import antlr.TokenStreamException;
-import antlr.TokenBuffer;
-import antlr.LLkParser;
-import antlr.Token;
-import antlr.TokenStream;
-import antlr.RecognitionException;
-import antlr.NoViableAltException;
-import antlr.MismatchedTokenException;
-import antlr.SemanticException;
-import antlr.ParserSharedInputState;
+
import antlr.collections.impl.BitSet;
+
public class ANTLRTokdefParser extends antlr.LLkParser
- implements ANTLRTokdefParserTokenTypes
- {
+ implements ANTLRTokdefParserTokenTypes {
+
+ public static final String[] _tokenNames = {
+ "<0>",
+ "EOF",
+ "<2>",
+ "NULL_TREE_LOOKAHEAD",
+ "ID",
+ "STRING",
+ "ASSIGN",
+ "LPAREN",
+ "RPAREN",
+ "INT",
+ "WS",
+ "SL_COMMENT",
+ "ML_COMMENT",
+ "ESC",
+ "DIGIT",
+ "XDIGIT",
+ "VOCAB"
+ };
+
+ private static final long _tokenSet_0_data_[] = { 2L, 0L };
+
+ public static final BitSet _tokenSet_0 = new BitSet(_tokenSet_0_data_);
+
+ private static final long _tokenSet_1_data_[] = { 50L, 0L };
+
+ public static final BitSet _tokenSet_1 = new BitSet(_tokenSet_1_data_);
+
+
+ public ANTLRTokdefParser(ParserSharedInputState state) {
+ super(state, 3);
+ tokenNames = _tokenNames;
+ }
+
+ public ANTLRTokdefParser(TokenBuffer tokenBuf) {
+ this(tokenBuf, 3);
+ }
+
+ protected ANTLRTokdefParser(TokenBuffer tokenBuf, int k) {
+ super(tokenBuf, k);
+ tokenNames = _tokenNames;
+ }
+
+ public ANTLRTokdefParser(TokenStream lexer) {
+ this(lexer, 3);
+ }
+
+ protected ANTLRTokdefParser(TokenStream lexer, int k) {
+ super(lexer, k);
+ tokenNames = _tokenNames;
+ }
+
+ public final void file(
+ ImportVocabTokenManager tm
+ ) throws RecognitionException, TokenStreamException {
+
+ Token name = null;
+
+ try {
+ // for error handling
+ name = LT(1);
+ match(ID);
+ {
+ _loop3:
+ do {
+ if ((LA(1) == ID || LA(1) == STRING)) {
+ line(tm);
+ } else {
+ break _loop3;
+ }
+
+ } while (true);
+ }
+ } catch (RecognitionException ex) {
+ reportError(ex);
+ consume();
+ consumeUntil(_tokenSet_0);
+ }
+ }
+
+ public final void line(
+ ImportVocabTokenManager tm
+ ) throws RecognitionException, TokenStreamException {
+
+ Token s1 = null;
+ Token lab = null;
+ Token s2 = null;
+ Token id = null;
+ Token para = null;
+ Token id2 = null;
+ Token i = null;
+ Token t = null;
+ Token s = null;
+
+ try {
+ // for error handling
+ {
+ if ((LA(1) == STRING)) {
+ s1 = LT(1);
+ match(STRING);
+ s = s1;
+ } else if ((LA(1) == ID) && (LA(2) == ASSIGN) && (LA(3) == STRING)) {
+ lab = LT(1);
+ match(ID);
+ t = lab;
+ match(ASSIGN);
+ s2 = LT(1);
+ match(STRING);
+ s = s2;
+ } else if ((LA(1) == ID) && (LA(2) == LPAREN)) {
+ id = LT(1);
+ match(ID);
+ t = id;
+ match(LPAREN);
+ para = LT(1);
+ match(STRING);
+ match(RPAREN);
+ } else if ((LA(1) == ID) && (LA(2) == ASSIGN) && (LA(3) == INT)) {
+ id2 = LT(1);
+ match(ID);
+ t = id2;
+ } else {
+ throw new NoViableAltException(LT(1), getFilename());
+ }
+
+ }
+ match(ASSIGN);
+ i = LT(1);
+ match(INT);
+
+ Integer value = Integer.valueOf(i.getText());
+ // if literal found, define as a string literal
+ if (s != null) {
+ tm.define(s.getText(), value.intValue());
+ // if label, then label the string and map label to token symbol also
+ if (t != null) {
+ StringLiteralSymbol sl =
+ (StringLiteralSymbol)tm.getTokenSymbol(s.getText());
+ sl.setLabel(t.getText());
+ tm.mapToTokenSymbol(t.getText(), sl);
+ }
+ }
+ // define token (not a literal)
+ else if (t != null) {
+ tm.define(t.getText(), value.intValue());
+ if (para != null) {
+ TokenSymbol ts = tm.getTokenSymbol(t.getText());
+ ts.setParaphrase(
+ para.getText()
+ );
+ }
+ }
- public static final String[] _tokenNames = {
- "<0>",
- "EOF",
- "<2>",
- "NULL_TREE_LOOKAHEAD",
- "ID",
- "STRING",
- "ASSIGN",
- "LPAREN",
- "RPAREN",
- "INT",
- "WS",
- "SL_COMMENT",
- "ML_COMMENT",
- "ESC",
- "DIGIT",
- "XDIGIT",
- "VOCAB"
- };
-
- private static final long _tokenSet_0_data_[] = { 2L, 0L };
- public static final BitSet _tokenSet_0 = new BitSet(_tokenSet_0_data_);
- private static final long _tokenSet_1_data_[] = { 50L, 0L };
- public static final BitSet _tokenSet_1 = new BitSet(_tokenSet_1_data_);
-
-
-public ANTLRTokdefParser(ParserSharedInputState state) {
- super(state,3);
- tokenNames = _tokenNames;
-}
-public ANTLRTokdefParser(TokenBuffer tokenBuf) {
- this(tokenBuf,3);
-}
-protected ANTLRTokdefParser(TokenBuffer tokenBuf, int k) {
- super(tokenBuf,k);
- tokenNames = _tokenNames;
-}
-public ANTLRTokdefParser(TokenStream lexer) {
- this(lexer,3);
-}
-protected ANTLRTokdefParser(TokenStream lexer, int k) {
- super(lexer,k);
- tokenNames = _tokenNames;
-}
- public final void file(
- ImportVocabTokenManager tm
- ) throws RecognitionException, TokenStreamException {
-
- Token name = null;
-
- try { // for error handling
- name = LT(1);
- match(ID);
- {
- _loop3:
- do {
- if ((LA(1)==ID||LA(1)==STRING)) {
- line(tm);
- }
- else {
- break _loop3;
- }
-
- } while (true);
- }
- }
- catch (RecognitionException ex) {
- reportError(ex);
- consume();
- consumeUntil(_tokenSet_0);
- }
- }
- public final void line(
- ImportVocabTokenManager tm
- ) throws RecognitionException, TokenStreamException {
-
- Token s1 = null;
- Token lab = null;
- Token s2 = null;
- Token id = null;
- Token para = null;
- Token id2 = null;
- Token i = null;
- Token t=null; Token s=null;
-
- try { // for error handling
- {
- if ((LA(1)==STRING)) {
- s1 = LT(1);
- match(STRING);
- s = s1;
- }
- else if ((LA(1)==ID) && (LA(2)==ASSIGN) && (LA(3)==STRING)) {
- lab = LT(1);
- match(ID);
- t = lab;
- match(ASSIGN);
- s2 = LT(1);
- match(STRING);
- s = s2;
- }
- else if ((LA(1)==ID) && (LA(2)==LPAREN)) {
- id = LT(1);
- match(ID);
- t=id;
- match(LPAREN);
- para = LT(1);
- match(STRING);
- match(RPAREN);
- }
- else if ((LA(1)==ID) && (LA(2)==ASSIGN) && (LA(3)==INT)) {
- id2 = LT(1);
- match(ID);
- t=id2;
- }
- else {
- throw new NoViableAltException(LT(1), getFilename());
- }
-
- }
- match(ASSIGN);
- i = LT(1);
- match(INT);
-
- Integer value = Integer.valueOf(i.getText());
- // if literal found, define as a string literal
- if ( s!=null ) {
- tm.define(s.getText(), value.intValue());
- // if label, then label the string and map label to token symbol also
- if ( t!=null ) {
- StringLiteralSymbol sl =
- (StringLiteralSymbol) tm.getTokenSymbol(s.getText());
- sl.setLabel(t.getText());
- tm.mapToTokenSymbol(t.getText(), sl);
- }
- }
- // define token (not a literal)
- else if ( t!=null ) {
- tm.define(t.getText(), value.intValue());
- if ( para!=null ) {
- TokenSymbol ts = tm.getTokenSymbol(t.getText());
- ts.setParaphrase(
- para.getText()
- );
- }
- }
-
- }
- catch (RecognitionException ex) {
- reportError(ex);
- consume();
- consumeUntil(_tokenSet_1);
- }
- }
+ } catch (RecognitionException ex) {
+ reportError(ex);
+ consume();
+ consumeUntil(_tokenSet_1);
+ }
+ }
}
1.2 +32 -17 e/src/jsrc/antlr/ANTLRTokdefParserTokenTypes.java
Index: ANTLRTokdefParserTokenTypes.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/ANTLRTokdefParserTokenTypes.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ANTLRTokdefParserTokenTypes.java 2001/10/03 21:04:13 1.1
+++ ANTLRTokdefParserTokenTypes.java 2001/10/04 01:09:13 1.2
@@ -2,24 +2,39 @@
/*
* ANTLR-generated file resulting from grammar tokdef.g
- *
+ *
* Terence Parr, MageLang Institute
* ANTLR Version 2.7.0a2; 1989-1999
*/
- public interface ANTLRTokdefParserTokenTypes {
- int EOF = 1;
- int NULL_TREE_LOOKAHEAD = 3;
- int ID = 4;
- int STRING = 5;
- int ASSIGN = 6;
- int LPAREN = 7;
- int RPAREN = 8;
- int INT = 9;
- int WS = 10;
- int SL_COMMENT = 11;
- int ML_COMMENT = 12;
- int ESC = 13;
- int DIGIT = 14;
- int XDIGIT = 15;
- int VOCAB = 16;
+public interface ANTLRTokdefParserTokenTypes {
+
+ int EOF = 1;
+
+ int NULL_TREE_LOOKAHEAD = 3;
+
+ int ID = 4;
+
+ int STRING = 5;
+
+ int ASSIGN = 6;
+
+ int LPAREN = 7;
+
+ int RPAREN = 8;
+
+ int INT = 9;
+
+ int WS = 10;
+
+ int SL_COMMENT = 11;
+
+ int ML_COMMENT = 12;
+
+ int ESC = 13;
+
+ int DIGIT = 14;
+
+ int XDIGIT = 15;
+
+ int VOCAB = 16;
}
1.2 +128 -64 e/src/jsrc/antlr/ANTLRTokenTypes.java
Index: ANTLRTokenTypes.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/ANTLRTokenTypes.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ANTLRTokenTypes.java 2001/10/03 21:04:13 1.1
+++ ANTLRTokenTypes.java 2001/10/04 01:09:13 1.2
@@ -3,68 +3,132 @@
package antlr;
public interface ANTLRTokenTypes {
- int EOF = 1;
- int NULL_TREE_LOOKAHEAD = 3;
- int LITERAL_tokens = 4;
- int LITERAL_header = 5;
- int STRING_LITERAL = 6;
- int ACTION = 7;
- int DOC_COMMENT = 8;
- int LITERAL_lexclass = 9;
- int LITERAL_class = 10;
- int LITERAL_extends = 11;
- int LITERAL_Lexer = 12;
- int LITERAL_TreeParser = 13;
- int OPTIONS = 14;
- int ASSIGN = 15;
- int SEMI = 16;
- int RCURLY = 17;
- int LITERAL_charVocabulary = 18;
- int CHAR_LITERAL = 19;
- int INT = 20;
- int OR = 21;
- int RANGE = 22;
- int TOKENS = 23;
- int TOKEN_REF = 24;
- int OPEN_ELEMENT_OPTION = 25;
- int CLOSE_ELEMENT_OPTION = 26;
- int LPAREN = 27;
- int RULE_REF = 28;
- int RPAREN = 29;
- int LITERAL_Parser = 30;
- int LITERAL_protected = 31;
- int LITERAL_public = 32;
- int LITERAL_private = 33;
- int BANG = 34;
- int ARG_ACTION = 35;
- int LITERAL_returns = 36;
- int COLON = 37;
- int LITERAL_throws = 38;
- int COMMA = 39;
- int LITERAL_exception = 40;
- int LITERAL_catch = 41;
- int NOT_OP = 42;
- int SEMPRED = 43;
- int TREE_BEGIN = 44;
- int QUESTION = 45;
- int STAR = 46;
- int PLUS = 47;
- int IMPLIES = 48;
- int CARET = 49;
- int WILDCARD = 50;
- int LITERAL_options = 51;
- int WS = 52;
- int COMMENT = 53;
- int SL_COMMENT = 54;
- int ML_COMMENT = 55;
- int ESC = 56;
- int DIGIT = 57;
- int XDIGIT = 58;
- int VOCAB = 59;
- int NESTED_ARG_ACTION = 60;
- int NESTED_ACTION = 61;
- int WS_LOOP = 62;
- int INTERNAL_RULE_REF = 63;
- int WS_OPT = 64;
- int NOT_USEFUL = 65;
+
+ int EOF = 1;
+
+ int NULL_TREE_LOOKAHEAD = 3;
+
+ int LITERAL_tokens = 4;
+
+ int LITERAL_header = 5;
+
+ int STRING_LITERAL = 6;
+
+ int ACTION = 7;
+
+ int DOC_COMMENT = 8;
+
+ int LITERAL_lexclass = 9;
+
+ int LITERAL_class = 10;
+
+ int LITERAL_extends = 11;
+
+ int LITERAL_Lexer = 12;
+
+ int LITERAL_TreeParser = 13;
+
+ int OPTIONS = 14;
+
+ int ASSIGN = 15;
+
+ int SEMI = 16;
+
+ int RCURLY = 17;
+
+ int LITERAL_charVocabulary = 18;
+
+ int CHAR_LITERAL = 19;
+
+ int INT = 20;
+
+ int OR = 21;
+
+ int RANGE = 22;
+
+ int TOKENS = 23;
+
+ int TOKEN_REF = 24;
+
+ int OPEN_ELEMENT_OPTION = 25;
+
+ int CLOSE_ELEMENT_OPTION = 26;
+
+ int LPAREN = 27;
+
+ int RULE_REF = 28;
+
+ int RPAREN = 29;
+
+ int LITERAL_Parser = 30;
+
+ int LITERAL_protected = 31;
+
+ int LITERAL_public = 32;
+
+ int LITERAL_private = 33;
+
+ int BANG = 34;
+
+ int ARG_ACTION = 35;
+
+ int LITERAL_returns = 36;
+
+ int COLON = 37;
+
+ int LITERAL_throws = 38;
+
+ int COMMA = 39;
+
+ int LITERAL_exception = 40;
+
+ int LITERAL_catch = 41;
+
+ int NOT_OP = 42;
+
+ int SEMPRED = 43;
+
+ int TREE_BEGIN = 44;
+
+ int QUESTION = 45;
+
+ int STAR = 46;
+
+ int PLUS = 47;
+
+ int IMPLIES = 48;
+
+ int CARET = 49;
+
+ int WILDCARD = 50;
+
+ int LITERAL_options = 51;
+
+ int WS = 52;
+
+ int COMMENT = 53;
+
+ int SL_COMMENT = 54;
+
+ int ML_COMMENT = 55;
+
+ int ESC = 56;
+
+ int DIGIT = 57;
+
+ int XDIGIT = 58;
+
+ int VOCAB = 59;
+
+ int NESTED_ARG_ACTION = 60;
+
+ int NESTED_ACTION = 61;
+
+ int WS_LOOP = 62;
+
+ int INTERNAL_RULE_REF = 63;
+
+ int WS_OPT = 64;
+
+ int NOT_USEFUL = 65;
}
1.2 +19 -124 e/src/jsrc/antlr/ASTFactory.java
Index: ASTFactory.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/ASTFactory.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ASTFactory.java 2001/10/03 21:04:13 1.1
+++ ASTFactory.java 2001/10/04 01:09:13 1.2
@@ -4,7 +4,7 @@
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
- * $Id: ASTFactory.java,v 1.1 2001/10/03 21:04:13 markm Exp $
+ * $Id: ASTFactory.java,v 1.2 2001/10/04 01:09:13 markm Exp $
*/
import antlr.collections.AST;
@@ -20,79 +20,32 @@
* type of node to create, but you can override
* create to make heterogeneous nodes etc...
*/
-public class ASTFactory {
- /** Name of AST class to create during tree construction.
- * Null implies that the create method should create
- * a default AST type such as CommonAST.
- */
- protected String theASTNodeType = null;
- protected Class theASTNodeTypeClass = null;
+public class ASTFactory implements AbsASTFactory {
/** Add a child to the current AST */
public void addASTChild(ASTPair currentAST, AST child) {
- if (child != null) {
- if (currentAST.root == null) {
- // Make new child the current root
- currentAST.root = child;
- }
- else {
- if (currentAST.child == null) {
- // Add new child to current root
- currentAST.root.setFirstChild(child);
- }
- else {
- currentAST.child.setNextSibling(child);
- }
- }
- // Make new child the current child
- currentAST.child = child;
- currentAST.advanceChildToEnd();
- }
+ throw new RuntimeException("XXX not yet implemented");
}
- /** Create a new empty AST node; if the user did not specify
- * an AST node type, then create a default one: CommonAST.
- */
- public AST create() {
- AST t = null;
- if (theASTNodeTypeClass == null) {
- t = new CommonAST();
- } else {
- try {
- t = (AST) theASTNodeTypeClass.newInstance(); // make a new one
- } catch (Exception e) {
- antlr.Tool.warning("Can't create AST Node " + theASTNodeType);
- return null;
- }
- }
- return t;
- }
- public AST create(int type) {
- AST t = create();
- t.initialize(type,"");
- return t;
- }
- public AST create(int type, String txt) {
- AST t = create();
- t.initialize(type,txt);
- return t;
+ public AST create(int type) {
+ return new CommonAST(type, "", null, null);
+ }
+
+ public AST create(int type, String txt) {
+ return new CommonAST(type, txt, null, null);
}
/** Create a new empty AST node; if the user did not specify
* an AST node type, then create a default one: CommonAST.
*/
- public AST create(AST tr) {
- if ( tr==null ) return null; // create(null) == null
- AST t = create();
- t.initialize(tr);
- return t;
+ public AST create(AST tr) {
+ if (tr == null) return null; // create(null) == null
+ return new CommonAST(tr, null, null);
}
- public AST create(Token tok) {
- AST t = create();
- t.initialize(tok);
- return t;
+ public AST create(Token tok) {
+ return new CommonAST(tok,null, null);
}
/** Copy a single node. clone() is not used because
@@ -102,31 +55,19 @@
* tracked. Returns null if t is null.
*/
public AST dup(AST t) {
- return create(t); // if t==null, create returns null
+ return create(t); // if t==null, create returns null
}
/** Duplicate tree including siblings of root. */
public AST dupList(AST t) {
- AST result = dupTree(t); // if t == null, then result==null
- AST nt = result;
- while (t != null) { // for each sibling of the root
- t = t.getNextSibling();
- nt.setNextSibling(dupTree(t)); // dup each subtree, building new tree
- nt = nt.getNextSibling();
- }
- return result;
+ throw new RuntimeException("XXX not yet implemented");
}
/**Duplicate a tree, assuming this is a root node of a tree--
* duplicate that node and what's below; ignore siblings of root node.
*/
public AST dupTree(AST t) {
- AST result = dup(t); // make copy of root
- // copy all children of root.
- if ( t!=null ) {
- result.setFirstChild( dupList(t.getFirstChild()) );
- }
- return result;
+ throw new RuntimeException("XXX not yet implemented");
}
/** Make a tree from a list of nodes. The first element in the
@@ -136,64 +77,18 @@
* yields tree (nil a b).
*/
public AST make(AST[] nodes) {
- if ( nodes==null || nodes.length==0 ) return null;
- AST root = nodes[0];
- AST tail = null;
- if (root != null) {
- root.setFirstChild(null); // don't leave any old pointers set
- }
- // link in children;
- for (int i=1; i<nodes.length; i++) {
- if ( nodes[i]==null ) continue; // ignore null nodes
- if (root == null) {
- // Set the root and set it up for a flat list
- root = tail = nodes[i];
- }
- else if ( tail==null ) {
- root.setFirstChild(nodes[i]);
- tail = root.getFirstChild();
- }
- else {
- tail.setNextSibling(nodes[i]);
- tail = tail.getNextSibling();
- }
- // Chase tail to last sibling
- while (tail.getNextSibling() != null) {
- tail = tail.getNextSibling();
- }
- }
- return root;
+ throw new RuntimeException("XXX not yet implemented");
}
/** Make a tree from a list of nodes, where the nodes are contained
* in an ASTArray object
*/
public AST make(ASTArray nodes) {
- return make(nodes.array);
+ return make(nodes.array);
}
/** Make an AST the root of current AST */
public void makeASTRoot(ASTPair currentAST, AST root) {
- if (root != null) {
- // Add the current root as a child of new root
- root.addChild(currentAST.root);
- // The new current child is the last sibling of the old root
- currentAST.child = currentAST.root;
- currentAST.advanceChildToEnd();
- // Set the new root
- currentAST.root = root;
- }
- }
-
- public void setASTNodeType(String t) {
- theASTNodeType = t;
- try {
- theASTNodeTypeClass = Class.forName(t); // get class def
- } catch (Exception e) {
- // either class not found,
- // class is interface/abstract, or
- // class or initializer is not accessible.
- antlr.Tool.warning("Can't find/access AST Node type"+t);
- }
+ throw new RuntimeException("XXX not yet implemented");
}
}
1.2 +51 -49 e/src/jsrc/antlr/ASTIterator.java
Index: ASTIterator.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/ASTIterator.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ASTIterator.java 2001/10/03 21:04:13 1.1
+++ ASTIterator.java 2001/10/04 01:09:13 1.2
@@ -4,73 +4,75 @@
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
- * $Id: ASTIterator.java,v 1.1 2001/10/03 21:04:13 markm Exp $
+ * $Id: ASTIterator.java,v 1.2 2001/10/04 01:09:13 markm Exp $
*/
import antlr.collections.AST;
public class ASTIterator {
+
protected AST cursor = null;
+
protected AST original = null;
public ASTIterator(AST t) {
- original = cursor = t;
+ original = cursor = t;
}
/** Is 'sub' a subtree of 't' beginning at the root? */
public boolean isSubtree(AST t, AST sub) {
- AST sibling;
+ AST sibling;
- // the empty tree is always a subset of any tree.
- if ( sub==null ) {
- return true;
- }
-
- // if the tree is empty, return true if the subtree template is too.
- if ( t==null ) {
- if ( sub!=null ) return false;
- return true;
- }
-
- // Otherwise, start walking sibling lists. First mismatch, return false.
- for (sibling=t;
- sibling!=null&&sub!=null;
- sibling=sibling.getNextSibling(), sub=sub.getNextSibling())
- {
- // as a quick optimization, check roots first.
- if ( sibling.getType() != sub.getType() ) return false;
- // if roots match, do full match test on children.
- if ( sibling.getFirstChild()!=null ) {
- if ( !isSubtree(sibling.getFirstChild(), sub.getFirstChild()) ) return false;
- }
- }
- return true;
+ // the empty tree is always a subset of any tree.
+ if (sub == null) {
+ return true;
+ }
+
+ // if the tree is empty, return true if the subtree template is too.
+ if (t == null) {
+ if (sub != null) return false;
+ return true;
+ }
+
+ // Otherwise, start walking sibling lists. First mismatch, return false.
+ for (sibling = t;
+ sibling != null && sub != null;
+ sibling = sibling.getNextSibling(), sub = sub.getNextSibling()) {
+ // as a quick optimization, check roots first.
+ if (sibling.getType() != sub.getType()) return false;
+ // if roots match, do full match test on children.
+ if (sibling.getFirstChild() != null) {
+ if (!isSubtree(sibling.getFirstChild(), sub.getFirstChild())) return false;
+ }
+ }
+ return true;
}
+
/** Find the next subtree with structure and token types equal to
- * those of 'template'.
- */
+ * those of 'template'.
+ */
public AST next(AST template) {
- AST t = null;
- AST sibling = null;
-
- if ( cursor==null ) { // do nothing if no tree to work on
- return null;
- }
-
- // Start walking sibling list looking for subtree matches.
- for ( ; cursor!=null; cursor=cursor.getNextSibling())
- {
- // as a quick optimization, check roots first.
- if ( cursor.getType() == template.getType() ) {
- // if roots match, do full match test on children.
- if ( cursor.getFirstChild()!=null ) {
- if ( isSubtree(cursor.getFirstChild(), template.getFirstChild()) ) {
- return cursor;
- }
- }
- }
- }
- return t;
+ AST t = null;
+ AST sibling = null;
+
+ if (cursor == null) {
+ // do nothing if no tree to work on
+ return null;
+ }
+
+ // Start walking sibling list looking for subtree matches.
+ for (; cursor != null; cursor = cursor.getNextSibling()) {
+ // as a quick optimization, check roots first.
+ if (cursor.getType() == template.getType()) {
+ // if roots match, do full match test on children.
+ if (cursor.getFirstChild() != null) {
+ if (isSubtree(cursor.getFirstChild(), template.getFirstChild())) {
+ return cursor;
+ }
+ }
+ }
+ }
+ return t;
}
}
1.2 +54 -23 e/src/jsrc/antlr/ASTNULLType.java
Index: ASTNULLType.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/ASTNULLType.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ASTNULLType.java 2001/10/03 21:04:13 1.1
+++ ASTNULLType.java 2001/10/04 01:09:13 1.2
@@ -4,37 +4,68 @@
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
- * $Id: ASTNULLType.java,v 1.1 2001/10/03 21:04:13 markm Exp $
+ * $Id: ASTNULLType.java,v 1.2 2001/10/04 01:09:13 markm Exp $
*/
import antlr.collections.AST;
import antlr.collections.ASTEnumeration;
-import antlr.Token;
/** There is only one instance of this class **/
public class ASTNULLType implements AST {
- public void addChild(AST c) {;}
- public boolean equals(AST t) {return false;}
- public boolean equalsList(AST t) {return false;}
- public boolean equalsListPartial(AST t) {return false;}
- public boolean equalsTree(AST t) {return false;}
- public boolean equalsTreePartial(AST t) {return false;}
- public ASTEnumeration findAll(AST tree) {return null;}
+
+ public boolean equals(AST t) {
+ return false;
+ }
+
+ public boolean equalsList(AST t) {
+ return false;
+ }
+
+ public boolean equalsListPartial(AST t) {
+ return false;
+ }
+
+ public boolean equalsTree(AST t) {
+ return false;
+ }
+
+ public boolean equalsTreePartial(AST t) {
+ return false;
+ }
+
+ public ASTEnumeration findAll(AST tree) {
+ return null;
+ }
+
public ASTEnumeration findAllPartial(AST subtree) {
- return null;
+ return null;
+ }
+
+ public AST getFirstChild() {
+ return this;
+ }
+
+ public AST getNextSibling() {
+ return this;
+ }
+
+ public String getText() {
+ return "<ASTNULL>";
+ }
+
+ public int getType() {
+ return Token.NULL_TREE_LOOKAHEAD;
+ }
+
+ public String toString() {
+ return getText();
+ }
+
+ public String toStringList() {
+ return getText();
+ }
+
+ public String toStringTree() {
+ return getText();
}
- public AST getFirstChild() { return this; }
- public AST getNextSibling() { return this; }
- public String getText() { return "<ASTNULL>"; }
- public int getType() { return Token.NULL_TREE_LOOKAHEAD; }
- public void initialize(int t, String txt) {}
- public void initialize(AST t) {}
- public void initialize(Token t) {}
- public void setFirstChild(AST c) {;}
- public void setNextSibling(AST n) {;}
- public void setText(String text) {;}
- public void setType(int ttype) {;}
- public String toString() {return getText();}
- public String toStringList() {return getText();}
- public String toStringTree() {return getText();}
}
1.2 +20 -18 e/src/jsrc/antlr/ASTPair.java
Index: ASTPair.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/ASTPair.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ASTPair.java 2001/10/03 21:04:13 1.1
+++ ASTPair.java 2001/10/04 01:09:13 1.2
@@ -4,40 +4,42 @@
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
- * $Id: ASTPair.java,v 1.1 2001/10/03 21:04:13 markm Exp $
+ * $Id: ASTPair.java,v 1.2 2001/10/04 01:09:13 markm Exp $
*/
import antlr.collections.AST;
/** ASTPair: utility class used for manipulating a pair of ASTs
- * representing the current AST root and current AST sibling.
- * This exists to compensate for the lack of pointers or 'var'
- * arguments in Java.
- */
+ * representing the current AST root and current AST sibling.
+ * This exists to compensate for the lack of pointers or 'var'
+ * arguments in Java.
+ */
public class ASTPair {
- public AST root; // current root of tree
- public AST child; // current child to which siblings are added
+
+ public AST root; // current root of tree
+
+ public AST child; // current child to which siblings are added
/** Make sure that child is the last sibling */
public final void advanceChildToEnd() {
- if (child != null) {
- while (child.getNextSibling() != null) {
- child = child.getNextSibling();
- }
- }
+ if (child != null) {
+ while (child.getNextSibling() != null) {
+ child = child.getNextSibling();
+ }
+ }
}
/** Copy an ASTPair. Don't call it clone() because we want type-safety */
public ASTPair copy() {
- ASTPair tmp = new ASTPair();
- tmp.root = root;
- tmp.child = child;
- return tmp;
+ ASTPair tmp = new ASTPair();
+ tmp.root = root;
+ tmp.child = child;
+ return tmp;
}
public String toString() {
- String r = root==null ? "null" : root.getText();
- String c = child==null ? "null" : child.getText();
- return "["+r+","+c+"]";
+ String r = root == null ? "null" : root.getText();
+ String c = child == null ? "null" : child.getText();
+ return "[" + r + "," + c + "]";
}
}
1.2 +2 -1 e/src/jsrc/antlr/ASTVisitor.java
Index: ASTVisitor.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/ASTVisitor.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ASTVisitor.java 2001/10/03 21:04:13 1.1
+++ ASTVisitor.java 2001/10/04 01:09:13 1.2
@@ -4,11 +4,12 @@
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
- * $Id: ASTVisitor.java,v 1.1 2001/10/03 21:04:13 markm Exp $
+ * $Id: ASTVisitor.java,v 1.2 2001/10/04 01:09:13 markm Exp $
*/
import antlr.collections.AST;
-public interface ASTVisitor{
+public interface ASTVisitor {
+
public void visit(AST node);
}
1.2 +21 -16 e/src/jsrc/antlr/ActionElement.java
Index: ActionElement.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/ActionElement.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ActionElement.java 2001/10/03 21:04:13 1.1
+++ ActionElement.java 2001/10/04 01:09:13 1.2
@@ -4,26 +4,31 @@
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
- * $Id: ActionElement.java,v 1.1 2001/10/03 21:04:13 markm Exp $
+ * $Id: ActionElement.java,v 1.2 2001/10/04 01:09:13 markm Exp $
*/
class ActionElement extends AlternativeElement {
- protected String actionText;
- protected boolean isSemPred = false;
+ protected String actionText;
- public ActionElement(Grammar g, Token t) {
- super(g);
- actionText = t.getText();
- line = t.getLine();
- }
- public void generate() {
- grammar.generator.gen(this);
- }
- public Lookahead look(int k) {
- return grammar.theLLkAnalyzer.look(k, this);
- }
- public String toString() {
- return " "+actionText + (isSemPred?"?":"");
- }
+ protected boolean isSemPred = false;
+
+
+ public ActionElement(Grammar g, Token t) {
+ super(g);
+ actionText = t.getText();
+ line = t.getLine();
+ }
+
+ public void generate() {
+ grammar.generator.gen(this);
+ }
+
+ public Lookahead look(int k) {
+ return grammar.theLLkAnalyzer.look(k, this);
+ }
+
+ public String toString() {
+ return " " + actionText + (isSemPred?"?":"");
+ }
}
1.2 +7 -5 e/src/jsrc/antlr/ActionTransInfo.java
Index: ActionTransInfo.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/ActionTransInfo.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ActionTransInfo.java 2001/10/03 21:04:13 1.1
+++ ActionTransInfo.java 2001/10/04 01:09:13 1.2
@@ -4,7 +4,7 @@
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
- * $Id: ActionTransInfo.java,v 1.1 2001/10/03 21:04:13 markm Exp $
+ * $Id: ActionTransInfo.java,v 1.2 2001/10/04 01:09:13 markm Exp $
*/
/**
@@ -12,11 +12,13 @@
* was translated (using the AST conversion rules).
*/
public class ActionTransInfo {
- public boolean assignToRoot = false; // somebody did a "#rule = "
- public String refRuleRoot = null; // somebody referenced #rule; string is translated var
+ public boolean assignToRoot = false; // somebody did a "#rule = "
- public String toString() {
- return "assignToRoot:"+assignToRoot+", refRuleRoot:"+refRuleRoot;
- }
+ public String refRuleRoot = null; // somebody referenced #rule; string is translated var
+
+
+ public String toString() {
+ return "assignToRoot:" + assignToRoot + ", refRuleRoot:" + refRuleRoot;
+ }
}
1.2 +61 -53 e/src/jsrc/antlr/Alternative.java
Index: Alternative.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/Alternative.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- Alternative.java 2001/10/03 21:04:13 1.1
+++ Alternative.java 2001/10/04 01:09:13 1.2
@@ -4,63 +4,71 @@
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
- * $Id: Alternative.java,v 1.1 2001/10/03 21:04:13 markm Exp $
+ * $Id: Alternative.java,v 1.2 2001/10/04 01:09:13 markm Exp $
*/
-import java.util.Hashtable;
+
/** Intermediate data class holds information about an alternative */
class Alternative {
- // Tracking alternative linked list
- AlternativeElement head; // head of alt element list
- AlternativeElement tail; // last element added
-
- // Syntactic predicate block if non-null
- protected SynPredBlock synPred;
- // Semantic predicate action if non-null
- protected String semPred;
- // Exception specification if non-null
- protected ExceptionSpec exceptionSpec;
- // Init action if non-null;
- protected Lookahead[] cache; // lookahead for alt. Filled in by
- // deterministic() only!!!!!!! Used for
- // code gen after calls to deterministic()
- // and used by deterministic for (...)*, (..)+,
- // and (..)? blocks. 1..k
- protected int lookaheadDepth; // each alt has different look depth possibly.
- // depth can be NONDETERMINISTIC too.
- // 0..n-1
- // If non-null, Tree specification ala -> A B C (not implemented)
- protected Token treeSpecifier = null;
- // True of AST generation is on for this alt
- private boolean doAutoGen;
-
-
- public Alternative() {
- }
- public Alternative(AlternativeElement firstElement) {
- addElement(firstElement);
- }
- public void addElement(AlternativeElement e)
- {
- // Link the element into the list
- if ( head == null ) {
- head = tail = e;
- }
- else {
- tail.next = e;
- tail = e;
- }
- }
- public boolean atStart() { return head == null; }
- public boolean getAutoGen() {
- // Don't build an AST if there is a tree-rewrite-specifier
- return doAutoGen && treeSpecifier == null;
- }
- public Token getTreeSpecifier() {
- return treeSpecifier;
- }
- public void setAutoGen(boolean doAutoGen_) {
- doAutoGen = doAutoGen_;
- }
+
+ // Tracking alternative linked list
+ AlternativeElement head; // head of alt element list
+
+ AlternativeElement tail; // last element added
+
+ // Syntactic predicate block if non-null
+ protected SynPredBlock synPred;
+ // Semantic predicate action if non-null
+ protected String semPred;
+ // Exception specification if non-null
+ protected ExceptionSpec exceptionSpec;
+ // Init action if non-null;
+ protected Lookahead[] cache; // lookahead for alt. Filled in by
+ // deterministic() only!!!!!!! Used for
+ // code gen after calls to deterministic()
+ // and used by deterministic for (...)*, (..)+,
+ // and (..)? blocks. 1..k
+ protected int lookaheadDepth; // each alt has different look depth possibly.
+ // depth can be NONDETERMINISTIC too.
+ // 0..n-1
+ // If non-null, Tree specification ala -> A B C (not implemented)
+ protected Token treeSpecifier = null;
+ // True of AST generation is on for this alt
+ private boolean doAutoGen;
+
+
+ public Alternative() {
+ }
+
+ public Alternative(AlternativeElement firstElement) {
+ addElement(firstElement);
+ }
+
+ public void addElement(AlternativeElement e) {
+ // Link the element into the list
+ if (head == null) {
+ head = tail = e;
+ } else {
+ tail.next = e;
+ tail = e;
+ }
+ }
+
+ public boolean atStart() {
+ return head == null;
+ }
+
+ public boolean getAutoGen() {
+ // Don't build an AST if there is a tree-rewrite-specifier
+ return doAutoGen && treeSpecifier == null;
+ }
+
+ public Token getTreeSpecifier() {
+ return treeSpecifier;
+ }
+
+ public void setAutoGen(boolean doAutoGen_) {
+ doAutoGen = doAutoGen_;
+ }
}
1.2 +142 -130 e/src/jsrc/antlr/AlternativeBlock.java
Index: AlternativeBlock.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/AlternativeBlock.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- AlternativeBlock.java 2001/10/03 21:04:13 1.1
+++ AlternativeBlock.java 2001/10/04 01:09:13 1.2
@@ -4,201 +4,213 @@
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
- * $Id: AlternativeBlock.java,v 1.1 2001/10/03 21:04:13 markm Exp $
+ * $Id: AlternativeBlock.java,v 1.2 2001/10/04 01:09:13 markm Exp $
*/
import antlr.collections.impl.Vector;
/**A list of alternatives */
class AlternativeBlock extends AlternativeElement {
+
protected String initAction = null; // string for init action {...}
+
protected Vector alternatives; // Contains Alternatives
protected String label; // can label a looping block to break out of it.
protected int alti, altj; // which alts are being compared at the moment with
- // deterministic()?
+ // deterministic()?
protected int analysisAlt; // which alt are we computing look on? Must be alti or altj
protected boolean hasAnAction = false; // does any alt have an action?
+
protected boolean hasASynPred = false; // does any alt have a syntactic predicate?
- protected int ID=0; // used to generate unique variables
+ protected int ID = 0; // used to generate unique variables
+
protected static int nblks; // how many blocks have we allocated?
+
boolean not = false; // true if block is inverted.
boolean greedy = true; // Blocks are greedy by default
- boolean greedySet=false; // but, if not explicitly greedy, warning might be generated
- protected boolean doAutoGen=true; // false if no AST (or text) to be generated for block
+ boolean greedySet = false; // but, if not explicitly greedy, warning might be generated
+ protected boolean doAutoGen = true; // false if no AST (or text) to be generated for block
+
protected boolean warnWhenFollowAmbig = true; // warn when an empty path or exit path
protected boolean generateAmbigWarnings = true; // the general warning "shut-up" mechanism
- // conflicts with alt of subrule.
- // Turning this off will suppress stuff
- // like the if-then-else ambig.
+ // conflicts with alt of subrule.
+ // Turning this off will suppress stuff
+ // like the if-then-else ambig.
public AlternativeBlock(Grammar g) {
- this(g,0,false);
+ this(g, 0, false);
}
public AlternativeBlock(Grammar g, int line, boolean not) {
- super(g);
- alternatives = new Vector(5);
- this.line = line;
- this.not = not;
- nblks++;
- ID = nblks;
+ super(g);
+ alternatives = new Vector(5);
+ this.line = line;
+ this.not = not;
+ nblks++;
+ ID = nblks;
}
public void addAlternative(Alternative alt) {
- alternatives.appendElement(alt);
+ alternatives.appendElement(alt);
}
+
public void generate() {
- grammar.generator.gen(this);
+ grammar.generator.gen(this);
}
- public Alternative getAlternativeAt(int i)
- {
- return (Alternative)alternatives.elementAt(i);
+
+ public Alternative getAlternativeAt(int i) {
+ return (Alternative)alternatives.elementAt(i);
}
+
public Vector getAlternatives() {
- return alternatives;
+ return alternatives;
}
- public boolean getAutoGen() {
- return doAutoGen;
+
+ public boolean getAutoGen() {
+ return doAutoGen;
}
- public String getInitAction() {
- return initAction;
+
+ public String getInitAction() {
+ return initAction;
}
+
public String getLabel() {
- return label;
+ return label;
}
+
public Lookahead look(int k) {
- return grammar.theLLkAnalyzer.look(k, this);
+ return grammar.theLLkAnalyzer.look(k, this);
}
+
public void prepareForAnalysis() {
- for (int i=0; i<alternatives.size(); i++) {
- // deterministic() uses an alternative cache and sets lookahead depth
- Alternative a = (Alternative)alternatives.elementAt(i);
- a.cache = new Lookahead[grammar.maxk+1];
- a.lookaheadDepth = GrammarAnalyzer.LOOKAHEAD_DEPTH_INIT;
- }
+ for (int i = 0; i < alternatives.size(); i++) {
+ // deterministic() uses an alternative cache and sets lookahead depth
+ Alternative a = (Alternative)alternatives.elementAt(i);
+ a.cache = new Lookahead[grammar.maxk + 1];
+ a.lookaheadDepth = GrammarAnalyzer.LOOKAHEAD_DEPTH_INIT;
+ }
}
+
/**Walk the syntactic predicate and, for a rule ref R, remove
- * the ref from the list of FOLLOW references for R (stored
- * in the symbol table.
- */
+ * the ref from the list of FOLLOW references for R (stored
+ * in the symbol table.
+ */
public void removeTrackingOfRuleRefs(Grammar g) {
- for (int i=0; i<alternatives.size(); i++) {
- Alternative alt = getAlternativeAt(i);
- AlternativeElement elem = alt.head;
- while ( elem!=null ) {
- if ( elem instanceof RuleRefElement ) {
- RuleRefElement rr = (RuleRefElement)elem;
- RuleSymbol rs = (RuleSymbol)g.getSymbol(rr.targetRule);
- if ( rs==null ) {
- grammar.tool.error("rule "+rr.targetRule+" referenced in (...)=>, but not defined");
- }
- else {
- rs.references.removeElement(rr);
- }
- }
- else if ( elem instanceof AlternativeBlock ) {// recurse into subrules
- ((AlternativeBlock)elem).removeTrackingOfRuleRefs(g);
- }
- elem = elem.next;
- }
- }
+ for (int i = 0; i < alternatives.size(); i++) {
+ Alternative alt = getAlternativeAt(i);
+ AlternativeElement elem = alt.head;
+ while (elem != null) {
+ if (elem instanceof RuleRefElement) {
+ RuleRefElement rr = (RuleRefElement)elem;
+ RuleSymbol rs = (RuleSymbol)g.getSymbol(rr.targetRule);
+ if (rs == null) {
+ grammar.tool.error("rule " + rr.targetRule + " referenced in (...)=>, but not defined");
+ } else {
+ rs.references.removeElement(rr);
+ }
+ } else if (elem instanceof AlternativeBlock) {
+ // recurse into subrules
+ ((AlternativeBlock)elem).removeTrackingOfRuleRefs(g);
+ }
+ elem = elem.next;
+ }
+ }
}
+
public void setAlternatives(Vector v) {
- alternatives = v;
+ alternatives = v;
}
+
public void setAutoGen(boolean doAutoGen_) {
- doAutoGen = doAutoGen_;
+ doAutoGen = doAutoGen_;
}
+
public void setInitAction(String initAction_) {
- initAction = initAction_;
+ initAction = initAction_;
}
- public void setLabel(String label_) {
- label = label_;
+
+ public void setLabel(String label_) {
+ label = label_;
}
public void setOption(Token key, Token value) {
- if (key.getText().equals("warnWhenFollowAmbig")) {
- if (value.getText().equals("true")) {
- warnWhenFollowAmbig = true;
- } else if (value.getText().equals("false")) {
- warnWhenFollowAmbig = false;
- } else {
- grammar.tool.error("Value for warnWhenFollowAmbig must be true or false", grammar.getFilename(), key.getLine());
- }
- }
- else if (key.getText().equals("generateAmbigWarnings")) {
- if (value.getText().equals("true")) {
- generateAmbigWarnings = true;
- } else if (value.getText().equals("false")) {
- generateAmbigWarnings = false;
- } else {
- grammar.tool.error("Value for generateAmbigWarnings must be true or false", grammar.getFilename(), key.getLine());
- }
- }
- else if (key.getText().equals("greedy")) {
- if (value.getText().equals("true")) {
- greedy = true;
- greedySet = true;
- } else if (value.getText().equals("false")) {
- greedy = false;
- greedySet = true;
- } else {
- grammar.tool.error("Value for greedy must be true or false", grammar.getFilename(), key.getLine());
- }
- }
- else {
- grammar.tool.error("Invalid subrule option: " + key.getText(), grammar.getFilename(), key.getLine());
- }
+ if (key.getText().equals("warnWhenFollowAmbig")) {
+ if (value.getText().equals("true")) {
+ warnWhenFollowAmbig = true;
+ } else if (value.getText().equals("false")) {
+ warnWhenFollowAmbig = false;
+ } else {
+ grammar.tool.error("Value for warnWhenFollowAmbig must be true or false", grammar.getFilename(), key.getLine());
+ }
+ } else if (key.getText().equals("generateAmbigWarnings")) {
+ if (value.getText().equals("true")) {
+ generateAmbigWarnings = true;
+ } else if (value.getText().equals("false")) {
+ generateAmbigWarnings = false;
+ } else {
+ grammar.tool.error("Value for generateAmbigWarnings must be true or false", grammar.getFilename(), key.getLine());
+ }
+ } else if (key.getText().equals("greedy")) {
+ if (value.getText().equals("true")) {
+ greedy = true;
+ greedySet = true;
+ } else if (value.getText().equals("false")) {
+ greedy = false;
+ greedySet = true;
+ } else {
+ grammar.tool.error("Value for greedy must be true or false", grammar.getFilename(), key.getLine());
+ }
+ } else {
+ grammar.tool.error("Invalid subrule option: " + key.getText(), grammar.getFilename(), key.getLine());
+ }
}
public String toString() {
- String s=" (";
- if ( initAction!=null ) {
- s += initAction;
- }
- for (int i=0; i<alternatives.size(); i++) {
- Alternative alt = getAlternativeAt(i);
- Lookahead cache[] = alt.cache;
- int k = alt.lookaheadDepth;
- // dump lookahead set
- if ( k==GrammarAnalyzer.LOOKAHEAD_DEPTH_INIT ) {
- }
- else if ( k==GrammarAnalyzer.NONDETERMINISTIC ) {
- s += "{?}:";
- }
- else {
- s += " {";
- for (int j=1; j<=k; j++) {
- s += cache[j].toString(",",grammar.tokenManager.getVocabulary());
- if ( j<k && cache[j+1]!=null ) s += ";";
- }
- s += "}:";
- }
- // dump alternative including pred (if any)
- AlternativeElement p = alt.head;
- String pred = alt.semPred;
- if ( pred!=null ) {
- s += pred;
- }
- while (p!=null ) {
- s += p;
- p = p.next;
- }
- if ( i<(alternatives.size()-1) ) {
- s += " |";
- }
- }
- s += " )";
- return s;
+ String s = " (";
+ if (initAction != null) {
+ s += initAction;
+ }
+ for (int i = 0; i < alternatives.size(); i++) {
+ Alternative alt = getAlternativeAt(i);
+ Lookahead cache[] = alt.cache;
+ int k = alt.lookaheadDepth;
+ // dump lookahead set
+ if (k == GrammarAnalyzer.LOOKAHEAD_DEPTH_INIT) {
+ } else if (k == GrammarAnalyzer.NONDETERMINISTIC) {
+ s += "{?}:";
+ } else {
+ s += " {";
+ for (int j = 1; j <= k; j++) {
+ s += cache[j].toString(",", grammar.tokenManager.getVocabulary());
+ if (j < k && cache[j + 1] != null) s += ";";
+ }
+ s += "}:";
+ }
+ // dump alternative including pred (if any)
+ AlternativeElement p = alt.head;
+ String pred = alt.semPred;
+ if (pred != null) {
+ s += pred;
+ }
+ while (p != null) {
+ s += p;
+ p = p.next;
+ }
+ if (i < (alternatives.size() - 1)) {
+ s += " |";
+ }
+ }
+ s += " )";
+ return s;
}
}
1.2 +24 -17 e/src/jsrc/antlr/AlternativeElement.java
Index: AlternativeElement.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/AlternativeElement.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- AlternativeElement.java 2001/10/03 21:04:13 1.1
+++ AlternativeElement.java 2001/10/04 01:09:13 1.2
@@ -4,28 +4,35 @@
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
- * $Id: AlternativeElement.java,v 1.1 2001/10/03 21:04:13 markm Exp $
+ * $Id: AlternativeElement.java,v 1.2 2001/10/04 01:09:13 markm Exp $
*/
abstract class AlternativeElement extends GrammarElement {
- AlternativeElement next;
- protected int autoGenType = AUTO_GEN_NONE;
- protected String enclosingRuleName;
+ AlternativeElement next;
+ protected int autoGenType = AUTO_GEN_NONE;
- public AlternativeElement(Grammar g) {
- super(g);
- }
- public AlternativeElement(Grammar g, int autoGenType_) {
- super(g);
- autoGenType = autoGenType_;
- }
- public int getAutoGenType() {
- return autoGenType;
- }
- public String getLabel() {
- return null;
- }
- public void setLabel(String label) {}
+ protected String enclosingRuleName;
+
+
+ public AlternativeElement(Grammar g) {
+ super(g);
+ }
+
+ public AlternativeElement(Grammar g, int autoGenType_) {
+ super(g);
+ autoGenType = autoGenType_;
+ }
+
+ public int getAutoGenType() {
+ return autoGenType;
+ }
+
+ public String getLabel() {
+ return null;
+ }
+
+ public void setLabel(String label) {
+ }
}
1.2 +281 -298 e/src/jsrc/antlr/BaseAST.java
Index: BaseAST.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/BaseAST.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- BaseAST.java 2001/10/03 21:04:13 1.1
+++ BaseAST.java 2001/10/04 01:09:13 1.2
@@ -4,15 +4,16 @@
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
- * $Id: BaseAST.java,v 1.1 2001/10/03 21:04:13 markm Exp $
+ * $Id: BaseAST.java,v 1.2 2001/10/04 01:09:13 markm Exp $
*/
import antlr.collections.AST;
import antlr.collections.ASTEnumeration;
import antlr.collections.impl.ASTEnumerator;
import antlr.collections.impl.Vector;
-import java.io.Serializable;
+
import java.io.IOException;
+import java.io.Serializable;
import java.io.Writer;
/**
@@ -41,153 +42,144 @@
* BaseAST or of CommonAST.
*/
public abstract class BaseAST implements AST, Serializable {
- protected BaseAST down;
- protected BaseAST right;
+
+ protected final BaseAST down;
+
+ protected final BaseAST right;
+
+ protected BaseAST(BaseAST car, BaseAST cdr) {
+ down = car;
+ right = cdr;
+ }
private static boolean verboseStringConversion = false;
+
private static String[] tokenNames = null;
-
- /**Add a node to the end of the child list for this node */
- public void addChild(AST node) {
- if ( node==null ) return;
- BaseAST t = this.down;
- if ( t!=null ) {
- while ( t.right!=null ) {
- t = t.right;
- }
- t.right = (BaseAST)node;
- }
- else {
- this.down = (BaseAST)node;
- }
- }
private void doWorkForFindAll(Vector v, AST target, boolean partialMatch) {
- AST sibling;
-
- // Start walking sibling lists, looking for matches.
- siblingWalk:
- for (sibling=this;
- sibling!=null;
- sibling=sibling.getNextSibling())
- {
- if ( (partialMatch && sibling.equalsTreePartial(target)) ||
- (!partialMatch && sibling.equalsTree(target)) ) {
- v.appendElement(sibling);
- }
- // regardless of match or not, check any children for matches
- if ( sibling.getFirstChild()!=null ) {
- ((BaseAST)sibling.getFirstChild()).doWorkForFindAll(v, target, partialMatch);
- }
- }
+ AST sibling;
+
+ // Start walking sibling lists, looking for matches.
+ siblingWalk:
+ for (sibling = this;
+ sibling != null;
+ sibling = sibling.getNextSibling()) {
+ if ((partialMatch && sibling.equalsTreePartial(target)) ||
+ (!partialMatch && sibling.equalsTree(target))) {
+ v.appendElement(sibling);
+ }
+ // regardless of match or not, check any children for matches
+ if (sibling.getFirstChild() != null) {
+ ((BaseAST)sibling.getFirstChild()).doWorkForFindAll(v, target, partialMatch);
+ }
+ }
}
/** Is node t equal to this in terms of token type and text? */
public boolean equals(AST t) {
- if ( t==null ) return false;
- return this.getText().equals(t.getText()) &&
- this.getType() == t.getType();
+ if (t == null) return false;
+ return this.getText().equals(t.getText()) &&
+ this.getType() == t.getType();
}
/** Is t an exact structural and equals() match of this tree. The
- * 'this' reference is considered the start of a sibling list.
- */
+ * 'this' reference is considered the start of a sibling list.
+ */
public boolean equalsList(AST t) {
- AST sibling;
+ AST sibling;
- // the empty tree is not a match of any non-null tree.
- if (t == null) {
- return false;
- }
-
- // Otherwise, start walking sibling lists. First mismatch, return false.
- for (sibling = this; sibling != null && t != null; sibling = sibling.getNextSibling(), t = t.getNextSibling()) {
- // as a quick optimization, check roots first.
- if (!sibling.equals(t)) {
- return false;
- }
- // if roots match, do full list match test on children.
- if (sibling.getFirstChild() != null) {
- if (!sibling.getFirstChild().equalsList(t.getFirstChild())) {
- return false;
- }
- }
- // sibling has no kids, make sure t doesn't either
- else if (t.getFirstChild() != null) {
- return false;
- }
- }
- if (sibling == null && t == null) {
- return true;
- }
- // one sibling list has more than the other
- return false;
+ // the empty tree is not a match of any non-null tree.
+ if (t == null) {
+ return false;
+ }
+
+ // Otherwise, start walking sibling lists. First mismatch, return false.
+ for (sibling = this; sibling != null && t != null; sibling = sibling.getNextSibling(), t = t.getNextSibling()) {
+ // as a quick optimization, check roots first.
+ if (!sibling.equals(t)) {
+ return false;
+ }
+ // if roots match, do full list match test on children.
+ if (sibling.getFirstChild() != null) {
+ if (!sibling.getFirstChild().equalsList(t.getFirstChild())) {
+ return false;
+ }
+ }
+ // sibling has no kids, make sure t doesn't either
+ else if (t.getFirstChild() != null) {
+ return false;
+ }
+ }
+ if (sibling == null && t == null) {
+ return true;
+ }
+ // one sibling list has more than the other
+ return false;
}
/** Is 'sub' a subtree of this list?
* The siblings of the root are NOT ignored.
*/
public boolean equalsListPartial(AST sub) {
- AST sibling;
+ AST sibling;
- // the empty tree is always a subset of any tree.
- if ( sub==null ) {
- return true;
- }
-
- // Otherwise, start walking sibling lists. First mismatch, return false.
- for (sibling=this;
- sibling!=null&&sub!=null;
- sibling=sibling.getNextSibling(), sub=sub.getNextSibling())
- {
- // as a quick optimization, check roots first.
- if ( !sibling.equals(sub) ) return false;
- // if roots match, do partial list match test on children.
- if ( sibling.getFirstChild()!=null ) {
- if ( !sibling.getFirstChild().equalsListPartial(sub.getFirstChild()) ) return false;
- }
- }
- if ( sibling==null && sub!=null ) {
- // nothing left to match in this tree, but subtree has more
- return false;
- }
- // either both are null or sibling has more, but subtree doesn't
- return true;
+ // the empty tree is always a subset of any tree.
+ if (sub == null) {
+ return true;
+ }
+
+ // Otherwise, start walking sibling lists. First mismatch, return false.
+ for (sibling = this;
+ sibling != null && sub != null;
+ sibling = sibling.getNextSibling(), sub = sub.getNextSibling()) {
+ // as a quick optimization, check roots first.
+ if (!sibling.equals(sub)) return false;
+ // if roots match, do partial list match test on children.
+ if (sibling.getFirstChild() != null) {
+ if (!sibling.getFirstChild().equalsListPartial(sub.getFirstChild())) return false;
+ }
+ }
+ if (sibling == null && sub != null) {
+ // nothing left to match in this tree, but subtree has more
+ return false;
+ }
+ // either both are null or sibling has more, but subtree doesn't
+ return true;
}
/** Is tree rooted at 'this' equal to 't'? The siblings
* of 'this' are ignored.
*/
public boolean equalsTree(AST t) {
- // check roots first.
- if ( !this.equals(t) ) return false;
- // if roots match, do full list match test on children.
- if ( this.getFirstChild()!=null ) {
- if ( !this.getFirstChild().equalsList(t.getFirstChild()) ) return false;
- }
- // sibling has no kids, make sure t doesn't either
- else if (t.getFirstChild() != null) {
- return false;
- }
- return true;
+ // check roots first.
+ if (!this.equals(t)) return false;
+ // if roots match, do full list match test on children.
+ if (this.getFirstChild() != null) {
+ if (!this.getFirstChild().equalsList(t.getFirstChild())) return false;
+ }
+ // sibling has no kids, make sure t doesn't either
+ else if (t.getFirstChild() != null) {
+ return false;
+ }
+ return true;
}
/** Is 't' a subtree of the tree rooted at 'this'? The siblings
- * of 'this' are ignored.
+ * of 'this' are ignored.
*/
public boolean equalsTreePartial(AST sub) {
- // the empty tree is always a subset of any tree.
- if ( sub==null ) {
- return true;
- }
-
- // check roots first.
- if ( !this.equals(sub) ) return false;
- // if roots match, do full list partial match test on children.
- if ( this.getFirstChild()!=null ) {
- if ( !this.getFirstChild().equalsListPartial(sub.getFirstChild()) ) return false;
- }
- return true;
+ // the empty tree is always a subset of any tree.
+ if (sub == null) {
+ return true;
+ }
+
+ // check roots first.
+ if (!this.equals(sub)) return false;
+ // if roots match, do full list partial match test on children.
+ if (this.getFirstChild() != null) {
+ if (!this.getFirstChild().equalsListPartial(sub.getFirstChild())) return false;
+ }
+ return true;
}
/** Walk the tree looking for all exact subtree matches. Return
@@ -195,17 +187,17 @@
* of subtree roots found herein.
*/
public ASTEnumeration findAll(AST target) {
- Vector roots = new Vector(10);
- AST sibling;
+ Vector roots = new Vector(10);
+ AST sibling;
- // the empty tree cannot result in an enumeration
- if ( target==null ) {
- return null;
- }
+ // the empty tree cannot result in an enumeration
+ if (target == null) {
+ return null;
+ }
- doWorkForFindAll(roots, target, false); // find all matches recursively
+ doWorkForFindAll(roots, target, false); // find all matches recursively
- return new ASTEnumerator(roots);
+ return new ASTEnumerator(roots);
}
/** Walk the tree looking for all subtrees. Return
@@ -213,218 +205,209 @@
* of subtree roots found herein.
*/
public ASTEnumeration findAllPartial(AST sub) {
- Vector roots = new Vector(10);
- AST sibling;
+ Vector roots = new Vector(10);
+ AST sibling;
- // the empty tree cannot result in an enumeration
- if ( sub==null ) {
- return null;
- }
+ // the empty tree cannot result in an enumeration
+ if (sub == null) {
+ return null;
+ }
- doWorkForFindAll(roots, sub, true); // find all matches recursively
+ doWorkForFindAll(roots, sub, true); // find all matches recursively
- return new ASTEnumerator(roots);
+ return new ASTEnumerator(roots);
}
/** Get the first child of this node; null if not children */
public AST getFirstChild() {
- return down;
+ return down;
}
/** Get the next sibling in line after this one */
public AST getNextSibling() {
- return right;
+ return right;
}
/** Get the token text for this node */
- public String getText() { return ""; }
-
- /** Get the token type for this node */
- public int getType() { return 0; }
-
- public abstract void initialize(int t, String txt);
-
- public abstract void initialize(AST t);
-
- public abstract void initialize(Token t);
-
- /** Remove all children */
- public void removeChildren() {
- down = null;
+ public String getText() {
+ return "";
}
- public void setFirstChild(AST c) {
- down = (BaseAST)c;
- }
-
- public void setNextSibling(AST n) {
- right = (BaseAST)n;
+ /** Get the token type for this node */
+ public int getType() {
+ return 0;
}
- /** Set the token text for this node */
- public void setText(String text) {;}
-
- /** Set the token type for this node */
- public void setType(int ttype) {;}
-
public static void setVerboseStringConversion(boolean verbose, String[] names) {
- verboseStringConversion = verbose;
- tokenNames = names;
+ verboseStringConversion = verbose;
+ tokenNames = names;
}
public String toString() {
- StringBuffer b = new StringBuffer();
- // if verbose and type name not same as text (keyword probably)
- if ( verboseStringConversion &&
- !getText().equalsIgnoreCase(tokenNames[getType()]) &&
- !getText().equalsIgnoreCase(Tool.stripFrontBack(tokenNames[getType()],"\"","\"")) ) {
- b.append('[');
- b.append(getText());
- b.append(",<");
- b.append(tokenNames[getType()]);
- b.append(">]");
- return b.toString();
- }
- return getText();
+ StringBuffer b = new StringBuffer();
+ // if verbose and type name not same as text (keyword probably)
+ if (verboseStringConversion &&
+ !getText().equalsIgnoreCase(tokenNames[getType()]) &&
+ !getText().equalsIgnoreCase(Tool.stripFrontBack(tokenNames[getType()], "\"", "\""))) {
+ b.append('[');
+ b.append(getText());
+ b.append(",<");
+ b.append(tokenNames[getType()]);
+ b.append(">]");
+ return b.toString();
+ }
+ return getText();
}
/** Print out a child-sibling tree in LISP notation */
public String toStringList() {
- AST t = this;
- String ts="";
- if ( t.getFirstChild()!=null ) ts+=" (";
- ts += " "+this.toString();
- if ( t.getFirstChild()!=null ) {
- ts += ((BaseAST)t.getFirstChild()).toStringList();
- }
- if ( t.getFirstChild()!=null ) ts+=" )";
- if ( t.getNextSibling()!=null ) {
- ts += ((BaseAST)t.getNextSibling()).toStringList();
- }
- return ts;
+ AST t = this;
+ String ts = "";
+ if (t.getFirstChild() != null) ts += " (";
+ ts += " " + this.toString();
+ if (t.getFirstChild() != null) {
+ ts += ((BaseAST)t.getFirstChild()).toStringList();
+ }
+ if (t.getFirstChild() != null) ts += " )";
+ if (t.getNextSibling() != null) {
+ ts += ((BaseAST)t.getNextSibling()).toStringList();
+ }
+ return ts;
}
public String toStringTree() {
- AST t = this;
- String ts="";
- if ( t.getFirstChild()!=null ) ts+=" (";
- ts += " "+this.toString();
- if ( t.getFirstChild()!=null ) {
- ts += ((BaseAST)t.getFirstChild()).toStringList();
- }
- if ( t.getFirstChild()!=null ) ts+=" )";
- return ts;
- }
-
- public static String decode(String text)
- {
- char c, c1, c2, c3, c4, c5;
- StringBuffer n = new StringBuffer();
- for (int i=0; i < text.length(); i++)
- {
- c = text.charAt(i);
- if (c == '&') {
- c1 = text.charAt(i+1); c2 = text.charAt(i+2);
- c3 = text.charAt(i+3); c4 = text.charAt(i+4);
- c5 = text.charAt(i+5);
-
- if ( c1 == 'a' && c2 == 'm' && c3 == 'p' && c4 == ';') {
- n.append("&");
- i += 5;
- }
- else if ( c1 == 'l' && c2 == 't' && c3 == ';') {
- n.append("<");
- i += 4;
- }
- else if ( c1 == 'g' && c2 == 't' && c3 == ';') {
- n.append(">");
- i += 4;
- }
- else if ( c1 == 'q' && c2 == 'u' && c3 == 'o' &&
- c4 == 't' && c5 == ';') {
- n.append("\"");
- i += 6;
- }
- else if ( c1 == 'a' && c2 == 'p' && c3 == 'o' &&
- c4 == 's' && c5 == ';') {
- n.append("'");
- i += 6;
- }
- else n.append("&");
- }
- else n.append(c);
- }
- return new String(n);
- }
-
- public static String encode(String text)
- {
- char c;
- StringBuffer n = new StringBuffer();
- for (int i=0; i < text.length(); i++)
- {
- c = text.charAt(i);
- switch (c) {
- case '&' : { n.append("&"); break; }
- case '<' : { n.append("<"); break; }
- case '>' : { n.append(">"); break; }
- case '"' : { n.append("""); break; }
- case '\'' : { n.append("'"); break; }
- default : { n.append(c); break; }
- }
- }
- return new String(n);
+ AST t = this;
+ String ts = "";
+ if (t.getFirstChild() != null) ts += " (";
+ ts += " " + this.toString();
+ if (t.getFirstChild() != null) {
+ ts += ((BaseAST)t.getFirstChild()).toStringList();
+ }
+ if (t.getFirstChild() != null) ts += " )";
+ return ts;
+ }
+
+ public static String decode(String text) {
+ char c, c1, c2, c3, c4, c5;
+ StringBuffer n = new StringBuffer();
+ for (int i = 0; i < text.length(); i++) {
+ c = text.charAt(i);
+ if (c == '&') {
+ c1 = text.charAt(i + 1);
+ c2 = text.charAt(i + 2);
+ c3 = text.charAt(i + 3);
+ c4 = text.charAt(i + 4);
+ c5 = text.charAt(i + 5);
+
+ if (c1 == 'a' && c2 == 'm' && c3 == 'p' && c4 == ';') {
+ n.append("&");
+ i += 5;
+ } else if (c1 == 'l' && c2 == 't' && c3 == ';') {
+ n.append("<");
+ i += 4;
+ } else if (c1 == 'g' && c2 == 't' && c3 == ';') {
+ n.append(">");
+ i += 4;
+ } else if (c1 == 'q' && c2 == 'u' && c3 == 'o' &&
+ c4 == 't' && c5 == ';') {
+ n.append("\"");
+ i += 6;
+ } else if (c1 == 'a' && c2 == 'p' && c3 == 'o' &&
+ c4 == 's' && c5 == ';') {
+ n.append("'");
+ i += 6;
+ } else
+ n.append("&");
+ } else
+ n.append(c);
+ }
+ return new String(n);
+ }
+
+ public static String encode(String text) {
+ char c;
+ StringBuffer n = new StringBuffer();
+ for (int i = 0; i < text.length(); i++) {
+ c = text.charAt(i);
+ switch (c) {
+ case '&':
+ {
+ n.append("&");
+ break;
+ }
+ case '<':
+ {
+ n.append("<");
+ break;
+ }
+ case '>':
+ {
+ n.append(">");
+ break;
+ }
+ case '"':
+ {
+ n.append(""");
+ break;
+ }
+ case '\'':
+ {
+ n.append("'");
+ break;
+ }
+ default :
+ {
+ n.append(c);
+ break;
+ }
+ }
+ }
+ return new String(n);
}
public void xmlSerializeNode(Writer out)
- throws IOException
- {
- StringBuffer buf = new StringBuffer(100);
- buf.append("<");
- buf.append(getClass().getName()+" ");
- buf.append("text=\""+encode(getText())+"\" type=\""+
- getType()+"\"/>");
- out.write(buf.toString());
+ throws IOException {
+ StringBuffer buf = new StringBuffer(100);
+ buf.append("<");
+ buf.append(getClass().getName() + " ");
+ buf.append("text=\"" + encode(getText()) + "\" type=\"" +
+ getType() + "\"/>");
+ out.write(buf.toString());
}
public void xmlSerializeRootOpen(Writer out)
- throws IOException
- {
- StringBuffer buf = new StringBuffer(100);
- buf.append("<");
- buf.append(getClass().getName()+" ");
- buf.append("text=\""+encode(getText())+"\" type=\""+
- getType()+"\">\n");
- out.write(buf.toString());
+ throws IOException {
+ StringBuffer buf = new StringBuffer(100);
+ buf.append("<");
+ buf.append(getClass().getName() + " ");
+ buf.append("text=\"" + encode(getText()) + "\" type=\"" +
+ getType() + "\">\n");
+ out.write(buf.toString());
}
public void xmlSerializeRootClose(Writer out)
- throws IOException
- {
- out.write("</"+getClass().getName()+">\n");
- }
-
- public void xmlSerialize(Writer out) throws IOException
- {
- // print out this node and all siblings
- for (AST node = this;
- node != null;
- node = node.getNextSibling())
- {
- if (node.getFirstChild() == null) {
- // print guts (class name, attributes)
- ((BaseAST)node).xmlSerializeNode(out);
- }
- else {
- ((BaseAST)node).xmlSerializeRootOpen(out);
-
- // print children
- ((BaseAST)node.getFirstChild()).xmlSerialize(out);
-
- // print end tag
- ((BaseAST)node).xmlSerializeRootClose(out);
- }
- }
+ throws IOException {
+ out.write("</" + getClass().getName() + ">\n");
+ }
+
+ public void xmlSerialize(Writer out) throws IOException {
+ // print out this node and all siblings
+ for (AST node = this;
+ node != null;
+ node = node.getNextSibling()) {
+ if (node.getFirstChild() == null) {
+ // print guts (class name, attributes)
+ ((BaseAST)node).xmlSerializeNode(out);
+ } else {
+ ((BaseAST)node).xmlSerializeRootOpen(out);
+
+ // print children
+ ((BaseAST)node.getFirstChild()).xmlSerialize(out);
+
+ // print end tag
+ ((BaseAST)node).xmlSerializeRootClose(out);
+ }
+ }
}
}
1.2 +17 -12 e/src/jsrc/antlr/BlockContext.java
Index: BlockContext.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/BlockContext.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- BlockContext.java 2001/10/03 21:04:13 1.1
+++ BlockContext.java 2001/10/04 01:09:14 1.2
@@ -4,7 +4,7 @@
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
- * $Id: BlockContext.java,v 1.1 2001/10/03 21:04:13 markm Exp $
+ * $Id: BlockContext.java,v 1.2 2001/10/04 01:09:14 markm Exp $
*/
/**BlockContext stores the information needed when creating an
@@ -13,18 +13,23 @@
* requires state such as "tail of current alternative."
*/
class BlockContext {
- AlternativeBlock block; // current block of alternatives
- int altNum; // which alt are we accepting 0..n-1
- BlockEndElement blockEnd; // used if nested
+ AlternativeBlock block; // current block of alternatives
- public void addAlternativeElement(AlternativeElement e) {
- currentAlt().addElement(e);
- }
- public Alternative currentAlt() {
- return (Alternative)block.alternatives.elementAt(altNum);
- }
- public AlternativeElement currentElement() {
- return currentAlt().tail;
- }
+ int altNum; // which alt are we accepting 0..n-1
+
+ BlockEndElement blockEnd; // used if nested
+
+
+ public void addAlternativeElement(AlternativeElement e) {
+ currentAlt().addElement(e);
+ }
+
+ public Alternative currentAlt() {
+ return (Alternative)block.alternatives.elementAt(altNum);
+ }
+
+ public AlternativeElement currentElement() {
+ return currentAlt().tail;
+ }
}
1.2 +17 -13 e/src/jsrc/antlr/BlockEndElement.java
Index: BlockEndElement.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/BlockEndElement.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- BlockEndElement.java 2001/10/03 21:04:13 1.1
+++ BlockEndElement.java 2001/10/04 01:09:14 1.2
@@ -4,26 +4,30 @@
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
- * $Id: BlockEndElement.java,v 1.1 2001/10/03 21:04:13 markm Exp $
+ * $Id: BlockEndElement.java,v 1.2 2001/10/04 01:09:14 markm Exp $
*/
/**All alternative blocks are "terminated" by BlockEndElements unless
* they are rule blocks (in which case they use RuleEndElement).
*/
class BlockEndElement extends AlternativeElement {
- protected boolean[] lock; // for analysis; used to avoid infinite loops
- protected AlternativeBlock block;// ending blocks know what block they terminate
+ protected boolean[] lock; // for analysis; used to avoid infinite loops
- public BlockEndElement(Grammar g) {
- super(g);
- lock = new boolean[g.maxk+1];
- }
- public Lookahead look(int k) {
- return grammar.theLLkAnalyzer.look(k, this);
- }
- public String toString() {
- //return " [BlkEnd]";
- return "";
- }
+ protected AlternativeBlock block;// ending blocks know what block they terminate
+
+
+ public BlockEndElement(Grammar g) {
+ super(g);
+ lock = new boolean[g.maxk + 1];
+ }
+
+ public Lookahead look(int k) {
+ return grammar.theLLkAnalyzer.look(k, this);
+ }
+
+ public String toString() {
+ //return " [BlkEnd]";
+ return "";
+ }
}
1.2 +14 -11 e/src/jsrc/antlr/BlockWithImpliedExitPath.java
Index: BlockWithImpliedExitPath.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/BlockWithImpliedExitPath.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- BlockWithImpliedExitPath.java 2001/10/03 21:04:13 1.1
+++ BlockWithImpliedExitPath.java 2001/10/04 01:09:14 1.2
@@ -4,21 +4,24 @@
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
- * $Id: BlockWithImpliedExitPath.java,v 1.1 2001/10/03 21:04:13 markm Exp $
+ * $Id: BlockWithImpliedExitPath.java,v 1.2 2001/10/04 01:09:14 markm Exp $
*/
abstract class BlockWithImpliedExitPath extends AlternativeBlock {
- protected int exitLookaheadDepth; // lookahead needed to handle optional path
- /** lookahead to bypass block; set
- * by deterministic(). 1..k of Lookahead
- */
- protected Lookahead[] exitCache = new Lookahead[grammar.maxk+1];
+ protected int exitLookaheadDepth; // lookahead needed to handle optional path
- public BlockWithImpliedExitPath(Grammar g) {
- super(g);
- }
-public BlockWithImpliedExitPath(Grammar g, int line) {
- super(g, line, false);
-}
+ /** lookahead to bypass block; set
+ * by deterministic(). 1..k of Lookahead
+ */
+ protected Lookahead[] exitCache = new Lookahead[grammar.maxk + 1];
+
+
+ public BlockWithImpliedExitPath(Grammar g) {
+ super(g);
+ }
+
+ public BlockWithImpliedExitPath(Grammar g, int line) {
+ super(g, line, false);
+ }
}
1.2 +15 -14 e/src/jsrc/antlr/ByteBuffer.java
Index: ByteBuffer.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/ByteBuffer.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ByteBuffer.java 2001/10/03 21:04:13 1.1
+++ ByteBuffer.java 2001/10/04 01:09:14 1.2
@@ -4,7 +4,7 @@
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
- * $Id: ByteBuffer.java,v 1.1 2001/10/03 21:04:13 markm Exp $
+ * $Id: ByteBuffer.java,v 1.2 2001/10/04 01:09:14 markm Exp $
*/
/**A Stream of characters fed to the lexer from a InputStream that can
@@ -20,10 +20,11 @@
* @see antlr.CharQueue
*/
// SAS: added this class to handle Binary input w/ FileInputStream
-import java.io.InputStream;
+
import java.io.IOException;
+import java.io.InputStream;
-public class ByteBuffer extends InputBuffer{
+public class ByteBuffer extends InputBuffer {
// char source
transient InputStream input;
@@ -31,21 +32,21 @@
/** Create a character buffer */
public ByteBuffer(InputStream input_) {
- super();
- input = input_;
+ super();
+ input = input_;
}
/** Ensure that the character buffer is sufficiently full */
public void fill(int amount) throws CharStreamException {
- try {
- syncConsume();
- // Fill the buffer sufficiently to hold needed characters
- while (queue.nbrEntries < amount + markerOffset) {
- // Append the next character
- queue.append((char) input.read());
- }
- } catch (IOException io) {
- throw new CharStreamIOException(io);
- }
+ try {
+ syncConsume();
+ // Fill the buffer sufficiently to hold needed characters
+ while (queue.nbrEntries < amount + markerOffset) {
+ // Append the next character
+ queue.append((char)input.read());
+ }
+ } catch (IOException io) {
+ throw new CharStreamIOException(io);
+ }
}
}
1.2 +16 -14 e/src/jsrc/antlr/CharBuffer.java
Index: CharBuffer.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/CharBuffer.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- CharBuffer.java 2001/10/03 21:04:13 1.1
+++ CharBuffer.java 2001/10/04 01:09:14 1.2
@@ -4,7 +4,7 @@
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
- * $Id: CharBuffer.java,v 1.1 2001/10/03 21:04:13 markm Exp $
+ * $Id: CharBuffer.java,v 1.2 2001/10/04 01:09:14 markm Exp $
*/
/**A Stream of characters fed to the lexer from a InputStream that can
@@ -20,32 +20,34 @@
* @see antlr.CharQueue
*/
-import java.io.Reader; // SAS: changed to properly read text files
import java.io.IOException;
+import java.io.Reader;
// SAS: Move most functionality into InputBuffer -- just the file-specific
// stuff is in here
public class CharBuffer extends InputBuffer {
+
// char source
transient Reader input;
/** Create a character buffer */
- public CharBuffer(Reader input_) { // SAS: for proper text i/o
- super();
- input = input_;
+ public CharBuffer(Reader input_) {
+ // SAS: for proper text i/o
+ super();
+ input = input_;
}
/** Ensure that the character buffer is sufficiently full */
public void fill(int amount) throws CharStreamException {
- try {
- syncConsume();
- // Fill the buffer sufficiently to hold needed characters
- while (queue.nbrEntries < amount + markerOffset) {
- // Append the next character
- queue.append((char) input.read());
- }
- } catch (IOException io) {
- throw new CharStreamIOException(io);
- }
+ try {
+ syncConsume();
+ // Fill the buffer sufficiently to hold needed characters
+ while (queue.nbrEntries < amount + markerOffset) {
+ // Append the next character
+ queue.append((char)input.read());
+ }
+ } catch (IOException io) {
+ throw new CharStreamIOException(io);
+ }
}
}
1.2 +9 -6 e/src/jsrc/antlr/CharFormatter.java
Index: CharFormatter.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/CharFormatter.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- CharFormatter.java 2001/10/03 21:04:13 1.1
+++ CharFormatter.java 2001/10/04 01:09:14 1.2
@@ -4,17 +4,20 @@
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
- * $Id: CharFormatter.java,v 1.1 2001/10/03 21:04:13 markm Exp $
+ * $Id: CharFormatter.java,v 1.2 2001/10/04 01:09:14 markm Exp $
*/
/** Interface used by BitSet to format elements of the set when
- * converting to string
- */
+ * converting to string
+ */
public interface CharFormatter {
+
+ public String escapeChar(int c, boolean forCharLiteral);
+
+ public String escapeString(String s);
+
+ public String literalChar(int c);
- public String escapeChar(int c, boolean forCharLiteral);
- public String escapeString(String s);
- public String literalChar(int c);
- public String literalString(String s);
+ public String literalString(String s);
}
1.2 +16 -14 e/src/jsrc/antlr/CharLiteralElement.java
Index: CharLiteralElement.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/CharLiteralElement.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- CharLiteralElement.java 2001/10/03 21:04:13 1.1
+++ CharLiteralElement.java 2001/10/04 01:09:14 1.2
@@ -4,24 +4,26 @@
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
- * $Id: CharLiteralElement.java,v 1.1 2001/10/03 21:04:13 markm Exp $
+ * $Id: CharLiteralElement.java,v 1.2 2001/10/04 01:09:14 markm Exp $
*/
class CharLiteralElement extends GrammarAtom {
- public CharLiteralElement(LexerGrammar g, Token t, boolean inverted, int autoGenType) {
- super(g, t, AUTO_GEN_NONE);
- tokenType = ANTLRLexer.tokenTypeForCharLiteral(t.getText());
- g.charVocabulary.add(tokenType);
- line = t.getLine();
- not = inverted;
- this.autoGenType = autoGenType;
- }
- public void generate() {
- grammar.generator.gen(this);
- }
- public Lookahead look(int k) {
- return grammar.theLLkAnalyzer.look(k, this);
- }
+ public CharLiteralElement(LexerGrammar g, Token t, boolean inverted, int autoGenType) {
+ super(g, t, AUTO_GEN_NONE);
+ tokenType = ANTLRLexer.tokenTypeForCharLiteral(t.getText());
+ g.charVocabulary.add(tokenType);
+ line = t.getLine();
+ not = inverted;
+ this.autoGenType = autoGenType;
+ }
+
+ public void generate() {
+ grammar.generator.gen(this);
+ }
+
+ public Lookahead look(int k) {
+ return grammar.theLLkAnalyzer.look(k, this);
+ }
}
1.2 +70 -66 e/src/jsrc/antlr/CharQueue.java
Index: CharQueue.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/CharQueue.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- CharQueue.java 2001/10/03 21:04:13 1.1
+++ CharQueue.java 2001/10/04 01:09:14 1.2
@@ -4,75 +4,79 @@
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
- * $Id: CharQueue.java,v 1.1 2001/10/03 21:04:13 markm Exp $
+ * $Id: CharQueue.java,v 1.2 2001/10/04 01:09:14 markm Exp $
*/
/** A circular buffer object used by CharBuffer */
public class CharQueue {
- // Physical circular buffer of characters
- protected char[] buffer;
- // buffer.length-1 for quick modulous
- protected int sizeLessOne;
- // physical index of front token
- protected int offset;
- // number of characters in the queue
- protected int nbrEntries;
-
-
- public CharQueue(int minSize) {
- // Find first power of 2 >= to requested size
- int size;
- for (size = 2; size < minSize; size *= 2) {;}
- init(size);
- }
- /** Add token to end of the queue
- * @param tok The token to add
- */
- public final void append(char tok)
- {
- if (nbrEntries == buffer.length)
- {
- expand();
- }
- buffer[(offset + nbrEntries) & sizeLessOne] = tok;
- nbrEntries++;
- }
- /** Fetch a token from the queue by index
- * @param idx The index of the token to fetch, where zero is the token at the front of the queue
- */
- public final char elementAt(int idx) {
- return buffer[(offset + idx) & sizeLessOne];
- }
- /** Expand the token buffer by doubling its capacity */
- private final void expand()
- {
- char[] newBuffer = new char[buffer.length * 2];
- // Copy the contents to the new buffer
- // Note that this will store the first logical item in the
- // first physical array element.
- for (int i = 0; i < buffer.length; i++)
- {
- newBuffer[i] = elementAt(i);
- }
- // Re-initialize with new contents, keep old nbrEntries
- buffer = newBuffer;
- sizeLessOne = buffer.length - 1;
- offset = 0;
- }
- /** Initialize the queue.
- * @param size The initial size of the queue
- */
- private final void init(int size) {
- // Allocate buffer
- buffer = new char[size];
- // Other initialization
- sizeLessOne = size - 1;
- offset = 0;
- nbrEntries = 0;
- }
- /** Remove char from front of queue */
- public final void removeFirst() {
- offset = (offset+1) & sizeLessOne;
- nbrEntries--;
- }
+
+ // Physical circular buffer of characters
+ protected char[] buffer;
+ // buffer.length-1 for quick modulous
+ protected int sizeLessOne;
+ // physical index of front token
+ protected int offset;
+ // number of characters in the queue
+ protected int nbrEntries;
+
+
+ public CharQueue(int minSize) {
+ // Find first power of 2 >= to requested size
+ int size;
+ for (size = 2; size < minSize; size *= 2) {
+ ;
+ }
+ init(size);
+ }
+
+ /** Add token to end of the queue
+ * @param tok The token to add
+ */
+ public final void append(char tok) {
+ if (nbrEntries == buffer.length) {
+ expand();
+ }
+ buffer[(offset + nbrEntries) & sizeLessOne] = tok;
+ nbrEntries++;
+ }
+
+ /** Fetch a token from the queue by index
+ * @param idx The index of the token to fetch, where zero is the token at the front of the queue
+ */
+ public final char elementAt(int idx) {
+ return buffer[(offset + idx) & sizeLessOne];
+ }
+
+ /** Expand the token buffer by doubling its capacity */
+ private final void expand() {
+ char[] newBuffer = new char[buffer.length * 2];
+ // Copy the contents to the new buffer
+ // Note that this will store the first logical item in the
+ // first physical array element.
+ for (int i = 0; i < buffer.length; i++) {
+ newBuffer[i] = elementAt(i);
+ }
+ // Re-initialize with new contents, keep old nbrEntries
+ buffer = newBuffer;
+ sizeLessOne = buffer.length - 1;
+ offset = 0;
+ }
+
+ /** Initialize the queue.
+ * @param size The initial size of the queue
+ */
+ private final void init(int size) {
+ // Allocate buffer
+ buffer = new char[size];
+ // Other initialization
+ sizeLessOne = size - 1;
+ offset = 0;
+ nbrEntries = 0;
+ }
+
+ /** Remove char from front of queue */
+ public final void removeFirst() {
+ offset = (offset + 1) & sizeLessOne;
+ nbrEntries--;
+ }
}
1.2 +48 -36 e/src/jsrc/antlr/CharRangeElement.java
Index: CharRangeElement.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/CharRangeElement.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- CharRangeElement.java 2001/10/03 21:04:13 1.1
+++ CharRangeElement.java 2001/10/04 01:09:14 1.2
@@ -4,44 +4,56 @@
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
- * $Id: CharRangeElement.java,v 1.1 2001/10/03 21:04:13 markm Exp $
+ * $Id: CharRangeElement.java,v 1.2 2001/10/04 01:09:14 markm Exp $
*/
class CharRangeElement extends AlternativeElement {
- String label;
- protected char begin=0;
- protected char end =0;
- protected String beginText;
- protected String endText;
-
-
- public CharRangeElement(LexerGrammar g, Token t1, Token t2, int autoGenType) {
- super(g);
- begin = (char)ANTLRLexer.tokenTypeForCharLiteral(t1.getText());
- beginText = t1.getText();
- end = (char)ANTLRLexer.tokenTypeForCharLiteral(t2.getText());
- endText = t2.getText();
- line = t1.getLine();
- // track which characters are referenced in the grammar
- for (int i=begin; i<=end; i++) {
- g.charVocabulary.add(i);
- }
- this.autoGenType = autoGenType;
- }
- public void generate() {
- grammar.generator.gen(this);
- }
- public String getLabel() {
- return label;
- }
- public Lookahead look(int k) {
- return grammar.theLLkAnalyzer.look(k, this);
- }
- public void setLabel(String label_) {
- label = label_;
- }
- public String toString() {
- if ( label!=null ) return " "+label+":"+beginText+".."+endText;
- else return " "+beginText+".."+endText;
- }
+
+ String label;
+
+ protected char begin = 0;
+
+ protected char end = 0;
+
+ protected String beginText;
+
+ protected String endText;
+
+
+ public CharRangeElement(LexerGrammar g, Token t1, Token t2, int autoGenType) {
+ super(g);
+ begin = (char)ANTLRLexer.tokenTypeForCharLiteral(t1.getText());
+ beginText = t1.getText();
+ end = (char)ANTLRLexer.tokenTypeForCharLiteral(t2.getText());
+ endText = t2.getText();
+ line = t1.getLine();
+ // track which characters are referenced in the grammar
+ for (int i = begin; i <= end; i++) {
+ g.charVocabulary.add(i);
+ }
+ this.autoGenType = autoGenType;
+ }
+
+ public void generate() {
+ grammar.generator.gen(this);
+ }
+
+ public String getLabel() {
+ return label;
+ }
+
+ public Lookahead look(int k) {
+ return grammar.theLLkAnalyzer.look(k, this);
+ }
+
+ public void setLabel(String label_) {
+ label = label_;
+ }
+
+ public String toString() {
+ if (label != null)
+ return " " + label + ":" + beginText + ".." + endText;
+ else
+ return " " + beginText + ".." + endText;
+ }
}
1.2 +195 -167 e/src/jsrc/antlr/CharScanner.java
Index: CharScanner.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/CharScanner.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- CharScanner.java 2001/10/03 21:04:13 1.1
+++ CharScanner.java 2001/10/04 01:09:14 1.2
@@ -4,21 +4,29 @@
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
- * $Id: CharScanner.java,v 1.1 2001/10/03 21:04:13 markm Exp $
+ * $Id: CharScanner.java,v 1.2 2001/10/04 01:09:14 markm Exp $
*/
-import java.util.Hashtable;
import antlr.collections.impl.BitSet;
-import java.io.IOException;
+
+import java.util.Hashtable;
+
public abstract class CharScanner implements TokenStream {
+
static final char NO_CHAR = 0;
- public static final char EOF_CHAR = (char) - 1;
+
+ public static final char EOF_CHAR = (char)-1;
+
protected ANTLRStringBuffer text; // text of current token
protected boolean saveConsumedInput = true; // does consume() save characters?
+
protected Class tokenObjectClass; // what kind of tokens to create?
+
protected boolean caseSensitive = true;
+
protected boolean caseSensitiveLiterals = true;
+
protected Hashtable literals; // set by subclass
protected Token _returnToken = null; // used to return tokens w/o using return val.
@@ -33,179 +41,193 @@
* acceptPath=true;
*/
protected boolean commitToPath = false;
- /** Used to keep track of indentdepth for traceIn/Out */
- protected int traceDepth = 0;
+
+ /** Used to keep track of indentdepth for traceIn/Out */
+ protected int traceDepth = 0;
public CharScanner() {
- text = new ANTLRStringBuffer();
- hashString = new ANTLRHashString(this);
- setTokenObjectClass("antlr.CommonToken");
+ text = new ANTLRStringBuffer();
+ hashString = new ANTLRHashString(this);
+ setTokenObjectClass("antlr.CommonToken");
}
- public CharScanner(InputBuffer cb) { // SAS: use generic buffer
- this();
- inputState = new LexerSharedInputState(cb);
+ public CharScanner(InputBuffer cb) {
+ // SAS: use generic buffer
+ this();
+ inputState = new LexerSharedInputState(cb);
}
public CharScanner(LexerSharedInputState sharedState) {
- this();
- inputState = sharedState;
+ this();
+ inputState = sharedState;
}
public void append(char c) {
- if ( saveConsumedInput ) {
- text.append(c);
- }
+ if (saveConsumedInput) {
+ text.append(c);
+ }
}
public void append(String s) {
- if ( saveConsumedInput ) {
- text.append(s);
- }
+ if (saveConsumedInput) {
+ text.append(s);
+ }
}
public void commit() {
- inputState.input.commit();
+ inputState.input.commit();
}
public void consume() throws CharStreamException {
- if (inputState.guessing == 0) {
- char c = LA(1);
- if (caseSensitive) {
- append(c);
- } else {
- // use input.LA(), not LA(), to get original case
- // CharScanner.LA() would toLower it.
- append(inputState.input.LA(1));
- }
- if ( c=='\t' ) {
- tab();
- }
- else {
- inputState.column++;
- }
- }
- inputState.input.consume();
+ if (inputState.guessing == 0) {
+ char c = LA(1);
+ if (caseSensitive) {
+ append(c);
+ } else {
+ // use input.LA(), not LA(), to get original case
+ // CharScanner.LA() would toLower it.
+ append(inputState.input.LA(1));
+ }
+ if (c == '\t') {
+ tab();
+ } else {
+ inputState.column++;
+ }
+ }
+ inputState.input.consume();
}
/** Consume chars until one matches the given char */
public void consumeUntil(int c) throws CharStreamException {
- while (LA(1) != EOF_CHAR && LA(1) != c)
- {
- consume();
- }
+ while (LA(1) != EOF_CHAR && LA(1) != c) {
+ consume();
+ }
}
/** Consume chars until one matches the given set */
public void consumeUntil(BitSet set) throws CharStreamException {
- while (LA(1) != EOF_CHAR && !set.member(LA(1))) {
- consume();
- }
+ while (LA(1) != EOF_CHAR && !set.member(LA(1))) {
+ consume();
+ }
}
- public boolean getCaseSensitive() { return caseSensitive; }
+ public boolean getCaseSensitive() {
+ return caseSensitive;
+ }
- public final boolean getCaseSensitiveLiterals() { return caseSensitiveLiterals; }
+ public final boolean getCaseSensitiveLiterals() {
+ return caseSensitiveLiterals;
+ }
public int getColumn() {
- return inputState.column;
+ return inputState.column;
}
public void setColumn(int c) {
- inputState.column = c;
+ inputState.column = c;
}
- public boolean getCommitToPath() { return commitToPath; }
+ public boolean getCommitToPath() {
+ return commitToPath;
+ }
- public String getFilename() {return inputState.filename;}
+ public String getFilename() {
+ return inputState.filename;
+ }
public InputBuffer getInputBuffer() {
- return inputState.input;
+ return inputState.input;
}
public LexerSharedInputState getInputState() {
- return inputState;
+ return inputState;
}
public void setInputState(LexerSharedInputState state) {
- inputState = state;
+ inputState = state;
}
- public int getLine() { return inputState.line; }
+ public int getLine() {
+ return inputState.line;
+ }
/** return a copy of the current text buffer */
public String getText() {
- return text.toString();
+ return text.toString();
}
public Token getTokenObject() {
- return _returnToken;
+ return _returnToken;
}
public char LA(int i) throws CharStreamException {
- if (caseSensitive) {
- return inputState.input.LA(i);
- } else {
- return toLower(inputState.input.LA(i));
- }
+ if (caseSensitive) {
+ return inputState.input.LA(i);
+ } else {
+ return toLower(inputState.input.LA(i));
+ }
}
protected Token makeToken(int t) {
- try {
- Token tok = (Token)tokenObjectClass.newInstance();
- tok.setType(t);
- tok.setColumn(inputState.tokenStartColumn);
- tok.setLine(inputState.tokenStartLine);
- // tracking real start line now: tok.setLine(inputState.line);
- return tok;
- }
- catch (InstantiationException ie) {
- panic("can't instantiate token: "+tokenObjectClass);
- }
- catch (IllegalAccessException iae) {
- panic("Token class is not accessible"+tokenObjectClass);
- }
- return Token.badToken;
+ try {
+ Token tok = (Token)tokenObjectClass.newInstance();
+ tok.setType(t);
+ tok.setColumn(inputState.tokenStartColumn);
+ tok.setLine(inputState.tokenStartLine);
+ // tracking real start line now: tok.setLine(inputState.line);
+ return tok;
+ } catch (InstantiationException ie) {
+ panic("can't instantiate token: " + tokenObjectClass);
+ } catch (IllegalAccessException iae) {
+ panic("Token class is not accessible" + tokenObjectClass);
+ }
+ return Token.badToken;
}
+
public int mark() {
- return inputState.input.mark();
+ return inputState.input.mark();
}
+
public void match(char c) throws MismatchedCharException, CharStreamException {
- if ( LA(1) != c ) {
- throw new MismatchedCharException(LA(1), c, false, this);
- }
- consume();
+ if (LA(1) != c) {
+ throw new MismatchedCharException(LA(1), c, false, this);
+ }
+ consume();
}
+
public void match(BitSet b) throws MismatchedCharException, CharStreamException {
- if ( !b.member(LA(1)) ) {
- throw new MismatchedCharException(LA(1), b, false, this);
- } else {
- consume();
- }
+ if (!b.member(LA(1))) {
+ throw new MismatchedCharException(LA(1), b, false, this);
+ } else {
+ consume();
+ }
}
+
public void match(String s) throws MismatchedCharException, CharStreamException {
- int len = s.length();
- for (int i=0; i<len; i++) {
- if ( LA(1) != s.charAt(i) ) {
- throw new MismatchedCharException(LA(1), s.charAt(i), false, this);
- }
- consume();
- }
+ int len = s.length();
+ for (int i = 0; i < len; i++) {
+ if (LA(1) != s.charAt(i)) {
+ throw new MismatchedCharException(LA(1), s.charAt(i), false, this);
+ }
+ consume();
+ }
}
+
public void matchNot(char c) throws MismatchedCharException, CharStreamException {
- if ( LA(1) == c ) {
- throw new MismatchedCharException(LA(1), c, true, this);
- }
- consume();
+ if (LA(1) == c) {
+ throw new MismatchedCharException(LA(1), c, true, this);
+ }
+ consume();
}
+
public void matchRange(char c1, char c2) throws MismatchedCharException, CharStreamException {
- if ( LA(1) < c1 || LA(1) > c2 ) throw new MismatchedCharException(LA(1), c1, c2, false, this);
- consume();
+ if (LA(1) < c1 || LA(1) > c2) throw new MismatchedCharException(LA(1), c1, c2, false, this);
+ consume();
}
public void newline() {
- inputState.line++;
- inputState.column = 1;
+ inputState.line++;
+ inputState.column = 1;
}
/** advance the current column number by an appropriate amount.
@@ -214,89 +236,94 @@
* called from consume().
*/
public void tab() {
- // update inputState.column as function of
- // inputState.column and tab stops.
- // For example, if tab stops are columns 1 and 5 etc...
- // and column is 3, then add 2 to column.
- inputState.column++;
+ // update inputState.column as function of
+ // inputState.column and tab stops.
+ // For example, if tab stops are columns 1 and 5 etc...
+ // and column is 3, then add 2 to column.
+ inputState.column++;
}
public void panic() {
- System.err.println("CharScanner: panic");
- System.exit(1);
+ System.err.println("CharScanner: panic");
+ System.exit(1);
}
public void panic(String s) {
- System.err.println("CharScanner; panic: "+s);
- System.exit(1);
+ System.err.println("CharScanner; panic: " + s);
+ System.exit(1);
}
/** Parser error-reporting function can be overridden in subclass */
public void reportError(RecognitionException ex) {
- System.err.println(ex);
+ System.err.println(ex);
}
/** Parser error-reporting function can be overridden in subclass */
public void reportError(String s) {
- if ( getFilename()==null ) {
- System.err.println("error: " + s);
- }
- else {
- System.err.println(getFilename()+": error: " + s);
- }
+ if (getFilename() == null) {
+ System.err.println("error: " + s);
+ } else {
+ System.err.println(getFilename() + ": error: " + s);
+ }
}
/** Parser warning-reporting function can be overridden in subclass */
public void reportWarning(String s) {
- if ( getFilename()==null ) {
- System.err.println("warning: "+s);
- }
- else {
- System.err.println(getFilename()+": warning: " + s);
- }
+ if (getFilename() == null) {
+ System.err.println("warning: " + s);
+ } else {
+ System.err.println(getFilename() + ": warning: " + s);
+ }
}
public void resetText() {
- text.setLength(0);
- inputState.tokenStartColumn = inputState.column;
- inputState.tokenStartLine = inputState.line;
+ text.setLength(0);
+ inputState.tokenStartColumn = inputState.column;
+ inputState.tokenStartLine = inputState.line;
}
public void rewind(int pos) {
- inputState.input.rewind(pos);
+ inputState.input.rewind(pos);
}
- public void setCaseSensitive(boolean t) { caseSensitive = t; }
+ public void setCaseSensitive(boolean t) {
+ caseSensitive = t;
+ }
- public void setCommitToPath(boolean commit) { commitToPath = commit; }
+ public void setCommitToPath(boolean commit) {
+ commitToPath = commit;
+ }
- public void setFilename(String f) {inputState.filename=f;}
+ public void setFilename(String f) {
+ inputState.filename = f;
+ }
- public void setLine(int line) { inputState.line = line; }
+ public void setLine(int line) {
+ inputState.line = line;
+ }
public void setText(String s) {
- resetText();
- text.append(s);
+ resetText();
+ text.append(s);
}
public void setTokenObjectClass(String cl) {
- try {
- tokenObjectClass = Class.forName(cl);
- }
- catch (ClassNotFoundException ce) {
- panic("ClassNotFoundException: "+cl);
- }
+ try {
+ tokenObjectClass = Class.forName(cl);
+ } catch (ClassNotFoundException ce) {
+ panic("ClassNotFoundException: " + cl);
+ }
}
// Test the token text against the literals table
// Override this method to perform a different literals test
public int testLiteralsTable(int ttype) {
- hashString.setBuffer(text.getBuffer(), text.length());
- Integer literalsIndex = (Integer)literals.get(hashString);
- if (literalsIndex != null) {
- ttype = literalsIndex.intValue();
- }
- return ttype;
+ hashString.setBuffer(text.getBuffer(), text.length());
+ Integer literalsIndex = (Integer)literals.get(hashString);
+ if (literalsIndex != null) {
+ ttype = literalsIndex.intValue();
+ }
+ return ttype;
}
/** Test the text passed in against the literals table
@@ -305,34 +332,35 @@
* a token.
*/
public int testLiteralsTable(String text, int ttype) {
- ANTLRHashString s = new ANTLRHashString(text, this);
- Integer literalsIndex = (Integer)literals.get(s);
- if (literalsIndex != null) {
- ttype = literalsIndex.intValue();
- }
- return ttype;
+ ANTLRHashString s = new ANTLRHashString(text, this);
+ Integer literalsIndex = (Integer)literals.get(s);
+ if (literalsIndex != null) {
+ ttype = literalsIndex.intValue();
+ }
+ return ttype;
}
// Override this method to get more specific case handling
public char toLower(char c) {
- return Character.toLowerCase(c);
+ return Character.toLowerCase(c);
+ }
+
+ public void traceIndent() {
+ for (int i = 0; i < traceDepth; i++)
+ System.out.print(" ");
+ }
+
+ public void traceIn(String rname) throws CharStreamException {
+ traceDepth += 1;
+ traceIndent();
+ System.out.println("> lexer " + rname + "; c==" + LA(1));
}
- public void traceIndent() {
- for( int i = 0; i < traceDepth; i++ )
- System.out.print(" ");
- }
- public void traceIn(String rname) throws CharStreamException {
- traceDepth += 1;
- traceIndent();
- System.out.println("> lexer "+rname+"; c==" + LA(1));
- }
-
- public void traceOut(String rname) throws CharStreamException {
- traceIndent();
- System.out.println("< lexer "+rname+"; c==" + LA(1));
- traceDepth -= 1;
- }
+ public void traceOut(String rname) throws CharStreamException {
+ traceIndent();
+ System.out.println("< lexer " + rname + "; c==" + LA(1));
+ traceDepth -= 1;
+ }
/** This method is called by YourLexer.nextToken() when the lexer has
* hit EOF condition. EOF is NOT a character.
1.2 +2 -1 e/src/jsrc/antlr/CharStreamException.java
Index: CharStreamException.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/CharStreamException.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- CharStreamException.java 2001/10/03 21:04:13 1.1
+++ CharStreamException.java 2001/10/04 01:09:14 1.2
@@ -4,18 +4,19 @@
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
- * $Id: CharStreamException.java,v 1.1 2001/10/03 21:04:13 markm Exp $
+ * $Id: CharStreamException.java,v 1.2 2001/10/04 01:09:14 markm Exp $
*/
/**
* Anything that goes wrong while generating a stream of characters
*/
public class CharStreamException extends ANTLRException {
+
/**
* CharStreamException constructor comment.
* @param s java.lang.String
*/
public CharStreamException(String s) {
- super(s);
+ super(s);
}
}
1.2 +3 -2 e/src/jsrc/antlr/CharStreamIOException.java
Index: CharStreamIOException.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/CharStreamIOException.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- CharStreamIOException.java 2001/10/03 21:04:13 1.1
+++ CharStreamIOException.java 2001/10/04 01:09:14 1.2
@@ -4,7 +4,7 @@
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
- * $Id: CharStreamIOException.java,v 1.1 2001/10/03 21:04:13 markm Exp $
+ * $Id: CharStreamIOException.java,v 1.2 2001/10/04 01:09:14 markm Exp $
*/
import java.io.IOException;
@@ -13,10 +13,11 @@
* Wrap an IOException in a CharStreamException
*/
public class CharStreamIOException extends CharStreamException {
+
public IOException io;
public CharStreamIOException(IOException io) {
- super(io.getMessage());
- this.io = io;
+ super(io.getMessage());
+ this.io = io;
}
}
1.2 +308 -314 e/src/jsrc/antlr/CodeGenerator.java
Index: CodeGenerator.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/CodeGenerator.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- CodeGenerator.java 2001/10/03 21:04:13 1.1
+++ CodeGenerator.java 2001/10/04 01:09:14 1.2
@@ -4,15 +4,15 @@
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
- * $Id: CodeGenerator.java,v 1.1 2001/10/03 21:04:13 markm Exp $
+ * $Id: CodeGenerator.java,v 1.2 2001/10/04 01:09:14 markm Exp $
*/
-import java.io.PrintWriter; // SAS: for proper text i/o
-import java.io.IOException;
-import java.io.FileWriter; // SAS: for proper text i/o
-import antlr.collections.impl.Vector;
import antlr.collections.impl.BitSet;
+import antlr.collections.impl.Vector;
+import java.io.IOException;
+import java.io.PrintWriter;
+
/**A generic ANTLR code generator. All code generators
* Derive from this class.
*
@@ -47,20 +47,28 @@
* @see antlr.Lookahead
*/
public abstract class CodeGenerator {
+
/** Current tab indentation for code output */
- protected int tabs=0;
+ protected int tabs = 0;
+
/** Current output Stream */
transient protected PrintWriter currentOutput; // SAS: for proper text i/o
+
/** The grammar for which we generate code */
protected Grammar grammar = null;
+
/** List of all bitsets that must be dumped. These are Vectors of BitSet. */
protected Vector bitsetsUsed;
+
/** The antlr Tool */
protected Tool tool;
+
/** The grammar behavior */
protected DefineGrammarSymbols behavior;
+
/** The LLk analyzer */
protected LLkGrammarAnalyzer analyzer;
+
/** Object used to format characters in the target language.
* subclass must initialize this to the language-specific formatter
*/
@@ -71,6 +79,7 @@
/** Default values for code-generation thresholds */
protected static final int DEFAULT_MAKE_SWITCH_THRESHOLD = 2;
+
protected static final int DEFAULT_BITSET_TEST_THRESHOLD = 4;
/** This is a hint for the language-specific code generator.
@@ -88,23 +97,25 @@
* This is modified by the grammar option "codeGenBitsetTestThreshold"
*/
protected int bitsetTestThreshold = DEFAULT_BITSET_TEST_THRESHOLD;
-
+
private static boolean OLD_ACTION_TRANSLATOR = true;
public static String TokenTypesFileSuffix = "TokenTypes";
+
public static String TokenTypesFileExt = ".txt";
/** Construct code generator base class */
- public CodeGenerator() {}
+ public CodeGenerator() {
+ }
/** Output a String to the currentOutput stream.
* Ignored if string is null.
* @param s The string to output
*/
protected void _print(String s) {
- if (s != null) {
- currentOutput.print(s);
- }
+ if (s != null) {
+ currentOutput.print(s);
+ }
}
/** Print an action without leading tabs, attempting to
@@ -113,56 +124,52 @@
* @param s The action string to output
*/
protected void _printAction(String s) {
- if (s == null) {
- return;
- }
-
- // Skip leading newlines, tabs and spaces
- int start = 0;
- while (start < s.length() && Character.isSpaceChar(s.charAt(start)) )
- {
- start++;
- }
-
- // Skip leading newlines, tabs and spaces
- int end = s.length()-1;
- while ( end > start && Character.isSpaceChar(s.charAt(end)) )
- {
- end--;
- }
-
- char c=0;
- for (int i = start; i <= end;)
- {
- c = s.charAt(i);
- i++;
- boolean newline = false;
- switch (c)
- {
- case '\n':
- newline=true;
- break;
- case '\r':
- if ( i<=end && s.charAt(i)=='\n' ) {
- i++;
- }
- newline=true;
- break;
- default:
- currentOutput.print(c);
- break;
- }
- if ( newline ) {
- currentOutput.println();
- printTabs();
- // Absorb leading whitespace
- while (i <= end && Character.isSpaceChar(s.charAt(i)) ) {
- i++;
- }
- newline=false;
- }
- }
- currentOutput.println();
+ if (s == null) {
+ return;
+ }
+
+ // Skip leading newlines, tabs and spaces
+ int start = 0;
+ while (start < s.length() && Character.isSpaceChar(s.charAt(start))) {
+ start++;
+ }
+
+ // Skip leading newlines, tabs and spaces
+ int end = s.length() - 1;
+ while (end > start && Character.isSpaceChar(s.charAt(end))) {
+ end--;
+ }
+
+ char c = 0;
+ for (int i = start; i <= end;) {
+ c = s.charAt(i);
+ i++;
+ boolean newline = false;
+ switch (c) {
+ case '\n':
+ newline = true;
+ break;
+ case '\r':
+ if (i <= end && s.charAt(i) == '\n') {
+ i++;
+ }
+ newline = true;
+ break;
+ default:
+ currentOutput.print(c);
+ break;
+ }
+ if (newline) {
+ currentOutput.println();
+ printTabs();
+ // Absorb leading whitespace
+ while (i <= end && Character.isSpaceChar(s.charAt(i))) {
+ i++;
+ }
+ newline = false;
+ }
+ }
+ currentOutput.println();
}
/** Output a String followed by newline, to the currentOutput stream.
@@ -170,9 +177,9 @@
* @param s The string to output
*/
protected void _println(String s) {
- if (s != null) {
- currentOutput.println(s);
- }
+ if (s != null) {
+ currentOutput.println(s);
+ }
}
/** Test if a set element array represents a contiguous range.
@@ -180,28 +187,28 @@
* @return true if the elements are a contiguous range (with two or more).
*/
public static boolean elementsAreRange(int[] elems) {
- if (elems.length==0) {
- return false;
- }
- int begin = elems[0];
- int end = elems[elems.length-1];
- if ( elems.length<=2 ) {
- // Not enough elements for a range expression
- return false;
- }
- if ( end-begin+1 > elems.length ) {
- // The set does not represent a contiguous range
- return false;
- }
- int v = begin+1;
- for (int i=1; i<elems.length-1; i++) {
- if ( v != elems[i] ) {
- // The set does not represent a contiguous range
- return false;
- }
- v++;
- }
- return true;
+ if (elems.length == 0) {
+ return false;
+ }
+ int begin = elems[0];
+ int end = elems[elems.length - 1];
+ if (elems.length <= 2) {
+ // Not enough elements for a range expression
+ return false;
+ }
+ if (end - begin + 1 > elems.length) {
+ // The set does not represent a contiguous range
+ return false;
+ }
+ int v = begin + 1;
+ for (int i = 1; i < elems.length - 1; i++) {
+ if (v != elems[i]) {
+ // The set does not represent a contiguous range
+ return false;
+ }
+ v++;
+ }
+ return true;
}
/** Get the identifier portion of an argument-action token.
@@ -212,7 +219,7 @@
* @return A string containing the text of the identifier
*/
protected String extractIdOfAction(Token t) {
- return extractIdOfAction(t.getText(), t.getLine());
+ return extractIdOfAction(t.getText(), t.getLine());
}
/** Get the identifier portion of an argument-action.
@@ -224,22 +231,20 @@
* @return A string containing the text of the identifier
*/
protected String extractIdOfAction(String s, int line) {
- s = removeAssignmentFromDeclaration(s);
- // Search back from the end for a non alphanumeric. That marks the
- // beginning of the identifier
- for (int i = s.length()-2; i >=0; i--)
- {
- // TODO: make this work for language-independent identifiers?
- if (!Character.isLetterOrDigit(s.charAt(i)) && s.charAt(i) != '_')
- {
- // Found end of type part
- return s.substring(i+1);
- }
- }
- // Something is bogus, but we cannot parse the language-specific
- // actions any better. The compiler will have to catch the problem.
- tool.warning("Ill-formed action", grammar.getFilename(), line);
- return "";
+ s = removeAssignmentFromDeclaration(s);
+ // Search back from the end for a non alphanumeric. That marks the
+ // beginning of the identifier
+ for (int i = s.length() - 2; i >= 0; i--) {
+ // TODO: make this work for language-independent identifiers?
+ if (!Character.isLetterOrDigit(s.charAt(i)) && s.charAt(i) != '_') {
+ // Found end of type part
+ return s.substring(i + 1);
+ }
+ }
+ // Something is bogus, but we cannot parse the language-specific
+ // actions any better. The compiler will have to catch the problem.
+ tool.warning("Ill-formed action", grammar.getFilename(), line);
+ return "";
}
/** Get the type string out of an argument-action token.
@@ -250,7 +255,7 @@
* @return A string containing the text of the type
*/
protected String extractTypeOfAction(Token t) {
- return extractTypeOfAction(t.getText(), t.getLine());
+ return extractTypeOfAction(t.getText(), t.getLine());
}
/** Get the type portion of an argument-action.
@@ -262,22 +267,20 @@
* @return A string containing the text of the type
*/
protected String extractTypeOfAction(String s, int line) {
- s = removeAssignmentFromDeclaration(s);
- // Search back from the end for a non alphanumeric. That marks the
- // beginning of the identifier
- for (int i = s.length()-2; i >=0; i--)
- {
- // TODO: make this work for language-independent identifiers?
- if (!Character.isLetterOrDigit(s.charAt(i)) && s.charAt(i) != '_')
- {
- // Found end of type part
- return s.substring(0,i+1);
- }
- }
- // Something is bogus, but we cannot parse the language-specific
- // actions any better. The compiler will have to catch the problem.
- tool.warning("Ill-formed action", grammar.getFilename(), line);
- return "";
+ s = removeAssignmentFromDeclaration(s);
+ // Search back from the end for a non alphanumeric. That marks the
+ // beginning of the identifier
+ for (int i = s.length() - 2; i >= 0; i--) {
+ // TODO: make this work for language-independent identifiers?
+ if (!Character.isLetterOrDigit(s.charAt(i)) && s.charAt(i) != '_') {
+ // Found end of type part
+ return s.substring(0, i + 1);
+ }
+ }
+ // Something is bogus, but we cannot parse the language-specific
+ // actions any better. The compiler will have to catch the problem.
+ tool.warning("Ill-formed action", grammar.getFilename(), line);
+ return "";
}
/** Generate the code for all grammars
@@ -362,68 +365,66 @@
/** Generate the token types as a text file for persistence across shared lexer/parser */
protected void genTokenInterchange(TokenManager tm) throws IOException {
- // Open the token output Java file and set the currentOutput stream
- String fName = tm.getName() + TokenTypesFileSuffix+TokenTypesFileExt;
- currentOutput = antlr.Tool.openOutputFile(fName);
-
- println("// $ANTLR "+Tool.version+": "+
- Tool.fileMinusPath(tool.grammarFile)+
- " -> "+
- fName+
- "$");
-
- tabs = 0;
-
- // Header
- println(tm.getName() + " // output token vocab name");
-
- // Generate a definition for each token type
- Vector v = tm.getVocabulary();
- for (int i = Token.MIN_USER_TYPE; i < v.size(); i++) {
- String s = (String)v.elementAt(i);
- if ( DEBUG_CODE_GENERATOR ) {
- System.out.println("gen persistence file entry for: "+s);
- }
- if (s != null && !s.startsWith("<") ) {
- // if literal, find label
- if ( s.startsWith("\"") ) {
- StringLiteralSymbol sl = (StringLiteralSymbol)tm.getTokenSymbol(s);
- if ( sl!=null && sl.label != null ) {
- print(sl.label+"=");
- }
- println(s + "=" + i);
- }
- else {
- print(s);
- // check for a paraphrase
- TokenSymbol ts = (TokenSymbol)tm.getTokenSymbol(s);
- if ( ts==null ) {
- tool.warning("undefined token symbol: "+s);
- }
- else {
- if ( ts.getParaphrase()!=null ) {
- print("("+ts.getParaphrase()+")");
- }
- }
- println("=" + i);
- }
- }
- }
-
- // Close the tokens output file
- currentOutput.close();
- currentOutput = null;
- }
-
- /** Process a string for an simple expression for use in xx/action.g
- * it is used to cast simple tokens/references to the right type for
- * the generated language.
- * @param str A String.
- */
- public String processStringForASTConstructor( String str )
- {
- return str;
- }
+ // Open the token output Java file and set the currentOutput stream
+ String fName = tm.getName() + TokenTypesFileSuffix + TokenTypesFileExt;
+ currentOutput = antlr.Tool.openOutputFile(fName);
+
+ println("// $ANTLR " + Tool.version + ": " +
+ Tool.fileMinusPath(tool.grammarFile) +
+ " -> " +
+ fName +
+ "$");
+
+ tabs = 0;
+
+ // Header
+ println(tm.getName() + " // output token vocab name");
+
+ // Generate a definition for each token type
+ Vector v = tm.getVocabulary();
+ for (int i = Token.MIN_USER_TYPE; i < v.size(); i++) {
+ String s = (String)v.elementAt(i);
+ if (DEBUG_CODE_GENERATOR) {
+ System.out.println("gen persistence file entry for: " + s);
+ }
+ if (s != null && !s.startsWith("<")) {
+ // if literal, find label
+ if (s.startsWith("\"")) {
+ StringLiteralSymbol sl = (StringLiteralSymbol)tm.getTokenSymbol(s);
+ if (sl != null && sl.label != null) {
+ print(sl.label + "=");
+ }
+ println(s + "=" + i);
+ } else {
+ print(s);
+ // check for a paraphrase
+ TokenSymbol ts = (TokenSymbol)tm.getTokenSymbol(s);
+ if (ts == null) {
+ tool.warning("undefined token symbol: " + s);
+ } else {
+ if (ts.getParaphrase() != null) {
+ print("(" + ts.getParaphrase() + ")");
+ }
+ }
+ println("=" + i);
+ }
+ }
+ }
+
+ // Close the tokens output file
+ currentOutput.close();
+ currentOutput = null;
+ }
+
+ /** Process a string for an simple expression for use in xx/action.g
+ * it is used to cast simple tokens/references to the right type for
+ * the generated language.
+ * @param str A String.
+ */
+ public String processStringForASTConstructor(String str) {
+ return str;
+ }
+
/** Get a string for an expression to generate creation of an AST subtree.
* @param v A Vector of String, where each element is an expression in the target language yielding an AST node.
*/
@@ -439,13 +440,12 @@
* if the language does not allow '_' or numerals in identifiers.
* @param index The index of the bitset in the bitset list.
*/
- protected String getBitsetName(int index)
- {
- return "_tokenSet_" + index;
+ protected String getBitsetName(int index) {
+ return "_tokenSet_" + index;
}
public static String lexerRuleName(String id) {
- return "m"+id;
+ return "m" + id;
}
/** Map an identifier to it's corresponding tree-node variable.
@@ -461,38 +461,36 @@
* if the bitset is already in the list, ignore the request.
* Always adds the bitset to the end of the list, so the
* caller can rely on the position of bitsets in the list.
- * The returned position can be used to format the bitset
+ * The returned position can be used to format the bitset
* name, since it is invariant.
* @param p Bit set to mark for code generation
* @param forParser true if the bitset is used for the parser, false for the lexer
* @return The position of the bitset in the list.
*/
protected int markBitsetForGen(BitSet p) {
- // Is the bitset (or an identical one) already marked for gen?
- for (int i = 0; i < bitsetsUsed.size(); i++)
- {
- BitSet set = (BitSet)bitsetsUsed.elementAt(i);
- if (p.equals(set))
- {
- // Use the identical one already stored
- return i;
- }
- }
-
- // Add the new bitset
- bitsetsUsed.appendElement(p.clone());
- return bitsetsUsed.size()-1;
+ // Is the bitset (or an identical one) already marked for gen?
+ for (int i = 0; i < bitsetsUsed.size(); i++) {
+ BitSet set = (BitSet)bitsetsUsed.elementAt(i);
+ if (p.equals(set)) {
+ // Use the identical one already stored
+ return i;
+ }
+ }
+
+ // Add the new bitset
+ bitsetsUsed.appendElement(p.clone());
+ return bitsetsUsed.size() - 1;
}
/** Output tab indent followed by a String, to the currentOutput stream.
* Ignored if string is null.
- * @param s The string to output.
+ * @param s The string to output.
*/
protected void print(String s) {
- if (s != null) {
- printTabs();
- currentOutput.print(s);
- }
+ if (s != null) {
+ printTabs();
+ currentOutput.print(s);
+ }
}
/** Print an action with leading tabs, attempting to
@@ -500,11 +498,11 @@
* Ignored if string is null.
* @param s The action string to output
*/
- protected void printAction(String s) {
- if (s != null) {
- printTabs();
- _printAction(s);
- }
+ protected void printAction(String s) {
+ if (s != null) {
+ printTabs();
+ _printAction(s);
+ }
}
/** Output tab indent followed by a String followed by newline,
@@ -512,60 +510,57 @@
* @param s The string to output
*/
protected void println(String s) {
- if (s != null) {
- printTabs();
- currentOutput.println(s);
- }
+ if (s != null) {
+ printTabs();
+ currentOutput.println(s);
+ }
}
- /** Output the current tab indentation. This outputs the number of tabs
+ /** Output the current tab indentation. This outputs the number of tabs
* indicated by the "tabs" variable to the currentOutput stream.
*/
protected void printTabs() {
- for (int i=1; i<=tabs; i++) {
- currentOutput.print("\t");
- }
+ for (int i = 1; i <= tabs; i++) {
+ currentOutput.print("\t");
+ }
}
/** Lexically process tree-specifiers in the action.
* This will replace #id and #(...) with the appropriate
* function calls and/or variables.
- *
+ *
* This is the default Java action translator, but I have made
- * it work for C++ also.
+ * it work for C++ also.
*/
protected String processActionForTreeSpecifiers(String actionStr, int line, RuleBlock currentRule, ActionTransInfo tInfo) {
- if ( actionStr==null || actionStr.length()==0 ) return null;
- // The action trans info tells us (at the moment) whether an
- // assignment was done to the rule's tree root.
- if (grammar==null) return actionStr;
- if ( (grammar.buildAST && actionStr.indexOf('#') != -1) ||
- grammar instanceof TreeWalkerGrammar ||
- (grammar instanceof LexerGrammar && actionStr.indexOf('$') != -1) ) {
- // Create a lexer to read an action and return the translated version
- antlr.actions.java.ActionLexer lexer = new antlr.actions.java.ActionLexer(actionStr, currentRule, this, tInfo);
- lexer.setLineOffset(line);
- lexer.setTool(tool);
- try {
- lexer.mACTION(true);
- actionStr = lexer.getTokenObject().getText();
- // System.out.println("action translated: "+actionStr);
- // System.out.println("trans info is "+tInfo);
- }
- catch (RecognitionException ex) {
- lexer.reportError(ex);
- return actionStr;
- }
- catch (TokenStreamException tex) {
- antlr.Tool.panic("Error reading action:"+actionStr);
- return actionStr;
- }
- catch (CharStreamException io) {
- antlr.Tool.panic("Error reading action:"+actionStr);
- return actionStr;
- }
- }
- return actionStr;
+ if (actionStr == null || actionStr.length() == 0) return null;
+ // The action trans info tells us (at the moment) whether an
+ // assignment was done to the rule's tree root.
+ if (grammar == null) return actionStr;
+ if ((grammar.buildAST && actionStr.indexOf('#') != -1) ||
+ grammar instanceof TreeWalkerGrammar ||
+ (grammar instanceof LexerGrammar && actionStr.indexOf('$') != -1)) {
+ // Create a lexer to read an action and return the translated version
+ antlr.actions.java.ActionLexer lexer = new antlr.actions.java.ActionLexer(actionStr, currentRule, this, tInfo);
+ lexer.setLineOffset(line);
+ lexer.setTool(tool);
+ try {
+ lexer.mACTION(true);
+ actionStr = lexer.getTokenObject().getText();
+ // System.out.println("action translated: "+actionStr);
+ // System.out.println("trans info is "+tInfo);
+ } catch (RecognitionException ex) {
+ lexer.reportError(ex);
+ return actionStr;
+ } catch (TokenStreamException tex) {
+ antlr.Tool.panic("Error reading action:" + actionStr);
+ return actionStr;
+ } catch (CharStreamException io) {
+ antlr.Tool.panic("Error reading action:" + actionStr);
+ return actionStr;
+ }
+ }
+ return actionStr;
}
/**
@@ -574,85 +569,84 @@
* @return the declaration without any assignment portion
*/
protected String removeAssignmentFromDeclaration(String d) {
- // If d contains an equal sign, then it's a declaration
- // with an initialization. Strip off the initialization part.
- if (d.indexOf('=') >= 0) d = d.substring(0, d.indexOf('=')).trim();
- return d;
- }
+ // If d contains an equal sign, then it's a declaration
+ // with an initialization. Strip off the initialization part.
+ if (d.indexOf('=') >= 0) d = d.substring(0, d.indexOf('=')).trim();
+ return d;
+ }
/** Set all fields back like one just created */
private void reset() {
- tabs = 0;
- // Allocate list of bitsets tagged for code generation
- bitsetsUsed = new Vector();
- currentOutput = null;
- grammar = null;
- DEBUG_CODE_GENERATOR = false;
- makeSwitchThreshold = DEFAULT_MAKE_SWITCH_THRESHOLD;
- bitsetTestThreshold = DEFAULT_BITSET_TEST_THRESHOLD;
+ tabs = 0;
+ // Allocate list of bitsets tagged for code generation
+ bitsetsUsed = new Vector();
+ currentOutput = null;
+ grammar = null;
+ DEBUG_CODE_GENERATOR = false;
+ makeSwitchThreshold = DEFAULT_MAKE_SWITCH_THRESHOLD;
+ bitsetTestThreshold = DEFAULT_BITSET_TEST_THRESHOLD;
}
public static String reverseLexerRuleName(String id) {
- return id.substring(1,id.length());
+ return id.substring(1, id.length());
}
public void setAnalyzer(LLkGrammarAnalyzer analyzer_) {
- analyzer = analyzer_;
+ analyzer = analyzer_;
}
- public void setBehavior(DefineGrammarSymbols behavior_) {
- behavior = behavior_;
+ public void setBehavior(DefineGrammarSymbols behavior_) {
+ behavior = behavior_;
}
/** Set a grammar for the code generator to use */
protected void setGrammar(Grammar g) {
- reset();
- grammar = g;
- // Lookup make-switch threshold in the grammar generic options
- if (grammar.hasOption("codeGenMakeSwitchThreshold")) {
- try {
- makeSwitchThreshold = grammar.getIntegerOption("codeGenMakeSwitchThreshold");
- //System.out.println("setting codeGenMakeSwitchThreshold to " + makeSwitchThreshold);
- } catch (NumberFormatException e) {
- tool.error(
- "option 'codeGenMakeSwitchThreshold' must be an integer",
- grammar.getClassName(),
- grammar.getOption("codeGenMakeSwitchThreshold").getLine()
- );
- }
- }
-
- // Lookup bitset-test threshold in the grammar generic options
- if (grammar.hasOption("codeGenBitsetTestThreshold")) {
- try {
- bitsetTestThreshold = grammar.getIntegerOption("codeGenBitsetTestThreshold");
- //System.out.println("setting codeGenBitsetTestThreshold to " + bitsetTestThreshold);
- } catch (NumberFormatException e) {
- tool.error(
- "option 'codeGenBitsetTestThreshold' must be an integer",
- grammar.getClassName(),
- grammar.getOption("codeGenBitsetTestThreshold").getLine()
- );
- }
- }
-
- // Lookup debug code-gen in the grammar generic options
- if (grammar.hasOption("codeGenDebug")) {
- Token t = grammar.getOption("codeGenDebug");
- if (t.getText().equals("true")) {
- //System.out.println("setting code-generation debug ON");
- DEBUG_CODE_GENERATOR = true;
- }
- else if (t.getText().equals("false")) {
- //System.out.println("setting code-generation debug OFF");
- DEBUG_CODE_GENERATOR = false;
- }
- else {
- tool.error("option 'codeGenDebug' must be true or false", grammar.getClassName(), t.getLine());
- }
- }
+ reset();
+ grammar = g;
+ // Lookup make-switch threshold in the grammar generic options
+ if (grammar.hasOption("codeGenMakeSwitchThreshold")) {
+ try {
+ makeSwitchThreshold = grammar.getIntegerOption("codeGenMakeSwitchThreshold");
+ //System.out.println("setting codeGenMakeSwitchThreshold to " + makeSwitchThreshold);
+ } catch (NumberFormatException e) {
+ tool.error(
+ "option 'codeGenMakeSwitchThreshold' must be an integer",
+ grammar.getClassName(),
+ grammar.getOption("codeGenMakeSwitchThreshold").getLine()
+ );
+ }
+ }
+
+ // Lookup bitset-test threshold in the grammar generic options
+ if (grammar.hasOption("codeGenBitsetTestThreshold")) {
+ try {
+ bitsetTestThreshold = grammar.getIntegerOption("codeGenBitsetTestThreshold");
+ //System.out.println("setting codeGenBitsetTestThreshold to " + bitsetTestThreshold);
+ } catch (NumberFormatException e) {
+ tool.error(
+ "option 'codeGenBitsetTestThreshold' must be an integer",
+ grammar.getClassName(),
+ grammar.getOption("codeGenBitsetTestThreshold").getLine()
+ );
+ }
+ }
+
+ // Lookup debug code-gen in the grammar generic options
+ if (grammar.hasOption("codeGenDebug")) {
+ Token t = grammar.getOption("codeGenDebug");
+ if (t.getText().equals("true")) {
+ //System.out.println("setting code-generation debug ON");
+ DEBUG_CODE_GENERATOR = true;
+ } else if (t.getText().equals("false")) {
+ //System.out.println("setting code-generation debug OFF");
+ DEBUG_CODE_GENERATOR = false;
+ } else {
+ tool.error("option 'codeGenDebug' must be true or false", grammar.getClassName(), t.getLine());
+ }
+ }
}
+
public void setTool(Tool tool_) {
- tool = tool_;
+ tool = tool_;
}
}
1.2 +26 -36 e/src/jsrc/antlr/CommonAST.java
Index: CommonAST.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/CommonAST.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- CommonAST.java 2001/10/03 21:04:13 1.1
+++ CommonAST.java 2001/10/04 01:09:14 1.2
@@ -4,50 +4,40 @@
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
- * $Id: CommonAST.java,v 1.1 2001/10/03 21:04:13 markm Exp $
+ * $Id: CommonAST.java,v 1.2 2001/10/04 01:09:14 markm Exp $
*/
import antlr.collections.AST;
/** Common AST node implementation */
public class CommonAST extends BaseAST {
- int ttype = Token.INVALID_TYPE;
- String text;
+ final int ttype;
- /** Get the token text for this node */
- public String getText() { return text; }
+ final String text;
- /** Get the token type for this node */
- public int getType() { return ttype; }
-
- public void initialize(int t, String txt) {
- setType(t);
- setText(txt);
- }
-
- public void initialize(AST t) {
- setText(t.getText());
- setType(t.getType());
- }
-
- public CommonAST() {
- }
-
- public CommonAST(Token tok) {
- initialize(tok);
- }
-
- public void initialize(Token tok) {
- setText(tok.getText());
- setType(tok.getType());
- }
- /** Set the token text for this node */
- public void setText(String text_) {
- text = text_;
- }
- /** Set the token type for this node */
- public void setType(int ttype_) {
- ttype = ttype_;
- }
+
+ /** Get the token text for this node */
+ public String getText() {
+ return text;
+ }
+
+ /** Get the token type for this node */
+ public int getType() {
+ return ttype;
+ }
+
+ public CommonAST(int t, String txt, BaseAST car, BaseAST cdr) {
+ super(car, cdr);
+ ttype = t;
+ text = txt;
+ }
+
+ public CommonAST(AST t, BaseAST car, BaseAST cdr) {
+ this(t.getType(), t.getText(), car, cdr);
+ }
+
+ public CommonAST(Token tok, BaseAST car, BaseAST cdr) {
+ this(tok.getType(), tok.getText(), car, cdr);
+ }
}
1.2 +11 -14 e/src/jsrc/antlr/CommonASTWithHiddenTokens.java
Index: CommonASTWithHiddenTokens.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/CommonASTWithHiddenTokens.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- CommonASTWithHiddenTokens.java 2001/10/03 21:04:13 1.1
+++ CommonASTWithHiddenTokens.java 2001/10/04 01:09:14 1.2
@@ -4,31 +4,28 @@
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
- * $Id: CommonASTWithHiddenTokens.java,v 1.1 2001/10/03 21:04:13 markm Exp $
+ * $Id: CommonASTWithHiddenTokens.java,v 1.2 2001/10/04 01:09:14 markm Exp $
*/
/** A CommonAST whose initialization copies hidden token
* information from the Token used to create a node.
*/
public class CommonASTWithHiddenTokens extends CommonAST {
+
protected CommonHiddenStreamToken hiddenBefore, hiddenAfter; // references to hidden tokens
-
- public CommonASTWithHiddenTokens() {
- super();
- }
- public CommonASTWithHiddenTokens(Token tok) {
- super(tok);
+ public CommonHiddenStreamToken getHiddenAfter() {
+ return hiddenAfter;
}
- public CommonHiddenStreamToken getHiddenAfter() { return hiddenAfter; }
-
- public CommonHiddenStreamToken getHiddenBefore() { return hiddenBefore; }
+ public CommonHiddenStreamToken getHiddenBefore() {
+ return hiddenBefore;
+ }
- public void initialize(Token tok) {
- CommonHiddenStreamToken t = (CommonHiddenStreamToken)tok;
- super.initialize(t);
- hiddenBefore = t.getHiddenBefore();
- hiddenAfter = t.getHiddenAfter();
+ public CommonASTWithHiddenTokens(Token tok, BaseAST car, BaseAST cdr) {
+ super(tok, car, cdr);
+ CommonHiddenStreamToken t = (CommonHiddenStreamToken)tok;
+ hiddenBefore = t.getHiddenBefore();
+ hiddenAfter = t.getHiddenAfter();
}
}
1.2 +10 -8 e/src/jsrc/antlr/CommonHiddenStreamToken.java
Index: CommonHiddenStreamToken.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/CommonHiddenStreamToken.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- CommonHiddenStreamToken.java 2001/10/03 21:04:13 1.1
+++ CommonHiddenStreamToken.java 2001/10/04 01:09:14 1.2
@@ -4,38 +4,40 @@
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
- * $Id: CommonHiddenStreamToken.java,v 1.1 2001/10/03 21:04:13 markm Exp $
+ * $Id: CommonHiddenStreamToken.java,v 1.2 2001/10/04 01:09:14 markm Exp $
*/
public class CommonHiddenStreamToken extends CommonToken {
+
protected CommonHiddenStreamToken hiddenBefore;
+
protected CommonHiddenStreamToken hiddenAfter;
-
+
public CommonHiddenStreamToken() {
- super();
+ super();
}
public CommonHiddenStreamToken(int t, String txt) {
- super(t, txt);
+ super(t, txt);
}
public CommonHiddenStreamToken(String s) {
- super(s);
+ super(s);
}
public CommonHiddenStreamToken getHiddenAfter() {
- return hiddenAfter;
+ return hiddenAfter;
}
public CommonHiddenStreamToken getHiddenBefore() {
- return hiddenBefore;
+ return hiddenBefore;
}
protected void setHiddenAfter(CommonHiddenStreamToken t) {
- hiddenAfter = t;
+ hiddenAfter = t;
}
protected void setHiddenBefore(CommonHiddenStreamToken t) {
- hiddenBefore = t;
+ hiddenBefore = t;
}
}
1.2 +30 -12 e/src/jsrc/antlr/CommonToken.java
Index: CommonToken.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/CommonToken.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- CommonToken.java 2001/10/03 21:04:13 1.1
+++ CommonToken.java 2001/10/04 01:09:14 1.2
@@ -4,38 +4,56 @@
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
- * $Id: CommonToken.java,v 1.1 2001/10/03 21:04:13 markm Exp $
+ * $Id: CommonToken.java,v 1.2 2001/10/04 01:09:14 markm Exp $
*/
public class CommonToken extends Token {
+
// most tokens will want line and text information
protected int line;
+
protected String text = null;
+
protected int col;
-
- public CommonToken() {}
+ public CommonToken() {
+ }
+
public CommonToken(int t, String txt) {
- type = t;
- setText(txt);
+ type = t;
+ setText(txt);
}
- public CommonToken(String s) { text = s; }
+ public CommonToken(String s) {
+ text = s;
+ }
- public int getLine() { return line; }
+ public int getLine() {
+ return line;
+ }
- public String getText() { return text; }
+ public String getText() {
+ return text;
+ }
- public void setLine(int l) { line = l; }
+ public void setLine(int l) {
+ line = l;
+ }
- public void setText(String s) { text = s; }
+ public void setText(String s) {
+ text = s;
+ }
public String toString() {
- return "[\""+getText()+"\",<"+type+">,line="+line+",col="+col+"]";
+ return "[\"" + getText() + "\",<" + type + ">,line=" + line + ",col=" + col + "]";
}
/** Return token's start column */
- public int getColumn() { return col; }
+ public int getColumn() {
+ return col;
+ }
- public void setColumn(int c) { col = c; }
+ public void setColumn(int c) {
+ col = c;
+ }
}
1.2 +26 -22 e/src/jsrc/antlr/CppBlockFinishingInfo.java
Index: CppBlockFinishingInfo.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/CppBlockFinishingInfo.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- CppBlockFinishingInfo.java 2001/10/03 21:04:13 1.1
+++ CppBlockFinishingInfo.java 2001/10/04 01:09:14 1.2
@@ -4,32 +4,36 @@
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
- * $Id: CppBlockFinishingInfo.java,v 1.1 2001/10/03 21:04:13 markm Exp $
+ * $Id: CppBlockFinishingInfo.java,v 1.2 2001/10/04 01:09:14 markm Exp $
*/
// C++ code generator by Pete Wells: pete@yamuna.demon.co.uk
class CppBlockFinishingInfo {
- String postscript; // what to generate to terminate block
- boolean generatedSwitch;// did block finish with "default:" of switch?
- boolean generatedAnIf;
-
- /** When generating an if or switch, end-of-token lookahead sets
- * will become the else or default clause, don't generate an
- * error clause in this case.
- */
- boolean needAnErrorClause;
-
-
- public CppBlockFinishingInfo() {
- postscript=null;
- generatedSwitch=false;
- needAnErrorClause = true;
- }
- public CppBlockFinishingInfo(String ps, boolean genS, boolean generatedAnIf, boolean n) {
- postscript = ps;
- generatedSwitch = genS;
- this.generatedAnIf = generatedAnIf;
- needAnErrorClause = n;
- }
+
+ String postscript; // what to generate to terminate block
+
+ boolean generatedSwitch;// did block finish with "default:" of switch?
+
+ boolean generatedAnIf;
+
+ /** When generating an if or switch, end-of-token lookahead sets
+ * will become the else or default clause, don't generate an
+ * error clause in this case.
+ */
+ boolean needAnErrorClause;
+
+
+ public CppBlockFinishingInfo() {
+ postscript = null;
+ generatedSwitch = false;
+ needAnErrorClause = true;
+ }
+
+ public CppBlockFinishingInfo(String ps, boolean genS, boolean generatedAnIf, boolean n) {
+ postscript = ps;
+ generatedSwitch = genS;
+ this.generatedAnIf = generatedAnIf;
+ needAnErrorClause = n;
+ }
}
1.2 +62 -61 e/src/jsrc/antlr/CppCharFormatter.java
Index: CppCharFormatter.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/CppCharFormatter.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- CppCharFormatter.java 2001/10/03 21:04:13 1.1
+++ CppCharFormatter.java 2001/10/04 01:09:14 1.2
@@ -4,7 +4,7 @@
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
- * $Id: CppCharFormatter.java,v 1.1 2001/10/03 21:04:13 markm Exp $
+ * $Id: CppCharFormatter.java,v 1.2 2001/10/04 01:09:14 markm Exp $
*/
// C++ code generator by Pete Wells: pete@yamuna.demon.co.uk
@@ -12,68 +12,69 @@
class CppCharFormatter implements CharFormatter {
- /** Given a character value, return a string representing the character
- * that can be embedded inside a string literal or character literal
- * This works for Java/C/C++ code-generation and languages with compatible
- * special-character-escapment.
- * Code-generators for languages should override this method.
- * @param c The character of interest.
- * @param forCharLiteral true to escape for char literal, false for string literal
- */
- public String escapeChar(int c, boolean forCharLiteral) {
- switch (c) {
- case '\n' : return "\\n";
- case '\t' : return "\\t";
- case '\r' : return "\\r";
- case '\\' : return "\\\\";
- case '\'' : return forCharLiteral ? "\\'" : "'";
- case '"' : return forCharLiteral ? "\"" : "\\\"";
- default :
- if ( c<' '||c>126 ) {
- if (c > 255) {
- return "\\u" + Integer.toString(c,16);
- }
- else {
- return "\\" + Integer.toString(c,8);
- }
- }
- else {
- return String.valueOf((char)c);
- }
- }
- }
-
- /** Converts a String into a representation that can be use as a literal
- * when surrounded by double-quotes.
- * @param s The String to be changed into a literal
- */
- public String escapeString(String s)
- {
- String retval = new String();
- for (int i = 0; i < s.length(); i++)
- {
- retval += escapeChar(s.charAt(i), false);
- }
- return retval;
- }
-
- /** Given a character value, return a string representing the character
- * literal that can be recognized by the target language compiler.
- * This works for languages that use single-quotes for character literals.
- * Code-generators for languages should override this method.
- * @param c The character of interest.
- */
- public String literalChar(int c) {
- return "static_cast<unsigned char>('" + escapeChar(c, true) + "')";
- }
-
- /** Converts a String into a string literal
- * This works for languages that use double-quotes for string literals.
- * Code-generators for languages should override this method.
- * @param s The String to be changed into a literal
- */
- public String literalString(String s)
- {
- return "\"" + escapeString(s) + "\"";
- }
+ /** Given a character value, return a string representing the character
+ * that can be embedded inside a string literal or character literal
+ * This works for Java/C/C++ code-generation and languages with compatible
+ * special-character-escapment.
+ * Code-generators for languages should override this method.
+ * @param c The character of interest.
+ * @param forCharLiteral true to escape for char literal, false for string literal
+ */
+ public String escapeChar(int c, boolean forCharLiteral) {
+ switch (c) {
+ case '\n':
+ return "\\n";
+ case '\t':
+ return "\\t";
+ case '\r':
+ return "\\r";
+ case '\\':
+ return "\\\\";
+ case '\'':
+ return forCharLiteral ? "\\'" : "'";
+ case '"':
+ return forCharLiteral ? "\"" : "\\\"";
+ default :
+ if (c < ' ' || c > 126) {
+ if (c > 255) {
+ return "\\u" + Integer.toString(c, 16);
+ } else {
+ return "\\" + Integer.toString(c, 8);
+ }
+ } else {
+ return String.valueOf((char)c);
+ }
+ }
+ }
+
+ /** Converts a String into a representation that can be use as a literal
+ * when surrounded by double-quotes.
+ * @param s The String to be changed into a literal
+ */
+ public String escapeString(String s) {
+ String retval = new String();
+ for (int i = 0; i < s.length(); i++) {
+ retval += escapeChar(s.charAt(i), false);
+ }
+ return retval;
+ }
+
+ /** Given a character value, return a string representing the character
+ * literal that can be recognized by the target language compiler.
+ * This works for languages that use single-quotes for character literals.
+ * Code-generators for languages should override this method.
+ * @param c The character of interest.
+ */
+ public String literalChar(int c) {
+ return "static_cast<unsigned char>('" + escapeChar(c, true) + "')";
+ }
+
+ /** Converts a String into a string literal
+ * This works for languages that use double-quotes for string literals.
+ * Code-generators for languages should override this method.
+ * @param s The String to be changed into a literal
+ */
+ public String literalString(String s) {
+ return "\"" + escapeString(s) + "\"";
+ }
}
1.2 +3911 -3935e/src/jsrc/antlr/CppCodeGenerator.java
Index: CppCodeGenerator.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/CppCodeGenerator.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- CppCodeGenerator.java 2001/10/03 21:04:13 1.1
+++ CppCodeGenerator.java 2001/10/04 01:09:14 1.2
@@ -4,3076 +4,3066 @@
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
- * $Id: CppCodeGenerator.java,v 1.1 2001/10/03 21:04:13 markm Exp $
+ * $Id: CppCodeGenerator.java,v 1.2 2001/10/04 01:09:14 markm Exp $
*/
// C++ code generator by Pete Wells: pete@yamuna.demon.co.uk
// #line generation contributed by: Ric Klaren <klaren@cs.utwente.nl>
-import java.util.Enumeration;
-import java.util.Hashtable;
-import antlr.collections.impl.BitSet;
-import antlr.collections.impl.Vector;
-import java.io.PrintWriter; //SAS: changed for proper text file io
-import java.io.IOException;
-import java.io.FileWriter;
-
-/**Generate MyParser.cpp, MyParser.hpp, MyLexer.cpp, MyLexer.hpp and MyParserTokenTypes.hpp */
-public class CppCodeGenerator extends CodeGenerator {
- // non-zero if inside syntactic predicate generation
- protected int syntacticPredLevel = 0;
-
- // Are we generating ASTs (for parsers and tree parsers) right now?
- protected boolean genAST = false;
-
- // Are we saving the text consumed (for lexers) right now?
- protected boolean saveText = false;
-
- // Generate #line's
- protected boolean genHashLines = true;
- // Used to keep track of lineno in output
- protected int outputLine;
- protected String outputFile;
-
- // Grammar parameters set up to handle different grammar classes.
- // These are used to get instanceof tests out of code generation
- boolean usingCustomAST = false;
- String labeledElementType;
- String labeledElementASTType; // mostly the same as labeledElementType except in parsers
- String labeledElementASTInit;
- String labeledElementInit;
- String commonExtraArgs;
- String commonExtraParams;
- String commonLocalVars;
- String lt1Value;
- String exceptionThrown;
- String throwNoViable;
-
- // Tracks the rule being generated. Used for mapTreeId
- RuleBlock currentRule;
- // Tracks the rule or labeled subrule being generated. Used for AST generation.
- String currentASTResult;
- // Mapping between the ids used in the current alt, and the
- // names of variables used to represent their AST values.
- Hashtable treeVariableMap = new Hashtable();
- // Count of unnamed generated variables
- int astVarNumber = 1;
- // Special value used to mark duplicate in treeVariableMap
- protected static final String NONUNIQUE = new String();
-
- public static final int caseSizeThreshold = 127; // ascii is max
-
- private Vector semPreds;
-
- private static String namespaceStd = "ANTLR_USE_NAMESPACE(std)";
- private static String namespaceAntlr = "ANTLR_USE_NAMESPACE(antlr)";
- private static NameSpace nameSpace = null;
-
- private static final String preIncludeCpp = "pre_include_cpp";
- private static final String preIncludeHpp = "pre_include_hpp";
- private static final String postIncludeCpp = "post_include_cpp";
- private static final String postIncludeHpp = "post_include_hpp";
-
- /** Create a C++ code-generator using the given Grammar.
- * The caller must still call setTool, setBehavior, and setAnalyzer
- * before generating code.
- */
- public CppCodeGenerator() {
- super();
- charFormatter = new CppCharFormatter();
- }
- /** Adds a semantic predicate string to the sem pred vector
- These strings will be used to build an array of sem pred names
- when building a debugging parser. This method should only be
- called when the debug option is specified
- */
- protected int addSemPred(String predicate) {
- semPreds.appendElement(predicate);
- return semPreds.size()-1;
- }
- public void exitIfError() {
- if (tool.hasError) {
- System.out.println("Exiting due to errors.");
- System.exit(1);
- }
- }
- protected int countLines( String s )
- {
- int lines = 0;
- for( int i = 0; i < s.length(); i++ )
- {
- if( s.charAt(i) == '\n' )
- lines++;
- }
- return lines;
- }
- /** Output a String to the currentOutput stream.
- * Ignored if string is null.
- * @param s The string to output
- */
- protected void _print(String s) {
- if (s != null) {
- outputLine += countLines(s);
- currentOutput.print(s);
- }
- }
- /** Print an action without leading tabs, attempting to
- * preserve the current indentation level for multi-line actions
- * Ignored if string is null.
- * @param s The action string to output
- */
- protected void _printAction(String s) {
- if (s != null) {
- outputLine += countLines(s)+1;
- super._printAction(s);
- }
- }
- /** Print an action stored in a token surrounded by #line stuff */
- public void printAction(Token t) {
- if (t != null) {
- genLineNo(t.getLine());
- printTabs();
- _printAction(t.getText());
- genLineNo2();
- }
- }
- /** Print a header action by #line stuff
- * @param name The name of the header part
- */
- public void printHeaderAction(String name) {
- Token a = (antlr.Token)behavior.headerActions.get(name);
- if (a != null) {
- genLineNo(a.getLine());
- println(a.getText());
- genLineNo2();
- }
- }
- /** Output a String followed by newline, to the currentOutput stream.
- * Ignored if string is null.
- * @param s The string to output
- */
- protected void _println(String s) {
- if (s != null) {
- outputLine += countLines(s)+1;
- currentOutput.println(s);
- }
- }
- /** Output tab indent followed by a String followed by newline,
- * to the currentOutput stream. Ignored if string is null.
- * @param s The string to output
- */
- protected void println(String s) {
- if (s != null) {
- printTabs();
- outputLine += countLines(s)+1;
- currentOutput.println(s);
- }
- }
-
- /** Generate a #line or // line depending on options */
- public void genLineNo(int line) {
- if ( line == 0 ) {
- line++;
- }
- if( genHashLines )
- _println("#line "+line+" \""+Tool.fileMinusPath(tool.grammarFile)+"\"");
- else
- println("// line "+line+" \""+Tool.fileMinusPath(tool.grammarFile)+"\"");
- }
-
- /** Generate a #line or // line depending on options */
- public void genLineNo(GrammarElement el)
- {
- if( el != null )
- genLineNo(el.getLine());
- }
- /** Generate a #line or // line depending on options */
- public void genLineNo(Token t)
- {
- if (t != null)
- genLineNo(t.getLine());
- }
- /** Generate a #line or // line depending on options */
- public void genLineNo2()
- {
- if( genHashLines )
- {
- _println("#line "+(outputLine+1)+" \""+outputFile+"\"");
- }
- }
- /**Generate the parser, lexer, treeparser, and token types in C++ */
- public void gen() {
- // Do the code generation
- try {
- // Loop over all grammars
- Enumeration grammarIter = behavior.grammars.elements();
- while (grammarIter.hasMoreElements()) {
- Grammar g = (Grammar)grammarIter.nextElement();
- // Connect all the components to each other
- g.setGrammarAnalyzer(analyzer);
- g.setCodeGenerator(this);
- analyzer.setGrammar(g);
- // To get right overloading behavior across hetrogeneous grammars
- setupGrammarParameters(g);
- g.generate();
- exitIfError();
- }
-
- // Loop over all token managers (some of which are lexers)
- Enumeration tmIter = behavior.tokenManagers.elements();
- while (tmIter.hasMoreElements()) {
- TokenManager tm = (TokenManager)tmIter.nextElement();
- if (!tm.isReadOnly()) {
- // Write the token manager tokens as C++
- // this must appear before genTokenInterchange so that
- // labels are set on string literals
- genTokenTypes(tm);
- // Write the token manager tokens as plain text
- genTokenInterchange(tm);
- }
- exitIfError();
- }
- }
- catch (IOException e) {
- System.out.println(e.getMessage());
- }
- }
- /** Generate code for the given grammar element.
- * @param blk The {...} action to generate
- */
- public void gen(ActionElement action) {
- if ( DEBUG_CODE_GENERATOR ) System.out.println("genAction("+action+")");
- if ( action.isSemPred ) {
- genSemPred(action.actionText, action.line);
- }
- else {
- if ( grammar.hasSyntacticPredicate ) {
- println("if ( inputState->guessing==0 ) {");
- tabs++;
- }
-
- ActionTransInfo tInfo = new ActionTransInfo();
- String actionStr = processActionForTreeSpecifiers(action.actionText, action.getLine(), currentRule, tInfo);
-
- if ( tInfo.refRuleRoot!=null ) {
- // Somebody referenced "#rule", make sure translated var is valid
- // assignment to #rule is left as a ref also, meaning that assignments
- // with no other refs like "#rule = foo();" still forces this code to be
- // generated (unnecessarily).
- println(tInfo.refRuleRoot + " = "+labeledElementASTType+"(currentAST.root);");
- }
-
- // dump the translated action
- genLineNo(action);
- printAction(actionStr);
- genLineNo2();
-
- if ( tInfo.assignToRoot ) {
- // Somebody did a "#rule=", reset internal currentAST.root
- println("currentAST.root = "+tInfo.refRuleRoot+";");
- // reset the child pointer too to be last sibling in sibling list
- // now use if else in stead of x ? y : z to shut CC 4.2 up.
- println("if ( "+tInfo.refRuleRoot+"!="+labeledElementASTInit+" &&");
- tabs++;
- println(tInfo.refRuleRoot+"->getFirstChild() != "+labeledElementASTInit+" )");
- println(" currentAST.child = "+tInfo.refRuleRoot+"->getFirstChild();");
- tabs--;
- println("else");
- tabs++;
- println("currentAST.child = "+tInfo.refRuleRoot+";");
- tabs--;
- println("currentAST.advanceChildToEnd();");
- }
-
- if ( grammar.hasSyntacticPredicate ) {
- tabs--;
- println("}");
- }
- }
- }
- /** Generate code for the given grammar element.
- * @param blk The "x|y|z|..." block to generate
- */
- public void gen(AlternativeBlock blk) {
- if ( DEBUG_CODE_GENERATOR ) System.out.println("gen("+blk+")");
- println("{");
- genBlockPreamble(blk);
-
- // Tell AST generation to build subrule result
- String saveCurrentASTResult = currentASTResult;
- if (blk.getLabel() != null) {
- currentASTResult = blk.getLabel();
- }
-
- boolean ok = grammar.theLLkAnalyzer.deterministic(blk);
-
- CppBlockFinishingInfo howToFinish = genCommonBlock(blk, true);
- genBlockFinish(howToFinish, throwNoViable);
-
- println("}");
-
- // Restore previous AST generation
- currentASTResult = saveCurrentASTResult;
- }
- /** Generate code for the given grammar element.
- * @param blk The block-end element to generate. Block-end
- * elements are synthesized by the grammar parser to represent
- * the end of a block.
- */
- public void gen(BlockEndElement end) {
- if ( DEBUG_CODE_GENERATOR ) System.out.println("genRuleEnd("+end+")");
- }
- /** Generate code for the given grammar element.
- * @param blk The character literal reference to generate
- */
- public void gen(CharLiteralElement atom) {
- if ( DEBUG_CODE_GENERATOR ) System.out.println("genChar("+atom+")");
-
- if ( atom.getLabel()!=null ) {
- println(atom.getLabel() + " = " + lt1Value + ";");
- }
-
- boolean oldsaveText = saveText;
- saveText = saveText && atom.getAutoGenType()==GrammarElement.AUTO_GEN_NONE;
- genMatch(atom);
- saveText = oldsaveText;
- }
- /** Generate code for the given grammar element.
- * @param blk The character-range reference to generate
- */
- public void gen(CharRangeElement r) {
- if ( r.getLabel()!=null && syntacticPredLevel == 0) {
- println(r.getLabel() + " = " + lt1Value + ";");
- }
- println("matchRange("+textOrChar(r.beginText)+","+textOrChar(r.endText)+");");
- }
- /** Generate the lexer C++ files */
- public void gen(LexerGrammar g) throws IOException {
- // If debugging, create a new sempred vector for this grammar
- if (g.debuggingOutput)
- semPreds = new Vector();
-
- setGrammar(g);
- if (!(grammar instanceof LexerGrammar)) {
- tool.panic("Internal error generating lexer");
- }
-
- genBody(g);
- genInclude(g);
- }
- /** Generate code for the given grammar element.
- * @param blk The (...)+ block to generate
- */
- public void gen(OneOrMoreBlock blk) {
- if ( DEBUG_CODE_GENERATOR ) System.out.println("gen+("+blk+")");
- String label;
- String cnt;
- println("{");
- genBlockPreamble(blk);
- if ( blk.getLabel() != null ) {
- cnt = "_cnt_"+blk.getLabel();
- }
- else {
- cnt = "_cnt" + blk.ID;
- }
- println("int "+cnt+"=0;");
- if ( blk.getLabel() != null ) {
- label = blk.getLabel();
- }
- else {
- label = "_loop" + blk.ID;
- }
-
- println("for (;;) {");
- tabs++;
-
- // Tell AST generation to build subrule result
- String saveCurrentASTResult = currentASTResult;
- if (blk.getLabel() != null) {
- currentASTResult = blk.getLabel();
- }
-
- boolean ok = grammar.theLLkAnalyzer.deterministic(blk);
-
- // generate exit test if greedy set to false
- // and an alt is ambiguous with exit branch
- // or when lookahead derived purely from end-of-file
- // Lookahead analysis stops when end-of-file is hit,
- // returning set {epsilon}. Since {epsilon} is not
- // ambig with any real tokens, no error is reported
- // by deterministic() routines and we have to check
- // for the case where the lookahead depth didn't get
- // set to NONDETERMINISTIC (this only happens when the
- // FOLLOW contains real atoms + epsilon).
- boolean generateNonGreedyExitPath = false;
- int nonGreedyExitDepth = grammar.maxk;
-
- if ( !blk.greedy &&
- blk.exitLookaheadDepth<=grammar.maxk &&
- blk.exitCache[blk.exitLookaheadDepth].containsEpsilon() )
- {
- generateNonGreedyExitPath = true;
- nonGreedyExitDepth = blk.exitLookaheadDepth;
- }
- else if ( !blk.greedy &&
- blk.exitLookaheadDepth==LLkGrammarAnalyzer.NONDETERMINISTIC )
- {
- generateNonGreedyExitPath = true;
- }
-
- // generate exit test if greedy set to false
- // and an alt is ambiguous with exit branch
- if ( generateNonGreedyExitPath ) {
- if ( DEBUG_CODE_GENERATOR ) {
- System.out.println("nongreedy (...)+ loop; exit depth is "+
- blk.exitLookaheadDepth);
- }
- String predictExit =
- getLookaheadTestExpression(blk.exitCache,
- nonGreedyExitDepth);
- println("// nongreedy exit test");
- println("if ( "+cnt+">=1 && "+predictExit+") goto "+label+";");
- }
-
- CppBlockFinishingInfo howToFinish = genCommonBlock(blk, false);
- genBlockFinish(
- howToFinish,
- "if ( "+cnt+">=1 ) { goto "+label+"; } else {" + throwNoViable + "}"
- );
-
- println(cnt+"++;");
- tabs--;
- println("}");
- println(label+":;");
- println("}");
-
- // Restore previous AST generation
- currentASTResult = saveCurrentASTResult;
- }
- /** Generate the parser C++ file */
- public void gen(ParserGrammar g) throws IOException {
-
- // if debugging, set up a new vector to keep track of sempred
- // strings for this grammar
- if (g.debuggingOutput)
- semPreds = new Vector();
-
- setGrammar(g);
- if (!(grammar instanceof ParserGrammar)) {
- tool.panic("Internal error generating parser");
- }
-
- genBody(g);
- genInclude(g);
- }
- /** Generate code for the given grammar element.
- * @param blk The rule-reference to generate
- */
- public void gen(RuleRefElement rr) {
- if ( DEBUG_CODE_GENERATOR ) System.out.println("genRR("+rr+")");
- RuleSymbol rs = (RuleSymbol)grammar.getSymbol(rr.targetRule);
- if (rs == null || !rs.isDefined())
- {
- // Is this redundant???
- tool.error("Rule '" + rr.targetRule + "' is not defined", grammar.getFilename(), rr.getLine());
- return;
- }
- if (!(rs instanceof RuleSymbol))
- {
- // Is this redundant???
- tool.error("'" + rr.targetRule + "' does not name a grammar rule", grammar.getFilename(), rr.getLine());
- return;
- }
-
- genErrorTryForElement(rr);
-
- // AST value for labeled rule refs in tree walker.
- // This is not AST construction; it is just the input tree node value.
- if ( grammar instanceof TreeWalkerGrammar &&
- rr.getLabel() != null &&
- syntacticPredLevel == 0 )
- {
- println(rr.getLabel() + " = (_t == ASTNULL) ? "+labeledElementASTInit+" : "+lt1Value+";");
- }
-
- // if in lexer and ! on rule ref or alt or rule, save buffer index to kill later
- if ( grammar instanceof LexerGrammar && (!saveText||rr.getAutoGenType()==GrammarElement.AUTO_GEN_BANG) ) {
- println("_saveIndex = text.length();");
- }
-
- // Process return value assignment if any
- printTabs();
- if (rr.idAssign != null)
- {
- // Warn if the rule has no return type
- if (rs.block.returnAction == null)
- {
- tool.warning("Rule '" + rr.targetRule + "' has no return type", grammar.getFilename(), rr.getLine());
- }
- _print(rr.idAssign + "=");
- } else {
- // Warn about return value if any, but not inside syntactic predicate
- if ( !(grammar instanceof LexerGrammar) && syntacticPredLevel == 0 && rs.block.returnAction != null)
- {
- tool.warning("Rule '" + rr.targetRule + "' returns a value", grammar.getFilename(), rr.getLine());
- }
- }
-
- // Call the rule
- GenRuleInvocation(rr);
-
- // if in lexer and ! on element or alt or rule, save buffer index to kill later
- if ( grammar instanceof LexerGrammar && (!saveText||rr.getAutoGenType()==GrammarElement.AUTO_GEN_BANG) ) {
- println("text.erase(_saveIndex);");
- }
-
- // if not in a syntactic predicate
- if (syntacticPredLevel == 0) {
- boolean doNoGuessTest = (
- grammar.hasSyntacticPredicate &&
- (
- grammar.buildAST && rr.getLabel() != null ||
- (genAST && rr.getAutoGenType() == GrammarElement.AUTO_GEN_NONE)
- )
- );
- if (doNoGuessTest) {
- println("if (inputState->guessing==0) {");
- tabs++;
- }
-
- if (grammar.buildAST && rr.getLabel() != null) {
- // always gen variable for rule return on labeled rules
- // FIXME: maybe broken....
- println(rr.getLabel() + "_AST = "+labeledElementASTType+"(returnAST);");
- }
- if (genAST) {
- switch (rr.getAutoGenType()) {
- case GrammarElement.AUTO_GEN_NONE:
- // println("theASTFactory.addASTChild(currentAST, returnAST);");
- println("astFactory.addASTChild(currentAST, "+namespaceAntlr+"RefAST(returnAST));");
- break;
- case GrammarElement.AUTO_GEN_CARET:
- tool.error("Internal: encountered ^ after rule reference");
- break;
- default:
- break;
- }
- }
-
- // if a lexer and labeled, Token label defined at rule level, just set it here
- if ( grammar instanceof LexerGrammar && rr.getLabel() != null ) {
- println(rr.getLabel()+"=_returnToken;");
- }
-
- if (doNoGuessTest) {
- tabs--;
- println("}");
- }
- }
- genErrorCatchForElement(rr);
- }
- /** Generate code for the given grammar element.
- * @param blk The string-literal reference to generate
- */
- public void gen(StringLiteralElement atom) {
- if ( DEBUG_CODE_GENERATOR ) System.out.println("genString("+atom+")");
-
- // Variable declarations for labeled elements
- if (atom.getLabel()!=null && syntacticPredLevel == 0) {
- println(atom.getLabel() + " = " + lt1Value + ";");
- }
-
- // AST
- genElementAST(atom);
-
- // is there a bang on the literal?
- boolean oldsaveText = saveText;
- saveText = saveText && atom.getAutoGenType()==GrammarElement.AUTO_GEN_NONE;
-
- // matching
- genMatch(atom);
-
- saveText = oldsaveText;
-
- // tack on tree cursor motion if doing a tree walker
- if (grammar instanceof TreeWalkerGrammar) {
- println("_t = _t->getNextSibling();");
- }
- }
- /** Generate code for the given grammar element.
- * @param blk The token-range reference to generate
- */
- public void gen(TokenRangeElement r) {
- genErrorTryForElement(r);
- if ( r.getLabel()!=null && syntacticPredLevel == 0) {
- println(r.getLabel() + " = " + lt1Value + ";");
- }
-
- // AST
- genElementAST(r);
-
- // match
- println("matchRange("+r.beginText+","+r.endText+");");
- genErrorCatchForElement(r);
- }
- /** Generate code for the given grammar element.
- * @param blk The token-reference to generate
- */
- public void gen(TokenRefElement atom) {
- if ( DEBUG_CODE_GENERATOR ) System.out.println("genTokenRef("+atom+")");
- if ( grammar instanceof LexerGrammar ) {
- tool.panic("Token reference found in lexer");
- }
- genErrorTryForElement(atom);
- // Assign Token value to token label variable
- if ( atom.getLabel()!=null && syntacticPredLevel == 0) {
- println(atom.getLabel() + " = " + lt1Value + ";");
- }
-
- // AST
- genElementAST(atom);
- // matching
- genMatch(atom);
- genErrorCatchForElement(atom);
-
- // tack on tree cursor motion if doing a tree walker
- if (grammar instanceof TreeWalkerGrammar) {
- println("_t = _t->getNextSibling();");
- }
- }
- public void gen(TreeElement t) {
- // save AST cursor
- println(labeledElementType+" __t" + t.ID + " = _t;");
-
- // If there is a label on the root, then assign that to the variable
- if (t.root.getLabel() != null) {
-// println(t.root.getLabel() + " = _t==ASTNULL ? "+namespaceAntlr+"nullAST :"+labeledElementASTType+"(_t);");
- println(t.root.getLabel() + " = (_t == ASTNULL) ? "+labeledElementASTInit+" : _t;");
- }
-
- // Generate AST variables
- genElementAST(t.root);
- if (grammar.buildAST) {
- // Save the AST construction state
- println(namespaceAntlr+"ASTPair __currentAST" + t.ID + " = currentAST;");
- // Make the next item added a child of the TreeElement root
- println("currentAST.root = currentAST.child;");
- println("currentAST.child = "+labeledElementASTInit+";");
- }
-
- // match root
- genMatch(t.root);
- // move to list of children
- println("_t = _t->getFirstChild();");
-
- // walk list of children, generating code for each
- for (int i=0; i<t.getAlternatives().size(); i++) {
- Alternative a = t.getAlternativeAt(i);
- AlternativeElement e = a.head;
- while ( e != null ) {
- e.generate();
- e = e.next;
- }
- }
-
- if (grammar.buildAST) {
- // restore the AST construction state to that just after the
- // tree root was added
- println("currentAST = __currentAST" + t.ID + ";");
- }
- // restore AST cursor
- println("_t = __t" + t.ID + ";");
- // move cursor to sibling of tree just parsed
- println("_t = _t->getNextSibling();");
- }
- /** Generate the tree-parser C++ files */
- public void gen(TreeWalkerGrammar g) throws IOException {
- setGrammar(g);
- if (!(grammar instanceof TreeWalkerGrammar)) {
- tool.panic("Internal error generating tree-walker");
- }
-
- genBody(g);
- genInclude(g);
- }
- /** Generate code for the given grammar element.
- * @param wc The wildcard element to generate
- */
- public void gen(WildcardElement wc) {
- // Variable assignment for labeled elements
- if (wc.getLabel()!=null && syntacticPredLevel == 0) {
- println(wc.getLabel() + " = " + lt1Value + ";");
- }
-
- // AST
- genElementAST(wc);
- // Match anything but EOF
- if (grammar instanceof TreeWalkerGrammar) {
- println("if ( _t == "+labeledElementASTInit+" ) throw "+namespaceAntlr+"MismatchedTokenException();");
- }
- else if (grammar instanceof LexerGrammar) {
- if ( grammar instanceof LexerGrammar &&
- (!saveText||wc.getAutoGenType()==GrammarElement.AUTO_GEN_BANG) ) {
- println("_saveIndex = text.length();");
- }
- println("matchNot(EOF/*_CHAR*/);");
- if ( grammar instanceof LexerGrammar &&
- (!saveText||wc.getAutoGenType()==GrammarElement.AUTO_GEN_BANG) ) {
- println("text.erase(_saveIndex);"); // kill text atom put in buffer
- }
- }
- else {
- println("matchNot(" + getValueString(Token.EOF_TYPE) + ");");
- }
-
- // tack on tree cursor motion if doing a tree walker
- if (grammar instanceof TreeWalkerGrammar) {
- println("_t = _t->getNextSibling();");
- }
- }
- /** Generate code for the given grammar element.
- * @param blk The (...)* block to generate
- */
- public void gen(ZeroOrMoreBlock blk) {
- if ( DEBUG_CODE_GENERATOR ) System.out.println("gen*("+blk+")");
- println("{");
- genBlockPreamble(blk);
- String label;
- if ( blk.getLabel() != null ) {
- label = blk.getLabel();
- }
- else {
- label = "_loop" + blk.ID;
- }
- println("for (;;) {");
- tabs++;
-
- // Tell AST generation to build subrule result
- String saveCurrentASTResult = currentASTResult;
- if (blk.getLabel() != null) {
- currentASTResult = blk.getLabel();
- }
-
- boolean ok = grammar.theLLkAnalyzer.deterministic(blk);
-
- // generate exit test if greedy set to false
- // and an alt is ambiguous with exit branch
- // or when lookahead derived purely from end-of-file
- // Lookahead analysis stops when end-of-file is hit,
- // returning set {epsilon}. Since {epsilon} is not
- // ambig with any real tokens, no error is reported
- // by deterministic() routines and we have to check
- // for the case where the lookahead depth didn't get
- // set to NONDETERMINISTIC (this only happens when the
- // FOLLOW contains real atoms + epsilon).
- boolean generateNonGreedyExitPath = false;
- int nonGreedyExitDepth = grammar.maxk;
-
- if ( !blk.greedy &&
- blk.exitLookaheadDepth<=grammar.maxk &&
- blk.exitCache[blk.exitLookaheadDepth].containsEpsilon() )
- {
- generateNonGreedyExitPath = true;
- nonGreedyExitDepth = blk.exitLookaheadDepth;
- }
- else if ( !blk.greedy &&
- blk.exitLookaheadDepth==LLkGrammarAnalyzer.NONDETERMINISTIC )
- {
- generateNonGreedyExitPath = true;
- }
- if ( generateNonGreedyExitPath ) {
- if ( DEBUG_CODE_GENERATOR ) {
- System.out.println("nongreedy (...)* loop; exit depth is "+
- blk.exitLookaheadDepth);
- }
- String predictExit =
- getLookaheadTestExpression(blk.exitCache,
- nonGreedyExitDepth);
- println("// nongreedy exit test");
- println("if ("+predictExit+") goto "+label+";");
- }
-
- CppBlockFinishingInfo howToFinish = genCommonBlock(blk, false);
- genBlockFinish(howToFinish, "goto " + label + ";");
-
- tabs--;
- println("}");
- println(label+":;");
- println("}");
-
- // Restore previous AST generation
- currentASTResult = saveCurrentASTResult;
- }
- /** Generate an alternative.
- * @param alt The alternative to generate
- * @param blk The block to which the alternative belongs
- */
- protected void genAlt(Alternative alt, AlternativeBlock blk) {
- // Save the AST generation state, and set it to that of the alt
- boolean savegenAST = genAST;
- genAST = genAST && alt.getAutoGen();
-
- boolean oldsaveTest = saveText;
- saveText = saveText && alt.getAutoGen();
-
- // Reset the variable name map for the alternative
- Hashtable saveMap = treeVariableMap;
- treeVariableMap = new Hashtable();
-
- // Generate try block around the alt for error handling
- if (alt.exceptionSpec != null) {
- println("try { // for error handling");
- tabs++;
- }
-
- AlternativeElement elem = alt.head;
- while ( !(elem instanceof BlockEndElement) ) {
- elem.generate(); // alt can begin with anything. Ask target to gen.
- elem = elem.next;
- }
-
- if ( genAST) {
- if (blk instanceof RuleBlock) {
- // Set the AST return value for the rule
- RuleBlock rblk = (RuleBlock)blk;
- println(rblk.getRuleName() + "_AST = "+labeledElementASTType+"(currentAST.root);");
- }
- else if (blk.getLabel() != null) {
- // ### future: also set AST value for labeled subrules.
- // println(blk.getLabel() + "_AST = "+labeledElementASTType+"(currentAST.root);");
- }
- }
-
- if (alt.exceptionSpec != null) {
- // close try block
- tabs--;
- println("}");
- genErrorHandler(alt.exceptionSpec);
- }
-
- genAST = savegenAST;
- saveText = oldsaveTest;
-
- treeVariableMap = saveMap;
- }
- /** Generate all the bitsets to be used in the parser or lexer
- * Generate the raw bitset data like "long _tokenSet1_data[] = {...};"
- * and the BitSet object declarations like "BitSet _tokenSet1 = new BitSet(_tokenSet1_data);"
- * Note that most languages do not support object initialization inside a
- * class definition, so other code-generators may have to separate the
- * bitset declarations from the initializations (e.g., put the initializations
- * in the generated constructor instead).
- * @param bitsetList The list of bitsets to generate.
- * @param maxVocabulary Ensure that each generated bitset can contain at least this value.
- * @param dumpSets Dump out the token definitions of the contents of the bitset
- * only for grammars/parsers.
- */
- protected void genBitsets(
- Vector bitsetList,
- int maxVocabulary,
- String prefix,
- boolean dumpSets
- ) {
- println("");
- for (int i = 0; i < bitsetList.size(); i++)
- {
- BitSet p = (BitSet)bitsetList.elementAt(i);
- // Ensure that generated BitSet is large enough for vocabulary
- p.growToInclude(maxVocabulary);
- // initialization data
- println(
- "const unsigned long " + prefix + getBitsetName(i) + "_data_" + "[] = { " +
- p.toStringOfHalfWords() +
- " };"
- );
-
- if( dumpSets )
- {
- // Dump the contents of the bitset in readable format...
- String t = "// ";
- for( int j = 0; j < p.size(); j++ )
- {
- if ( p.member( j ) )
- {
- t += grammar.tokenManager.getTokenStringAt(j)+" ";
- if( t.length() > 70 )
- {
- println(t);
- t = "// ";
- }
- }
- }
- if ( t != "// " )
- println(t);
- }
-
- // BitSet object
- println(
- "const "+namespaceAntlr+"BitSet " + prefix + getBitsetName(i) + "(" +
- getBitsetName(i) + "_data_," + p.size()/32 +
- ");"
- );
- }
- }
- protected void genBitsetsHeader(
- Vector bitsetList,
- int maxVocabulary
- ) {
- println("");
- for (int i = 0; i < bitsetList.size(); i++)
- {
- BitSet p = (BitSet)bitsetList.elementAt(i);
- // Ensure that generated BitSet is large enough for vocabulary
- p.growToInclude(maxVocabulary);
- // initialization data
- println("static const unsigned long " + getBitsetName(i) + "_data_" + "[];");
- // BitSet object
- println("static const "+namespaceAntlr+"BitSet " + getBitsetName(i) + ";");
- }
- }
- /** Generate the finish of a block, using a combination of the info
- * returned from genCommonBlock() and the action to perform when
- * no alts were taken
- * @param howToFinish The return of genCommonBlock()
- * @param noViableAction What to generate when no alt is taken
- */
- private void genBlockFinish(CppBlockFinishingInfo howToFinish, String noViableAction)
- {
- if (howToFinish.needAnErrorClause &&
- (howToFinish.generatedAnIf || howToFinish.generatedSwitch)) {
- if ( howToFinish.generatedAnIf ) {
- println("else {");
- }
- else {
- println("{");
- }
- tabs++;
- println(noViableAction);
- tabs--;
- println("}");
- }
-
- if ( howToFinish.postscript!=null ) {
- println(howToFinish.postscript);
- }
- }
- /** Generate the header for a block, which may be a RuleBlock or a
- * plain AlternativeBLock. This generates any variable declarations,
- * init-actions, and syntactic-predicate-testing variables.
- * @blk The block for which the preamble is to be generated.
- */
- protected void genBlockPreamble(AlternativeBlock blk) {
- // define labels for rule blocks.
- if ( blk instanceof RuleBlock ) {
- RuleBlock rblk = (RuleBlock)blk;
- if ( rblk.labeledElements!=null ) {
- for (int i=0; i<rblk.labeledElements.size(); i++) {
-
- AlternativeElement a = (AlternativeElement)rblk.labeledElements.elementAt(i);
- //System.out.println("looking at labeled element: "+a);
- // Variables for labeled rule refs and subrules are different than
- // variables for grammar atoms. This test is a little tricky because
- // we want to get all rule refs and ebnf, but not rule blocks or
- // syntactic predicates
- if (
- a instanceof RuleRefElement ||
- a instanceof AlternativeBlock &&
- !(a instanceof RuleBlock) &&
- !(a instanceof SynPredBlock)
- ) {
-
- if (
- !(a instanceof RuleRefElement) &&
- ((AlternativeBlock)a).not &&
- analyzer.subruleCanBeInverted(((AlternativeBlock)a), grammar instanceof LexerGrammar)
- ) {
- // Special case for inverted subrules that will be inlined.
- // Treat these like token or char literal references
- println(labeledElementType + " " + a.getLabel() + " = " + labeledElementInit + ";");
- if (grammar.buildAST) {
- println(labeledElementASTType+" " + a.getLabel() + "_AST = "+labeledElementASTInit+";");
- }
- }
- else {
- if (grammar.buildAST) {
- // Always gen AST variables for labeled elements, even if the
- // element itself is marked with !
- println(labeledElementASTType+" " + a.getLabel() + "_AST = "+labeledElementASTInit+";");
- }
- if ( grammar instanceof LexerGrammar ) {
- println(namespaceAntlr+"RefToken "+a.getLabel()+";");
- }
- if (grammar instanceof TreeWalkerGrammar) {
- // always generate rule-ref variables for tree walker
- println(labeledElementType + " " + a.getLabel() + " = " + labeledElementInit + ";");
- }
- }
- }
- else {
- // It is a token or literal reference. Generate the
- // correct variable type for this grammar
- println(labeledElementType + " " + a.getLabel() + " = " + labeledElementInit + ";");
- // In addition, generate *_AST variables if building ASTs
- if (grammar.buildAST) {
- println(labeledElementASTType+" " + a.getLabel() + "_AST = "+labeledElementASTInit+";");
- }
- }
- }
- }
- }
-
- // dump out init action
- if ( blk.initAction!=null ) {
- genLineNo(blk);
- printAction(
- processActionForTreeSpecifiers(blk.initAction, 0, currentRule, null)
- );
- genLineNo2();
- }
- }
- public void genBody(LexerGrammar g) throws IOException {
- outputFile = grammar.getClassName() + ".cpp";
- outputLine = 1;
- currentOutput = antlr.Tool.openOutputFile(outputFile);
- //SAS: changed for proper text file io
-
- genAST = false; // no way to gen trees.
- saveText = true; // save consumed characters.
-
- tabs=0;
-
- // Generate header common to all C++ output files
- genHeader(outputFile);
-
- printHeaderAction(preIncludeCpp);
- // Generate header specific to lexer C++ file
- println("#include \"" + grammar.getClassName() + ".hpp\"");
- println("#include \"antlr/CharBuffer.hpp\"");
- println("#include \"antlr/TokenStreamException.hpp\"");
- println("#include \"antlr/TokenStreamIOException.hpp\"");
- println("#include \"antlr/TokenStreamRecognitionException.hpp\"");
- println("#include \"antlr/CharStreamException.hpp\"");
- println("#include \"antlr/CharStreamIOException.hpp\"");
- println("#include \"antlr/NoViableAltForCharException.hpp\"");
- if (grammar.debuggingOutput)
- println("#include \"antlr/DebuggingInputBuffer.hpp\"");
- println("");
- printHeaderAction(postIncludeCpp);
-
- if (nameSpace != null)
- nameSpace.emitDeclarations(currentOutput);
-
- // Generate user-defined lexer file preamble
- printAction(grammar.preambleAction);
-
- // Generate lexer class definition
- String sup=null;
- if ( grammar.superClass!=null ) {
- sup = grammar.superClass;
- }
- else {
- sup = grammar.getSuperClass();
- if (sup.lastIndexOf('.') != -1)
- sup = sup.substring(sup.lastIndexOf('.')+1);
- sup = namespaceAntlr + sup;
- }
-
- //
- // Generate the constructor from InputStream
- //
- println(grammar.getClassName() + "::" + grammar.getClassName() + "(" + namespaceStd + "istream& in)");
- tabs++;
- // if debugging, wrap the input buffer in a debugger
- if (grammar.debuggingOutput)
- println(": " + sup + "(new "+namespaceAntlr+"DebuggingInputBuffer(new "+namespaceAntlr+"CharBuffer(in)))");
- else
- println(": " + sup + "(new "+namespaceAntlr+"CharBuffer(in))");
- tabs--;
- println("{");
- tabs++;
-
- // if debugging, set up array variables and call user-overridable
- // debugging setup method
- if ( grammar.debuggingOutput ) {
- println("setRuleNames(_ruleNames);");
- println("setSemPredNames(_semPredNames);");
- println("setupDebugging();");
- }
-
- println("setCaseSensitive("+g.caseSensitive+");");
- println("initLiterals();");
- tabs--;
- println("}");
- println("");
-
- // Generate the constructor from InputBuffer
- println(grammar.getClassName() + "::" + grammar.getClassName() + "("+namespaceAntlr+"InputBuffer& ib)");
- tabs++;
- // if debugging, wrap the input buffer in a debugger
- if (grammar.debuggingOutput)
- println(": " + sup + "(new "+namespaceAntlr+"DebuggingInputBuffer(ib))");
- else
- println(": " + sup + "(ib)");
- tabs--;
- println("{");
- tabs++;
-
- // if debugging, set up array variables and call user-overridable
- // debugging setup method
- if ( grammar.debuggingOutput ) {
- println("setRuleNames(_ruleNames);");
- println("setSemPredNames(_semPredNames);");
- println("setupDebugging();");
- }
-
- println("setCaseSensitive("+g.caseSensitive+");");
- println("initLiterals();");
- tabs--;
- println("}");
- println("");
-
- // Generate the constructor from LexerSharedInputState
- println(grammar.getClassName() + "::" + grammar.getClassName() + "(const "+namespaceAntlr+"LexerSharedInputState& state)");
- tabs++;
- println(": " + sup + "(state)");
- tabs--;
- println("{");
- tabs++;
-
- // if debugging, set up array variables and call user-overridable
- // debugging setup method
- if ( grammar.debuggingOutput ) {
- println("setRuleNames(_ruleNames);");
- println("setSemPredNames(_semPredNames);");
- println("setupDebugging();");
- }
-
- println("setCaseSensitive("+g.caseSensitive+");");
- println("initLiterals();");
- tabs--;
- println("}");
- println("");
-
- println("void " + grammar.getClassName() + "::initLiterals()");
- println("{");
- tabs++;
- // Generate the initialization of the map
- // containing the string literals used in the lexer
- // The literals variable itself is in CharScanner
- /* TJP: get keys now and check to make sure it's a literal not
- * a label to a literal; was dup'ing string literals before
- * change.
- *
- Enumeration ids = grammar.tokenManager.getTokenSymbolElements();
- while ( ids.hasMoreElements() ) {
- TokenSymbol sym = (TokenSymbol)ids.nextElement();
- if ( sym instanceof StringLiteralSymbol ) {
- StringLiteralSymbol s = (StringLiteralSymbol)sym;
- println("literals["+s.getId()+"] = "+s.getTokenType()+";");
- }
- }
- */
- // TJP changed it to following loop.
- Enumeration keys = grammar.tokenManager.getTokenSymbolKeys();
- while ( keys.hasMoreElements() ) {
- String key = (String)keys.nextElement();
- if ( key.charAt(0) != '"' ) {
- continue;
- }
- TokenSymbol sym = grammar.tokenManager.getTokenSymbol(key);
- if ( sym instanceof StringLiteralSymbol ) {
- StringLiteralSymbol s = (StringLiteralSymbol)sym;
- println("literals["+s.getId()+"] = "+s.getTokenType()+";");
- }
- }
-
- // Generate the setting of various generated options.
- tabs--;
- println("}");
-
- // Generate getCaseSensitiveLiterals() method
- println("bool " + grammar.getClassName() + "::getCaseSensitiveLiterals() const");
- println("{");
- tabs++;
- println("return "+g.caseSensitiveLiterals + ";");
- tabs--;
- println("}");
-
- Enumeration ids;
- // generate the rule name array for debugging
- if (grammar.debuggingOutput) {
- println("const char* "+grammar.getClassName()+"::_ruleNames[] = {");
- tabs++;
-
- ids = grammar.rules.elements();
- int ruleNum=0;
- while ( ids.hasMoreElements() ) {
- GrammarSymbol sym = (GrammarSymbol) ids.nextElement();
- if ( sym instanceof RuleSymbol)
- println("\""+((RuleSymbol)sym).getId()+"\",");
- }
- println("0");
- tabs--;
- println("};");
- }
-
- // Generate nextToken() rule.
- // nextToken() is a synthetic lexer rule that is the implicit OR of all
- // user-defined lexer rules.
- genNextToken();
-
- // Generate code for each rule in the lexer
- ids = grammar.rules.elements();
- int ruleNum=0;
- while ( ids.hasMoreElements() ) {
- RuleSymbol sym = (RuleSymbol) ids.nextElement();
- // Don't generate the synthetic rules
- if (!sym.getId().equals("mnextToken")) {
- genRule(sym, false, ruleNum++, grammar.getClassName() + "::");
- }
- exitIfError();
- }
-
- // Generate the semantic predicate map for debugging
- if (grammar.debuggingOutput)
- genSemPredMap(grammar.getClassName() + "::");
-
- // Generate the bitsets used throughout the lexer
- genBitsets(bitsetsUsed, ((LexerGrammar)grammar).charVocabulary.size(), grammar.getClassName() + "::", false);
-
- println("");
- if (nameSpace != null)
- nameSpace.emitClosures(currentOutput);
-
- // Close the lexer output stream
- currentOutput.close();
- currentOutput = null;
- }
- public void genBody(ParserGrammar g) throws IOException {
- // Open the output stream for the parser and set the currentOutput
- outputFile = grammar.getClassName() + ".cpp";
- outputLine = 1;
- currentOutput = antlr.Tool.openOutputFile(outputFile);
- //SAS: changed for proper text file io
-
- genAST = grammar.buildAST;
-
- tabs = 0;
-
- // Generate the header common to all output files.
- genHeader(outputFile);
-
- printHeaderAction(preIncludeCpp);
-
- // Generate header for the parser
- println("#include \"" + grammar.getClassName() + ".hpp\"");
- println("#include \"antlr/NoViableAltException.hpp\"");
- println("#include \"antlr/SemanticException.hpp\"");
-
- printHeaderAction(postIncludeCpp);
-
- if (nameSpace != null)
- nameSpace.emitDeclarations(currentOutput);
-
- // Output the user-defined parser preamble
- printAction(grammar.preambleAction);
-
- String sup=null;
- if ( grammar.superClass!=null )
- sup = grammar.superClass;
- else {
- sup = grammar.getSuperClass();
- if (sup.lastIndexOf('.') != -1)
- sup = sup.substring(sup.lastIndexOf('.')+1);
- sup = namespaceAntlr + sup;
- }
-
- // set up an array of all the rule names so the debugger can
- // keep track of them only by number -- less to store in tree...
- if (grammar.debuggingOutput) {
- println("const char* "+grammar.getClassName()+"::_ruleNames[] = {");
- tabs++;
-
- Enumeration ids = grammar.rules.elements();
- int ruleNum=0;
- while ( ids.hasMoreElements() ) {
- GrammarSymbol sym = (GrammarSymbol) ids.nextElement();
- if ( sym instanceof RuleSymbol)
- println("\""+((RuleSymbol)sym).getId()+"\",");
- }
- println("0");
- tabs--;
- println("};");
- }
-
- // Generate parser class constructor from TokenBuffer
- print(grammar.getClassName() + "::" + grammar.getClassName());
- println("("+namespaceAntlr+"TokenBuffer& tokenBuf, int k)");
- println(": " + sup + "(tokenBuf,k)");
- println("{");
- tabs++;
- println("setTokenNames(_tokenNames);");
- // if debugging, set up arrays and call the user-overridable
- // debugging setup method
- if ( grammar.debuggingOutput ) {
- println("setRuleNames(_ruleNames);");
- println("setSemPredNames(_semPredNames);");
- println("setupDebugging();");
- }
- tabs--;
- println("}");
- println("");
-
- print(grammar.getClassName() + "::" + grammar.getClassName());
- println("("+namespaceAntlr+"TokenBuffer& tokenBuf)");
- println(": " + sup + "(tokenBuf," + grammar.maxk + ")");
- println("{");
- tabs++;
- println("setTokenNames(_tokenNames);");
- // if debugging, set up arrays and call the user-overridable
- // debugging setup method
- if ( grammar.debuggingOutput ) {
- println("setRuleNames(_ruleNames);");
- println("setSemPredNames(_semPredNames);");
- println("setupDebugging();");
- }
- tabs--;
- println("}");
- println("");
-
- // Generate parser class constructor from TokenStream
- print(grammar.getClassName() + "::" + grammar.getClassName());
- println("("+namespaceAntlr+"TokenStream& lexer, int k)");
- println(": " + sup + "(lexer,k)");
- println("{");
- tabs++;
- println("setTokenNames(_tokenNames);");
- // if debugging, set up arrays and call the user-overridable
- // debugging setup method
- if ( grammar.debuggingOutput ) {
- println("setRuleNames(_ruleNames);");
- println("setSemPredNames(_semPredNames);");
- println("setupDebugging(&lexer);");
- }
- tabs--;
- println("}");
- println("");
-
- print(grammar.getClassName() + "::" + grammar.getClassName());
- println("("+namespaceAntlr+"TokenStream& lexer)");
- println(": " + sup + "(lexer," + grammar.maxk + ")");
- println("{");
- tabs++;
- println("setTokenNames(_tokenNames);");
- // if debugging, set up arrays and call the user-overridable
- // debugging setup method
- if ( grammar.debuggingOutput ) {
- println("setRuleNames(_ruleNames);");
- println("setSemPredNames(_semPredNames);");
- println("setupDebugging(&lexer);");
- }
- tabs--;
- println("}");
- println("");
-
- print(grammar.getClassName() + "::" + grammar.getClassName());
- println("(const "+namespaceAntlr+"ParserSharedInputState& state)");
- println(": " + sup + "(state," + grammar.maxk + ")");
- println("{");
- tabs++;
- println("setTokenNames(_tokenNames);");
- // if debugging, set up arrays and call the user-overridable
- // debugging setup method
- if ( grammar.debuggingOutput ) {
- println("setRuleNames(_ruleNames);");
- println("setSemPredNames(_semPredNames);");
- println("setupDebugging(&lexer);");
- }
- tabs--;
- println("}");
- println("");
-
- // Generate code for each rule in the grammar
- Enumeration ids = grammar.rules.elements();
- int ruleNum=0;
- while ( ids.hasMoreElements() ) {
- GrammarSymbol sym = (GrammarSymbol) ids.nextElement();
- if ( sym instanceof RuleSymbol) {
- RuleSymbol rs = (RuleSymbol)sym;
- genRule(rs, rs.references.size()==0, ruleNum++, grammar.getClassName() + "::");
- }
- exitIfError();
- }
- if ( usingCustomAST )
- {
-// println("void "+grammar.getClassName()+"::setASTNodeFactory("+labeledElementASTType+" (*factory)() )");
-// println("{");
-// println("}");
-// println("");
-
- // when we are using a custom ast override Parser::getAST to return the
- // custom AST type
- println(labeledElementASTType+" "+grammar.getClassName()+"::getAST()");
- println("{");
- println("\treturn returnAST;");
- println("}");
- println("");
- }
-
- // Generate the token names
- genTokenStrings(grammar.getClassName() + "::");
-
- // Generate the bitsets used throughout the grammar
- genBitsets(bitsetsUsed, grammar.tokenManager.maxTokenType(), grammar.getClassName() + "::", true);
-
- // Generate the semantic predicate map for debugging
- if (grammar.debuggingOutput)
- genSemPredMap(grammar.getClassName() + "::");
-
- // Close class definition
- println("");
- println("");
- if (nameSpace != null)
- nameSpace.emitClosures(currentOutput);
-
- // Close the parser output stream
- currentOutput.close();
- currentOutput = null;
- }
- public void genBody(TreeWalkerGrammar g) throws IOException {
- // Open the output stream for the parser and set the currentOutput
- outputFile = grammar.getClassName() + ".cpp";
- outputLine = 1;
- currentOutput = antlr.Tool.openOutputFile(outputFile);
- //SAS: changed for proper text file io
-
- genAST = grammar.buildAST;
- tabs = 0;
-
- // Generate the header common to all output files.
- genHeader(outputFile);
-
- printHeaderAction(preIncludeCpp);
-
- // Generate header for the parser
- println("#include \"" + grammar.getClassName() + ".hpp\"");
- println("#include \"antlr/Token.hpp\"");
- println("#include \"antlr/AST.hpp\"");
- println("#include \"antlr/NoViableAltException.hpp\"");
- println("#include \"antlr/MismatchedTokenException.hpp\"");
- println("#include \"antlr/SemanticException.hpp\"");
- println("#include \"antlr/BitSet.hpp\"");
-
- printHeaderAction(postIncludeCpp);
-
- if (nameSpace != null)
- nameSpace.emitDeclarations(currentOutput);
-
- // Output the user-defined parser premamble
- printAction(grammar.preambleAction);
-
- // Generate parser class definition
- String sup=null;
- if ( grammar.superClass!=null ) {
- sup = grammar.superClass;
- }
- else {
- sup = grammar.getSuperClass();
- if (sup.lastIndexOf('.') != -1)
- sup = sup.substring(sup.lastIndexOf('.')+1);
- sup = namespaceAntlr + sup;
- }
-
- // Generate default parser class constructor
- println(grammar.getClassName() + "::" + grammar.getClassName() + "()");
- println("\t: "+namespaceAntlr+"TreeParser() {");
- tabs++;
- println("setTokenNames(_tokenNames);");
- tabs--;
- println("}");
- println("");
-
- // Generate code for each rule in the grammar
- Enumeration ids = grammar.rules.elements();
- int ruleNum=0;
- String ruleNameInits = "";
- while ( ids.hasMoreElements() ) {
- GrammarSymbol sym = (GrammarSymbol) ids.nextElement();
- if ( sym instanceof RuleSymbol) {
- RuleSymbol rs = (RuleSymbol)sym;
- genRule(rs, rs.references.size()==0, ruleNum++, grammar.getClassName() + "::");
- }
- exitIfError();
- }
-
- if ( usingCustomAST )
- {
- // when we are using a custom ast override Parser::getAST to return the
- // custom AST type
- println(labeledElementASTType+" "+grammar.getClassName()+"::getAST()");
- println("{");
- println("\treturn returnAST;");
- println("}");
- println("");
- }
-
- // Generate the token names
- genTokenStrings(grammar.getClassName() + "::");
-
- // Generate the bitsets used throughout the grammar
- genBitsets(bitsetsUsed, grammar.tokenManager.maxTokenType(), grammar.getClassName() + "::", true);
-
- // Close class definition
- println("");
- println("");
-
- if (nameSpace != null)
- nameSpace.emitClosures(currentOutput);
-
- // Close the parser output stream
- currentOutput.close();
- currentOutput = null;
- }
- /** Generate a series of case statements that implement a BitSet test.
- * @param p The Bitset for which cases are to be generated
- */
- protected void genCases(BitSet p) {
- if ( DEBUG_CODE_GENERATOR ) System.out.println("genCases("+p+")");
- int[] elems;
-
- elems = p.toArray();
- // Wrap cases four-per-line for lexer, one-per-line for parser
- int wrap = 1; //(grammar instanceof LexerGrammar) ? 4 : 1;
- int j=1;
- boolean startOfLine = true;
- for (int i = 0; i < elems.length; i++) {
- if (j==1) {
- print("");
- } else {
- _print(" ");
- }
- _print("case " + getValueString(elems[i]) + ":");
-
- if (j==wrap) {
- _println("");
- startOfLine = true;
- j=1;
- }
- else {
- j++;
- startOfLine = false;
- }
- }
- if (!startOfLine) {
- _println("");
- }
- }
- /**Generate common code for a block of alternatives; return a postscript
- * that needs to be generated at the end of the block. Other routines
- * may append else-clauses and such for error checking before the postfix
- * is generated.
- * If the grammar is a lexer, then generate alternatives in an order where
- * alternatives requiring deeper lookahead are generated first, and
- * EOF in the lookahead set reduces the depth of the lookahead.
- * @param blk The block to generate
- * @param noTestForSingle If true, then it does not generate a test for a single alternative.
- */
- public CppBlockFinishingInfo genCommonBlock(
- AlternativeBlock blk,
- boolean noTestForSingle)
- {
- int nIF=0;
- boolean createdLL1Switch = false;
- int closingBracesOfIFSequence = 0;
- CppBlockFinishingInfo finishingInfo = new CppBlockFinishingInfo();
- if ( DEBUG_CODE_GENERATOR ) System.out.println("genCommonBlk("+blk+")");
-
- // Save the AST generation state, and set it to that of the block
- boolean savegenAST = genAST;
- genAST = genAST && blk.getAutoGen();
-
- boolean oldsaveTest = saveText;
- saveText = saveText && blk.getAutoGen();
-
- // Is this block inverted? If so, generate special-case code
- if (
- blk.not &&
- analyzer.subruleCanBeInverted(blk, grammar instanceof LexerGrammar)
- ) {
- Lookahead p = analyzer.look(1, blk);
- // Variable assignment for labeled elements
- if (blk.getLabel() != null && syntacticPredLevel == 0) {
- println(blk.getLabel() + " = " + lt1Value + ";");
- }
-
- // AST
- genElementAST(blk);
-
- String astArgs="";
- if (grammar instanceof TreeWalkerGrammar) {
- if( usingCustomAST )
- astArgs="static_cast<"+namespaceAntlr+"RefAST"+">(_t),";
- else
- astArgs="_t,";
- }
-
- // match the bitset for the alternative
- println("match(" + astArgs + getBitsetName(markBitsetForGen(p.fset)) + ");");
-
- // tack on tree cursor motion if doing a tree walker
- if (grammar instanceof TreeWalkerGrammar) {
- println("_t = _t->getNextSibling();");
- }
- return finishingInfo;
- }
-
- // Special handling for single alt
- if (blk.getAlternatives().size() == 1) {
- Alternative alt = blk.getAlternativeAt(0);
- // Generate a warning if there is a synPred for single alt.
- if (alt.synPred != null)
- {
- tool.warning(
- "Syntactic predicate superfluous for single alternative",
- grammar.getFilename(),
- blk.getAlternativeAt(0).synPred.getLine()
- );
- }
- if (noTestForSingle) {
- if (alt.semPred != null) {
- // Generate validating predicate
- genSemPred(alt.semPred, blk.line);
- }
- genAlt(alt, blk);
- return finishingInfo;
- }
- }
-
- // count number of simple LL(1) cases; only do switch for
- // many LL(1) cases (no preds, no end of token refs)
- // We don't care about exit paths for (...)*, (...)+
- // because we don't explicitly have a test for them
- // as an alt in the loop.
- //
- // Also, we now count how many unicode lookahead sets
- // there are--they must be moved to DEFAULT or ELSE
- // clause.
-
- int nLL1 = 0;
- for (int i=0; i<blk.getAlternatives().size(); i++) {
- Alternative a = blk.getAlternativeAt(i);
- if ( suitableForCaseExpression(a) ) {
- nLL1++;
- }
- }
-
- // do LL(1) cases
- if ( nLL1 >= makeSwitchThreshold) {
- // Determine the name of the item to be compared
- String testExpr = lookaheadString(1);
- createdLL1Switch = true;
- // when parsing trees, convert null to valid tree node with NULL lookahead
- if ( grammar instanceof TreeWalkerGrammar ) {
- println("if (_t == "+labeledElementASTInit+" )");
- tabs++;
- println("_t = ASTNULL;");
- tabs--;
- }
- println("switch ( "+testExpr+") {");
- for (int i=0; i<blk.alternatives.size(); i++) {
- Alternative alt = blk.getAlternativeAt(i);
- // ignore any non-LL(1) alts, predicated alts or end-of-token alts
- // or end-of-token alts for case expressions
- if ( !suitableForCaseExpression(alt) ) {
- continue;
- }
- Lookahead p = alt.cache[1];
- if (p.fset.degree() == 0 && !p.containsEpsilon()) {
- tool.warning("Alternate omitted due to empty prediction set",
- grammar.getFilename(),
- alt.head.getLine());
- }
- else {
- genCases(p.fset);
- println("{");
- tabs++;
- genAlt(alt, blk);
- println("break;");
- tabs--;
- println("}");
- }
- }
- println("default:");
- tabs++;
- }
-
- // do non-LL(1) and nondeterministic cases
- // This is tricky in the lexer, because of cases like:
- // STAR : '*' ;
- // ASSIGN_STAR : "*=";
- // Since nextToken is generated without a loop, then the STAR will
- // have end-of-token as it's lookahead set for LA(2). So, we must generate the
- // alternatives containing trailing end-of-token in their lookahead sets *after*
- // the alternatives without end-of-token. This implements the usual
- // lexer convention that longer matches come before shorter ones, e.g.
- // "*=" matches ASSIGN_STAR not STAR
- //
- // For non-lexer grammars, this does not sort the alternates by depth
- // Note that alts whose lookahead is purely end-of-token at k=1 end up
- // as default or else clauses.
- int startDepth = (grammar instanceof LexerGrammar) ? grammar.maxk : 0;
- for (int altDepth = startDepth; altDepth >= 0; altDepth--) {
- if ( DEBUG_CODE_GENERATOR ) System.out.println("checking depth "+altDepth);
- for (int i=0; i<blk.alternatives.size(); i++) {
- Alternative alt = blk.getAlternativeAt(i);
- if ( DEBUG_CODE_GENERATOR ) System.out.println("genAlt: "+i);
- // if we made a switch above, ignore what we already took care
- // of. Specifically, LL(1) alts with no preds
- // that do not have end-of-token in their prediction set
- if ( createdLL1Switch &&
- suitableForCaseExpression(alt) ) {
- if ( DEBUG_CODE_GENERATOR )
- System.out.println("ignoring alt because it was in the switch");
- continue;
- }
- String e;
-
- boolean unpredicted = false;
-
- if (grammar instanceof LexerGrammar) {
- // Calculate the "effective depth" of the alt, which is the max
- // depth at which cache[depth]!=end-of-token
- int effectiveDepth = alt.lookaheadDepth;
- if (effectiveDepth == GrammarAnalyzer.NONDETERMINISTIC) {
- // use maximum lookahead
- effectiveDepth = grammar.maxk;
- }
- while ( effectiveDepth >= 1 &&
- alt.cache[effectiveDepth].containsEpsilon() )
- {
- effectiveDepth--;
- }
- // Ignore alts whose effective depth is other than the ones we
- // are generating for this iteration.
- if (effectiveDepth != altDepth) {
- if ( DEBUG_CODE_GENERATOR )
- System.out.println("ignoring alt because effectiveDepth!=altDepth;"+effectiveDepth+"!="+altDepth);
- continue;
- }
- unpredicted = lookaheadIsEmpty(alt, effectiveDepth);
- e = getLookaheadTestExpression(alt, effectiveDepth);
- } else {
- unpredicted = lookaheadIsEmpty(alt, grammar.maxk);
- e = getLookaheadTestExpression(alt, grammar.maxk);
- }
-
- // Was it a big unicode range that forced unsuitability
- // for a case expression?
- if ( alt.cache[1].fset.degree() > caseSizeThreshold )
- {
- if ( nIF==0 )
- {
- // generate this only for the first if the elseif's
- // are covered by this one
- if ( grammar instanceof TreeWalkerGrammar ) {
- println("if (_t == "+labeledElementASTInit+" )");
- tabs++;
- println("_t = ASTNULL;");
- tabs--;
- }
- println("if " + e + " {");
- }
- else
- println("else if " + e + " {");
- }
- else if (unpredicted &&
- alt.semPred==null &&
- alt.synPred==null)
- {
- // The alt has empty prediction set and no
- // predicate to help out. if we have not
- // generated a previous if, just put {...} around
- // the end-of-token clause
- if ( nIF==0 ) {
- println("{");
- }
- else {
- println("else {");
- }
- finishingInfo.needAnErrorClause = false;
- }
- else
- {
- // check for sem and syn preds
- // Add any semantic predicate expression to the lookahead test
- if ( alt.semPred != null ) {
- // if debugging, wrap the evaluation of the predicate in a method
- //
- // translate $ and # references
- ActionTransInfo tInfo = new ActionTransInfo();
- String actionStr = processActionForTreeSpecifiers(alt.semPred,
- blk.line,
- currentRule,
- tInfo);
- // ignore translation info...we don't need to do anything with it.
-
- // call that will inform SemanticPredicateListeners of the result
- if (((grammar instanceof ParserGrammar) || (grammar instanceof LexerGrammar)) && grammar.debuggingOutput)
- e = "("+e+"&& fireSemanticPredicateEvaluated(antlr.debug.SemanticPredicateEvent.PREDICTING,"+ //FIXME
- addSemPred(charFormatter.escapeString(actionStr))+","+actionStr+"))";
- else
- e = "("+e+"&&("+actionStr +"))";
- }
-
- // Generate any syntactic predicates
- if ( nIF>0 ) {
- if ( alt.synPred != null ) {
- println("else {");
- tabs++;
- genSynPred( alt.synPred, e );
- closingBracesOfIFSequence++;
- }
- else {
- println("else if " + e + " {");
- }
- }
- else {
- if ( alt.synPred != null ) {
- genSynPred( alt.synPred, e );
- }
- else {
- // when parsing trees, convert null to valid tree node
- // with NULL lookahead.
- if ( grammar instanceof TreeWalkerGrammar ) {
- println("if (_t == "+labeledElementASTInit+" )");
- tabs++;
- println("_t = ASTNULL;");
- tabs--;
- }
- println("if " + e + " {");
- }
- }
-
- }
-
- nIF++;
- tabs++;
- genAlt(alt, blk);
- tabs--;
- println("}");
- }
- }
- String ps = "";
- for (int i=1; i<=closingBracesOfIFSequence; i++) {
- tabs--; // does JavaCodeGenerator need this?
- ps+="}";
- }
-
- // Restore the AST generation state
- genAST = savegenAST;
-
- // restore save text state
- saveText=oldsaveTest;
-
- // Return the finishing info.
- if ( createdLL1Switch ) {
- tabs--;
- finishingInfo.postscript = ps+"}";
- finishingInfo.generatedSwitch = true;
- finishingInfo.generatedAnIf = nIF>0;
- //return new CppBlockFinishingInfo(ps+"}",true,nIF>0); // close up switch statement
-
- }
- else {
- finishingInfo.postscript = ps;
- finishingInfo.generatedSwitch = false;
- finishingInfo.generatedAnIf = nIF>0;
- //return new CppBlockFinishingInfo(ps, false,nIF>0);
- }
- return finishingInfo;
- }
-
- private static boolean suitableForCaseExpression(Alternative a) {
- return a.lookaheadDepth == 1 &&
- a.semPred == null &&
- !a.cache[1].containsEpsilon() &&
- a.cache[1].fset.degree()<=caseSizeThreshold;
- }
-
- /** Generate code to link an element reference into the AST */
- private void genElementAST(AlternativeElement el) {
- // handle case where you're not building trees, but are in tree walker.
- // Just need to get labels set up.
- if ( grammar instanceof TreeWalkerGrammar && !grammar.buildAST ) {
- String elementRef;
- String astName;
-
- // Generate names and declarations of the AST variable(s)
- if (el.getLabel() == null) {
- elementRef = lt1Value;
- // Generate AST variables for unlabeled stuff
- astName = "tmp" + astVarNumber + "_AST";
- astVarNumber++;
- // Map the generated AST variable in the alternate
- mapTreeVariable(el, astName);
- // Generate an "input" AST variable also
- println(labeledElementASTType+" "+astName+"_in = "+elementRef+";");
- }
- return;
- }
-
- if (grammar.buildAST && syntacticPredLevel == 0) {
- boolean doNoGuessTest = (
- grammar.hasSyntacticPredicate &&
- (
- el.getLabel() != null ||
- el.getAutoGenType() != GrammarElement.AUTO_GEN_BANG
- )
- );
-
- String elementRef;
- String astName;
-
- // Generate names and declarations of the AST variable(s)
- if (el.getLabel() != null) {
- elementRef = el.getLabel();
- astName = el.getLabel() + "_AST";
- } else {
- elementRef = lt1Value;
- // Generate AST variables for unlabeled stuff
- astName = "tmp" + astVarNumber + "_AST";
- astVarNumber++;
- // Generate the declaration
- if ( el instanceof GrammarAtom ) {
- GrammarAtom ga = (GrammarAtom)el;
- if ( ga.getASTNodeType()!=null ) {
- println("Ref"+ga.getASTNodeType()+" " + astName + ";");
- }
- else {
- println(labeledElementASTType+" " + astName + " = "+labeledElementASTInit+";");
- }
- }
- else {
- println(labeledElementASTType+" " + astName + " = "+labeledElementASTInit+";");
- }
-
- // Map the generated AST variable in the alternate
- mapTreeVariable(el, astName);
- if (grammar instanceof TreeWalkerGrammar) {
- // Generate an "input" AST variable also
- println(labeledElementASTType+" " + astName + "_in = "+labeledElementASTInit+";");
- }
- }
-
- // Enclose actions with !guessing
- if (doNoGuessTest) {
- println("if (inputState->guessing==0) {");
- tabs++;
- }
-
- if (el.getLabel() != null) {
- if ( el instanceof GrammarAtom ) {
- println(astName + " = "+
- getASTCreateString((GrammarAtom)el,elementRef) + ";");
- }
- else {
- println(astName + " = "+
- getASTCreateString(elementRef) + ";");
- }
- } else {
- elementRef = lt1Value;
- if ( el instanceof GrammarAtom ) {
- println(astName + " = "+
- getASTCreateString((GrammarAtom)el,elementRef) + ";");
- }
- else {
- println(astName + " = "+
- getASTCreateString(elementRef) + ";");
- }
- // Map the generated AST variable in the alternate
- if (grammar instanceof TreeWalkerGrammar) {
- // set "input" AST variable also
- println(astName + "_in = " + elementRef + ";");
- }
- }
-
- if (genAST) {
- switch (el.getAutoGenType()) {
- case GrammarElement.AUTO_GEN_NONE:
- println("astFactory.addASTChild(currentAST, "+namespaceAntlr+"RefAST(" + astName + "));");
- break;
- case GrammarElement.AUTO_GEN_CARET:
-// println("astFactory.makeASTRoot(currentAST, " + astName + ");");
- println("astFactory.makeASTRoot(currentAST, "+namespaceAntlr+"RefAST(" + astName + "));");
- break;
- default:
- break;
- }
- }
- if (doNoGuessTest) {
- tabs--;
- println("}");
- }
- }
- }
- /** Close the try block and generate catch phrases
- * if the element has a labeled handler in the rule
- */
- private void genErrorCatchForElement(AlternativeElement el) {
- if (el.getLabel() == null) return;
- String r = el.enclosingRuleName;
- if ( grammar instanceof LexerGrammar ) {
- r = CodeGenerator.lexerRuleName(el.enclosingRuleName);
- }
- RuleSymbol rs = (RuleSymbol)grammar.getSymbol(r);
- if (rs == null) {
- tool.panic("Enclosing rule not found!");
- }
- ExceptionSpec ex = rs.block.findExceptionSpec(el.getLabel());
- if (ex != null) {
- tabs--;
- println("}");
- genErrorHandler(ex);
- }
- }
- /** Generate the catch phrases for a user-specified error handler */
- private void genErrorHandler(ExceptionSpec ex) {
- // Each ExceptionHandler in the ExceptionSpec is a separate catch
- for (int i = 0; i < ex.handlers.size(); i++)
- {
- ExceptionHandler handler = (ExceptionHandler)ex.handlers.elementAt(i);
- // Generate catch phrase
- println("catch (" + handler.exceptionTypeAndName.getText() + ") {");
- tabs++;
- if (grammar.hasSyntacticPredicate) {
- println("if (inputState->guessing==0) {");
- tabs++;
- }
-
- // When not guessing, execute user handler action
- genLineNo(handler.action);
- printAction(
- processActionForTreeSpecifiers(handler.action.getText(), 0, currentRule, null)
- );
- genLineNo2();
-
- if (grammar.hasSyntacticPredicate) {
- tabs--;
- println("} else {");
- tabs++;
- // When guessing, rethrow exception
- println(
- "throw " +
- extractIdOfAction(handler.exceptionTypeAndName) +
- ";"
- );
- tabs--;
- println("}");
- }
- // Close catch phrase
- tabs--;
- println("}");
- }
- }
- /** Generate a try { opening if the element has a labeled handler in the rule */
- private void genErrorTryForElement(AlternativeElement el) {
- if (el.getLabel() == null) return;
- String r = el.enclosingRuleName;
- if ( grammar instanceof LexerGrammar ) {
- r = CodeGenerator.lexerRuleName(el.enclosingRuleName);
- }
- RuleSymbol rs = (RuleSymbol)grammar.getSymbol(r);
- if (rs == null) {
- tool.panic("Enclosing rule not found!");
- }
- ExceptionSpec ex = rs.block.findExceptionSpec(el.getLabel());
- if (ex != null) {
- println("try { // for error handling");
- tabs++;
- }
- }
- /** Generate a header that is common to all C++ files */
- protected void genHeader(String fileName)
- {
- println("/* $ANTLR "+Tool.version+": "+
- "\""+Tool.fileMinusPath(tool.grammarFile)+"\""+
- " -> "+
- "\""+fileName+"\"$ */");
- }
- // these are unique to C++ mode
-
- public void genInclude(LexerGrammar g) throws IOException {
- outputFile = grammar.getClassName() + ".hpp";
- outputLine = 1;
- currentOutput = antlr.Tool.openOutputFile(outputFile);
- //SAS: changed for proper text file io
-
- genAST = false; // no way to gen trees.
- saveText = true; // save consumed characters.
-
- tabs=0;
-
- // Generate a guard wrapper
- println("#ifndef INC_"+grammar.getClassName()+"_hpp_");
- println("#define INC_"+grammar.getClassName()+"_hpp_");
- println("");
-
- printHeaderAction(preIncludeHpp);
-
- println("#include \"antlr/config.hpp\"");
-
- // Generate header common to all C++ output files
- genHeader(outputFile);
-
- // Generate header specific to lexer header file
- println("#include \"antlr/CommonToken.hpp\"");
- println("#include \"antlr/InputBuffer.hpp\"");
- println("#include \"antlr/BitSet.hpp\"");
- println("#include \"" + grammar.tokenManager.getName() + TokenTypesFileSuffix+".hpp\"");
-
- // Find the name of the super class
- String sup=null;
- if ( grammar.superClass!=null ) {
- sup = grammar.superClass;
- println("#include \""+sup+".hpp\"");
- }
- else {
- sup = grammar.getSuperClass();
- if (sup.lastIndexOf('.') != -1)
- sup = sup.substring(sup.lastIndexOf('.')+1);
- println("#include \"antlr/"+sup+".hpp\"");
- sup = namespaceAntlr + sup;
- }
-
- // Do not use printAction because we assume tabs==0
- printHeaderAction(postIncludeHpp);
-
- if (nameSpace != null)
- nameSpace.emitDeclarations(currentOutput);
-
- printHeaderAction("");
-
- // print javadoc comment if any
- if ( grammar.comment!=null ) {
- _println(grammar.comment);
- }
-
- // Generate lexer class definition
- print("class " + grammar.getClassName() + " : public " + sup);
- println(", public " + grammar.tokenManager.getName() + TokenTypesFileSuffix);
-
- Token tsuffix = (Token)grammar.options.get("classHeaderSuffix");
- if ( tsuffix != null ) {
- String suffix = Tool.stripFrontBack(tsuffix.getText(),"\"","\"");
- if ( suffix != null ) {
- print(", "+suffix); // must be an interface name for Java
- }
- }
- println(" {");
-
- // Generate user-defined lexer class members
- if (grammar.classMemberAction != null) {
- genLineNo(grammar.classMemberAction);
- print(
- processActionForTreeSpecifiers(grammar.classMemberAction.getText(), 0, currentRule, null)
- );
- genLineNo2();
- }
-
- // Generate initLiterals() method
- tabs=0;
- println("private:");
- tabs=1;
- println("void initLiterals();");
-
- // Generate getCaseSensitiveLiterals() method
- tabs=0;
- println("public:");
- tabs=1;
- println("bool getCaseSensitiveLiterals() const;");
-
- // Make constructors public
- tabs=0;
- println("public:");
- tabs=1;
-
- // Generate the constructor from std::istream
- println(grammar.getClassName() + "(" + namespaceStd + "istream& in);");
-
- // Generate the constructor from InputBuffer
- println(grammar.getClassName() + "("+namespaceAntlr+"InputBuffer& ib);");
-
- println(grammar.getClassName() + "(const "+namespaceAntlr+"LexerSharedInputState& state);");
-
- // Generate nextToken() rule.
- // nextToken() is a synthetic lexer rule that is the implicit OR of all
- // user-defined lexer rules.
- println(namespaceAntlr+"RefToken nextToken();");
-
- // Generate code for each rule in the lexer
- Enumeration ids = grammar.rules.elements();
- while ( ids.hasMoreElements() ) {
- RuleSymbol sym = (RuleSymbol) ids.nextElement();
- // Don't generate the synthetic rules
- if (!sym.getId().equals("mnextToken")) {
- genRuleHeader(sym, false);
- }
- exitIfError();
- }
-
- // Make the rest private
- tabs=0;
- println("private:");
- tabs=1;
-
- // generate the rule name array for debugging
- if ( grammar.debuggingOutput ) {
- println("static const char* _ruleNames[];");
- }
-
- // Generate the semantic predicate map for debugging
- if (grammar.debuggingOutput)
- println("static const char* _semPredNames[];");
-
- // Generate the bitsets used throughout the lexer
- genBitsetsHeader(bitsetsUsed, ((LexerGrammar)grammar).charVocabulary.size());
-
- tabs=0;
- println("};");
- println("");
- if (nameSpace != null)
- nameSpace.emitClosures(currentOutput);
-
- // Generate a guard wrapper
- println("#endif /*INC_"+grammar.getClassName()+"_hpp_*/");
-
- // Close the lexer output stream
- currentOutput.close();
- currentOutput = null;
- }
- public void genInclude(ParserGrammar g) throws IOException {
- // Open the output stream for the parser and set the currentOutput
- outputFile = grammar.getClassName() + ".hpp";
- outputLine = 1;
- currentOutput = antlr.Tool.openOutputFile(outputFile);
- //SAS: changed for proper text file io
-
- genAST = grammar.buildAST;
-
- tabs = 0;
-
- // Generate a guard wrapper
- println("#ifndef INC_"+grammar.getClassName()+"_hpp_");
- println("#define INC_"+grammar.getClassName()+"_hpp_");
- println("");
- printHeaderAction(preIncludeHpp);
- println("#include \"antlr/config.hpp\"");
-
- // Generate the header common to all output files.
- genHeader(outputFile);
-
- // Generate header for the parser
- println("#include \"antlr/TokenStream.hpp\"");
- println("#include \"antlr/TokenBuffer.hpp\"");
- println("#include \"" + grammar.tokenManager.getName() + TokenTypesFileSuffix+".hpp\"");
-
- // Generate parser class definition
- String sup=null;
- if ( grammar.superClass!=null ) {
- sup = grammar.superClass;
- println("#include \""+sup+".hpp\"");
- }
- else {
- sup = grammar.getSuperClass();
- if (sup.lastIndexOf('.') != -1)
- sup = sup.substring(sup.lastIndexOf('.')+1);
- println("#include \"antlr/"+sup+".hpp\"");
- sup = namespaceAntlr + sup;
- }
- println("");
-
- // Do not use printAction because we assume tabs==0
- printHeaderAction(postIncludeHpp);
-
- if (nameSpace != null)
- nameSpace.emitDeclarations(currentOutput);
-
- printHeaderAction("");
-
- // print javadoc comment if any
- if ( grammar.comment!=null ) {
- _println(grammar.comment);
- }
-
- // generate the actual class definition
- print("class " + grammar.getClassName() + " : public " + sup);
- println(", public " + grammar.tokenManager.getName() + TokenTypesFileSuffix);
-
- Token tsuffix = (Token)grammar.options.get("classHeaderSuffix");
- if ( tsuffix != null ) {
- String suffix = Tool.stripFrontBack(tsuffix.getText(),"\"","\"");
- if ( suffix != null )
- print(", "+suffix); // must be an interface name for Java
- }
- println(" {");
-
- // set up an array of all the rule names so the debugger can
- // keep track of them only by number -- less to store in tree...
- if (grammar.debuggingOutput) {
- println("public: static const char* _ruleNames[];");
- }
-
- // Generate user-defined parser class members
- if (grammar.classMemberAction != null) {
- genLineNo(grammar.classMemberAction.getLine());
- print(
- processActionForTreeSpecifiers(grammar.classMemberAction.getText(), 0, currentRule, null)
- );
- genLineNo2();
- }
-
- // Generate parser class constructor from TokenBuffer
- tabs=0;
- println("protected:");
- tabs=1;
- println(grammar.getClassName() + "("+namespaceAntlr+"TokenBuffer& tokenBuf, int k);");
- tabs=0;
- println("public:");
- tabs=1;
- println(grammar.getClassName() + "("+namespaceAntlr+"TokenBuffer& tokenBuf);");
-
- // Generate parser class constructor from TokenStream
- tabs=0;
- println("protected:");
- tabs=1;
- println(grammar.getClassName()+"("+namespaceAntlr+"TokenStream& lexer, int k);");
- tabs=0;
- println("public:");
- tabs=1;
- println(grammar.getClassName()+"("+namespaceAntlr+"TokenStream& lexer);");
-
- println(grammar.getClassName()+"(const "+namespaceAntlr+"ParserSharedInputState& state);");
-
- // Generate code for each rule in the grammar
- Enumeration ids = grammar.rules.elements();
- while ( ids.hasMoreElements() ) {
- GrammarSymbol sym = (GrammarSymbol) ids.nextElement();
- if ( sym instanceof RuleSymbol) {
- RuleSymbol rs = (RuleSymbol)sym;
- genRuleHeader(rs, rs.references.size()==0);
- }
- exitIfError();
- }
-
- if ( usingCustomAST )
- {
- // when we are using a custom ast override Parser::getAST to return the
- // custom AST type
- tabs=0; println("public:"); tabs=1;
- println(labeledElementASTType+" getAST();");
- println("");
- tabs=0; println("protected:"); tabs=1;
- println(labeledElementASTType+" returnAST;");
- }
-
- // Make the rest private
- tabs=0;
- println("private:");
- tabs=1;
-
- // Generate the token names
- println("static const char* _tokenNames[];");
-
- // Generate the bitsets used throughout the grammar
- genBitsetsHeader(bitsetsUsed, grammar.tokenManager.maxTokenType());
-
- // Generate the semantic predicate map for debugging
- if (grammar.debuggingOutput)
- println("static const char* _semPredNames[];");
-
- // Close class definition
- tabs=0;
- println("};");
- println("");
- if (nameSpace != null)
- nameSpace.emitClosures(currentOutput);
-
- // Generate a guard wrapper
- println("#endif /*INC_"+grammar.getClassName()+"_hpp_*/");
-
- // Close the parser output stream
- currentOutput.close();
- currentOutput = null;
- }
- public void genInclude(TreeWalkerGrammar g) throws IOException {
- // Open the output stream for the parser and set the currentOutput
- outputFile = grammar.getClassName() + ".hpp";
- outputLine = 1;
- currentOutput = antlr.Tool.openOutputFile(outputFile);
- //SAS: changed for proper text file io
-
- genAST = grammar.buildAST;
- tabs = 0;
-
- // Generate a guard wrapper
- println("#ifndef INC_"+grammar.getClassName()+"_hpp_");
- println("#define INC_"+grammar.getClassName()+"_hpp_");
- println("");
- printHeaderAction(preIncludeHpp);
- println("#include \"antlr/config.hpp\"");
- println("#include \"" + grammar.tokenManager.getName() + TokenTypesFileSuffix+".hpp\"");
-
- // Generate the header common to all output files.
- genHeader(outputFile);
-
- // Find the name of the super class
- String sup=null;
- if ( grammar.superClass!=null ) {
- sup = grammar.superClass;
- println("#include \""+sup+".hpp\"");
- }
- else {
- sup = grammar.getSuperClass();
- if (sup.lastIndexOf('.') != -1)
- sup = sup.substring(sup.lastIndexOf('.')+1);
- println("#include \"antlr/"+sup+".hpp\"");
- sup = namespaceAntlr + sup;
- }
- println("");
-
- // Generate header for the parser
-
- // Do not use printAction because we assume tabs==0
- printHeaderAction(postIncludeHpp);
-
- if (nameSpace != null)
- nameSpace.emitDeclarations(currentOutput);
-
- printHeaderAction("");
-
- // print javadoc comment if any
- if ( grammar.comment!=null ) {
- _println(grammar.comment);
- }
-
- // Generate parser class definition
- print("class " + grammar.getClassName() + " : public "+sup);
- println(", public " + grammar.tokenManager.getName() + TokenTypesFileSuffix);
-
- Token tsuffix = (Token)grammar.options.get("classHeaderSuffix");
- if ( tsuffix != null ) {
- String suffix = Tool.stripFrontBack(tsuffix.getText(),"\"","\"");
- if ( suffix != null ) {
- print(", "+suffix); // must be an interface name for Java
- }
- }
- println(" {");
-
- // Generate user-defined parser class members
- if (grammar.classMemberAction != null) {
- genLineNo(grammar.classMemberAction.getLine());
- print(
- processActionForTreeSpecifiers(grammar.classMemberAction.getText(), 0, currentRule, null)
- );
- genLineNo2();
- }
-
- // Generate default parser class constructor
- tabs=0;
- println("public:");
- tabs=1;
- println(grammar.getClassName() + "();");
-
- // Generate code for each rule in the grammar
- Enumeration ids = grammar.rules.elements();
- String ruleNameInits = "";
- while ( ids.hasMoreElements() ) {
- GrammarSymbol sym = (GrammarSymbol) ids.nextElement();
- if ( sym instanceof RuleSymbol) {
- RuleSymbol rs = (RuleSymbol)sym;
- genRuleHeader(rs, rs.references.size()==0);
- }
- exitIfError();
- }
- if ( usingCustomAST )
- {
- // when we are using a custom ast override TreeParser::getAST to return the
- // custom AST type
- tabs=0; println("public:"); tabs=1;
- println(labeledElementASTType+" getAST();");
- println("");
- tabs=0; println("protected:"); tabs=1;
- println(labeledElementASTType+" returnAST;");
- println(labeledElementASTType+" _retTree;");
- }
-
- // Make the rest private
- tabs=0;
- println("private:");
- tabs=1;
-
- // Generate the token names
- println("static const char* _tokenNames[];");
-
- // Generate the bitsets used throughout the grammar
- genBitsetsHeader(bitsetsUsed, grammar.tokenManager.maxTokenType());
-
- // Close class definition
- tabs=0;
- println("};");
- println("");
- if (nameSpace != null)
- nameSpace.emitClosures(currentOutput);
-
- // Generate a guard wrapper
- println("#endif /*INC_"+grammar.getClassName()+"_hpp_*/");
-
- // Close the parser output stream
- currentOutput.close();
- currentOutput = null;
- }
- private void genLiteralsTest() {
- println("_ttype = testLiteralsTable(_ttype);");
- }
- private void genLiteralsTestForPartialToken() {
- println("_ttype = testLiteralsTable(text.substr(_begin, text.length()-_begin),_ttype);");
- }
- protected void genMatch(BitSet b) {
- }
- protected void genMatch(GrammarAtom atom) {
- if ( atom instanceof StringLiteralElement ) {
- if ( grammar instanceof LexerGrammar ) {
- genMatchUsingAtomText(atom);
- }
- else {
- genMatchUsingAtomTokenType(atom);
- }
- }
- else if ( atom instanceof CharLiteralElement ) {
- if ( grammar instanceof LexerGrammar ) {
- genMatchUsingAtomText(atom);
- }
- else {
- tool.error("cannot ref character literals in grammar: "+atom);
- }
- }
- else if ( atom instanceof TokenRefElement ) {
- genMatchUsingAtomText(atom);
- }
- }
- protected void genMatchUsingAtomText(GrammarAtom atom) {
- // match() for trees needs the _t cursor
- String astArgs="";
- if (grammar instanceof TreeWalkerGrammar) {
- if( usingCustomAST )
- astArgs="static_cast<"+namespaceAntlr+"RefAST"+">(_t),";
- else
- astArgs="_t,";
- }
-
- // if in lexer and ! on element, save buffer index to kill later
- if ( grammar instanceof LexerGrammar && (!saveText||atom.getAutoGenType()==GrammarElement.AUTO_GEN_BANG) ) {
- println("_saveIndex=text.length();");
- }
-
- print(atom.not ? "matchNot(" : "match(");
- _print(astArgs);
-
- // print out what to match
- if (atom.atomText.equals("EOF")) {
- // horrible hack to handle EOF case
- _print(namespaceAntlr+"Token::EOF_TYPE");
- }
- else {
- _print(textOrChar(atom.atomText));
- }
- _println(");");
-
- if ( grammar instanceof LexerGrammar && (!saveText||atom.getAutoGenType()==GrammarElement.AUTO_GEN_BANG) ) {
- println("text.erase(_saveIndex);"); // kill text atom put in buffer
- }
- }
- protected void genMatchUsingAtomTokenType(GrammarAtom atom) {
- // match() for trees needs the _t cursor
- String astArgs="";
- if (grammar instanceof TreeWalkerGrammar) {
- if( usingCustomAST )
- astArgs="static_cast<"+namespaceAntlr+"RefAST"+">(_t),";
- else
- astArgs="_t,";
- }
-
- // If the literal can be mangled, generate the symbolic constant instead
- String mangledName = null;
- String s = astArgs + getValueString(atom.getType());
-
- // matching
- println( (atom.not ? "matchNot(" : "match(") + s + ");");
- }
- /** Generate the nextToken() rule.
- * nextToken() is a synthetic lexer rule that is the implicit OR of all
- * user-defined lexer rules.
- * @param RuleBlock
- */
- public void genNextToken() {
- // Are there any public rules? If not, then just generate a
- // fake nextToken().
- boolean hasPublicRules = false;
- for (int i = 0; i < grammar.rules.size(); i++) {
- RuleSymbol rs = (RuleSymbol)grammar.rules.elementAt(i);
- if ( rs.isDefined() && rs.access.equals("public") ) {
- hasPublicRules = true;
- break;
- }
- }
- if (!hasPublicRules) {
- println("");
- println(namespaceAntlr+"RefToken "+grammar.getClassName()+"::nextToken() { return "+namespaceAntlr+"RefToken(new "+namespaceAntlr+"CommonToken("+namespaceAntlr+"Token::EOF_TYPE, \"\")); }");
- println("");
- return;
- }
+import antlr.collections.impl.BitSet;
+import antlr.collections.impl.Vector;
- // Create the synthesized nextToken() rule
- RuleBlock nextTokenBlk = MakeGrammar.createNextTokenRule(grammar, grammar.rules, "nextToken");
- // Define the nextToken rule symbol
- RuleSymbol nextTokenRs = new RuleSymbol("mnextToken");
- nextTokenRs.setDefined();
- nextTokenRs.setBlock(nextTokenBlk);
- nextTokenRs.access = "private";
- grammar.define(nextTokenRs);
- // Analyze the nextToken rule
- boolean ok = grammar.theLLkAnalyzer.deterministic(nextTokenBlk);
-
- // Generate the next token rule
- String filterRule=null;
- if ( ((LexerGrammar)grammar).filterMode ) {
- filterRule = ((LexerGrammar)grammar).filterRule;
- }
+import java.io.IOException;
+import java.util.Enumeration;
+import java.util.Hashtable;
- println("");
- println(namespaceAntlr+"RefToken "+grammar.getClassName()+"::nextToken()");
- println("{");
- tabs++;
- println(namespaceAntlr+"RefToken theRetToken;");
- println("for (;;) {");
- tabs++;
- println(namespaceAntlr+"RefToken theRetToken;");
- println("int _ttype = "+namespaceAntlr+"Token::INVALID_TYPE;");
- if ( ((LexerGrammar)grammar).filterMode ) {
- println("setCommitToPath(false);");
- if ( filterRule!=null ) {
- // Here's a good place to ensure that the filter rule actually exists
- if ( !grammar.isDefined(CodeGenerator.lexerRuleName(filterRule)) ) {
- grammar.tool.error("Filter rule "+filterRule+" does not exist in this lexer");
- }
- else {
- RuleSymbol rs = (RuleSymbol)grammar.getSymbol(CodeGenerator.lexerRuleName(filterRule));
- if ( !rs.isDefined() ) {
- grammar.tool.error("Filter rule "+filterRule+" does not exist in this lexer");
- }
- else if ( rs.access.equals("public") ) {
- grammar.tool.error("Filter rule "+filterRule+" must be protected");
- }
- }
- println("int _m;");
- println("_m = mark();");
- }
- }
- println("resetText();");
+/**Generate MyParser.cpp, MyParser.hpp, MyLexer.cpp, MyLexer.hpp and MyParserTokenTypes.hpp */
+public class CppCodeGenerator extends CodeGenerator {
- println("try { // for char stream error handling");
- tabs++;
+ // non-zero if inside syntactic predicate generation
+ protected int syntacticPredLevel = 0;
- // Generate try around whole thing to trap scanner errors
- println("try { // for lexical error handling");
- tabs++;
-
- // Test for public lexical rules with empty paths
- for (int i=0; i<nextTokenBlk.getAlternatives().size(); i++) {
- Alternative a = nextTokenBlk.getAlternativeAt(i);
- if ( a.cache[1].containsEpsilon() ) {
- tool.warning("found optional path in nextToken()");
- }
- }
+ // Are we generating ASTs (for parsers and tree parsers) right now?
+ protected boolean genAST = false;
- // Generate the block
- String newline = System.getProperty("line.separator");
- CppBlockFinishingInfo howToFinish = genCommonBlock(nextTokenBlk, false);
- String errFinish = "if (LA(1)==EOF_CHAR) {uponEOF(); _returnToken = makeToken("+namespaceAntlr+"Token::EOF_TYPE);}";
- errFinish += newline+"\t\t\t\t";
- if ( ((LexerGrammar)grammar).filterMode ) {
- if ( filterRule==null ) {
- errFinish += "else {consume(); goto tryAgain;}";
- }
- else {
- errFinish += "else {"+newline+
- "\t\t\t\t\tcommit();"+newline+
- "\t\t\t\t\ttry {m"+filterRule+"(false);}"+newline+
- "\t\t\t\t\tcatch("+namespaceAntlr+"RecognitionException& e) {"+newline+
- "\t\t\t\t\t // catastrophic failure"+newline+
- "\t\t\t\t\t reportError(e);"+newline+
- "\t\t\t\t\t consume();"+newline+
- "\t\t\t\t\t}"+newline+
- "\t\t\t\t\tgoto tryAgain;"+newline+
- "\t\t\t\t}";
- }
- }
- else {
- errFinish += "else {"+throwNoViable+"}";
- }
- genBlockFinish(howToFinish, errFinish);
+ // Are we saving the text consumed (for lexers) right now?
+ protected boolean saveText = false;
- // at this point a valid token has been matched, undo "mark" that was done
- if ( ((LexerGrammar)grammar).filterMode && filterRule!=null ) {
- println("commit();");
- }
+ // Generate #line's
+ protected boolean genHashLines = true;
+ // Used to keep track of lineno in output
+ protected int outputLine;
+
+ protected String outputFile;
+
+ // Grammar parameters set up to handle different grammar classes.
+ // These are used to get instanceof tests out of code generation
+ boolean usingCustomAST = false;
+
+ String labeledElementType;
+
+ String labeledElementASTType; // mostly the same as labeledElementType except in parsers
+
+ String labeledElementASTInit;
+
+ String labeledElementInit;
+
+ String commonExtraArgs;
+
+ String commonExtraParams;
+
+ String commonLocalVars;
+
+ String lt1Value;
+
+ String exceptionThrown;
+
+ String throwNoViable;
+
+ // Tracks the rule being generated. Used for mapTreeId
+ RuleBlock currentRule;
+ // Tracks the rule or labeled subrule being generated. Used for AST generation.
+ String currentASTResult;
+ // Mapping between the ids used in the current alt, and the
+ // names of variables used to represent their AST values.
+ Hashtable treeVariableMap = new Hashtable();
+ // Count of unnamed generated variables
+ int astVarNumber = 1;
+ // Special value used to mark duplicate in treeVariableMap
+ protected static final String NONUNIQUE = new String();
+
+ public static final int caseSizeThreshold = 127; // ascii is max
+
+ private Vector semPreds;
+
+ private static String namespaceStd = "ANTLR_USE_NAMESPACE(std)";
+
+ private static String namespaceAntlr = "ANTLR_USE_NAMESPACE(antlr)";
+
+ private static NameSpace nameSpace = null;
+
+ private static final String preIncludeCpp = "pre_include_cpp";
+
+ private static final String preIncludeHpp = "pre_include_hpp";
+
+ private static final String postIncludeCpp = "post_include_cpp";
+
+ private static final String postIncludeHpp = "post_include_hpp";
+
+ /** Create a C++ code-generator using the given Grammar.
+ * The caller must still call setTool, setBehavior, and setAnalyzer
+ * before generating code.
+ */
+ public CppCodeGenerator() {
+ super();
+ charFormatter = new CppCharFormatter();
+ }
+
+ /** Adds a semantic predicate string to the sem pred vector
+ These strings will be used to build an array of sem pred names
+ when building a debugging parser. This method should only be
+ called when the debug option is specified
+ */
+ protected int addSemPred(String predicate) {
+ semPreds.appendElement(predicate);
+ return semPreds.size() - 1;
+ }
+
+ public void exitIfError() {
+ if (tool.hasError) {
+ System.out.println("Exiting due to errors.");
+ System.exit(1);
+ }
+ }
+
+ protected int countLines(String s) {
+ int lines = 0;
+ for (int i = 0; i < s.length(); i++) {
+ if (s.charAt(i) == '\n')
+ lines++;
+ }
+ return lines;
+ }
+
+ /** Output a String to the currentOutput stream.
+ * Ignored if string is null.
+ * @param s The string to output
+ */
+ protected void _print(String s) {
+ if (s != null) {
+ outputLine += countLines(s);
+ currentOutput.print(s);
+ }
+ }
+
+ /** Print an action without leading tabs, attempting to
+ * preserve the current indentation level for multi-line actions
+ * Ignored if string is null.
+ * @param s The action string to output
+ */
+ protected void _printAction(String s) {
+ if (s != null) {
+ outputLine += countLines(s) + 1;
+ super._printAction(s);
+ }
+ }
+
+ /** Print an action stored in a token surrounded by #line stuff */
+ public void printAction(Token t) {
+ if (t != null) {
+ genLineNo(t.getLine());
+ printTabs();
+ _printAction(t.getText());
+ genLineNo2();
+ }
+ }
+
+ /** Print a header action by #line stuff
+ * @param name The name of the header part
+ */
+ public void printHeaderAction(String name) {
+ Token a = (antlr.Token)behavior.headerActions.get(name);
+ if (a != null) {
+ genLineNo(a.getLine());
+ println(a.getText());
+ genLineNo2();
+ }
+ }
+
+ /** Output a String followed by newline, to the currentOutput stream.
+ * Ignored if string is null.
+ * @param s The string to output
+ */
+ protected void _println(String s) {
+ if (s != null) {
+ outputLine += countLines(s) + 1;
+ currentOutput.println(s);
+ }
+ }
+
+ /** Output tab indent followed by a String followed by newline,
+ * to the currentOutput stream. Ignored if string is null.
+ * @param s The string to output
+ */
+ protected void println(String s) {
+ if (s != null) {
+ printTabs();
+ outputLine += countLines(s) + 1;
+ currentOutput.println(s);
+ }
+ }
+
+ /** Generate a #line or // line depending on options */
+ public void genLineNo(int line) {
+ if (line == 0) {
+ line++;
+ }
+ if (genHashLines)
+ _println("#line " + line + " \"" + Tool.fileMinusPath(tool.grammarFile) + "\"");
+ else
+ println("// line " + line + " \"" + Tool.fileMinusPath(tool.grammarFile) + "\"");
+ }
+
+ /** Generate a #line or // line depending on options */
+ public void genLineNo(GrammarElement el) {
+ if (el != null)
+ genLineNo(el.getLine());
+ }
+
+ /** Generate a #line or // line depending on options */
+ public void genLineNo(Token t) {
+ if (t != null)
+ genLineNo(t.getLine());
+ }
+
+ /** Generate a #line or // line depending on options */
+ public void genLineNo2() {
+ if (genHashLines) {
+ _println("#line " + (outputLine + 1) + " \"" + outputFile + "\"");
+ }
+ }
+
+ /**Generate the parser, lexer, treeparser, and token types in C++ */
+ public void gen() {
+ // Do the code generation
+ try {
+ // Loop over all grammars
+ Enumeration grammarIter = behavior.grammars.elements();
+ while (grammarIter.hasMoreElements()) {
+ Grammar g = (Grammar)grammarIter.nextElement();
+ // Connect all the components to each other
+ g.setGrammarAnalyzer(analyzer);
+ g.setCodeGenerator(this);
+ analyzer.setGrammar(g);
+ // To get right overloading behavior across hetrogeneous grammars
+ setupGrammarParameters(g);
+ g.generate();
+ exitIfError();
+ }
+
+ // Loop over all token managers (some of which are lexers)
+ Enumeration tmIter = behavior.tokenManagers.elements();
+ while (tmIter.hasMoreElements()) {
+ TokenManager tm = (TokenManager)tmIter.nextElement();
+ if (!tm.isReadOnly()) {
+ // Write the token manager tokens as C++
+ // this must appear before genTokenInterchange so that
+ // labels are set on string literals
+ genTokenTypes(tm);
+ // Write the token manager tokens as plain text
+ genTokenInterchange(tm);
+ }
+ exitIfError();
+ }
+ } catch (IOException e) {
+ System.out.println(e.getMessage());
+ }
+ }
+
+ /** Generate code for the given grammar element.
+ * @param blk The {...} action to generate
+ */
+ public void gen(ActionElement action) {
+ if (DEBUG_CODE_GENERATOR) System.out.println("genAction(" + action + ")");
+ if (action.isSemPred) {
+ genSemPred(action.actionText, action.line);
+ } else {
+ if (grammar.hasSyntacticPredicate) {
+ println("if ( inputState->guessing==0 ) {");
+ tabs++;
+ }
+
+ ActionTransInfo tInfo = new ActionTransInfo();
+ String actionStr = processActionForTreeSpecifiers(action.actionText, action.getLine(), currentRule, tInfo);
+
+ if (tInfo.refRuleRoot != null) {
+ // Somebody referenced "#rule", make sure translated var is valid
+ // assignment to #rule is left as a ref also, meaning that assignments
+ // with no other refs like "#rule = foo();" still forces this code to be
+ // generated (unnecessarily).
+ println(tInfo.refRuleRoot + " = " + labeledElementASTType + "(currentAST.root);");
+ }
+
+ // dump the translated action
+ genLineNo(action);
+ printAction(actionStr);
+ genLineNo2();
+
+ if (tInfo.assignToRoot) {
+ // Somebody did a "#rule=", reset internal currentAST.root
+ println("currentAST.root = " + tInfo.refRuleRoot + ";");
+ // reset the child pointer too to be last sibling in sibling list
+ // now use if else in stead of x ? y : z to shut CC 4.2 up.
+ println("if ( " + tInfo.refRuleRoot + "!=" + labeledElementASTInit + " &&");
+ tabs++;
+ println(tInfo.refRuleRoot + "->getFirstChild() != " + labeledElementASTInit + " )");
+ println(" currentAST.child = " + tInfo.refRuleRoot + "->getFirstChild();");
+ tabs--;
+ println("else");
+ tabs++;
+ println("currentAST.child = " + tInfo.refRuleRoot + ";");
+ tabs--;
+ println("currentAST.advanceChildToEnd();");
+ }
+
+ if (grammar.hasSyntacticPredicate) {
+ tabs--;
+ println("}");
+ }
+ }
+ }
+
+ /** Generate code for the given grammar element.
+ * @param blk The "x|y|z|..." block to generate
+ */
+ public void gen(AlternativeBlock blk) {
+ if (DEBUG_CODE_GENERATOR) System.out.println("gen(" + blk + ")");
+ println("{");
+ genBlockPreamble(blk);
+
+ // Tell AST generation to build subrule result
+ String saveCurrentASTResult = currentASTResult;
+ if (blk.getLabel() != null) {
+ currentASTResult = blk.getLabel();
+ }
+
+ boolean ok = grammar.theLLkAnalyzer.deterministic(blk);
+
+ CppBlockFinishingInfo howToFinish = genCommonBlock(blk, true);
+ genBlockFinish(howToFinish, throwNoViable);
+
+ println("}");
+
+ // Restore previous AST generation
+ currentASTResult = saveCurrentASTResult;
+ }
+
+ /** Generate code for the given grammar element.
+ * @param blk The block-end element to generate. Block-end
+ * elements are synthesized by the grammar parser to represent
+ * the end of a block.
+ */
+ public void gen(BlockEndElement end) {
+ if (DEBUG_CODE_GENERATOR) System.out.println("genRuleEnd(" + end + ")");
+ }
+
+ /** Generate code for the given grammar element.
+ * @param blk The character literal reference to generate
+ */
+ public void gen(CharLiteralElement atom) {
+ if (DEBUG_CODE_GENERATOR) System.out.println("genChar(" + atom + ")");
+
+ if (atom.getLabel() != null) {
+ println(atom.getLabel() + " = " + lt1Value + ";");
+ }
+
+ boolean oldsaveText = saveText;
+ saveText = saveText && atom.getAutoGenType() == GrammarElement.AUTO_GEN_NONE;
+ genMatch(atom);
+ saveText = oldsaveText;
+ }
+
+ /** Generate code for the given grammar element.
+ * @param blk The character-range reference to generate
+ */
+ public void gen(CharRangeElement r) {
+ if (r.getLabel() != null && syntacticPredLevel == 0) {
+ println(r.getLabel() + " = " + lt1Value + ";");
+ }
+ println("matchRange(" + textOrChar(r.beginText) + "," + textOrChar(r.endText) + ");");
+ }
+
+ /** Generate the lexer C++ files */
+ public void gen(LexerGrammar g) throws IOException {
+ // If debugging, create a new sempred vector for this grammar
+ if (g.debuggingOutput)
+ semPreds = new Vector();
+
+ setGrammar(g);
+ if (!(grammar instanceof LexerGrammar)) {
+ tool.panic("Internal error generating lexer");
+ }
+
+ genBody(g);
+ genInclude(g);
+ }
+
+ /** Generate code for the given grammar element.
+ * @param blk The (...)+ block to generate
+ */
+ public void gen(OneOrMoreBlock blk) {
+ if (DEBUG_CODE_GENERATOR) System.out.println("gen+(" + blk + ")");
+ String label;
+ String cnt;
+ println("{");
+ genBlockPreamble(blk);
+ if (blk.getLabel() != null) {
+ cnt = "_cnt_" + blk.getLabel();
+ } else {
+ cnt = "_cnt" + blk.ID;
+ }
+ println("int " + cnt + "=0;");
+ if (blk.getLabel() != null) {
+ label = blk.getLabel();
+ } else {
+ label = "_loop" + blk.ID;
+ }
+
+ println("for (;;) {");
+ tabs++;
+
+ // Tell AST generation to build subrule result
+ String saveCurrentASTResult = currentASTResult;
+ if (blk.getLabel() != null) {
+ currentASTResult = blk.getLabel();
+ }
+
+ boolean ok = grammar.theLLkAnalyzer.deterministic(blk);
+
+ // generate exit test if greedy set to false
+ // and an alt is ambiguous with exit branch
+ // or when lookahead derived purely from end-of-file
+ // Lookahead analysis stops when end-of-file is hit,
+ // returning set {epsilon}. Since {epsilon} is not
+ // ambig with any real tokens, no error is reported
+ // by deterministic() routines and we have to check
+ // for the case where the lookahead depth didn't get
+ // set to NONDETERMINISTIC (this only happens when the
+ // FOLLOW contains real atoms + epsilon).
+ boolean generateNonGreedyExitPath = false;
+ int nonGreedyExitDepth = grammar.maxk;
+
+ if (!blk.greedy &&
+ blk.exitLookaheadDepth <= grammar.maxk &&
+ blk.exitCache[blk.exitLookaheadDepth].containsEpsilon()) {
+ generateNonGreedyExitPath = true;
+ nonGreedyExitDepth = blk.exitLookaheadDepth;
+ } else if (!blk.greedy &&
+ blk.exitLookaheadDepth == LLkGrammarAnalyzer.NONDETERMINISTIC) {
+ generateNonGreedyExitPath = true;
+ }
+
+ // generate exit test if greedy set to false
+ // and an alt is ambiguous with exit branch
+ if (generateNonGreedyExitPath) {
+ if (DEBUG_CODE_GENERATOR) {
+ System.out.println("nongreedy (...)+ loop; exit depth is " +
+ blk.exitLookaheadDepth);
+ }
+ String predictExit =
+ getLookaheadTestExpression(blk.exitCache,
+ nonGreedyExitDepth);
+ println("// nongreedy exit test");
+ println("if ( " + cnt + ">=1 && " + predictExit + ") goto " + label + ";");
+ }
+
+ CppBlockFinishingInfo howToFinish = genCommonBlock(blk, false);
+ genBlockFinish(
+ howToFinish,
+ "if ( " + cnt + ">=1 ) { goto " + label + "; } else {" + throwNoViable + "}"
+ );
+
+ println(cnt + "++;");
+ tabs--;
+ println("}");
+ println(label + ":;");
+ println("}");
+
+ // Restore previous AST generation
+ currentASTResult = saveCurrentASTResult;
+ }
+
+ /** Generate the parser C++ file */
+ public void gen(ParserGrammar g) throws IOException {
+
+ // if debugging, set up a new vector to keep track of sempred
+ // strings for this grammar
+ if (g.debuggingOutput)
+ semPreds = new Vector();
+
+ setGrammar(g);
+ if (!(grammar instanceof ParserGrammar)) {
+ tool.panic("Internal error generating parser");
+ }
+
+ genBody(g);
+ genInclude(g);
+ }
+
+ /** Generate code for the given grammar element.
+ * @param blk The rule-reference to generate
+ */
+ public void gen(RuleRefElement rr) {
+ if (DEBUG_CODE_GENERATOR) System.out.println("genRR(" + rr + ")");
+ RuleSymbol rs = (RuleSymbol)grammar.getSymbol(rr.targetRule);
+ if (rs == null || !rs.isDefined()) {
+ // Is this redundant???
+ tool.error("Rule '" + rr.targetRule + "' is not defined", grammar.getFilename(), rr.getLine());
+ return;
+ }
+ if (!(rs instanceof RuleSymbol)) {
+ // Is this redundant???
+ tool.error("'" + rr.targetRule + "' does not name a grammar rule", grammar.getFilename(), rr.getLine());
+ return;
+ }
+
+ genErrorTryForElement(rr);
+
+ // AST value for labeled rule refs in tree walker.
+ // This is not AST construction; it is just the input tree node value.
+ if (grammar instanceof TreeWalkerGrammar &&
+ rr.getLabel() != null &&
+ syntacticPredLevel == 0) {
+ println(rr.getLabel() + " = (_t == ASTNULL) ? " + labeledElementASTInit + " : " + lt1Value + ";");
+ }
+
+ // if in lexer and ! on rule ref or alt or rule, save buffer index to kill later
+ if (grammar instanceof LexerGrammar && (!saveText || rr.getAutoGenType() == GrammarElement.AUTO_GEN_BANG)) {
+ println("_saveIndex = text.length();");
+ }
+
+ // Process return value assignment if any
+ printTabs();
+ if (rr.idAssign != null) {
+ // Warn if the rule has no return type
+ if (rs.block.returnAction == null) {
+ tool.warning("Rule '" + rr.targetRule + "' has no return type", grammar.getFilename(), rr.getLine());
+ }
+ _print(rr.idAssign + "=");
+ } else {
+ // Warn about return value if any, but not inside syntactic predicate
+ if (!(grammar instanceof LexerGrammar) && syntacticPredLevel == 0 && rs.block.returnAction != null) {
+ tool.warning("Rule '" + rr.targetRule + "' returns a value", grammar.getFilename(), rr.getLine());
+ }
+ }
+
+ // Call the rule
+ GenRuleInvocation(rr);
+
+ // if in lexer and ! on element or alt or rule, save buffer index to kill later
+ if (grammar instanceof LexerGrammar && (!saveText || rr.getAutoGenType() == GrammarElement.AUTO_GEN_BANG)) {
+ println("text.erase(_saveIndex);");
+ }
+
+ // if not in a syntactic predicate
+ if (syntacticPredLevel == 0) {
+ boolean doNoGuessTest = (
+ grammar.hasSyntacticPredicate &&
+ (
+ grammar.buildAST && rr.getLabel() != null ||
+ (genAST && rr.getAutoGenType() == GrammarElement.AUTO_GEN_NONE)
+ )
+ );
+ if (doNoGuessTest) {
+ println("if (inputState->guessing==0) {");
+ tabs++;
+ }
+
+ if (grammar.buildAST && rr.getLabel() != null) {
+ // always gen variable for rule return on labeled rules
+ // FIXME: maybe broken....
+ println(rr.getLabel() + "_AST = " + labeledElementASTType + "(returnAST);");
+ }
+ if (genAST) {
+ switch (rr.getAutoGenType()) {
+ case GrammarElement.AUTO_GEN_NONE:
+ // println("theASTFactory.addASTChild(currentAST, returnAST);");
+ println("astFactory.addASTChild(currentAST, " + namespaceAntlr + "RefAST(returnAST));");
+ break;
+ case GrammarElement.AUTO_GEN_CARET:
+ tool.error("Internal: encountered ^ after rule reference");
+ break;
+ default:
+ break;
+ }
+ }
+
+ // if a lexer and labeled, Token label defined at rule level, just set it here
+ if (grammar instanceof LexerGrammar && rr.getLabel() != null) {
+ println(rr.getLabel() + "=_returnToken;");
+ }
+
+ if (doNoGuessTest) {
+ tabs--;
+ println("}");
+ }
+ }
+ genErrorCatchForElement(rr);
+ }
+
+ /** Generate code for the given grammar element.
+ * @param blk The string-literal reference to generate
+ */
+ public void gen(StringLiteralElement atom) {
+ if (DEBUG_CODE_GENERATOR) System.out.println("genString(" + atom + ")");
+
+ // Variable declarations for labeled elements
+ if (atom.getLabel() != null && syntacticPredLevel == 0) {
+ println(atom.getLabel() + " = " + lt1Value + ";");
+ }
+
+ // AST
+ genElementAST(atom);
+
+ // is there a bang on the literal?
+ boolean oldsaveText = saveText;
+ saveText = saveText && atom.getAutoGenType() == GrammarElement.AUTO_GEN_NONE;
+
+ // matching
+ genMatch(atom);
+
+ saveText = oldsaveText;
+
+ // tack on tree cursor motion if doing a tree walker
+ if (grammar instanceof TreeWalkerGrammar) {
+ println("_t = _t->getNextSibling();");
+ }
+ }
+
+ /** Generate code for the given grammar element.
+ * @param blk The token-range reference to generate
+ */
+ public void gen(TokenRangeElement r) {
+ genErrorTryForElement(r);
+ if (r.getLabel() != null && syntacticPredLevel == 0) {
+ println(r.getLabel() + " = " + lt1Value + ";");
+ }
+
+ // AST
+ genElementAST(r);
+
+ // match
+ println("matchRange(" + r.beginText + "," + r.endText + ");");
+ genErrorCatchForElement(r);
+ }
+
+ /** Generate code for the given grammar element.
+ * @param blk The token-reference to generate
+ */
+ public void gen(TokenRefElement atom) {
+ if (DEBUG_CODE_GENERATOR) System.out.println("genTokenRef(" + atom + ")");
+ if (grammar instanceof LexerGrammar) {
+ tool.panic("Token reference found in lexer");
+ }
+ genErrorTryForElement(atom);
+ // Assign Token value to token label variable
+ if (atom.getLabel() != null && syntacticPredLevel == 0) {
+ println(atom.getLabel() + " = " + lt1Value + ";");
+ }
+
+ // AST
+ genElementAST(atom);
+ // matching
+ genMatch(atom);
+ genErrorCatchForElement(atom);
+
+ // tack on tree cursor motion if doing a tree walker
+ if (grammar instanceof TreeWalkerGrammar) {
+ println("_t = _t->getNextSibling();");
+ }
+ }
+
+ public void gen(TreeElement t) {
+ // save AST cursor
+ println(labeledElementType + " __t" + t.ID + " = _t;");
- // Generate literals test if desired
- // make sure _ttype is set first; note _returnToken must be
- // non-null as the rule was required to create it.
- println("if ( !_returnToken ) goto tryAgain; // found SKIP token");
- println("_ttype = _returnToken->getType();");
- if ( ((LexerGrammar)grammar).getTestLiterals()) {
- genLiteralsTest();
- }
+ // If there is a label on the root, then assign that to the variable
+ if (t.root.getLabel() != null) {
+// println(t.root.getLabel() + " = _t==ASTNULL ? "+namespaceAntlr+"nullAST :"+labeledElementASTType+"(_t);");
+ println(t.root.getLabel() + " = (_t == ASTNULL) ? " + labeledElementASTInit + " : _t;");
+ }
- // return token created by rule reference in switch
- println("_returnToken->setType(_ttype);");
- println("return _returnToken;");
-
- // Close try block
- tabs--;
- println("}");
- println("catch ("+namespaceAntlr+"RecognitionException& e) {");
- tabs++;
- if ( ((LexerGrammar)grammar).filterMode ) {
- if ( filterRule==null ) {
- println("if ( !getCommitToPath() ) {consume(); goto tryAgain;}");
- }
- else {
- println("if ( !getCommitToPath() ) {");
- tabs++;
- println("rewind(_m);");
- println("resetText();");
- println("try {m"+filterRule+"(false);}");
- println("catch("+namespaceAntlr+"RecognitionException& ee) {");
- println(" // horrendous failure: error in filter rule");
- println(" reportError(ee);");
- println(" consume();");
- println("}");
- println("goto tryAgain;");
- tabs--;
- println("}");
- }
- }
- if ( nextTokenBlk.getDefaultErrorHandler() ) {
- println("reportError(e);");
- println("consume();");
- }
- else {
- // pass on to invoking routine
- println("throw "+namespaceAntlr+"TokenStreamRecognitionException(e);");
- }
- tabs--;
- println("}");
+ // Generate AST variables
+ genElementAST(t.root);
+ if (grammar.buildAST) {
+ // Save the AST construction state
+ println(namespaceAntlr + "ASTPair __currentAST" + t.ID + " = currentAST;");
+ // Make the next item added a child of the TreeElement root
+ println("currentAST.root = currentAST.child;");
+ println("currentAST.child = " + labeledElementASTInit + ";");
+ }
+
+ // match root
+ genMatch(t.root);
+ // move to list of children
+ println("_t = _t->getFirstChild();");
+
+ // walk list of children, generating code for each
+ for (int i = 0; i < t.getAlternatives().size(); i++) {
+ Alternative a = t.getAlternativeAt(i);
+ AlternativeElement e = a.head;
+ while (e != null) {
+ e.generate();
+ e = e.next;
+ }
+ }
+
+ if (grammar.buildAST) {
+ // restore the AST construction state to that just after the
+ // tree root was added
+ println("currentAST = __currentAST" + t.ID + ";");
+ }
+ // restore AST cursor
+ println("_t = __t" + t.ID + ";");
+ // move cursor to sibling of tree just parsed
+ println("_t = _t->getNextSibling();");
+ }
+
+ /** Generate the tree-parser C++ files */
+ public void gen(TreeWalkerGrammar g) throws IOException {
+ setGrammar(g);
+ if (!(grammar instanceof TreeWalkerGrammar)) {
+ tool.panic("Internal error generating tree-walker");
+ }
+
+ genBody(g);
+ genInclude(g);
+ }
+
+ /** Generate code for the given grammar element.
+ * @param wc The wildcard element to generate
+ */
+ public void gen(WildcardElement wc) {
+ // Variable assignment for labeled elements
+ if (wc.getLabel() != null && syntacticPredLevel == 0) {
+ println(wc.getLabel() + " = " + lt1Value + ";");
+ }
+
+ // AST
+ genElementAST(wc);
+ // Match anything but EOF
+ if (grammar instanceof TreeWalkerGrammar) {
+ println("if ( _t == " + labeledElementASTInit + " ) throw " + namespaceAntlr + "MismatchedTokenException();");
+ } else if (grammar instanceof LexerGrammar) {
+ if (grammar instanceof LexerGrammar &&
+ (!saveText || wc.getAutoGenType() == GrammarElement.AUTO_GEN_BANG)) {
+ println("_saveIndex = text.length();");
+ }
+ println("matchNot(EOF/*_CHAR*/);");
+ if (grammar instanceof LexerGrammar &&
+ (!saveText || wc.getAutoGenType() == GrammarElement.AUTO_GEN_BANG)) {
+ println("text.erase(_saveIndex);"); // kill text atom put in buffer
+ }
+ } else {
+ println("matchNot(" + getValueString(Token.EOF_TYPE) + ");");
+ }
+
+ // tack on tree cursor motion if doing a tree walker
+ if (grammar instanceof TreeWalkerGrammar) {
+ println("_t = _t->getNextSibling();");
+ }
+ }
+
+ /** Generate code for the given grammar element.
+ * @param blk The (...)* block to generate
+ */
+ public void gen(ZeroOrMoreBlock blk) {
+ if (DEBUG_CODE_GENERATOR) System.out.println("gen*(" + blk + ")");
+ println("{");
+ genBlockPreamble(blk);
+ String label;
+ if (blk.getLabel() != null) {
+ label = blk.getLabel();
+ } else {
+ label = "_loop" + blk.ID;
+ }
+ println("for (;;) {");
+ tabs++;
+
+ // Tell AST generation to build subrule result
+ String saveCurrentASTResult = currentASTResult;
+ if (blk.getLabel() != null) {
+ currentASTResult = blk.getLabel();
+ }
+
+ boolean ok = grammar.theLLkAnalyzer.deterministic(blk);
+
+ // generate exit test if greedy set to false
+ // and an alt is ambiguous with exit branch
+ // or when lookahead derived purely from end-of-file
+ // Lookahead analysis stops when end-of-file is hit,
+ // returning set {epsilon}. Since {epsilon} is not
+ // ambig with any real tokens, no error is reported
+ // by deterministic() routines and we have to check
+ // for the case where the lookahead depth didn't get
+ // set to NONDETERMINISTIC (this only happens when the
+ // FOLLOW contains real atoms + epsilon).
+ boolean generateNonGreedyExitPath = false;
+ int nonGreedyExitDepth = grammar.maxk;
+
+ if (!blk.greedy &&
+ blk.exitLookaheadDepth <= grammar.maxk &&
+ blk.exitCache[blk.exitLookaheadDepth].containsEpsilon()) {
+ generateNonGreedyExitPath = true;
+ nonGreedyExitDepth = blk.exitLookaheadDepth;
+ } else if (!blk.greedy &&
+ blk.exitLookaheadDepth == LLkGrammarAnalyzer.NONDETERMINISTIC) {
+ generateNonGreedyExitPath = true;
+ }
+ if (generateNonGreedyExitPath) {
+ if (DEBUG_CODE_GENERATOR) {
+ System.out.println("nongreedy (...)* loop; exit depth is " +
+ blk.exitLookaheadDepth);
+ }
+ String predictExit =
+ getLookaheadTestExpression(blk.exitCache,
+ nonGreedyExitDepth);
+ println("// nongreedy exit test");
+ println("if (" + predictExit + ") goto " + label + ";");
+ }
+
+ CppBlockFinishingInfo howToFinish = genCommonBlock(blk, false);
+ genBlockFinish(howToFinish, "goto " + label + ";");
+
+ tabs--;
+ println("}");
+ println(label + ":;");
+ println("}");
+
+ // Restore previous AST generation
+ currentASTResult = saveCurrentASTResult;
+ }
+
+ /** Generate an alternative.
+ * @param alt The alternative to generate
+ * @param blk The block to which the alternative belongs
+ */
+ protected void genAlt(Alternative alt, AlternativeBlock blk) {
+ // Save the AST generation state, and set it to that of the alt
+ boolean savegenAST = genAST;
+ genAST = genAST && alt.getAutoGen();
+
+ boolean oldsaveTest = saveText;
+ saveText = saveText && alt.getAutoGen();
+
+ // Reset the variable name map for the alternative
+ Hashtable saveMap = treeVariableMap;
+ treeVariableMap = new Hashtable();
+
+ // Generate try block around the alt for error handling
+ if (alt.exceptionSpec != null) {
+ println("try { // for error handling");
+ tabs++;
+ }
+
+ AlternativeElement elem = alt.head;
+ while (!(elem instanceof BlockEndElement)) {
+ elem.generate(); // alt can begin with anything. Ask target to gen.
+ elem = elem.next;
+ }
+
+ if (genAST) {
+ if (blk instanceof RuleBlock) {
+ // Set the AST return value for the rule
+ RuleBlock rblk = (RuleBlock)blk;
+ println(rblk.getRuleName() + "_AST = " + labeledElementASTType + "(currentAST.root);");
+ } else if (blk.getLabel() != null) {
+ // ### future: also set AST value for labeled subrules.
+ // println(blk.getLabel() + "_AST = "+labeledElementASTType+"(currentAST.root);");
+ }
+ }
+
+ if (alt.exceptionSpec != null) {
+ // close try block
+ tabs--;
+ println("}");
+ genErrorHandler(alt.exceptionSpec);
+ }
+
+ genAST = savegenAST;
+ saveText = oldsaveTest;
+
+ treeVariableMap = saveMap;
+ }
+
+ /** Generate all the bitsets to be used in the parser or lexer
+ * Generate the raw bitset data like "long _tokenSet1_data[] = {...};"
+ * and the BitSet object declarations like "BitSet _tokenSet1 = new BitSet(_tokenSet1_data);"
+ * Note that most languages do not support object initialization inside a
+ * class definition, so other code-generators may have to separate the
+ * bitset declarations from the initializations (e.g., put the initializations
+ * in the generated constructor instead).
+ * @param bitsetList The list of bitsets to generate.
+ * @param maxVocabulary Ensure that each generated bitset can contain at least this value.
+ * @param dumpSets Dump out the token definitions of the contents of the bitset
+ * only for grammars/parsers.
+ */
+ protected void genBitsets(
+ Vector bitsetList,
+ int maxVocabulary,
+ String prefix,
+ boolean dumpSets
+ ) {
+ println("");
+ for (int i = 0; i < bitsetList.size(); i++) {
+ BitSet p = (BitSet)bitsetList.elementAt(i);
+ // Ensure that generated BitSet is large enough for vocabulary
+ p.growToInclude(maxVocabulary);
+ // initialization data
+ println(
+ "const unsigned long " + prefix + getBitsetName(i) + "_data_" + "[] = { " +
+ p.toStringOfHalfWords() +
+ " };"
+ );
+
+ if (dumpSets) {
+ // Dump the contents of the bitset in readable format...
+ String t = "// ";
+ for (int j = 0; j < p.size(); j++) {
+ if (p.member(j)) {
+ t += grammar.tokenManager.getTokenStringAt(j) + " ";
+ if (t.length() > 70) {
+ println(t);
+ t = "// ";
+ }
+ }
+ }
+ if (t != "// ")
+ println(t);
+ }
+
+ // BitSet object
+ println(
+ "const " + namespaceAntlr + "BitSet " + prefix + getBitsetName(i) + "(" +
+ getBitsetName(i) + "_data_," + p.size() / 32 +
+ ");"
+ );
+ }
+ }
+
+ protected void genBitsetsHeader(
+ Vector bitsetList,
+ int maxVocabulary
+ ) {
+ println("");
+ for (int i = 0; i < bitsetList.size(); i++) {
+ BitSet p = (BitSet)bitsetList.elementAt(i);
+ // Ensure that generated BitSet is large enough for vocabulary
+ p.growToInclude(maxVocabulary);
+ // initialization data
+ println("static const unsigned long " + getBitsetName(i) + "_data_" + "[];");
+ // BitSet object
+ println("static const " + namespaceAntlr + "BitSet " + getBitsetName(i) + ";");
+ }
+ }
+
+ /** Generate the finish of a block, using a combination of the info
+ * returned from genCommonBlock() and the action to perform when
+ * no alts were taken
+ * @param howToFinish The return of genCommonBlock()
+ * @param noViableAction What to generate when no alt is taken
+ */
+ private void genBlockFinish(CppBlockFinishingInfo howToFinish, String noViableAction) {
+ if (howToFinish.needAnErrorClause &&
+ (howToFinish.generatedAnIf || howToFinish.generatedSwitch)) {
+ if (howToFinish.generatedAnIf) {
+ println("else {");
+ } else {
+ println("{");
+ }
+ tabs++;
+ println(noViableAction);
+ tabs--;
+ println("}");
+ }
+
+ if (howToFinish.postscript != null) {
+ println(howToFinish.postscript);
+ }
+ }
+
+ /** Generate the header for a block, which may be a RuleBlock or a
+ * plain AlternativeBLock. This generates any variable declarations,
+ * init-actions, and syntactic-predicate-testing variables.
+ * @blk The block for which the preamble is to be generated.
+ */
+ protected void genBlockPreamble(AlternativeBlock blk) {
+ // define labels for rule blocks.
+ if (blk instanceof RuleBlock) {
+ RuleBlock rblk = (RuleBlock)blk;
+ if (rblk.labeledElements != null) {
+ for (int i = 0; i < rblk.labeledElements.size(); i++) {
+
+ AlternativeElement a = (AlternativeElement)rblk.labeledElements.elementAt(i);
+ //System.out.println("looking at labeled element: "+a);
+ // Variables for labeled rule refs and subrules are different than
+ // variables for grammar atoms. This test is a little tricky because
+ // we want to get all rule refs and ebnf, but not rule blocks or
+ // syntactic predicates
+ if (
+ a instanceof RuleRefElement ||
+ a instanceof AlternativeBlock &&
+ !(a instanceof RuleBlock) &&
+ !(a instanceof SynPredBlock)
+ ) {
+
+ if (
+ !(a instanceof RuleRefElement) &&
+ ((AlternativeBlock)a).not &&
+ analyzer.subruleCanBeInverted(((AlternativeBlock)a), grammar instanceof LexerGrammar)
+ ) {
+ // Special case for inverted subrules that will be inlined.
+ // Treat these like token or char literal references
+ println(labeledElementType + " " + a.getLabel() + " = " + labeledElementInit + ";");
+ if (grammar.buildAST) {
+ println(labeledElementASTType + " " + a.getLabel() + "_AST = " + labeledElementASTInit + ";");
+ }
+ } else {
+ if (grammar.buildAST) {
+ // Always gen AST variables for labeled elements, even if the
+ // element itself is marked with !
+ println(labeledElementASTType + " " + a.getLabel() + "_AST = " + labeledElementASTInit + ";");
+ }
+ if (grammar instanceof LexerGrammar) {
+ println(namespaceAntlr + "RefToken " + a.getLabel() + ";");
+ }
+ if (grammar instanceof TreeWalkerGrammar) {
+ // always generate rule-ref variables for tree walker
+ println(labeledElementType + " " + a.getLabel() + " = " + labeledElementInit + ";");
+ }
+ }
+ } else {
+ // It is a token or literal reference. Generate the
+ // correct variable type for this grammar
+ println(labeledElementType + " " + a.getLabel() + " = " + labeledElementInit + ";");
+ // In addition, generate *_AST variables if building ASTs
+ if (grammar.buildAST) {
+ println(labeledElementASTType + " " + a.getLabel() + "_AST = " + labeledElementASTInit + ";");
+ }
+ }
+ }
+ }
+ }
+
+ // dump out init action
+ if (blk.initAction != null) {
+ genLineNo(blk);
+ printAction(
+ processActionForTreeSpecifiers(blk.initAction, 0, currentRule, null)
+ );
+ genLineNo2();
+ }
+ }
+
+ public void genBody(LexerGrammar g) throws IOException {
+ outputFile = grammar.getClassName() + ".cpp";
+ outputLine = 1;
+ currentOutput = antlr.Tool.openOutputFile(outputFile);
+ //SAS: changed for proper text file io
+
+ genAST = false; // no way to gen trees.
+ saveText = true; // save consumed characters.
+
+ tabs = 0;
+
+ // Generate header common to all C++ output files
+ genHeader(outputFile);
+
+ printHeaderAction(preIncludeCpp);
+ // Generate header specific to lexer C++ file
+ println("#include \"" + grammar.getClassName() + ".hpp\"");
+ println("#include \"antlr/CharBuffer.hpp\"");
+ println("#include \"antlr/TokenStreamException.hpp\"");
+ println("#include \"antlr/TokenStreamIOException.hpp\"");
+ println("#include \"antlr/TokenStreamRecognitionException.hpp\"");
+ println("#include \"antlr/CharStreamException.hpp\"");
+ println("#include \"antlr/CharStreamIOException.hpp\"");
+ println("#include \"antlr/NoViableAltForCharException.hpp\"");
+ if (grammar.debuggingOutput)
+ println("#include \"antlr/DebuggingInputBuffer.hpp\"");
+ println("");
+ printHeaderAction(postIncludeCpp);
+
+ if (nameSpace != null)
+ nameSpace.emitDeclarations(currentOutput);
+
+ // Generate user-defined lexer file preamble
+ printAction(grammar.preambleAction);
+
+ // Generate lexer class definition
+ String sup = null;
+ if (grammar.superClass != null) {
+ sup = grammar.superClass;
+ } else {
+ sup = grammar.getSuperClass();
+ if (sup.lastIndexOf('.') != -1)
+ sup = sup.substring(sup.lastIndexOf('.') + 1);
+ sup = namespaceAntlr + sup;
+ }
+
+ //
+ // Generate the constructor from InputStream
+ //
+ println(grammar.getClassName() + "::" + grammar.getClassName() + "(" + namespaceStd + "istream& in)");
+ tabs++;
+ // if debugging, wrap the input buffer in a debugger
+ if (grammar.debuggingOutput)
+ println(": " + sup + "(new " + namespaceAntlr + "DebuggingInputBuffer(new " + namespaceAntlr + "CharBuffer(in)))");
+ else
+ println(": " + sup + "(new " + namespaceAntlr + "CharBuffer(in))");
+ tabs--;
+ println("{");
+ tabs++;
+
+ // if debugging, set up array variables and call user-overridable
+ // debugging setup method
+ if (grammar.debuggingOutput) {
+ println("setRuleNames(_ruleNames);");
+ println("setSemPredNames(_semPredNames);");
+ println("setupDebugging();");
+ }
+
+ println("setCaseSensitive(" + g.caseSensitive + ");");
+ println("initLiterals();");
+ tabs--;
+ println("}");
+ println("");
+
+ // Generate the constructor from InputBuffer
+ println(grammar.getClassName() + "::" + grammar.getClassName() + "(" + namespaceAntlr + "InputBuffer& ib)");
+ tabs++;
+ // if debugging, wrap the input buffer in a debugger
+ if (grammar.debuggingOutput)
+ println(": " + sup + "(new " + namespaceAntlr + "DebuggingInputBuffer(ib))");
+ else
+ println(": " + sup + "(ib)");
+ tabs--;
+ println("{");
+ tabs++;
+
+ // if debugging, set up array variables and call user-overridable
+ // debugging setup method
+ if (grammar.debuggingOutput) {
+ println("setRuleNames(_ruleNames);");
+ println("setSemPredNames(_semPredNames);");
+ println("setupDebugging();");
+ }
+
+ println("setCaseSensitive(" + g.caseSensitive + ");");
+ println("initLiterals();");
+ tabs--;
+ println("}");
+ println("");
+
+ // Generate the constructor from LexerSharedInputState
+ println(grammar.getClassName() + "::" + grammar.getClassName() + "(const " + namespaceAntlr + "LexerSharedInputState& state)");
+ tabs++;
+ println(": " + sup + "(state)");
+ tabs--;
+ println("{");
+ tabs++;
+
+ // if debugging, set up array variables and call user-overridable
+ // debugging setup method
+ if (grammar.debuggingOutput) {
+ println("setRuleNames(_ruleNames);");
+ println("setSemPredNames(_semPredNames);");
+ println("setupDebugging();");
+ }
+
+ println("setCaseSensitive(" + g.caseSensitive + ");");
+ println("initLiterals();");
+ tabs--;
+ println("}");
+ println("");
+
+ println("void " + grammar.getClassName() + "::initLiterals()");
+ println("{");
+ tabs++;
+ // Generate the initialization of the map
+ // containing the string literals used in the lexer
+ // The literals variable itself is in CharScanner
+ /* TJP: get keys now and check to make sure it's a literal not
+ * a label to a literal; was dup'ing string literals before
+ * change.
+ *
+ Enumeration ids = grammar.tokenManager.getTokenSymbolElements();
+ while ( ids.hasMoreElements() ) {
+ TokenSymbol sym = (TokenSymbol)ids.nextElement();
+ if ( sym instanceof StringLiteralSymbol ) {
+ StringLiteralSymbol s = (StringLiteralSymbol)sym;
+ println("literals["+s.getId()+"] = "+s.getTokenType()+";");
+ }
+ }
+ */
+ // TJP changed it to following loop.
+ Enumeration keys = grammar.tokenManager.getTokenSymbolKeys();
+ while (keys.hasMoreElements()) {
+ String key = (String)keys.nextElement();
+ if (key.charAt(0) != '"') {
+ continue;
+ }
+ TokenSymbol sym = grammar.tokenManager.getTokenSymbol(key);
+ if (sym instanceof StringLiteralSymbol) {
+ StringLiteralSymbol s = (StringLiteralSymbol)sym;
+ println("literals[" + s.getId() + "] = " + s.getTokenType() + ";");
+ }
+ }
+
+ // Generate the setting of various generated options.
+ tabs--;
+ println("}");
+
+ // Generate getCaseSensitiveLiterals() method
+ println("bool " + grammar.getClassName() + "::getCaseSensitiveLiterals() const");
+ println("{");
+ tabs++;
+ println("return " + g.caseSensitiveLiterals + ";");
+ tabs--;
+ println("}");
+
+ Enumeration ids;
+ // generate the rule name array for debugging
+ if (grammar.debuggingOutput) {
+ println("const char* " + grammar.getClassName() + "::_ruleNames[] = {");
+ tabs++;
+
+ ids = grammar.rules.elements();
+ int ruleNum = 0;
+ while (ids.hasMoreElements()) {
+ GrammarSymbol sym = (GrammarSymbol)ids.nextElement();
+ if (sym instanceof RuleSymbol)
+ println("\"" + ((RuleSymbol)sym).getId() + "\",");
+ }
+ println("0");
+ tabs--;
+ println("};");
+ }
+
+ // Generate nextToken() rule.
+ // nextToken() is a synthetic lexer rule that is the implicit OR of all
+ // user-defined lexer rules.
+ genNextToken();
+
+ // Generate code for each rule in the lexer
+ ids = grammar.rules.elements();
+ int ruleNum = 0;
+ while (ids.hasMoreElements()) {
+ RuleSymbol sym = (RuleSymbol)ids.nextElement();
+ // Don't generate the synthetic rules
+ if (!sym.getId().equals("mnextToken")) {
+ genRule(sym, false, ruleNum++, grammar.getClassName() + "::");
+ }
+ exitIfError();
+ }
+
+ // Generate the semantic predicate map for debugging
+ if (grammar.debuggingOutput)
+ genSemPredMap(grammar.getClassName() + "::");
+
+ // Generate the bitsets used throughout the lexer
+ genBitsets(bitsetsUsed, ((LexerGrammar)grammar).charVocabulary.size(), grammar.getClassName() + "::", false);
+
+ println("");
+ if (nameSpace != null)
+ nameSpace.emitClosures(currentOutput);
+
+ // Close the lexer output stream
+ currentOutput.close();
+ currentOutput = null;
+ }
+
+ public void genBody(ParserGrammar g) throws IOException {
+ // Open the output stream for the parser and set the currentOutput
+ outputFile = grammar.getClassName() + ".cpp";
+ outputLine = 1;
+ currentOutput = antlr.Tool.openOutputFile(outputFile);
+ //SAS: changed for proper text file io
+
+ genAST = grammar.buildAST;
+
+ tabs = 0;
+
+ // Generate the header common to all output files.
+ genHeader(outputFile);
+
+ printHeaderAction(preIncludeCpp);
+
+ // Generate header for the parser
+ println("#include \"" + grammar.getClassName() + ".hpp\"");
+ println("#include \"antlr/NoViableAltException.hpp\"");
+ println("#include \"antlr/SemanticException.hpp\"");
+
+ printHeaderAction(postIncludeCpp);
+
+ if (nameSpace != null)
+ nameSpace.emitDeclarations(currentOutput);
+
+ // Output the user-defined parser preamble
+ printAction(grammar.preambleAction);
+
+ String sup = null;
+ if (grammar.superClass != null)
+ sup = grammar.superClass;
+ else {
+ sup = grammar.getSuperClass();
+ if (sup.lastIndexOf('.') != -1)
+ sup = sup.substring(sup.lastIndexOf('.') + 1);
+ sup = namespaceAntlr + sup;
+ }
+
+ // set up an array of all the rule names so the debugger can
+ // keep track of them only by number -- less to store in tree...
+ if (grammar.debuggingOutput) {
+ println("const char* " + grammar.getClassName() + "::_ruleNames[] = {");
+ tabs++;
+
+ Enumeration ids = grammar.rules.elements();
+ int ruleNum = 0;
+ while (ids.hasMoreElements()) {
+ GrammarSymbol sym = (GrammarSymbol)ids.nextElement();
+ if (sym instanceof RuleSymbol)
+ println("\"" + ((RuleSymbol)sym).getId() + "\",");
+ }
+ println("0");
+ tabs--;
+ println("};");
+ }
+
+ // Generate parser class constructor from TokenBuffer
+ print(grammar.getClassName() + "::" + grammar.getClassName());
+ println("(" + namespaceAntlr + "TokenBuffer& tokenBuf, int k)");
+ println(": " + sup + "(tokenBuf,k)");
+ println("{");
+ tabs++;
+ println("setTokenNames(_tokenNames);");
+ // if debugging, set up arrays and call the user-overridable
+ // debugging setup method
+ if (grammar.debuggingOutput) {
+ println("setRuleNames(_ruleNames);");
+ println("setSemPredNames(_semPredNames);");
+ println("setupDebugging();");
+ }
+ tabs--;
+ println("}");
+ println("");
+
+ print(grammar.getClassName() + "::" + grammar.getClassName());
+ println("(" + namespaceAntlr + "TokenBuffer& tokenBuf)");
+ println(": " + sup + "(tokenBuf," + grammar.maxk + ")");
+ println("{");
+ tabs++;
+ println("setTokenNames(_tokenNames);");
+ // if debugging, set up arrays and call the user-overridable
+ // debugging setup method
+ if (grammar.debuggingOutput) {
+ println("setRuleNames(_ruleNames);");
+ println("setSemPredNames(_semPredNames);");
+ println("setupDebugging();");
+ }
+ tabs--;
+ println("}");
+ println("");
+
+ // Generate parser class constructor from TokenStream
+ print(grammar.getClassName() + "::" + grammar.getClassName());
+ println("(" + namespaceAntlr + "TokenStream& lexer, int k)");
+ println(": " + sup + "(lexer,k)");
+ println("{");
+ tabs++;
+ println("setTokenNames(_tokenNames);");
+ // if debugging, set up arrays and call the user-overridable
+ // debugging setup method
+ if (grammar.debuggingOutput) {
+ println("setRuleNames(_ruleNames);");
+ println("setSemPredNames(_semPredNames);");
+ println("setupDebugging(&lexer);");
+ }
+ tabs--;
+ println("}");
+ println("");
+
+ print(grammar.getClassName() + "::" + grammar.getClassName());
+ println("(" + namespaceAntlr + "TokenStream& lexer)");
+ println(": " + sup + "(lexer," + grammar.maxk + ")");
+ println("{");
+ tabs++;
+ println("setTokenNames(_tokenNames);");
+ // if debugging, set up arrays and call the user-overridable
+ // debugging setup method
+ if (grammar.debuggingOutput) {
+ println("setRuleNames(_ruleNames);");
+ println("setSemPredNames(_semPredNames);");
+ println("setupDebugging(&lexer);");
+ }
+ tabs--;
+ println("}");
+ println("");
+
+ print(grammar.getClassName() + "::" + grammar.getClassName());
+ println("(const " + namespaceAntlr + "ParserSharedInputState& state)");
+ println(": " + sup + "(state," + grammar.maxk + ")");
+ println("{");
+ tabs++;
+ println("setTokenNames(_tokenNames);");
+ // if debugging, set up arrays and call the user-overridable
+ // debugging setup method
+ if (grammar.debuggingOutput) {
+ println("setRuleNames(_ruleNames);");
+ println("setSemPredNames(_semPredNames);");
+ println("setupDebugging(&lexer);");
+ }
+ tabs--;
+ println("}");
+ println("");
+
+ // Generate code for each rule in the grammar
+ Enumeration ids = grammar.rules.elements();
+ int ruleNum = 0;
+ while (ids.hasMoreElements()) {
+ GrammarSymbol sym = (GrammarSymbol)ids.nextElement();
+ if (sym instanceof RuleSymbol) {
+ RuleSymbol rs = (RuleSymbol)sym;
+ genRule(rs, rs.references.size() == 0, ruleNum++, grammar.getClassName() + "::");
+ }
+ exitIfError();
+ }
+ if (usingCustomAST) {
+// println("void "+grammar.getClassName()+"::setASTNodeFactory("+labeledElementASTType+" (*factory)() )");
+// println("{");
+// println("}");
+// println("");
- // close CharStreamException try
- tabs--;
- println("}");
- println("catch ("+namespaceAntlr+"CharStreamIOException& csie) {");
- println(" throw "+namespaceAntlr+"TokenStreamIOException(csie.io);");
- println("}");
- println("catch ("+namespaceAntlr+"CharStreamException& cse) {");
- println(" throw "+namespaceAntlr+"TokenStreamException(cse.getMessage());");
- println("}");
-
- // close for-loop
- _println("tryAgain:;");
- tabs--;
- println("}");
-
- // close method nextToken
- tabs--;
- println("}");
- println("");
- }
- /** Gen a named rule block.
- * ASTs are generated for each element of an alternative unless
- * the rule or the alternative have a '!' modifier.
- *
- * If an alternative defeats the default tree construction, it
- * must set <rule>_AST to the root of the returned AST.
- *
- * Each alternative that does automatic tree construction, builds
- * up root and child list pointers in an ASTPair structure.
- *
- * A rule finishes by setting the returnAST variable from the
- * ASTPair.
- *
- * @param rule The name of the rule to generate
- * @param startSymbol true if the rule is a start symbol (i.e., not referenced elsewhere)
- */
- public void genRule(RuleSymbol s, boolean startSymbol, int ruleNum, String prefix) {
+ // when we are using a custom ast override Parser::getAST to return the
+ // custom AST type
+ println(labeledElementASTType + " " + grammar.getClassName() + "::getAST()");
+ println("{");
+ println("\treturn returnAST;");
+ println("}");
+ println("");
+ }
+
+ // Generate the token names
+ genTokenStrings(grammar.getClassName() + "::");
+
+ // Generate the bitsets used throughout the grammar
+ genBitsets(bitsetsUsed, grammar.tokenManager.maxTokenType(), grammar.getClassName() + "::", true);
+
+ // Generate the semantic predicate map for debugging
+ if (grammar.debuggingOutput)
+ genSemPredMap(grammar.getClassName() + "::");
+
+ // Close class definition
+ println("");
+ println("");
+ if (nameSpace != null)
+ nameSpace.emitClosures(currentOutput);
+
+ // Close the parser output stream
+ currentOutput.close();
+ currentOutput = null;
+ }
+
+ public void genBody(TreeWalkerGrammar g) throws IOException {
+ // Open the output stream for the parser and set the currentOutput
+ outputFile = grammar.getClassName() + ".cpp";
+ outputLine = 1;
+ currentOutput = antlr.Tool.openOutputFile(outputFile);
+ //SAS: changed for proper text file io
+
+ genAST = grammar.buildAST;
+ tabs = 0;
+
+ // Generate the header common to all output files.
+ genHeader(outputFile);
+
+ printHeaderAction(preIncludeCpp);
+
+ // Generate header for the parser
+ println("#include \"" + grammar.getClassName() + ".hpp\"");
+ println("#include \"antlr/Token.hpp\"");
+ println("#include \"antlr/AST.hpp\"");
+ println("#include \"antlr/NoViableAltException.hpp\"");
+ println("#include \"antlr/MismatchedTokenException.hpp\"");
+ println("#include \"antlr/SemanticException.hpp\"");
+ println("#include \"antlr/BitSet.hpp\"");
+
+ printHeaderAction(postIncludeCpp);
+
+ if (nameSpace != null)
+ nameSpace.emitDeclarations(currentOutput);
+
+ // Output the user-defined parser premamble
+ printAction(grammar.preambleAction);
+
+ // Generate parser class definition
+ String sup = null;
+ if (grammar.superClass != null) {
+ sup = grammar.superClass;
+ } else {
+ sup = grammar.getSuperClass();
+ if (sup.lastIndexOf('.') != -1)
+ sup = sup.substring(sup.lastIndexOf('.') + 1);
+ sup = namespaceAntlr + sup;
+ }
+
+ // Generate default parser class constructor
+ println(grammar.getClassName() + "::" + grammar.getClassName() + "()");
+ println("\t: " + namespaceAntlr + "TreeParser() {");
+ tabs++;
+ println("setTokenNames(_tokenNames);");
+ tabs--;
+ println("}");
+ println("");
+
+ // Generate code for each rule in the grammar
+ Enumeration ids = grammar.rules.elements();
+ int ruleNum = 0;
+ String ruleNameInits = "";
+ while (ids.hasMoreElements()) {
+ GrammarSymbol sym = (GrammarSymbol)ids.nextElement();
+ if (sym instanceof RuleSymbol) {
+ RuleSymbol rs = (RuleSymbol)sym;
+ genRule(rs, rs.references.size() == 0, ruleNum++, grammar.getClassName() + "::");
+ }
+ exitIfError();
+ }
+
+ if (usingCustomAST) {
+ // when we are using a custom ast override Parser::getAST to return the
+ // custom AST type
+ println(labeledElementASTType + " " + grammar.getClassName() + "::getAST()");
+ println("{");
+ println("\treturn returnAST;");
+ println("}");
+ println("");
+ }
+
+ // Generate the token names
+ genTokenStrings(grammar.getClassName() + "::");
+
+ // Generate the bitsets used throughout the grammar
+ genBitsets(bitsetsUsed, grammar.tokenManager.maxTokenType(), grammar.getClassName() + "::", true);
+
+ // Close class definition
+ println("");
+ println("");
+
+ if (nameSpace != null)
+ nameSpace.emitClosures(currentOutput);
+
+ // Close the parser output stream
+ currentOutput.close();
+ currentOutput = null;
+ }
+
+ /** Generate a series of case statements that implement a BitSet test.
+ * @param p The Bitset for which cases are to be generated
+ */
+ protected void genCases(BitSet p) {
+ if (DEBUG_CODE_GENERATOR) System.out.println("genCases(" + p + ")");
+ int[] elems;
+
+ elems = p.toArray();
+ // Wrap cases four-per-line for lexer, one-per-line for parser
+ int wrap = 1; //(grammar instanceof LexerGrammar) ? 4 : 1;
+ int j = 1;
+ boolean startOfLine = true;
+ for (int i = 0; i < elems.length; i++) {
+ if (j == 1) {
+ print("");
+ } else {
+ _print(" ");
+ }
+ _print("case " + getValueString(elems[i]) + ":");
+
+ if (j == wrap) {
+ _println("");
+ startOfLine = true;
+ j = 1;
+ } else {
+ j++;
+ startOfLine = false;
+ }
+ }
+ if (!startOfLine) {
+ _println("");
+ }
+ }
+
+ /**Generate common code for a block of alternatives; return a postscript
+ * that needs to be generated at the end of the block. Other routines
+ * may append else-clauses and such for error checking before the postfix
+ * is generated.
+ * If the grammar is a lexer, then generate alternatives in an order where
+ * alternatives requiring deeper lookahead are generated first, and
+ * EOF in the lookahead set reduces the depth of the lookahead.
+ * @param blk The block to generate
+ * @param noTestForSingle If true, then it does not generate a test for a single alternative.
+ */
+ public CppBlockFinishingInfo genCommonBlock(
+ AlternativeBlock blk,
+ boolean noTestForSingle) {
+ int nIF = 0;
+ boolean createdLL1Switch = false;
+ int closingBracesOfIFSequence = 0;
+ CppBlockFinishingInfo finishingInfo = new CppBlockFinishingInfo();
+ if (DEBUG_CODE_GENERATOR) System.out.println("genCommonBlk(" + blk + ")");
+
+ // Save the AST generation state, and set it to that of the block
+ boolean savegenAST = genAST;
+ genAST = genAST && blk.getAutoGen();
+
+ boolean oldsaveTest = saveText;
+ saveText = saveText && blk.getAutoGen();
+
+ // Is this block inverted? If so, generate special-case code
+ if (
+ blk.not &&
+ analyzer.subruleCanBeInverted(blk, grammar instanceof LexerGrammar)
+ ) {
+ Lookahead p = analyzer.look(1, blk);
+ // Variable assignment for labeled elements
+ if (blk.getLabel() != null && syntacticPredLevel == 0) {
+ println(blk.getLabel() + " = " + lt1Value + ";");
+ }
+
+ // AST
+ genElementAST(blk);
+
+ String astArgs = "";
+ if (grammar instanceof TreeWalkerGrammar) {
+ if (usingCustomAST)
+ astArgs = "static_cast<" + namespaceAntlr + "RefAST" + ">(_t),";
+ else
+ astArgs = "_t,";
+ }
+
+ // match the bitset for the alternative
+ println("match(" + astArgs + getBitsetName(markBitsetForGen(p.fset)) + ");");
+
+ // tack on tree cursor motion if doing a tree walker
+ if (grammar instanceof TreeWalkerGrammar) {
+ println("_t = _t->getNextSibling();");
+ }
+ return finishingInfo;
+ }
+
+ // Special handling for single alt
+ if (blk.getAlternatives().size() == 1) {
+ Alternative alt = blk.getAlternativeAt(0);
+ // Generate a warning if there is a synPred for single alt.
+ if (alt.synPred != null) {
+ tool.warning(
+ "Syntactic predicate superfluous for single alternative",
+ grammar.getFilename(),
+ blk.getAlternativeAt(0).synPred.getLine()
+ );
+ }
+ if (noTestForSingle) {
+ if (alt.semPred != null) {
+ // Generate validating predicate
+ genSemPred(alt.semPred, blk.line);
+ }
+ genAlt(alt, blk);
+ return finishingInfo;
+ }
+ }
+
+ // count number of simple LL(1) cases; only do switch for
+ // many LL(1) cases (no preds, no end of token refs)
+ // We don't care about exit paths for (...)*, (...)+
+ // because we don't explicitly have a test for them
+ // as an alt in the loop.
+ //
+ // Also, we now count how many unicode lookahead sets
+ // there are--they must be moved to DEFAULT or ELSE
+ // clause.
+
+ int nLL1 = 0;
+ for (int i = 0; i < blk.getAlternatives().size(); i++) {
+ Alternative a = blk.getAlternativeAt(i);
+ if (suitableForCaseExpression(a)) {
+ nLL1++;
+ }
+ }
+
+ // do LL(1) cases
+ if (nLL1 >= makeSwitchThreshold) {
+ // Determine the name of the item to be compared
+ String testExpr = lookaheadString(1);
+ createdLL1Switch = true;
+ // when parsing trees, convert null to valid tree node with NULL lookahead
+ if (grammar instanceof TreeWalkerGrammar) {
+ println("if (_t == " + labeledElementASTInit + " )");
+ tabs++;
+ println("_t = ASTNULL;");
+ tabs--;
+ }
+ println("switch ( " + testExpr + ") {");
+ for (int i = 0; i < blk.alternatives.size(); i++) {
+ Alternative alt = blk.getAlternativeAt(i);
+ // ignore any non-LL(1) alts, predicated alts or end-of-token alts
+ // or end-of-token alts for case expressions
+ if (!suitableForCaseExpression(alt)) {
+ continue;
+ }
+ Lookahead p = alt.cache[1];
+ if (p.fset.degree() == 0 && !p.containsEpsilon()) {
+ tool.warning("Alternate omitted due to empty prediction set",
+ grammar.getFilename(),
+ alt.head.getLine());
+ } else {
+ genCases(p.fset);
+ println("{");
+ tabs++;
+ genAlt(alt, blk);
+ println("break;");
+ tabs--;
+ println("}");
+ }
+ }
+ println("default:");
+ tabs++;
+ }
+
+ // do non-LL(1) and nondeterministic cases
+ // This is tricky in the lexer, because of cases like:
+ // STAR : '*' ;
+ // ASSIGN_STAR : "*=";
+ // Since nextToken is generated without a loop, then the STAR will
+ // have end-of-token as it's lookahead set for LA(2). So, we must generate the
+ // alternatives containing trailing end-of-token in their lookahead sets *after*
+ // the alternatives without end-of-token. This implements the usual
+ // lexer convention that longer matches come before shorter ones, e.g.
+ // "*=" matches ASSIGN_STAR not STAR
+ //
+ // For non-lexer grammars, this does not sort the alternates by depth
+ // Note that alts whose lookahead is purely end-of-token at k=1 end up
+ // as default or else clauses.
+ int startDepth = (grammar instanceof LexerGrammar) ? grammar.maxk : 0;
+ for (int altDepth = startDepth; altDepth >= 0; altDepth--) {
+ if (DEBUG_CODE_GENERATOR) System.out.println("checking depth " + altDepth);
+ for (int i = 0; i < blk.alternatives.size(); i++) {
+ Alternative alt = blk.getAlternativeAt(i);
+ if (DEBUG_CODE_GENERATOR) System.out.println("genAlt: " + i);
+ // if we made a switch above, ignore what we already took care
+ // of. Specifically, LL(1) alts with no preds
+ // that do not have end-of-token in their prediction set
+ if (createdLL1Switch &&
+ suitableForCaseExpression(alt)) {
+ if (DEBUG_CODE_GENERATOR)
+ System.out.println("ignoring alt because it was in the switch");
+ continue;
+ }
+ String e;
+
+ boolean unpredicted = false;
+
+ if (grammar instanceof LexerGrammar) {
+ // Calculate the "effective depth" of the alt, which is the max
+ // depth at which cache[depth]!=end-of-token
+ int effectiveDepth = alt.lookaheadDepth;
+ if (effectiveDepth == GrammarAnalyzer.NONDETERMINISTIC) {
+ // use maximum lookahead
+ effectiveDepth = grammar.maxk;
+ }
+ while (effectiveDepth >= 1 &&
+ alt.cache[effectiveDepth].containsEpsilon()) {
+ effectiveDepth--;
+ }
+ // Ignore alts whose effective depth is other than the ones we
+ // are generating for this iteration.
+ if (effectiveDepth != altDepth) {
+ if (DEBUG_CODE_GENERATOR)
+ System.out.println("ignoring alt because effectiveDepth!=altDepth;" + effectiveDepth + "!=" + altDepth);
+ continue;
+ }
+ unpredicted = lookaheadIsEmpty(alt, effectiveDepth);
+ e = getLookaheadTestExpression(alt, effectiveDepth);
+ } else {
+ unpredicted = lookaheadIsEmpty(alt, grammar.maxk);
+ e = getLookaheadTestExpression(alt, grammar.maxk);
+ }
+
+ // Was it a big unicode range that forced unsuitability
+ // for a case expression?
+ if (alt.cache[1].fset.degree() > caseSizeThreshold) {
+ if (nIF == 0) {
+ // generate this only for the first if the elseif's
+ // are covered by this one
+ if (grammar instanceof TreeWalkerGrammar) {
+ println("if (_t == " + labeledElementASTInit + " )");
+ tabs++;
+ println("_t = ASTNULL;");
+ tabs--;
+ }
+ println("if " + e + " {");
+ } else
+ println("else if " + e + " {");
+ } else if (unpredicted &&
+ alt.semPred == null &&
+ alt.synPred == null) {
+ // The alt has empty prediction set and no
+ // predicate to help out. if we have not
+ // generated a previous if, just put {...} around
+ // the end-of-token clause
+ if (nIF == 0) {
+ println("{");
+ } else {
+ println("else {");
+ }
+ finishingInfo.needAnErrorClause = false;
+ } else {
+ // check for sem and syn preds
+ // Add any semantic predicate expression to the lookahead test
+ if (alt.semPred != null) {
+ // if debugging, wrap the evaluation of the predicate in a method
+ //
+ // translate $ and # references
+ ActionTransInfo tInfo = new ActionTransInfo();
+ String actionStr = processActionForTreeSpecifiers(alt.semPred,
+ blk.line,
+ currentRule,
+ tInfo);
+ // ignore translation info...we don't need to do anything with it.
+
+ // call that will inform SemanticPredicateListeners of the result
+ if (((grammar instanceof ParserGrammar) || (grammar instanceof LexerGrammar)) && grammar.debuggingOutput)
+ e = "(" + e + "&& fireSemanticPredicateEvaluated(antlr.debug.SemanticPredicateEvent.PREDICTING," + //FIXME
+ addSemPred(charFormatter.escapeString(actionStr)) + "," + actionStr + "))";
+ else
+ e = "(" + e + "&&(" + actionStr + "))";
+ }
+
+ // Generate any syntactic predicates
+ if (nIF > 0) {
+ if (alt.synPred != null) {
+ println("else {");
+ tabs++;
+ genSynPred(alt.synPred, e);
+ closingBracesOfIFSequence++;
+ } else {
+ println("else if " + e + " {");
+ }
+ } else {
+ if (alt.synPred != null) {
+ genSynPred(alt.synPred, e);
+ } else {
+ // when parsing trees, convert null to valid tree node
+ // with NULL lookahead.
+ if (grammar instanceof TreeWalkerGrammar) {
+ println("if (_t == " + labeledElementASTInit + " )");
+ tabs++;
+ println("_t = ASTNULL;");
+ tabs--;
+ }
+ println("if " + e + " {");
+ }
+ }
+
+ }
+
+ nIF++;
+ tabs++;
+ genAlt(alt, blk);
+ tabs--;
+ println("}");
+ }
+ }
+ String ps = "";
+ for (int i = 1; i <= closingBracesOfIFSequence; i++) {
+ tabs--; // does JavaCodeGenerator need this?
+ ps += "}";
+ }
+
+ // Restore the AST generation state
+ genAST = savegenAST;
+
+ // restore save text state
+ saveText = oldsaveTest;
+
+ // Return the finishing info.
+ if (createdLL1Switch) {
+ tabs--;
+ finishingInfo.postscript = ps + "}";
+ finishingInfo.generatedSwitch = true;
+ finishingInfo.generatedAnIf = nIF > 0;
+ //return new CppBlockFinishingInfo(ps+"}",true,nIF>0); // close up switch statement
+
+ } else {
+ finishingInfo.postscript = ps;
+ finishingInfo.generatedSwitch = false;
+ finishingInfo.generatedAnIf = nIF > 0;
+ //return new CppBlockFinishingInfo(ps, false,nIF>0);
+ }
+ return finishingInfo;
+ }
+
+ private static boolean suitableForCaseExpression(Alternative a) {
+ return a.lookaheadDepth == 1 &&
+ a.semPred == null &&
+ !a.cache[1].containsEpsilon() &&
+ a.cache[1].fset.degree() <= caseSizeThreshold;
+ }
+
+ /** Generate code to link an element reference into the AST */
+ private void genElementAST(AlternativeElement el) {
+ // handle case where you're not building trees, but are in tree walker.
+ // Just need to get labels set up.
+ if (grammar instanceof TreeWalkerGrammar && !grammar.buildAST) {
+ String elementRef;
+ String astName;
+
+ // Generate names and declarations of the AST variable(s)
+ if (el.getLabel() == null) {
+ elementRef = lt1Value;
+ // Generate AST variables for unlabeled stuff
+ astName = "tmp" + astVarNumber + "_AST";
+ astVarNumber++;
+ // Map the generated AST variable in the alternate
+ mapTreeVariable(el, astName);
+ // Generate an "input" AST variable also
+ println(labeledElementASTType + " " + astName + "_in = " + elementRef + ";");
+ }
+ return;
+ }
+
+ if (grammar.buildAST && syntacticPredLevel == 0) {
+ boolean doNoGuessTest = (
+ grammar.hasSyntacticPredicate &&
+ (
+ el.getLabel() != null ||
+ el.getAutoGenType() != GrammarElement.AUTO_GEN_BANG
+ )
+ );
+
+ String elementRef;
+ String astName;
+
+ // Generate names and declarations of the AST variable(s)
+ if (el.getLabel() != null) {
+ elementRef = el.getLabel();
+ astName = el.getLabel() + "_AST";
+ } else {
+ elementRef = lt1Value;
+ // Generate AST variables for unlabeled stuff
+ astName = "tmp" + astVarNumber + "_AST";
+ astVarNumber++;
+ // Generate the declaration
+ if (el instanceof GrammarAtom) {
+ GrammarAtom ga = (GrammarAtom)el;
+ if (ga.getASTNodeType() != null) {
+ println("Ref" + ga.getASTNodeType() + " " + astName + ";");
+ } else {
+ println(labeledElementASTType + " " + astName + " = " + labeledElementASTInit + ";");
+ }
+ } else {
+ println(labeledElementASTType + " " + astName + " = " + labeledElementASTInit + ";");
+ }
+
+ // Map the generated AST variable in the alternate
+ mapTreeVariable(el, astName);
+ if (grammar instanceof TreeWalkerGrammar) {
+ // Generate an "input" AST variable also
+ println(labeledElementASTType + " " + astName + "_in = " + labeledElementASTInit + ";");
+ }
+ }
+
+ // Enclose actions with !guessing
+ if (doNoGuessTest) {
+ println("if (inputState->guessing==0) {");
+ tabs++;
+ }
+
+ if (el.getLabel() != null) {
+ if (el instanceof GrammarAtom) {
+ println(astName + " = " +
+ getASTCreateString((GrammarAtom)el, elementRef) + ";");
+ } else {
+ println(astName + " = " +
+ getASTCreateString(elementRef) + ";");
+ }
+ } else {
+ elementRef = lt1Value;
+ if (el instanceof GrammarAtom) {
+ println(astName + " = " +
+ getASTCreateString((GrammarAtom)el, elementRef) + ";");
+ } else {
+ println(astName + " = " +
+ getASTCreateString(elementRef) + ";");
+ }
+ // Map the generated AST variable in the alternate
+ if (grammar instanceof TreeWalkerGrammar) {
+ // set "input" AST variable also
+ println(astName + "_in = " + elementRef + ";");
+ }
+ }
+
+ if (genAST) {
+ switch (el.getAutoGenType()) {
+ case GrammarElement.AUTO_GEN_NONE:
+ println("astFactory.addASTChild(currentAST, " + namespaceAntlr + "RefAST(" + astName + "));");
+ break;
+ case GrammarElement.AUTO_GEN_CARET:
+// println("astFactory.makeASTRoot(currentAST, " + astName + ");");
+ println("astFactory.makeASTRoot(currentAST, " + namespaceAntlr + "RefAST(" + astName + "));");
+ break;
+ default:
+ break;
+ }
+ }
+ if (doNoGuessTest) {
+ tabs--;
+ println("}");
+ }
+ }
+ }
+
+ /** Close the try block and generate catch phrases
+ * if the element has a labeled handler in the rule
+ */
+ private void genErrorCatchForElement(AlternativeElement el) {
+ if (el.getLabel() == null) return;
+ String r = el.enclosingRuleName;
+ if (grammar instanceof LexerGrammar) {
+ r = CodeGenerator.lexerRuleName(el.enclosingRuleName);
+ }
+ RuleSymbol rs = (RuleSymbol)grammar.getSymbol(r);
+ if (rs == null) {
+ tool.panic("Enclosing rule not found!");
+ }
+ ExceptionSpec ex = rs.block.findExceptionSpec(el.getLabel());
+ if (ex != null) {
+ tabs--;
+ println("}");
+ genErrorHandler(ex);
+ }
+ }
+
+ /** Generate the catch phrases for a user-specified error handler */
+ private void genErrorHandler(ExceptionSpec ex) {
+ // Each ExceptionHandler in the ExceptionSpec is a separate catch
+ for (int i = 0; i < ex.handlers.size(); i++) {
+ ExceptionHandler handler = (ExceptionHandler)ex.handlers.elementAt(i);
+ // Generate catch phrase
+ println("catch (" + handler.exceptionTypeAndName.getText() + ") {");
+ tabs++;
+ if (grammar.hasSyntacticPredicate) {
+ println("if (inputState->guessing==0) {");
+ tabs++;
+ }
+
+ // When not guessing, execute user handler action
+ genLineNo(handler.action);
+ printAction(
+ processActionForTreeSpecifiers(handler.action.getText(), 0, currentRule, null)
+ );
+ genLineNo2();
+
+ if (grammar.hasSyntacticPredicate) {
+ tabs--;
+ println("} else {");
+ tabs++;
+ // When guessing, rethrow exception
+ println(
+ "throw " +
+ extractIdOfAction(handler.exceptionTypeAndName) +
+ ";"
+ );
+ tabs--;
+ println("}");
+ }
+ // Close catch phrase
+ tabs--;
+ println("}");
+ }
+ }
+
+ /** Generate a try { opening if the element has a labeled handler in the rule */
+ private void genErrorTryForElement(AlternativeElement el) {
+ if (el.getLabel() == null) return;
+ String r = el.enclosingRuleName;
+ if (grammar instanceof LexerGrammar) {
+ r = CodeGenerator.lexerRuleName(el.enclosingRuleName);
+ }
+ RuleSymbol rs = (RuleSymbol)grammar.getSymbol(r);
+ if (rs == null) {
+ tool.panic("Enclosing rule not found!");
+ }
+ ExceptionSpec ex = rs.block.findExceptionSpec(el.getLabel());
+ if (ex != null) {
+ println("try { // for error handling");
+ tabs++;
+ }
+ }
+
+ /** Generate a header that is common to all C++ files */
+ protected void genHeader(String fileName) {
+ println("/* $ANTLR " + Tool.version + ": " +
+ "\"" + Tool.fileMinusPath(tool.grammarFile) + "\"" +
+ " -> " +
+ "\"" + fileName + "\"$ */");
+ }
+ // these are unique to C++ mode
+
+ public void genInclude(LexerGrammar g) throws IOException {
+ outputFile = grammar.getClassName() + ".hpp";
+ outputLine = 1;
+ currentOutput = antlr.Tool.openOutputFile(outputFile);
+ //SAS: changed for proper text file io
+
+ genAST = false; // no way to gen trees.
+ saveText = true; // save consumed characters.
+
+ tabs = 0;
+
+ // Generate a guard wrapper
+ println("#ifndef INC_" + grammar.getClassName() + "_hpp_");
+ println("#define INC_" + grammar.getClassName() + "_hpp_");
+ println("");
+
+ printHeaderAction(preIncludeHpp);
+
+ println("#include \"antlr/config.hpp\"");
+
+ // Generate header common to all C++ output files
+ genHeader(outputFile);
+
+ // Generate header specific to lexer header file
+ println("#include \"antlr/CommonToken.hpp\"");
+ println("#include \"antlr/InputBuffer.hpp\"");
+ println("#include \"antlr/BitSet.hpp\"");
+ println("#include \"" + grammar.tokenManager.getName() + TokenTypesFileSuffix + ".hpp\"");
+
+ // Find the name of the super class
+ String sup = null;
+ if (grammar.superClass != null) {
+ sup = grammar.superClass;
+ println("#include \"" + sup + ".hpp\"");
+ } else {
+ sup = grammar.getSuperClass();
+ if (sup.lastIndexOf('.') != -1)
+ sup = sup.substring(sup.lastIndexOf('.') + 1);
+ println("#include \"antlr/" + sup + ".hpp\"");
+ sup = namespaceAntlr + sup;
+ }
+
+ // Do not use printAction because we assume tabs==0
+ printHeaderAction(postIncludeHpp);
+
+ if (nameSpace != null)
+ nameSpace.emitDeclarations(currentOutput);
+
+ printHeaderAction("");
+
+ // print javadoc comment if any
+ if (grammar.comment != null) {
+ _println(grammar.comment);
+ }
+
+ // Generate lexer class definition
+ print("class " + grammar.getClassName() + " : public " + sup);
+ println(", public " + grammar.tokenManager.getName() + TokenTypesFileSuffix);
+
+ Token tsuffix = (Token)grammar.options.get("classHeaderSuffix");
+ if (tsuffix != null) {
+ String suffix = Tool.stripFrontBack(tsuffix.getText(), "\"", "\"");
+ if (suffix != null) {
+ print(", " + suffix); // must be an interface name for Java
+ }
+ }
+ println(" {");
+
+ // Generate user-defined lexer class members
+ if (grammar.classMemberAction != null) {
+ genLineNo(grammar.classMemberAction);
+ print(
+ processActionForTreeSpecifiers(grammar.classMemberAction.getText(), 0, currentRule, null)
+ );
+ genLineNo2();
+ }
+
+ // Generate initLiterals() method
+ tabs = 0;
+ println("private:");
+ tabs = 1;
+ println("void initLiterals();");
+
+ // Generate getCaseSensitiveLiterals() method
+ tabs = 0;
+ println("public:");
+ tabs = 1;
+ println("bool getCaseSensitiveLiterals() const;");
+
+ // Make constructors public
+ tabs = 0;
+ println("public:");
+ tabs = 1;
+
+ // Generate the constructor from std::istream
+ println(grammar.getClassName() + "(" + namespaceStd + "istream& in);");
+
+ // Generate the constructor from InputBuffer
+ println(grammar.getClassName() + "(" + namespaceAntlr + "InputBuffer& ib);");
+
+ println(grammar.getClassName() + "(const " + namespaceAntlr + "LexerSharedInputState& state);");
+
+ // Generate nextToken() rule.
+ // nextToken() is a synthetic lexer rule that is the implicit OR of all
+ // user-defined lexer rules.
+ println(namespaceAntlr + "RefToken nextToken();");
+
+ // Generate code for each rule in the lexer
+ Enumeration ids = grammar.rules.elements();
+ while (ids.hasMoreElements()) {
+ RuleSymbol sym = (RuleSymbol)ids.nextElement();
+ // Don't generate the synthetic rules
+ if (!sym.getId().equals("mnextToken")) {
+ genRuleHeader(sym, false);
+ }
+ exitIfError();
+ }
+
+ // Make the rest private
+ tabs = 0;
+ println("private:");
+ tabs = 1;
+
+ // generate the rule name array for debugging
+ if (grammar.debuggingOutput) {
+ println("static const char* _ruleNames[];");
+ }
+
+ // Generate the semantic predicate map for debugging
+ if (grammar.debuggingOutput)
+ println("static const char* _semPredNames[];");
+
+ // Generate the bitsets used throughout the lexer
+ genBitsetsHeader(bitsetsUsed, ((LexerGrammar)grammar).charVocabulary.size());
+
+ tabs = 0;
+ println("};");
+ println("");
+ if (nameSpace != null)
+ nameSpace.emitClosures(currentOutput);
+
+ // Generate a guard wrapper
+ println("#endif /*INC_" + grammar.getClassName() + "_hpp_*/");
+
+ // Close the lexer output stream
+ currentOutput.close();
+ currentOutput = null;
+ }
+
+ public void genInclude(ParserGrammar g) throws IOException {
+ // Open the output stream for the parser and set the currentOutput
+ outputFile = grammar.getClassName() + ".hpp";
+ outputLine = 1;
+ currentOutput = antlr.Tool.openOutputFile(outputFile);
+ //SAS: changed for proper text file io
+
+ genAST = grammar.buildAST;
+
+ tabs = 0;
+
+ // Generate a guard wrapper
+ println("#ifndef INC_" + grammar.getClassName() + "_hpp_");
+ println("#define INC_" + grammar.getClassName() + "_hpp_");
+ println("");
+ printHeaderAction(preIncludeHpp);
+ println("#include \"antlr/config.hpp\"");
+
+ // Generate the header common to all output files.
+ genHeader(outputFile);
+
+ // Generate header for the parser
+ println("#include \"antlr/TokenStream.hpp\"");
+ println("#include \"antlr/TokenBuffer.hpp\"");
+ println("#include \"" + grammar.tokenManager.getName() + TokenTypesFileSuffix + ".hpp\"");
+
+ // Generate parser class definition
+ String sup = null;
+ if (grammar.superClass != null) {
+ sup = grammar.superClass;
+ println("#include \"" + sup + ".hpp\"");
+ } else {
+ sup = grammar.getSuperClass();
+ if (sup.lastIndexOf('.') != -1)
+ sup = sup.substring(sup.lastIndexOf('.') + 1);
+ println("#include \"antlr/" + sup + ".hpp\"");
+ sup = namespaceAntlr + sup;
+ }
+ println("");
+
+ // Do not use printAction because we assume tabs==0
+ printHeaderAction(postIncludeHpp);
+
+ if (nameSpace != null)
+ nameSpace.emitDeclarations(currentOutput);
+
+ printHeaderAction("");
+
+ // print javadoc comment if any
+ if (grammar.comment != null) {
+ _println(grammar.comment);
+ }
+
+ // generate the actual class definition
+ print("class " + grammar.getClassName() + " : public " + sup);
+ println(", public " + grammar.tokenManager.getName() + TokenTypesFileSuffix);
+
+ Token tsuffix = (Token)grammar.options.get("classHeaderSuffix");
+ if (tsuffix != null) {
+ String suffix = Tool.stripFrontBack(tsuffix.getText(), "\"", "\"");
+ if (suffix != null)
+ print(", " + suffix); // must be an interface name for Java
+ }
+ println(" {");
+
+ // set up an array of all the rule names so the debugger can
+ // keep track of them only by number -- less to store in tree...
+ if (grammar.debuggingOutput) {
+ println("public: static const char* _ruleNames[];");
+ }
+
+ // Generate user-defined parser class members
+ if (grammar.classMemberAction != null) {
+ genLineNo(grammar.classMemberAction.getLine());
+ print(
+ processActionForTreeSpecifiers(grammar.classMemberAction.getText(), 0, currentRule, null)
+ );
+ genLineNo2();
+ }
+
+ // Generate parser class constructor from TokenBuffer
+ tabs = 0;
+ println("protected:");
+ tabs = 1;
+ println(grammar.getClassName() + "(" + namespaceAntlr + "TokenBuffer& tokenBuf, int k);");
+ tabs = 0;
+ println("public:");
+ tabs = 1;
+ println(grammar.getClassName() + "(" + namespaceAntlr + "TokenBuffer& tokenBuf);");
+
+ // Generate parser class constructor from TokenStream
+ tabs = 0;
+ println("protected:");
+ tabs = 1;
+ println(grammar.getClassName() + "(" + namespaceAntlr + "TokenStream& lexer, int k);");
+ tabs = 0;
+ println("public:");
+ tabs = 1;
+ println(grammar.getClassName() + "(" + namespaceAntlr + "TokenStream& lexer);");
+
+ println(grammar.getClassName() + "(const " + namespaceAntlr + "ParserSharedInputState& state);");
+
+ // Generate code for each rule in the grammar
+ Enumeration ids = grammar.rules.elements();
+ while (ids.hasMoreElements()) {
+ GrammarSymbol sym = (GrammarSymbol)ids.nextElement();
+ if (sym instanceof RuleSymbol) {
+ RuleSymbol rs = (RuleSymbol)sym;
+ genRuleHeader(rs, rs.references.size() == 0);
+ }
+ exitIfError();
+ }
+
+ if (usingCustomAST) {
+ // when we are using a custom ast override Parser::getAST to return the
+ // custom AST type
+ tabs = 0;
+ println("public:");
+ tabs = 1;
+ println(labeledElementASTType + " getAST();");
+ println("");
+ tabs = 0;
+ println("protected:");
+ tabs = 1;
+ println(labeledElementASTType + " returnAST;");
+ }
+
+ // Make the rest private
+ tabs = 0;
+ println("private:");
+ tabs = 1;
+
+ // Generate the token names
+ println("static const char* _tokenNames[];");
+
+ // Generate the bitsets used throughout the grammar
+ genBitsetsHeader(bitsetsUsed, grammar.tokenManager.maxTokenType());
+
+ // Generate the semantic predicate map for debugging
+ if (grammar.debuggingOutput)
+ println("static const char* _semPredNames[];");
+
+ // Close class definition
+ tabs = 0;
+ println("};");
+ println("");
+ if (nameSpace != null)
+ nameSpace.emitClosures(currentOutput);
+
+ // Generate a guard wrapper
+ println("#endif /*INC_" + grammar.getClassName() + "_hpp_*/");
+
+ // Close the parser output stream
+ currentOutput.close();
+ currentOutput = null;
+ }
+
+ public void genInclude(TreeWalkerGrammar g) throws IOException {
+ // Open the output stream for the parser and set the currentOutput
+ outputFile = grammar.getClassName() + ".hpp";
+ outputLine = 1;
+ currentOutput = antlr.Tool.openOutputFile(outputFile);
+ //SAS: changed for proper text file io
+
+ genAST = grammar.buildAST;
+ tabs = 0;
+
+ // Generate a guard wrapper
+ println("#ifndef INC_" + grammar.getClassName() + "_hpp_");
+ println("#define INC_" + grammar.getClassName() + "_hpp_");
+ println("");
+ printHeaderAction(preIncludeHpp);
+ println("#include \"antlr/config.hpp\"");
+ println("#include \"" + grammar.tokenManager.getName() + TokenTypesFileSuffix + ".hpp\"");
+
+ // Generate the header common to all output files.
+ genHeader(outputFile);
+
+ // Find the name of the super class
+ String sup = null;
+ if (grammar.superClass != null) {
+ sup = grammar.superClass;
+ println("#include \"" + sup + ".hpp\"");
+ } else {
+ sup = grammar.getSuperClass();
+ if (sup.lastIndexOf('.') != -1)
+ sup = sup.substring(sup.lastIndexOf('.') + 1);
+ println("#include \"antlr/" + sup + ".hpp\"");
+ sup = namespaceAntlr + sup;
+ }
+ println("");
+
+ // Generate header for the parser
+
+ // Do not use printAction because we assume tabs==0
+ printHeaderAction(postIncludeHpp);
+
+ if (nameSpace != null)
+ nameSpace.emitDeclarations(currentOutput);
+
+ printHeaderAction("");
+
+ // print javadoc comment if any
+ if (grammar.comment != null) {
+ _println(grammar.comment);
+ }
+
+ // Generate parser class definition
+ print("class " + grammar.getClassName() + " : public " + sup);
+ println(", public " + grammar.tokenManager.getName() + TokenTypesFileSuffix);
+
+ Token tsuffix = (Token)grammar.options.get("classHeaderSuffix");
+ if (tsuffix != null) {
+ String suffix = Tool.stripFrontBack(tsuffix.getText(), "\"", "\"");
+ if (suffix != null) {
+ print(", " + suffix); // must be an interface name for Java
+ }
+ }
+ println(" {");
+
+ // Generate user-defined parser class members
+ if (grammar.classMemberAction != null) {
+ genLineNo(grammar.classMemberAction.getLine());
+ print(
+ processActionForTreeSpecifiers(grammar.classMemberAction.getText(), 0, currentRule, null)
+ );
+ genLineNo2();
+ }
+
+ // Generate default parser class constructor
+ tabs = 0;
+ println("public:");
+ tabs = 1;
+ println(grammar.getClassName() + "();");
+
+ // Generate code for each rule in the grammar
+ Enumeration ids = grammar.rules.elements();
+ String ruleNameInits = "";
+ while (ids.hasMoreElements()) {
+ GrammarSymbol sym = (GrammarSymbol)ids.nextElement();
+ if (sym instanceof RuleSymbol) {
+ RuleSymbol rs = (RuleSymbol)sym;
+ genRuleHeader(rs, rs.references.size() == 0);
+ }
+ exitIfError();
+ }
+ if (usingCustomAST) {
+ // when we are using a custom ast override TreeParser::getAST to return the
+ // custom AST type
+ tabs = 0;
+ println("public:");
+ tabs = 1;
+ println(labeledElementASTType + " getAST();");
+ println("");
+ tabs = 0;
+ println("protected:");
+ tabs = 1;
+ println(labeledElementASTType + " returnAST;");
+ println(labeledElementASTType + " _retTree;");
+ }
+
+ // Make the rest private
+ tabs = 0;
+ println("private:");
+ tabs = 1;
+
+ // Generate the token names
+ println("static const char* _tokenNames[];");
+
+ // Generate the bitsets used throughout the grammar
+ genBitsetsHeader(bitsetsUsed, grammar.tokenManager.maxTokenType());
+
+ // Close class definition
+ tabs = 0;
+ println("};");
+ println("");
+ if (nameSpace != null)
+ nameSpace.emitClosures(currentOutput);
+
+ // Generate a guard wrapper
+ println("#endif /*INC_" + grammar.getClassName() + "_hpp_*/");
+
+ // Close the parser output stream
+ currentOutput.close();
+ currentOutput = null;
+ }
+
+ private void genLiteralsTest() {
+ println("_ttype = testLiteralsTable(_ttype);");
+ }
+
+ private void genLiteralsTestForPartialToken() {
+ println("_ttype = testLiteralsTable(text.substr(_begin, text.length()-_begin),_ttype);");
+ }
+
+ protected void genMatch(BitSet b) {
+ }
+
+ protected void genMatch(GrammarAtom atom) {
+ if (atom instanceof StringLiteralElement) {
+ if (grammar instanceof LexerGrammar) {
+ genMatchUsingAtomText(atom);
+ } else {
+ genMatchUsingAtomTokenType(atom);
+ }
+ } else if (atom instanceof CharLiteralElement) {
+ if (grammar instanceof LexerGrammar) {
+ genMatchUsingAtomText(atom);
+ } else {
+ tool.error("cannot ref character literals in grammar: " + atom);
+ }
+ } else if (atom instanceof TokenRefElement) {
+ genMatchUsingAtomText(atom);
+ }
+ }
+
+ protected void genMatchUsingAtomText(GrammarAtom atom) {
+ // match() for trees needs the _t cursor
+ String astArgs = "";
+ if (grammar instanceof TreeWalkerGrammar) {
+ if (usingCustomAST)
+ astArgs = "static_cast<" + namespaceAntlr + "RefAST" + ">(_t),";
+ else
+ astArgs = "_t,";
+ }
+
+ // if in lexer and ! on element, save buffer index to kill later
+ if (grammar instanceof LexerGrammar && (!saveText || atom.getAutoGenType() == GrammarElement.AUTO_GEN_BANG)) {
+ println("_saveIndex=text.length();");
+ }
+
+ print(atom.not ? "matchNot(" : "match(");
+ _print(astArgs);
+
+ // print out what to match
+ if (atom.atomText.equals("EOF")) {
+ // horrible hack to handle EOF case
+ _print(namespaceAntlr + "Token::EOF_TYPE");
+ } else {
+ _print(textOrChar(atom.atomText));
+ }
+ _println(");");
+
+ if (grammar instanceof LexerGrammar && (!saveText || atom.getAutoGenType() == GrammarElement.AUTO_GEN_BANG)) {
+ println("text.erase(_saveIndex);"); // kill text atom put in buffer
+ }
+ }
+
+ protected void genMatchUsingAtomTokenType(GrammarAtom atom) {
+ // match() for trees needs the _t cursor
+ String astArgs = "";
+ if (grammar instanceof TreeWalkerGrammar) {
+ if (usingCustomAST)
+ astArgs = "static_cast<" + namespaceAntlr + "RefAST" + ">(_t),";
+ else
+ astArgs = "_t,";
+ }
+
+ // If the literal can be mangled, generate the symbolic constant instead
+ String mangledName = null;
+ String s = astArgs + getValueString(atom.getType());
+
+ // matching
+ println((atom.not ? "matchNot(" : "match(") + s + ");");
+ }
+
+ /** Generate the nextToken() rule.
+ * nextToken() is a synthetic lexer rule that is the implicit OR of all
+ * user-defined lexer rules.
+ * @param RuleBlock
+ */
+ public void genNextToken() {
+ // Are there any public rules? If not, then just generate a
+ // fake nextToken().
+ boolean hasPublicRules = false;
+ for (int i = 0; i < grammar.rules.size(); i++) {
+ RuleSymbol rs = (RuleSymbol)grammar.rules.elementAt(i);
+ if (rs.isDefined() && rs.access.equals("public")) {
+ hasPublicRules = true;
+ break;
+ }
+ }
+ if (!hasPublicRules) {
+ println("");
+ println(namespaceAntlr + "RefToken " + grammar.getClassName() + "::nextToken() { return " + namespaceAntlr + "RefToken(new " + namespaceAntlr + "CommonToken(" + namespaceAntlr + "Token::EOF_TYPE, \"\")); }");
+ println("");
+ return;
+ }
+
+ // Create the synthesized nextToken() rule
+ RuleBlock nextTokenBlk = MakeGrammar.createNextTokenRule(grammar, grammar.rules, "nextToken");
+ // Define the nextToken rule symbol
+ RuleSymbol nextTokenRs = new RuleSymbol("mnextToken");
+ nextTokenRs.setDefined();
+ nextTokenRs.setBlock(nextTokenBlk);
+ nextTokenRs.access = "private";
+ grammar.define(nextTokenRs);
+ // Analyze the nextToken rule
+ boolean ok = grammar.theLLkAnalyzer.deterministic(nextTokenBlk);
+
+ // Generate the next token rule
+ String filterRule = null;
+ if (((LexerGrammar)grammar).filterMode) {
+ filterRule = ((LexerGrammar)grammar).filterRule;
+ }
+
+ println("");
+ println(namespaceAntlr + "RefToken " + grammar.getClassName() + "::nextToken()");
+ println("{");
+ tabs++;
+ println(namespaceAntlr + "RefToken theRetToken;");
+ println("for (;;) {");
+ tabs++;
+ println(namespaceAntlr + "RefToken theRetToken;");
+ println("int _ttype = " + namespaceAntlr + "Token::INVALID_TYPE;");
+ if (((LexerGrammar)grammar).filterMode) {
+ println("setCommitToPath(false);");
+ if (filterRule != null) {
+ // Here's a good place to ensure that the filter rule actually exists
+ if (!grammar.isDefined(CodeGenerator.lexerRuleName(filterRule))) {
+ grammar.tool.error("Filter rule " + filterRule + " does not exist in this lexer");
+ } else {
+ RuleSymbol rs = (RuleSymbol)grammar.getSymbol(CodeGenerator.lexerRuleName(filterRule));
+ if (!rs.isDefined()) {
+ grammar.tool.error("Filter rule " + filterRule + " does not exist in this lexer");
+ } else if (rs.access.equals("public")) {
+ grammar.tool.error("Filter rule " + filterRule + " must be protected");
+ }
+ }
+ println("int _m;");
+ println("_m = mark();");
+ }
+ }
+ println("resetText();");
+
+ println("try { // for char stream error handling");
+ tabs++;
+
+ // Generate try around whole thing to trap scanner errors
+ println("try { // for lexical error handling");
+ tabs++;
+
+ // Test for public lexical rules with empty paths
+ for (int i = 0; i < nextTokenBlk.getAlternatives().size(); i++) {
+ Alternative a = nextTokenBlk.getAlternativeAt(i);
+ if (a.cache[1].containsEpsilon()) {
+ tool.warning("found optional path in nextToken()");
+ }
+ }
+
+ // Generate the block
+ String newline = System.getProperty("line.separator");
+ CppBlockFinishingInfo howToFinish = genCommonBlock(nextTokenBlk, false);
+ String errFinish = "if (LA(1)==EOF_CHAR) {uponEOF(); _returnToken = makeToken(" + namespaceAntlr + "Token::EOF_TYPE);}";
+ errFinish += newline + "\t\t\t\t";
+ if (((LexerGrammar)grammar).filterMode) {
+ if (filterRule == null) {
+ errFinish += "else {consume(); goto tryAgain;}";
+ } else {
+ errFinish += "else {" + newline +
+ "\t\t\t\t\tcommit();" + newline +
+ "\t\t\t\t\ttry {m" + filterRule + "(false);}" + newline +
+ "\t\t\t\t\tcatch(" + namespaceAntlr + "RecognitionException& e) {" + newline +
+ "\t\t\t\t\t // catastrophic failure" + newline +
+ "\t\t\t\t\t reportError(e);" + newline +
+ "\t\t\t\t\t consume();" + newline +
+ "\t\t\t\t\t}" + newline +
+ "\t\t\t\t\tgoto tryAgain;" + newline +
+ "\t\t\t\t}";
+ }
+ } else {
+ errFinish += "else {" + throwNoViable + "}";
+ }
+ genBlockFinish(howToFinish, errFinish);
+
+ // at this point a valid token has been matched, undo "mark" that was done
+ if (((LexerGrammar)grammar).filterMode && filterRule != null) {
+ println("commit();");
+ }
+
+ // Generate literals test if desired
+ // make sure _ttype is set first; note _returnToken must be
+ // non-null as the rule was required to create it.
+ println("if ( !_returnToken ) goto tryAgain; // found SKIP token");
+ println("_ttype = _returnToken->getType();");
+ if (((LexerGrammar)grammar).getTestLiterals()) {
+ genLiteralsTest();
+ }
+
+ // return token created by rule reference in switch
+ println("_returnToken->setType(_ttype);");
+ println("return _returnToken;");
+
+ // Close try block
+ tabs--;
+ println("}");
+ println("catch (" + namespaceAntlr + "RecognitionException& e) {");
+ tabs++;
+ if (((LexerGrammar)grammar).filterMode) {
+ if (filterRule == null) {
+ println("if ( !getCommitToPath() ) {consume(); goto tryAgain;}");
+ } else {
+ println("if ( !getCommitToPath() ) {");
+ tabs++;
+ println("rewind(_m);");
+ println("resetText();");
+ println("try {m" + filterRule + "(false);}");
+ println("catch(" + namespaceAntlr + "RecognitionException& ee) {");
+ println(" // horrendous failure: error in filter rule");
+ println(" reportError(ee);");
+ println(" consume();");
+ println("}");
+ println("goto tryAgain;");
+ tabs--;
+ println("}");
+ }
+ }
+ if (nextTokenBlk.getDefaultErrorHandler()) {
+ println("reportError(e);");
+ println("consume();");
+ } else {
+ // pass on to invoking routine
+ println("throw " + namespaceAntlr + "TokenStreamRecognitionException(e);");
+ }
+ tabs--;
+ println("}");
+
+ // close CharStreamException try
+ tabs--;
+ println("}");
+ println("catch (" + namespaceAntlr + "CharStreamIOException& csie) {");
+ println(" throw " + namespaceAntlr + "TokenStreamIOException(csie.io);");
+ println("}");
+ println("catch (" + namespaceAntlr + "CharStreamException& cse) {");
+ println(" throw " + namespaceAntlr + "TokenStreamException(cse.getMessage());");
+ println("}");
+
+ // close for-loop
+ _println("tryAgain:;");
+ tabs--;
+ println("}");
+
+ // close method nextToken
+ tabs--;
+ println("}");
+ println("");
+ }
+
+ /** Gen a named rule block.
+ * ASTs are generated for each element of an alternative unless
+ * the rule or the alternative have a '!' modifier.
+ *
+ * If an alternative defeats the default tree construction, it
+ * must set <rule>_AST to the root of the returned AST.
+ *
+ * Each alternative that does automatic tree construction, builds
+ * up root and child list pointers in an ASTPair structure.
+ *
+ * A rule finishes by setting the returnAST variable from the
+ * ASTPair.
+ *
+ * @param rule The name of the rule to generate
+ * @param startSymbol true if the rule is a start symbol (i.e., not referenced elsewhere)
+ */
+ public void genRule(RuleSymbol s, boolean startSymbol, int ruleNum, String prefix) {
// tabs=1; // JavaCodeGenerator needs this
- if ( DEBUG_CODE_GENERATOR ) System.out.println("genRule("+ s.getId() +")");
- if ( !s.isDefined() ) {
- tool.error("undefined rule: "+ s.getId());
- return;
- }
-
- // Generate rule return type, name, arguments
- RuleBlock rblk = s.getBlock();
- currentRule = rblk;
- currentASTResult = s.getId();
-
- // Save the AST generation state, and set it to that of the rule
- boolean savegenAST = genAST;
- genAST = genAST && rblk.getAutoGen();
-
- // boolean oldsaveTest = saveText;
- saveText = rblk.getAutoGen();
-
- // print javadoc comment if any
- if ( s.comment!=null ) {
- _println(s.comment);
- }
+ if (DEBUG_CODE_GENERATOR) System.out.println("genRule(" + s.getId() + ")");
+ if (!s.isDefined()) {
+ tool.error("undefined rule: " + s.getId());
+ return;
+ }
+
+ // Generate rule return type, name, arguments
+ RuleBlock rblk = s.getBlock();
+ currentRule = rblk;
+ currentASTResult = s.getId();
+
+ // Save the AST generation state, and set it to that of the rule
+ boolean savegenAST = genAST;
+ genAST = genAST && rblk.getAutoGen();
+
+ // boolean oldsaveTest = saveText;
+ saveText = rblk.getAutoGen();
+
+ // print javadoc comment if any
+ if (s.comment != null) {
+ _println(s.comment);
+ }
- // Gen method access and final qualifier
+ // Gen method access and final qualifier
// print(s.access + " final ");
-
- // Gen method return type (note lexer return action set at rule creation)
- if (rblk.returnAction != null)
- {
- // Has specified return value
- _print(extractTypeOfAction(rblk.returnAction, rblk.getLine()) + " ");
- } else {
- // No specified return value
- _print("void ");
- }
-
- // Gen method name
- _print(prefix + s.getId() + "(");
- // Additional rule parameters common to all rules for this grammar
- _print(commonExtraParams);
- if (commonExtraParams.length() != 0 && rblk.argAction != null ) {
- _print(",");
- }
-
- // Gen arguments
- if (rblk.argAction != null)
- {
- // Has specified arguments
- _println("");
+ // Gen method return type (note lexer return action set at rule creation)
+ if (rblk.returnAction != null) {
+ // Has specified return value
+ _print(extractTypeOfAction(rblk.returnAction, rblk.getLine()) + " ");
+ } else {
+ // No specified return value
+ _print("void ");
+ }
+
+ // Gen method name
+ _print(prefix + s.getId() + "(");
+
+ // Additional rule parameters common to all rules for this grammar
+ _print(commonExtraParams);
+ if (commonExtraParams.length() != 0 && rblk.argAction != null) {
+ _print(",");
+ }
+
+ // Gen arguments
+ if (rblk.argAction != null) {
+ // Has specified arguments
+ _println("");
// FIXME: make argAction also a token? Hmmmmm
// genLineNo(rblk);
- tabs++;
- println(rblk.argAction);
- tabs--;
- print(")");
+ tabs++;
+ println(rblk.argAction);
+ tabs--;
+ print(")");
// genLineNo2(); // gcc gives error on the brace... hope it works for the others too
- } else {
- // No specified arguments
- _print(")");
- }
+ } else {
+ // No specified arguments
+ _print(")");
+ }
- // Gen throws clause and open curly
+ // Gen throws clause and open curly
// _print(" throws " + exceptionThrown);
// if ( !(grammar instanceof TreeWalkerGrammar) ) {
// _print(", IOException");
// }
- _println(" {");
- tabs++;
-
- // Convert return action to variable declaration
- if (rblk.returnAction != null)
- {
- genLineNo(rblk);
- println(rblk.returnAction + ";");
- genLineNo2();
- }
-
- // print out definitions needed by rules for various grammar types
- if (!commonLocalVars.equals(""))
- println(commonLocalVars);
-
- if (grammar.traceRules) {
- if ( grammar instanceof TreeWalkerGrammar ) {
- if ( usingCustomAST )
- println("Tracer traceInOut(this,\""+ s.getId() +"\",static_cast<"+namespaceAntlr+"RefAST"+">(_t));");
- else
- println("Tracer traceInOut(this,\""+ s.getId() +"\",_t);");
- }
- else {
- println("Tracer traceInOut(this, \""+ s.getId() +"\");");
- }
- }
+ _println(" {");
+ tabs++;
- if ( grammar instanceof LexerGrammar ) {
- // lexer rule default return value is the rule's token name
- // This is a horrible hack to support the built-in EOF lexer rule.
- if (s.getId().equals("mEOF"))
- println("_ttype = "+namespaceAntlr+"Token::EOF_TYPE;");
- else
- println("_ttype = "+ s.getId().substring(1)+";");
- println("int _saveIndex;"); // used for element! (so we can kill text matched for element)
+ // Convert return action to variable declaration
+ if (rblk.returnAction != null) {
+ genLineNo(rblk);
+ println(rblk.returnAction + ";");
+ genLineNo2();
+ }
+
+ // print out definitions needed by rules for various grammar types
+ if (!commonLocalVars.equals(""))
+ println(commonLocalVars);
+
+ if (grammar.traceRules) {
+ if (grammar instanceof TreeWalkerGrammar) {
+ if (usingCustomAST)
+ println("Tracer traceInOut(this,\"" + s.getId() + "\",static_cast<" + namespaceAntlr + "RefAST" + ">(_t));");
+ else
+ println("Tracer traceInOut(this,\"" + s.getId() + "\",_t);");
+ } else {
+ println("Tracer traceInOut(this, \"" + s.getId() + "\");");
+ }
+ }
+
+ if (grammar instanceof LexerGrammar) {
+ // lexer rule default return value is the rule's token name
+ // This is a horrible hack to support the built-in EOF lexer rule.
+ if (s.getId().equals("mEOF"))
+ println("_ttype = " + namespaceAntlr + "Token::EOF_TYPE;");
+ else
+ println("_ttype = " + s.getId().substring(1) + ";");
+ println("int _saveIndex;"); // used for element! (so we can kill text matched for element)
/*
println("boolean old_saveConsumedInput=saveConsumedInput;");
if ( !rblk.getAutoGen() ) { // turn off "save input" if ! on rule
println("saveConsumedInput=false;");
}
*/
- }
+ }
- // if debugging, write code to mark entry to the rule
- if ( grammar.debuggingOutput)
- if (grammar instanceof ParserGrammar)
- println("fireEnterRule(" + ruleNum + ",0);");
- else if (grammar instanceof LexerGrammar)
- println("fireEnterRule(" + ruleNum + ",_ttype);");
+ // if debugging, write code to mark entry to the rule
+ if (grammar.debuggingOutput)
+ if (grammar instanceof ParserGrammar)
+ println("fireEnterRule(" + ruleNum + ",0);");
+ else if (grammar instanceof LexerGrammar)
+ println("fireEnterRule(" + ruleNum + ",_ttype);");
- // Generate trace code if desired
+ // Generate trace code if desired
// if ( grammar.debuggingOutput || grammar.traceRules) {
// println("try { // debugging");
// tabs++;
// }
- // Initialize AST variables
- if (grammar instanceof TreeWalkerGrammar) {
- // "Input" value for rule
+ // Initialize AST variables
+ if (grammar instanceof TreeWalkerGrammar) {
+ // "Input" value for rule
// println(labeledElementASTType+" " + s.getId() + "_AST_in = "+labeledElementASTType+"(_t);");
- println(labeledElementASTType+" " + s.getId() + "_AST_in = _t;");
- }
- if (grammar.buildAST) {
- // Parser member used to pass AST returns from rule invocations
- println("returnAST = "+labeledElementASTInit+";");
- // Tracks AST construction
- println(namespaceAntlr+"ASTPair currentAST;"); // = new ASTPair();");
- // User-settable return value for rule.
- println(labeledElementASTType+" " + s.getId() + "_AST = "+labeledElementASTInit+";");
- }
-
- genBlockPreamble(rblk);
- println("");
-
- // Search for an unlabeled exception specification attached to the rule
- ExceptionSpec unlabeledUserSpec = rblk.findExceptionSpec("");
-
- // Generate try block around the entire rule for error handling
- if (unlabeledUserSpec != null || rblk.getDefaultErrorHandler() ) {
- println("try { // for error handling");
- tabs++;
- }
-
- // Generate the alternatives
- if ( rblk.alternatives.size()==1 ) {
- // One alternative -- use simple form
- Alternative alt = rblk.getAlternativeAt(0);
- String pred = alt.semPred;
- if ( pred!=null )
- genSemPred(pred, currentRule.line);
- if (alt.synPred != null) {
- tool.warning(
- "Syntactic predicate ignored for single alternative",
- grammar.getFilename(),
- alt.synPred.getLine()
- );
- }
- genAlt(alt, rblk);
- }
- else {
- // Multiple alternatives -- generate complex form
- boolean ok = grammar.theLLkAnalyzer.deterministic(rblk);
-
- CppBlockFinishingInfo howToFinish = genCommonBlock(rblk, false);
- genBlockFinish(howToFinish, throwNoViable);
- }
-
- // Generate catch phrase for error handling
- if (unlabeledUserSpec != null || rblk.getDefaultErrorHandler() ) {
- // Close the try block
- tabs--;
- println("}");
- }
-
- // Generate user-defined or default catch phrases
- if (unlabeledUserSpec != null) {
- genErrorHandler(unlabeledUserSpec);
- }
- else if (rblk.getDefaultErrorHandler()) {
- // Generate default catch phrase
- println("catch (" + exceptionThrown + "& ex) {");
- tabs++;
- // Generate code to handle error if not guessing
- if (grammar.hasSyntacticPredicate) {
- println("if( inputState->guessing == 0 ) {");
- tabs++;
- }
- println("reportError(ex);");
- if ( !(grammar instanceof TreeWalkerGrammar) ) {
- // Generate code to consume until token in k==1 follow set
- Lookahead follow = grammar.theLLkAnalyzer.FOLLOW(1, rblk.endNode);
- String followSetName = getBitsetName(markBitsetForGen(follow.fset));
- println("consume();");
- println("consumeUntil(" + followSetName + ");");
- } else {
- // Just consume one token
- println("if ( _t != "+labeledElementASTInit+" )");
- tabs++;
- println("_t = _t->getNextSibling();");
- tabs--;
- }
- if (grammar.hasSyntacticPredicate) {
- tabs--;
- // When guessing, rethrow exception
- println("} else {");
- tabs++;
- println("throw ex;");
- tabs--;
- println("}");
- }
- // Close catch phrase
- tabs--;
- println("}");
- }
-
- // Squirrel away the AST "return" value
- if (grammar.buildAST) {
- println("returnAST = " + s.getId() + "_AST;");
- }
+ println(labeledElementASTType + " " + s.getId() + "_AST_in = _t;");
+ }
+ if (grammar.buildAST) {
+ // Parser member used to pass AST returns from rule invocations
+ println("returnAST = " + labeledElementASTInit + ";");
+ // Tracks AST construction
+ println(namespaceAntlr + "ASTPair currentAST;"); // = new ASTPair();");
+ // User-settable return value for rule.
+ println(labeledElementASTType + " " + s.getId() + "_AST = " + labeledElementASTInit + ";");
+ }
+
+ genBlockPreamble(rblk);
+ println("");
+
+ // Search for an unlabeled exception specification attached to the rule
+ ExceptionSpec unlabeledUserSpec = rblk.findExceptionSpec("");
+
+ // Generate try block around the entire rule for error handling
+ if (unlabeledUserSpec != null || rblk.getDefaultErrorHandler()) {
+ println("try { // for error handling");
+ tabs++;
+ }
+
+ // Generate the alternatives
+ if (rblk.alternatives.size() == 1) {
+ // One alternative -- use simple form
+ Alternative alt = rblk.getAlternativeAt(0);
+ String pred = alt.semPred;
+ if (pred != null)
+ genSemPred(pred, currentRule.line);
+ if (alt.synPred != null) {
+ tool.warning(
+ "Syntactic predicate ignored for single alternative",
+ grammar.getFilename(),
+ alt.synPred.getLine()
+ );
+ }
+ genAlt(alt, rblk);
+ } else {
+ // Multiple alternatives -- generate complex form
+ boolean ok = grammar.theLLkAnalyzer.deterministic(rblk);
+
+ CppBlockFinishingInfo howToFinish = genCommonBlock(rblk, false);
+ genBlockFinish(howToFinish, throwNoViable);
+ }
+
+ // Generate catch phrase for error handling
+ if (unlabeledUserSpec != null || rblk.getDefaultErrorHandler()) {
+ // Close the try block
+ tabs--;
+ println("}");
+ }
+
+ // Generate user-defined or default catch phrases
+ if (unlabeledUserSpec != null) {
+ genErrorHandler(unlabeledUserSpec);
+ } else if (rblk.getDefaultErrorHandler()) {
+ // Generate default catch phrase
+ println("catch (" + exceptionThrown + "& ex) {");
+ tabs++;
+ // Generate code to handle error if not guessing
+ if (grammar.hasSyntacticPredicate) {
+ println("if( inputState->guessing == 0 ) {");
+ tabs++;
+ }
+ println("reportError(ex);");
+ if (!(grammar instanceof TreeWalkerGrammar)) {
+ // Generate code to consume until token in k==1 follow set
+ Lookahead follow = grammar.theLLkAnalyzer.FOLLOW(1, rblk.endNode);
+ String followSetName = getBitsetName(markBitsetForGen(follow.fset));
+ println("consume();");
+ println("consumeUntil(" + followSetName + ");");
+ } else {
+ // Just consume one token
+ println("if ( _t != " + labeledElementASTInit + " )");
+ tabs++;
+ println("_t = _t->getNextSibling();");
+ tabs--;
+ }
+ if (grammar.hasSyntacticPredicate) {
+ tabs--;
+ // When guessing, rethrow exception
+ println("} else {");
+ tabs++;
+ println("throw ex;");
+ tabs--;
+ println("}");
+ }
+ // Close catch phrase
+ tabs--;
+ println("}");
+ }
+
+ // Squirrel away the AST "return" value
+ if (grammar.buildAST) {
+ println("returnAST = " + s.getId() + "_AST;");
+ }
+
+ // Set return tree value for tree walkers
+ if (grammar instanceof TreeWalkerGrammar) {
+ println("_retTree = _t;");
+ }
+
+ // Generate literals test for lexer rules so marked
+ if (rblk.getTestLiterals()) {
+ if (s.access.equals("protected")) {
+ genLiteralsTestForPartialToken();
+ } else {
+ genLiteralsTest();
+ }
+ }
+
+ // if doing a lexer rule, dump code to create token if necessary
+ if (grammar instanceof LexerGrammar) {
+ println("if ( _createToken && _token==" + namespaceAntlr + "nullToken && _ttype!=" + namespaceAntlr + "Token::SKIP ) {");
+ println(" _token = makeToken(_ttype);");
+ println(" _token->setText(text.substr(_begin, text.length()-_begin));");
+ println("}");
+ println("_returnToken = _token;");
+ // It should be easy for an optimizing compiler to realize this does nothing
+ // but it avoids the warning about the variable being unused.
+ println("_saveIndex=0;");
+ }
+
+ // Gen the return statement if there is one (lexer has hard-wired return action)
+ if (rblk.returnAction != null) {
+ println("return " + extractIdOfAction(rblk.returnAction, rblk.getLine()) + ";");
+ }
- // Set return tree value for tree walkers
- if ( grammar instanceof TreeWalkerGrammar ) {
- println("_retTree = _t;");
- }
-
- // Generate literals test for lexer rules so marked
- if (rblk.getTestLiterals()) {
- if ( s.access.equals("protected") ) {
- genLiteralsTestForPartialToken();
- }
- else {
- genLiteralsTest();
- }
- }
-
- // if doing a lexer rule, dump code to create token if necessary
- if ( grammar instanceof LexerGrammar ) {
- println("if ( _createToken && _token=="+namespaceAntlr+"nullToken && _ttype!="+namespaceAntlr+"Token::SKIP ) {");
- println(" _token = makeToken(_ttype);");
- println(" _token->setText(text.substr(_begin, text.length()-_begin));");
- println("}");
- println("_returnToken = _token;");
- // It should be easy for an optimizing compiler to realize this does nothing
- // but it avoids the warning about the variable being unused.
- println("_saveIndex=0;");
- }
-
- // Gen the return statement if there is one (lexer has hard-wired return action)
- if (rblk.returnAction != null) {
- println("return " + extractIdOfAction(rblk.returnAction, rblk.getLine()) + ";");
- }
-
// if ( grammar.debuggingOutput || grammar.traceRules) {
//// tabs--;
//// println("} finally { // debugging");
@@ -3098,491 +3088,483 @@
//// tabs--;
//// println("}");
// }
-
- tabs--;
- println("}");
- println("");
-
- // Restore the AST generation state
- genAST = savegenAST;
-
- // restore char save state
- // saveText = oldsaveTest;
- }
- public void genRuleHeader(RuleSymbol s, boolean startSymbol) {
- tabs=1;
- if ( DEBUG_CODE_GENERATOR ) System.out.println("genRuleHeader("+ s.getId() +")");
- if ( !s.isDefined() ) {
- tool.error("undefined rule: "+ s.getId());
- return;
- }
-
- // Generate rule return type, name, arguments
- RuleBlock rblk = s.getBlock();
- currentRule = rblk;
- currentASTResult = s.getId();
-
- // Save the AST generation state, and set it to that of the rule
- boolean savegenAST = genAST;
- genAST = genAST && rblk.getAutoGen();
-
- // boolean oldsaveTest = saveText;
- saveText = rblk.getAutoGen();
-
- // Gen method access
- print(s.access + ": ");
-
- // Gen method return type (note lexer return action set at rule creation)
- if (rblk.returnAction != null)
- {
- // Has specified return value
- _print(extractTypeOfAction(rblk.returnAction, rblk.getLine()) + " ");
- } else {
- // No specified return value
- _print("void ");
- }
-
- // Gen method name
- _print(s.getId() + "(");
- // Additional rule parameters common to all rules for this grammar
- _print(commonExtraParams);
- if (commonExtraParams.length() != 0 && rblk.argAction != null ) {
- _print(",");
- }
-
- // Gen arguments
- if (rblk.argAction != null)
- {
- // Has specified arguments
- _println("");
- tabs++;
- println(rblk.argAction);
- tabs--;
- print(")");
- } else {
- // No specified arguments
- _print(")");
- }
- _println(";");
-
- tabs--;
-
- // Restore the AST generation state
- genAST = savegenAST;
-
- // restore char save state
- // saveText = oldsaveTest;
- }
- private void GenRuleInvocation(RuleRefElement rr) {
- // dump rule name
- _print(rr.targetRule + "(");
-
- // lexers must tell rule if it should set _returnToken
- if ( grammar instanceof LexerGrammar ) {
- // if labeled, could access Token, so tell rule to create
- if ( rr.getLabel() != null ) {
- _print("true");
- }
- else {
- _print("false");
- }
- if (commonExtraArgs.length() != 0 || rr.args!=null ) {
- _print(",");
- }
- }
-
- // Extra arguments common to all rules for this grammar
- _print(commonExtraArgs);
- if (commonExtraArgs.length() != 0 && rr.args!=null ) {
- _print(",");
- }
-
- // Process arguments to method, if any
- RuleSymbol rs = (RuleSymbol)grammar.getSymbol(rr.targetRule);
- if (rr.args != null)
- {
- // When not guessing, execute user arg action
- ActionTransInfo tInfo = new ActionTransInfo();
- String args = processActionForTreeSpecifiers(rr.args, 0, currentRule, tInfo);
- if ( tInfo.assignToRoot || tInfo.refRuleRoot!=null ) {
- tool.error("Arguments of rule reference '" + rr.targetRule + "' cannot set or ref #"+
- currentRule.getRuleName()+" on line "+rr.getLine());
- }
- _print(args);
-
- // Warn if the rule accepts no arguments
- if (rs.block.argAction == null)
- {
- tool.warning("Rule '" + rr.targetRule + "' accepts no arguments",
- grammar.getFilename(),
- rr.getLine());
- }
- } else {
- // For C++, no warning if rule has parameters, because there may be default
- // values for all of the parameters
- //if (rs.block.argAction != null) {
- // tool.warning("Missing parameters on reference to rule "+rr.targetRule, rr.getLine());
- //}
- }
- _println(");");
-
- // move down to the first child while parsing
- if ( grammar instanceof TreeWalkerGrammar ) {
- println("_t = _retTree;");
- }
- }
- protected void genSemPred(String pred, int line) {
- // translate $ and # references
- ActionTransInfo tInfo = new ActionTransInfo();
- pred = processActionForTreeSpecifiers(pred, line, currentRule, tInfo);
- // ignore translation info...we don't need to do anything with it.
- String escapedPred = charFormatter.escapeString(pred);
-
- // if debugging, wrap the semantic predicate evaluation in a method
- // that can tell SemanticPredicateListeners the result
- if (grammar.debuggingOutput && ((grammar instanceof ParserGrammar) ||
- (grammar instanceof LexerGrammar)))
- pred = "fireSemanticPredicateEvaluated(antlr.debug.SemanticPredicateEvent.VALIDATING," //FIXME
- + addSemPred(escapedPred) + "," + pred + ")";
- println("if (!(" + pred + "))");
- tabs++;
- println("throw "+namespaceAntlr+"SemanticException(\"" + escapedPred + "\");");
- tabs--;
- }
- /** Write an array of Strings which are the semantic predicate
- * expressions. The debugger will reference them by number only
- */
- protected void genSemPredMap(String prefix) {
- Enumeration e = semPreds.elements();
- println("const char* " + prefix + "_semPredNames[] = {");
- tabs++;
- while(e.hasMoreElements())
- println("\""+e.nextElement()+"\",");
- println("0");
- tabs--;
- println("};");
- }
- protected void genSynPred(SynPredBlock blk, String lookaheadExpr) {
- if ( DEBUG_CODE_GENERATOR ) System.out.println("gen=>("+blk+")");
-
- // Dump synpred result variable
- println("bool synPredMatched" + blk.ID + " = false;");
- // Gen normal lookahead test
- println("if (" + lookaheadExpr + ") {");
- tabs++;
-
- // Save input state
- if ( grammar instanceof TreeWalkerGrammar ) {
- println(labeledElementType + " __t" + blk.ID + " = _t;");
- }
- else {
- println("int _m" + blk.ID + " = mark();");
- }
-
- // Once inside the try, assume synpred works unless exception caught
- println("synPredMatched" + blk.ID + " = true;");
- println("inputState->guessing++;");
-
- // if debugging, tell listeners that a synpred has started
- if (grammar.debuggingOutput && ((grammar instanceof ParserGrammar) ||
- (grammar instanceof LexerGrammar))) {
- println("fireSyntacticPredicateStarted();");
- }
-
- syntacticPredLevel++;
- println("try {");
- tabs++;
- gen((AlternativeBlock)blk); // gen code to test predicate
- tabs--;
- //println("System.out.println(\"pred "+blk+" succeeded\");");
- println("}");
- println("catch (" + exceptionThrown + "& pe) {");
- tabs++;
- println("synPredMatched"+blk.ID+" = false;");
- //println("System.out.println(\"pred "+blk+" failed\");");
- tabs--;
- println("}");
-
- // Restore input state
- if ( grammar instanceof TreeWalkerGrammar ) {
- println("_t = __t"+blk.ID+";");
- }
- else {
- println("rewind(_m"+blk.ID+");");
- }
-
- println("inputState->guessing--;");
-
- // if debugging, tell listeners how the synpred turned out
- if (grammar.debuggingOutput && ((grammar instanceof ParserGrammar) ||
- (grammar instanceof LexerGrammar))) {
- println("if (synPredMatched" + blk.ID +")");
- println(" fireSyntacticPredicateSucceeded();");
- println("else");
- println(" fireSyntacticPredicateFailed();");
- }
-
- syntacticPredLevel--;
- tabs--;
-
- // Close lookahead test
- println("}");
-
- // Test synpred result
- println("if ( synPredMatched"+blk.ID+" ) {");
- }
- /** Generate a static array containing the names of the tokens,
- * indexed by the token type values. This static array is used
- * to format error messages so that the token identifers or literal
- * strings are displayed instead of the token numbers.
- *
- * If a lexical rule has a paraphrase, use it rather than the
- * token label.
- */
- public void genTokenStrings(String prefix) {
- // Generate a string for each token. This creates a static
- // array of Strings indexed by token type.
+ tabs--;
+ println("}");
+ println("");
+
+ // Restore the AST generation state
+ genAST = savegenAST;
+
+ // restore char save state
+ // saveText = oldsaveTest;
+ }
+
+ public void genRuleHeader(RuleSymbol s, boolean startSymbol) {
+ tabs = 1;
+ if (DEBUG_CODE_GENERATOR) System.out.println("genRuleHeader(" + s.getId() + ")");
+ if (!s.isDefined()) {
+ tool.error("undefined rule: " + s.getId());
+ return;
+ }
+
+ // Generate rule return type, name, arguments
+ RuleBlock rblk = s.getBlock();
+ currentRule = rblk;
+ currentASTResult = s.getId();
+
+ // Save the AST generation state, and set it to that of the rule
+ boolean savegenAST = genAST;
+ genAST = genAST && rblk.getAutoGen();
+
+ // boolean oldsaveTest = saveText;
+ saveText = rblk.getAutoGen();
+
+ // Gen method access
+ print(s.access + ": ");
+
+ // Gen method return type (note lexer return action set at rule creation)
+ if (rblk.returnAction != null) {
+ // Has specified return value
+ _print(extractTypeOfAction(rblk.returnAction, rblk.getLine()) + " ");
+ } else {
+ // No specified return value
+ _print("void ");
+ }
+
+ // Gen method name
+ _print(s.getId() + "(");
+
+ // Additional rule parameters common to all rules for this grammar
+ _print(commonExtraParams);
+ if (commonExtraParams.length() != 0 && rblk.argAction != null) {
+ _print(",");
+ }
+
+ // Gen arguments
+ if (rblk.argAction != null) {
+ // Has specified arguments
+ _println("");
+ tabs++;
+ println(rblk.argAction);
+ tabs--;
+ print(")");
+ } else {
+ // No specified arguments
+ _print(")");
+ }
+ _println(";");
+
+ tabs--;
+
+ // Restore the AST generation state
+ genAST = savegenAST;
+
+ // restore char save state
+ // saveText = oldsaveTest;
+ }
+
+ private void GenRuleInvocation(RuleRefElement rr) {
+ // dump rule name
+ _print(rr.targetRule + "(");
+
+ // lexers must tell rule if it should set _returnToken
+ if (grammar instanceof LexerGrammar) {
+ // if labeled, could access Token, so tell rule to create
+ if (rr.getLabel() != null) {
+ _print("true");
+ } else {
+ _print("false");
+ }
+ if (commonExtraArgs.length() != 0 || rr.args != null) {
+ _print(",");
+ }
+ }
+
+ // Extra arguments common to all rules for this grammar
+ _print(commonExtraArgs);
+ if (commonExtraArgs.length() != 0 && rr.args != null) {
+ _print(",");
+ }
+
+ // Process arguments to method, if any
+ RuleSymbol rs = (RuleSymbol)grammar.getSymbol(rr.targetRule);
+ if (rr.args != null) {
+ // When not guessing, execute user arg action
+ ActionTransInfo tInfo = new ActionTransInfo();
+ String args = processActionForTreeSpecifiers(rr.args, 0, currentRule, tInfo);
+ if (tInfo.assignToRoot || tInfo.refRuleRoot != null) {
+ tool.error("Arguments of rule reference '" + rr.targetRule + "' cannot set or ref #" +
+ currentRule.getRuleName() + " on line " + rr.getLine());
+ }
+ _print(args);
+
+ // Warn if the rule accepts no arguments
+ if (rs.block.argAction == null) {
+ tool.warning("Rule '" + rr.targetRule + "' accepts no arguments",
+ grammar.getFilename(),
+ rr.getLine());
+ }
+ } else {
+ // For C++, no warning if rule has parameters, because there may be default
+ // values for all of the parameters
+ //if (rs.block.argAction != null) {
+ // tool.warning("Missing parameters on reference to rule "+rr.targetRule, rr.getLine());
+ //}
+ }
+ _println(");");
+
+ // move down to the first child while parsing
+ if (grammar instanceof TreeWalkerGrammar) {
+ println("_t = _retTree;");
+ }
+ }
+
+ protected void genSemPred(String pred, int line) {
+ // translate $ and # references
+ ActionTransInfo tInfo = new ActionTransInfo();
+ pred = processActionForTreeSpecifiers(pred, line, currentRule, tInfo);
+ // ignore translation info...we don't need to do anything with it.
+ String escapedPred = charFormatter.escapeString(pred);
+
+ // if debugging, wrap the semantic predicate evaluation in a method
+ // that can tell SemanticPredicateListeners the result
+ if (grammar.debuggingOutput && ((grammar instanceof ParserGrammar) ||
+ (grammar instanceof LexerGrammar)))
+ pred = "fireSemanticPredicateEvaluated(antlr.debug.SemanticPredicateEvent.VALIDATING," //FIXME
+ + addSemPred(escapedPred) + "," + pred + ")";
+ println("if (!(" + pred + "))");
+ tabs++;
+ println("throw " + namespaceAntlr + "SemanticException(\"" + escapedPred + "\");");
+ tabs--;
+ }
+
+ /** Write an array of Strings which are the semantic predicate
+ * expressions. The debugger will reference them by number only
+ */
+ protected void genSemPredMap(String prefix) {
+ Enumeration e = semPreds.elements();
+ println("const char* " + prefix + "_semPredNames[] = {");
+ tabs++;
+ while (e.hasMoreElements())
+ println("\"" + e.nextElement() + "\",");
+ println("0");
+ tabs--;
+ println("};");
+ }
+
+ protected void genSynPred(SynPredBlock blk, String lookaheadExpr) {
+ if (DEBUG_CODE_GENERATOR) System.out.println("gen=>(" + blk + ")");
+
+ // Dump synpred result variable
+ println("bool synPredMatched" + blk.ID + " = false;");
+ // Gen normal lookahead test
+ println("if (" + lookaheadExpr + ") {");
+ tabs++;
+
+ // Save input state
+ if (grammar instanceof TreeWalkerGrammar) {
+ println(labeledElementType + " __t" + blk.ID + " = _t;");
+ } else {
+ println("int _m" + blk.ID + " = mark();");
+ }
+
+ // Once inside the try, assume synpred works unless exception caught
+ println("synPredMatched" + blk.ID + " = true;");
+ println("inputState->guessing++;");
+
+ // if debugging, tell listeners that a synpred has started
+ if (grammar.debuggingOutput && ((grammar instanceof ParserGrammar) ||
+ (grammar instanceof LexerGrammar))) {
+ println("fireSyntacticPredicateStarted();");
+ }
+
+ syntacticPredLevel++;
+ println("try {");
+ tabs++;
+ gen((AlternativeBlock)blk); // gen code to test predicate
+ tabs--;
+ //println("System.out.println(\"pred "+blk+" succeeded\");");
+ println("}");
+ println("catch (" + exceptionThrown + "& pe) {");
+ tabs++;
+ println("synPredMatched" + blk.ID + " = false;");
+ //println("System.out.println(\"pred "+blk+" failed\");");
+ tabs--;
+ println("}");
+
+ // Restore input state
+ if (grammar instanceof TreeWalkerGrammar) {
+ println("_t = __t" + blk.ID + ";");
+ } else {
+ println("rewind(_m" + blk.ID + ");");
+ }
+
+ println("inputState->guessing--;");
+
+ // if debugging, tell listeners how the synpred turned out
+ if (grammar.debuggingOutput && ((grammar instanceof ParserGrammar) ||
+ (grammar instanceof LexerGrammar))) {
+ println("if (synPredMatched" + blk.ID + ")");
+ println(" fireSyntacticPredicateSucceeded();");
+ println("else");
+ println(" fireSyntacticPredicateFailed();");
+ }
+
+ syntacticPredLevel--;
+ tabs--;
+
+ // Close lookahead test
+ println("}");
+
+ // Test synpred result
+ println("if ( synPredMatched" + blk.ID + " ) {");
+ }
+
+ /** Generate a static array containing the names of the tokens,
+ * indexed by the token type values. This static array is used
+ * to format error messages so that the token identifers or literal
+ * strings are displayed instead of the token numbers.
+ *
+ * If a lexical rule has a paraphrase, use it rather than the
+ * token label.
+ */
+ public void genTokenStrings(String prefix) {
+ // Generate a string for each token. This creates a static
+ // array of Strings indexed by token type.
// println("");
- println("const char* " + prefix + "_tokenNames[] = {");
- tabs++;
-
- // Walk the token vocabulary and generate a Vector of strings
- // from the tokens.
- Vector v = grammar.tokenManager.getVocabulary();
- for (int i = 0; i < v.size(); i++)
- {
- String s = (String)v.elementAt(i);
- if (s == null)
- {
- s = "<"+String.valueOf(i)+">";
- }
- if ( !s.startsWith("\"") && !s.startsWith("<") ) {
- TokenSymbol ts = (TokenSymbol)grammar.tokenManager.getTokenSymbol(s);
- if ( ts!=null && ts.getParaphrase()!=null ) {
- s = antlr.Tool.stripFrontBack(ts.getParaphrase(), "\"", "\"");
- }
- }
- print(charFormatter.literalString(s));
- _println(",");
- }
- println("0");
-
- // Close the string array initailizer
- tabs--;
- println("};");
- }
- /** Generate the token types C++ file */
- protected void genTokenTypes(TokenManager tm) throws IOException {
- // Open the token output header file and set the currentOutput stream
- outputFile = tm.getName() + TokenTypesFileSuffix+".hpp";
- outputLine = 1;
- currentOutput = antlr.Tool.openOutputFile(outputFile);
- //SAS: changed for proper text file io
-
- tabs = 0;
-
- // Generate a guard wrapper
- println("#ifndef INC_"+tm.getName()+TokenTypesFileSuffix+"_hpp_");
- println("#define INC_"+tm.getName()+TokenTypesFileSuffix+"_hpp_");
- println("");
-
- if (nameSpace != null)
- nameSpace.emitDeclarations(currentOutput);
-
- // Generate the header common to all C++ files
- genHeader(outputFile);
-
- // Encapsulate the definitions in an interface. This can be done
- // because they are all constants.
- println("struct " + tm.getName() + TokenTypesFileSuffix+" {");
- tabs++;
- println("enum {");
- tabs++;
-
- // Generate a definition for each token type
- Vector v = tm.getVocabulary();
-
- // Do special tokens manually
- println("EOF_ = " + Token.EOF_TYPE + ",");
-
- // Move the other special token to the end, so we can solve
- // the superfluous comma problem easily
-
- for (int i = Token.MIN_USER_TYPE; i < v.size(); i++) {
- String s = (String)v.elementAt(i);
- if (s != null) {
- if ( s.startsWith("\"") ) {
- // a string literal
- StringLiteralSymbol sl = (StringLiteralSymbol)tm.getTokenSymbol(s);
- if ( sl==null ) {
- antlr.Tool.panic("String literal "+s+" not in symbol table");
- }
- else if ( sl.label != null ) {
- println(sl.label + " = " + i + ",");
- }
- else {
- String mangledName = mangleLiteral(s);
- if (mangledName != null) {
- // We were able to create a meaningful mangled token name
- println(mangledName + " = " + i + ",");
- // if no label specified, make the label equal to the mangled name
- sl.label = mangledName;
- }
- else {
- println("// " + s + " = " + i);
- }
- }
- }
- else if ( !s.startsWith("<") ) {
- println(s + " = " + i + ",");
- }
- }
- }
+ println("const char* " + prefix + "_tokenNames[] = {");
+ tabs++;
- // Moved from above
- println("NULL_TREE_LOOKAHEAD = " + Token.NULL_TREE_LOOKAHEAD);
-
- // Close the enum
- tabs--;
- println("};");
-
- // Close the interface
- tabs--;
- println("};");
-
- if (nameSpace != null)
- nameSpace.emitClosures(currentOutput);
-
- // Generate a guard wrapper
- println("#endif /*INC_"+tm.getName()+TokenTypesFileSuffix+"_hpp_*/");
-
- // Close the tokens output file
- currentOutput.close();
- currentOutput = null;
- exitIfError();
- }
- /** Process a string for an simple expression for use in xx/action.g
- * it is used to cast simple tokens/references to the right type for
- * the generated language. Basically called for every element in
- * the vector to getASTCreateString(vector V)
- * @param str A String.
- */
- public String processStringForASTConstructor( String str )
- {
- if( usingCustomAST &&
- ((grammar instanceof TreeWalkerGrammar) ||
- (grammar instanceof ParserGrammar)) &&
- !(grammar.tokenManager.tokenDefined(str) ) )
- {
+ // Walk the token vocabulary and generate a Vector of strings
+ // from the tokens.
+ Vector v = grammar.tokenManager.getVocabulary();
+ for (int i = 0; i < v.size(); i++) {
+ String s = (String)v.elementAt(i);
+ if (s == null) {
+ s = "<" + String.valueOf(i) + ">";
+ }
+ if (!s.startsWith("\"") && !s.startsWith("<")) {
+ TokenSymbol ts = (TokenSymbol)grammar.tokenManager.getTokenSymbol(s);
+ if (ts != null && ts.getParaphrase() != null) {
+ s = antlr.Tool.stripFrontBack(ts.getParaphrase(), "\"", "\"");
+ }
+ }
+ print(charFormatter.literalString(s));
+ _println(",");
+ }
+ println("0");
+
+ // Close the string array initailizer
+ tabs--;
+ println("};");
+ }
+
+ /** Generate the token types C++ file */
+ protected void genTokenTypes(TokenManager tm) throws IOException {
+ // Open the token output header file and set the currentOutput stream
+ outputFile = tm.getName() + TokenTypesFileSuffix + ".hpp";
+ outputLine = 1;
+ currentOutput = antlr.Tool.openOutputFile(outputFile);
+ //SAS: changed for proper text file io
+
+ tabs = 0;
+
+ // Generate a guard wrapper
+ println("#ifndef INC_" + tm.getName() + TokenTypesFileSuffix + "_hpp_");
+ println("#define INC_" + tm.getName() + TokenTypesFileSuffix + "_hpp_");
+ println("");
+
+ if (nameSpace != null)
+ nameSpace.emitDeclarations(currentOutput);
+
+ // Generate the header common to all C++ files
+ genHeader(outputFile);
+
+ // Encapsulate the definitions in an interface. This can be done
+ // because they are all constants.
+ println("struct " + tm.getName() + TokenTypesFileSuffix + " {");
+ tabs++;
+ println("enum {");
+ tabs++;
+
+ // Generate a definition for each token type
+ Vector v = tm.getVocabulary();
+
+ // Do special tokens manually
+ println("EOF_ = " + Token.EOF_TYPE + ",");
+
+ // Move the other special token to the end, so we can solve
+ // the superfluous comma problem easily
+
+ for (int i = Token.MIN_USER_TYPE; i < v.size(); i++) {
+ String s = (String)v.elementAt(i);
+ if (s != null) {
+ if (s.startsWith("\"")) {
+ // a string literal
+ StringLiteralSymbol sl = (StringLiteralSymbol)tm.getTokenSymbol(s);
+ if (sl == null) {
+ antlr.Tool.panic("String literal " + s + " not in symbol table");
+ } else if (sl.label != null) {
+ println(sl.label + " = " + i + ",");
+ } else {
+ String mangledName = mangleLiteral(s);
+ if (mangledName != null) {
+ // We were able to create a meaningful mangled token name
+ println(mangledName + " = " + i + ",");
+ // if no label specified, make the label equal to the mangled name
+ sl.label = mangledName;
+ } else {
+ println("// " + s + " = " + i);
+ }
+ }
+ } else if (!s.startsWith("<")) {
+ println(s + " = " + i + ",");
+ }
+ }
+ }
+
+ // Moved from above
+ println("NULL_TREE_LOOKAHEAD = " + Token.NULL_TREE_LOOKAHEAD);
+
+ // Close the enum
+ tabs--;
+ println("};");
+
+ // Close the interface
+ tabs--;
+ println("};");
+
+ if (nameSpace != null)
+ nameSpace.emitClosures(currentOutput);
+
+ // Generate a guard wrapper
+ println("#endif /*INC_" + tm.getName() + TokenTypesFileSuffix + "_hpp_*/");
+
+ // Close the tokens output file
+ currentOutput.close();
+ currentOutput = null;
+ exitIfError();
+ }
+
+ /** Process a string for an simple expression for use in xx/action.g
+ * it is used to cast simple tokens/references to the right type for
+ * the generated language. Basically called for every element in
+ * the vector to getASTCreateString(vector V)
+ * @param str A String.
+ */
+ public String processStringForASTConstructor(String str) {
+ if (usingCustomAST &&
+ ((grammar instanceof TreeWalkerGrammar) ||
+ (grammar instanceof ParserGrammar)) &&
+ !(grammar.tokenManager.tokenDefined(str))) {
// System.out.println("processStringForASTConstructor: "+str+" with cast");
- return "static_cast<"+namespaceAntlr+"RefAST>("+str+")";
- }
- else
- {
+ return "static_cast<" + namespaceAntlr + "RefAST>(" + str + ")";
+ } else {
// System.out.println("processStringForASTConstructor: "+str);
- return str;
- }
- }
- /** Get a string for an expression to generate creation of an AST subtree.
- * @param v A Vector of String, where each element is an expression in the target language yielding an AST node.
- */
- public String getASTCreateString(Vector v) {
- if (v.size() == 0) {
- return "";
- }
- StringBuffer buf = new StringBuffer();
- // the labeledElementASTType here can probably be a cast or nothing
- // in the case of ! usingCustomAST
- buf.append(labeledElementASTType+
- "(astFactory.make((new "+namespaceAntlr+
- "ASTArray("+v.size()+"))");
- for (int i = 0; i < v.size(); i++) {
- buf.append("->add("+ v.elementAt(i) + ")");
- }
- buf.append("))");
- return buf.toString();
- }
- /** Get a string for an expression to generate creating of an AST node
- * @param str The arguments to the AST constructor
- */
- public String getASTCreateString(GrammarAtom atom, String str) {
- if ( atom!=null && atom.getASTNodeType() != null ) {
- // RK: I guess this is heterogeneous ast stuff...
- return "Ref"+atom.getASTNodeType()+"(new "+atom.getASTNodeType()+"("+str+"))";
- }
- else
- {
- // This is *SO* ugly... but it will have to do for now... 2.7.2 will
- // have better.
- boolean is_constructor = false;
- if( str.indexOf(',') != -1 )
- is_constructor = grammar.tokenManager.tokenDefined(str.substring(0,str.indexOf(',')));
+ return str;
+ }
+ }
+
+ /** Get a string for an expression to generate creation of an AST subtree.
+ * @param v A Vector of String, where each element is an expression in the target language yielding an AST node.
+ */
+ public String getASTCreateString(Vector v) {
+ if (v.size() == 0) {
+ return "";
+ }
+ StringBuffer buf = new StringBuffer();
+ // the labeledElementASTType here can probably be a cast or nothing
+ // in the case of ! usingCustomAST
+ buf.append(labeledElementASTType +
+ "(astFactory.make((new " + namespaceAntlr +
+ "ASTArray(" + v.size() + "))");
+ for (int i = 0; i < v.size(); i++) {
+ buf.append("->add(" + v.elementAt(i) + ")");
+ }
+ buf.append("))");
+ return buf.toString();
+ }
+
+ /** Get a string for an expression to generate creating of an AST node
+ * @param str The arguments to the AST constructor
+ */
+ public String getASTCreateString(GrammarAtom atom, String str) {
+ if (atom != null && atom.getASTNodeType() != null) {
+ // RK: I guess this is heterogeneous ast stuff...
+ return "Ref" + atom.getASTNodeType() + "(new " + atom.getASTNodeType() + "(" + str + "))";
+ } else {
+ // This is *SO* ugly... but it will have to do for now... 2.7.2 will
+ // have better.
+ boolean is_constructor = false;
+ if (str.indexOf(',') != -1)
+ is_constructor = grammar.tokenManager.tokenDefined(str.substring(0, str.indexOf(',')));
// System.out.println("getAstCreateString(as): "+str+" "+grammar.tokenManager.tokenDefined(str));
- if( usingCustomAST &&
- (grammar instanceof TreeWalkerGrammar) &&
- !(grammar.tokenManager.tokenDefined(str) ) &&
- ! is_constructor )
- return "astFactory.create(static_cast<"+namespaceAntlr+"RefAST>("+str+"))";
- else
- return "astFactory.create("+str+")";
- }
- }
-
- /** Get a string for an expression to generate creating of an AST node
- * @param str The arguments to the AST constructor
- */
- public String getASTCreateString(String str) {
+ if (usingCustomAST &&
+ (grammar instanceof TreeWalkerGrammar) &&
+ !(grammar.tokenManager.tokenDefined(str)) &&
+ !is_constructor)
+ return "astFactory.create(static_cast<" + namespaceAntlr + "RefAST>(" + str + "))";
+ else
+ return "astFactory.create(" + str + ")";
+ }
+ }
+
+ /** Get a string for an expression to generate creating of an AST node
+ * @param str The arguments to the AST constructor
+ */
+ public String getASTCreateString(String str) {
// System.out.println("getAstCreateString(str): "+str+" "+grammar.tokenManager.tokenDefined(str));
- if( usingCustomAST )
- return "static_cast<"+labeledElementASTType+">(astFactory.create(static_cast<"+namespaceAntlr+"RefAST>( /*ss*/ "+str+")))";
- else
- return "astFactory.create("+str+")";
- }
-
- protected String getLookaheadTestExpression(Lookahead[] look, int k) {
- StringBuffer e = new StringBuffer(100);
- boolean first = true;
-
- e.append("(");
- for (int i = 1; i <= k; i++) {
- BitSet p = look[i].fset;
- if (!first) {
- e.append(") && (");
- }
- first = false;
-
- // Syn preds can yield <end-of-syn-pred> (epsilon) lookahead.
- // There is no way to predict what that token would be. Just
- // allow anything instead.
- if (look[i].containsEpsilon()) {
- e.append("true");
- } else {
- e.append(getLookaheadTestTerm(i, p));
- }
- }
- e.append(")");
-
- return e.toString();
- }
- /** Generate a lookahead test expression for an alternate. This
- * will be a series of tests joined by '&&' and enclosed by '()',
- * the number of such tests being determined by the depth of the lookahead.
- */
- protected String getLookaheadTestExpression(Alternative alt, int maxDepth) {
- int depth = alt.lookaheadDepth;
- if ( depth == GrammarAnalyzer.NONDETERMINISTIC ) {
- // if the decision is nondeterministic, do the best we can: LL(k)
- // any predicates that are around will be generated later.
- depth = grammar.maxk;
- }
-
- if ( maxDepth==0 ) {
- // empty lookahead can result from alt with sem pred
- // that can see end of token. E.g., A : {pred}? ('a')? ;
- return "true";
- }
+ if (usingCustomAST)
+ return "static_cast<" + labeledElementASTType + ">(astFactory.create(static_cast<" + namespaceAntlr + "RefAST>( /*ss*/ " + str + ")))";
+ else
+ return "astFactory.create(" + str + ")";
+ }
+
+ protected String getLookaheadTestExpression(Lookahead[] look, int k) {
+ StringBuffer e = new StringBuffer(100);
+ boolean first = true;
+
+ e.append("(");
+ for (int i = 1; i <= k; i++) {
+ BitSet p = look[i].fset;
+ if (!first) {
+ e.append(") && (");
+ }
+ first = false;
+
+ // Syn preds can yield <end-of-syn-pred> (epsilon) lookahead.
+ // There is no way to predict what that token would be. Just
+ // allow anything instead.
+ if (look[i].containsEpsilon()) {
+ e.append("true");
+ } else {
+ e.append(getLookaheadTestTerm(i, p));
+ }
+ }
+ e.append(")");
+
+ return e.toString();
+ }
+
+ /** Generate a lookahead test expression for an alternate. This
+ * will be a series of tests joined by '&&' and enclosed by '()',
+ * the number of such tests being determined by the depth of the lookahead.
+ */
+ protected String getLookaheadTestExpression(Alternative alt, int maxDepth) {
+ int depth = alt.lookaheadDepth;
+ if (depth == GrammarAnalyzer.NONDETERMINISTIC) {
+ // if the decision is nondeterministic, do the best we can: LL(k)
+ // any predicates that are around will be generated later.
+ depth = grammar.maxk;
+ }
+
+ if (maxDepth == 0) {
+ // empty lookahead can result from alt with sem pred
+ // that can see end of token. E.g., A : {pred}? ('a')? ;
+ return "true";
+ }
/*
boolean first = true;
@@ -3606,457 +3588,451 @@
e.append(")");
*/
-
- return "(" + getLookaheadTestExpression(alt.cache,depth) + ")";
- }
- /**Generate a depth==1 lookahead test expression given the BitSet.
- * This may be one of:
- * 1) a series of 'x==X||' tests
- * 2) a range test using >= && <= where possible,
- * 3) a bitset membership test for complex comparisons
- * @param k The lookahead level
- * @param p The lookahead set for level k
- */
- protected String getLookaheadTestTerm(int k, BitSet p) {
- // Determine the name of the item to be compared
- String ts = lookaheadString(k);
-
- // Generate a range expression if possible
- int[] elems = p.toArray();
- if (elementsAreRange(elems)) {
- return getRangeExpression(k, elems);
- }
-
- // Generate a bitset membership test if possible
- StringBuffer e;
- int degree = p.degree();
- if ( degree == 0 ) {
- return "true";
- }
- if (degree >= bitsetTestThreshold) {
- int bitsetIdx = markBitsetForGen(p);
- return getBitsetName(bitsetIdx) + ".member(" + ts + ")";
- }
+ return "(" + getLookaheadTestExpression(alt.cache, depth) + ")";
+ }
- // Otherwise, generate the long-winded series of "x==X||" tests
- e = new StringBuffer();
- for (int i = 0; i < elems.length; i++) {
- // Get the compared-to item (token or character value)
- String cs = getValueString(elems[i]);
-
- // Generate the element comparison
- if ( i>0 ) e.append("||");
- e.append(ts);
- e.append("==");
- e.append(cs);
- }
- return e.toString();
- }
- /** Return an expression for testing a contiguous renage of elements
- * @param k The lookahead level
- * @param elems The elements representing the set, usually from BitSet.toArray().
- * @return String containing test expression.
- */
- public String getRangeExpression(int k, int[] elems) {
- if (!elementsAreRange(elems)) {
- tool.panic("getRangeExpression called with non-range");
- }
- int begin = elems[0];
- int end = elems[elems.length-1];
- return
- "(" + lookaheadString(k) + " >= " + getValueString(begin) + " && " +
- lookaheadString(k) + " <= " + getValueString(end) + ")";
- }
- /** getValueString: get a string representation of a token or char value
- * @param value The token or char value
- */
- private String getValueString(int value) {
- String cs;
- if ( grammar instanceof LexerGrammar ) {
- cs = charFormatter.literalChar(value);
- }
- else {
- TokenSymbol ts = grammar.tokenManager.getTokenSymbolAt(value);
- if ( ts == null ) {
- return ""+value; // return token type as string
- // tool.panic("vocabulary for token type " + value + " is null");
- }
- String tId = ts.getId();
- if ( ts instanceof StringLiteralSymbol ) {
- // if string literal, use predefined label if any
- // if no predefined, try to mangle into LITERAL_xxx.
- // if can't mangle, use int value as last resort
- StringLiteralSymbol sl = (StringLiteralSymbol)ts;
- String label = sl.getLabel();
- if ( label!=null ) {
- cs = label;
- }
- else {
- cs = mangleLiteral(tId);
- if (cs == null) {
- cs = String.valueOf(value);
- }
- }
- }
- else {
- if ( tId.equals("EOF") )
- cs = namespaceAntlr+"Token::EOF_TYPE";
- else
- cs = tId;
- }
- }
- return cs;
- }
- /**Is the lookahead for this alt empty? */
- protected boolean lookaheadIsEmpty(Alternative alt, int maxDepth) {
- int depth = alt.lookaheadDepth;
- if ( depth == GrammarAnalyzer.NONDETERMINISTIC ) {
- depth = grammar.maxk;
- }
- for (int i=1; i<=depth && i<=maxDepth; i++) {
- BitSet p = alt.cache[i].fset;
- if (p.degree() != 0) {
- return false;
- }
- }
- return true;
- }
- private String lookaheadString(int k) {
- if (grammar instanceof TreeWalkerGrammar) {
- return "_t->getType()";
- }
- return "LA(" + k + ")";
- }
- /** Mangle a string literal into a meaningful token name. This is
- * only possible for literals that are all characters. The resulting
- * mangled literal name is literalsPrefix with the text of the literal
- * appended.
- * @return A string representing the mangled literal, or null if not possible.
- */
- private String mangleLiteral(String s) {
- String mangled = antlr.Tool.literalsPrefix;
- for (int i = 1; i < s.length()-1; i++) {
- if (!Character.isLetter(s.charAt(i)) &&
- s.charAt(i) != '_') {
- return null;
- }
- mangled += s.charAt(i);
- }
- if ( antlr.Tool.upperCaseMangledLiterals ) {
- mangled = mangled.toUpperCase();
- }
- return mangled;
- }
- /** Map an identifier to it's corresponding tree-node variable.
- * This is context-sensitive, depending on the rule and alternative
- * being generated
- * @param idParam The identifier name to map
- * @return The mapped id (which may be the same as the input), or null if the mapping is invalid due to duplicates
- */
- public String mapTreeId(String idParam, ActionTransInfo transInfo) {
- // if not in an action of a rule, nothing to map.
- if ( currentRule==null ) return idParam;
+ /**Generate a depth==1 lookahead test expression given the BitSet.
+ * This may be one of:
+ * 1) a series of 'x==X||' tests
+ * 2) a range test using >= && <= where possible,
+ * 3) a bitset membership test for complex comparisons
+ * @param k The lookahead level
+ * @param p The lookahead set for level k
+ */
+ protected String getLookaheadTestTerm(int k, BitSet p) {
+ // Determine the name of the item to be compared
+ String ts = lookaheadString(k);
+
+ // Generate a range expression if possible
+ int[] elems = p.toArray();
+ if (elementsAreRange(elems)) {
+ return getRangeExpression(k, elems);
+ }
+
+ // Generate a bitset membership test if possible
+ StringBuffer e;
+ int degree = p.degree();
+ if (degree == 0) {
+ return "true";
+ }
+
+ if (degree >= bitsetTestThreshold) {
+ int bitsetIdx = markBitsetForGen(p);
+ return getBitsetName(bitsetIdx) + ".member(" + ts + ")";
+ }
+
+ // Otherwise, generate the long-winded series of "x==X||" tests
+ e = new StringBuffer();
+ for (int i = 0; i < elems.length; i++) {
+ // Get the compared-to item (token or character value)
+ String cs = getValueString(elems[i]);
+
+ // Generate the element comparison
+ if (i > 0) e.append("||");
+ e.append(ts);
+ e.append("==");
+ e.append(cs);
+ }
+ return e.toString();
+ }
+
+ /** Return an expression for testing a contiguous renage of elements
+ * @param k The lookahead level
+ * @param elems The elements representing the set, usually from BitSet.toArray().
+ * @return String containing test expression.
+ */
+ public String getRangeExpression(int k, int[] elems) {
+ if (!elementsAreRange(elems)) {
+ tool.panic("getRangeExpression called with non-range");
+ }
+ int begin = elems[0];
+ int end = elems[elems.length - 1];
+ return
+ "(" + lookaheadString(k) + " >= " + getValueString(begin) + " && " +
+ lookaheadString(k) + " <= " + getValueString(end) + ")";
+ }
+
+ /** getValueString: get a string representation of a token or char value
+ * @param value The token or char value
+ */
+ private String getValueString(int value) {
+ String cs;
+ if (grammar instanceof LexerGrammar) {
+ cs = charFormatter.literalChar(value);
+ } else {
+ TokenSymbol ts = grammar.tokenManager.getTokenSymbolAt(value);
+ if (ts == null) {
+ return "" + value; // return token type as string
+ // tool.panic("vocabulary for token type " + value + " is null");
+ }
+ String tId = ts.getId();
+ if (ts instanceof StringLiteralSymbol) {
+ // if string literal, use predefined label if any
+ // if no predefined, try to mangle into LITERAL_xxx.
+ // if can't mangle, use int value as last resort
+ StringLiteralSymbol sl = (StringLiteralSymbol)ts;
+ String label = sl.getLabel();
+ if (label != null) {
+ cs = label;
+ } else {
+ cs = mangleLiteral(tId);
+ if (cs == null) {
+ cs = String.valueOf(value);
+ }
+ }
+ } else {
+ if (tId.equals("EOF"))
+ cs = namespaceAntlr + "Token::EOF_TYPE";
+ else
+ cs = tId;
+ }
+ }
+ return cs;
+ }
+
+ /**Is the lookahead for this alt empty? */
+ protected boolean lookaheadIsEmpty(Alternative alt, int maxDepth) {
+ int depth = alt.lookaheadDepth;
+ if (depth == GrammarAnalyzer.NONDETERMINISTIC) {
+ depth = grammar.maxk;
+ }
+ for (int i = 1; i <= depth && i <= maxDepth; i++) {
+ BitSet p = alt.cache[i].fset;
+ if (p.degree() != 0) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ private String lookaheadString(int k) {
+ if (grammar instanceof TreeWalkerGrammar) {
+ return "_t->getType()";
+ }
+ return "LA(" + k + ")";
+ }
+
+ /** Mangle a string literal into a meaningful token name. This is
+ * only possible for literals that are all characters. The resulting
+ * mangled literal name is literalsPrefix with the text of the literal
+ * appended.
+ * @return A string representing the mangled literal, or null if not possible.
+ */
+ private String mangleLiteral(String s) {
+ String mangled = antlr.Tool.literalsPrefix;
+ for (int i = 1; i < s.length() - 1; i++) {
+ if (!Character.isLetter(s.charAt(i)) &&
+ s.charAt(i) != '_') {
+ return null;
+ }
+ mangled += s.charAt(i);
+ }
+ if (antlr.Tool.upperCaseMangledLiterals) {
+ mangled = mangled.toUpperCase();
+ }
+ return mangled;
+ }
+
+ /** Map an identifier to it's corresponding tree-node variable.
+ * This is context-sensitive, depending on the rule and alternative
+ * being generated
+ * @param idParam The identifier name to map
+ * @return The mapped id (which may be the same as the input), or null if the mapping is invalid due to duplicates
+ */
+ public String mapTreeId(String idParam, ActionTransInfo transInfo) {
+ // if not in an action of a rule, nothing to map.
+ if (currentRule == null) return idParam;
// System.out.print("mapTreeId: "+idParam+" ");
- boolean in_var = false;
- String id = idParam;
- if (grammar instanceof TreeWalkerGrammar) {
- if ( !grammar.buildAST ) {
- in_var = true;
+ boolean in_var = false;
+ String id = idParam;
+ if (grammar instanceof TreeWalkerGrammar) {
+ if (!grammar.buildAST) {
+ in_var = true;
// System.out.println("in_var1");
- }
- // If the id ends with "_in", then map it to the input variable
- else if (id.length() > 3 && id.lastIndexOf("_in") == id.length()-3) {
- // Strip off the "_in"
- id = id.substring(0, id.length()-3);
- in_var = true;
+ }
+ // If the id ends with "_in", then map it to the input variable
+ else if (id.length() > 3 && id.lastIndexOf("_in") == id.length() - 3) {
+ // Strip off the "_in"
+ id = id.substring(0, id.length() - 3);
+ in_var = true;
// System.out.println("in_var2");
- }
- }
+ }
+ }
// System.out.print(in_var+"\t");
- // Check the rule labels. If id is a label, then the output
- // variable is label_AST, and the input variable is plain label.
- for (int i = 0; i < currentRule.labeledElements.size(); i++) {
- AlternativeElement elt = (AlternativeElement)currentRule.labeledElements.elementAt(i);
- if (elt.getLabel().equals(id)) {
+ // Check the rule labels. If id is a label, then the output
+ // variable is label_AST, and the input variable is plain label.
+ for (int i = 0; i < currentRule.labeledElements.size(); i++) {
+ AlternativeElement elt = (AlternativeElement)currentRule.labeledElements.elementAt(i);
+ if (elt.getLabel().equals(id)) {
// if( in_var )
// System.out.println("returning (vec) "+(in_var ? id : id + "_AST"));
- return in_var ? id : id + "_AST";
- }
- }
-
- // Failing that, check the id-to-variable map for the alternative.
- // If the id is in the map, then output variable is the name in the
- // map, and input variable is name_in
- String s = (String)treeVariableMap.get(id);
- if (s != null) {
- if (s == NONUNIQUE) {
+ return in_var ? id : id + "_AST";
+ }
+ }
+
+ // Failing that, check the id-to-variable map for the alternative.
+ // If the id is in the map, then output variable is the name in the
+ // map, and input variable is name_in
+ String s = (String)treeVariableMap.get(id);
+ if (s != null) {
+ if (s == NONUNIQUE) {
// if( in_var )
// System.out.println("returning null (nonunique)");
- // There is more than one element with this id
- return null;
- } else if (s.equals(currentRule.getRuleName())) {
- // a recursive call to the enclosing rule is
- // ambiguous with the rule itself.
+ // There is more than one element with this id
+ return null;
+ } else if (s.equals(currentRule.getRuleName())) {
+ // a recursive call to the enclosing rule is
+ // ambiguous with the rule itself.
// if( in_var )
// System.out.println("returning null (rulename)");
- return null;
- } else {
+ return null;
+ } else {
// if( in_var )
// System.out.println("returning "+(in_var?s+"_in":s));
- return in_var ? s + "_in" : s;
- }
- }
-
- // Failing that, check the rule name itself. Output variable
- // is rule_AST; input variable is rule_AST_in (treeparsers).
- if (id.equals(currentRule.getRuleName())) {
- String r = in_var ? id + "_AST_in" : id + "_AST";
- if ( transInfo!=null ) {
- if ( !in_var ) {
- transInfo.refRuleRoot = r;
- }
- }
+ return in_var ? s + "_in" : s;
+ }
+ }
+
+ // Failing that, check the rule name itself. Output variable
+ // is rule_AST; input variable is rule_AST_in (treeparsers).
+ if (id.equals(currentRule.getRuleName())) {
+ String r = in_var ? id + "_AST_in" : id + "_AST";
+ if (transInfo != null) {
+ if (!in_var) {
+ transInfo.refRuleRoot = r;
+ }
+ }
// if( in_var )
// System.out.println("returning (r) "+r);
- return r;
- } else {
+ return r;
+ } else {
// if( in_var )
// System.out.println("returning (last) "+id);
- // id does not map to anything -- return itself.
- return id;
- }
- }
- /** Given an element and the name of an associated AST variable,
- * create a mapping between the element "name" and the variable name.
- */
- private void mapTreeVariable(AlternativeElement e, String name)
- {
- // For tree elements, defer to the root
- if (e instanceof TreeElement) {
- mapTreeVariable( ((TreeElement)e).root, name);
- return;
- }
-
- // Determine the name of the element, if any, for mapping purposes
- String elName = null;
-
- // Don't map labeled items
- if (e.getLabel() == null) {
- if (e instanceof TokenRefElement) {
- // use the token id
- elName = ((TokenRefElement)e).atomText;
- }
- else if (e instanceof RuleRefElement) {
- // use the rule name
- elName = ((RuleRefElement)e).targetRule;
- }
- }
- // Add the element to the tree variable map if it has a name
- if (elName != null) {
- if (treeVariableMap.get(elName) != null) {
- // Name is already in the map -- mark it as duplicate
- treeVariableMap.remove(elName);
- treeVariableMap.put(elName, NONUNIQUE);
- }
- else {
- treeVariableMap.put(elName, name);
- }
- }
- }
-
- /** Lexically process tree-specifiers in the action.
- * This will replace #id and #(...) with the appropriate
- * function calls and/or variables.
- */
- protected String processActionForTreeSpecifiers(String actionStr, int line, RuleBlock currentRule, ActionTransInfo tInfo) {
- if ( actionStr==null || actionStr.length()==0 )
- return null;
-
- // The action trans info tells us (at the moment) whether an
- // assignment was done to the rule's tree root.
- if (grammar==null)
- return actionStr;
-
- if ( (grammar.buildAST && actionStr.indexOf('#') != -1) ||
- grammar instanceof TreeWalkerGrammar ||
- (grammar instanceof LexerGrammar && actionStr.indexOf('$') != -1) ) {
- // Create a lexer to read an action and return the translated version
- antlr.actions.cpp.ActionLexer lexer =
- new antlr.actions.cpp.ActionLexer(actionStr, currentRule,
- this, tInfo);
- lexer.setLineOffset(line);
- lexer.setTool(tool);
-
- try {
- lexer.mACTION(true);
- actionStr = lexer.getTokenObject().getText();
- // System.out.println("action translated: "+actionStr);
- // System.out.println("trans info is "+tInfo);
- }
- catch (RecognitionException ex) {
- lexer.reportError(ex);
- return actionStr;
- }
- catch (TokenStreamException tex) {
- antlr.Tool.panic("Error reading action:"+actionStr);
- return actionStr;
- }
- catch (CharStreamException io) {
- antlr.Tool.panic("Error reading action:"+actionStr);
- return actionStr;
- }
- }
- return actionStr;
- }
-
- private String fixNameSpaceOption( String ns )
- {
- ns = Tool.stripFrontBack(ns,"\"","\"");
- if( ns.length() > 2 &&
- !ns.substring(ns.length()-2, ns.length()).equals("::") )
- ns += "::";
- return ns;
- }
-
- private void setupGrammarParameters(Grammar g) {
- if (g instanceof ParserGrammar ||
- g instanceof LexerGrammar ||
- g instanceof TreeWalkerGrammar
- )
- {
- /* RK: options also have to be added to Grammar.java and for options
- * on the file level entries have to be defined in
- * DefineGrammarSymbols.java and passed around via 'globals' in Tool.java
- */
- if( Tool.nameSpace != null )
- nameSpace = Tool.nameSpace;
-
- if( Tool.namespaceStd != null )
- namespaceStd = fixNameSpaceOption(Tool.namespaceStd);
-
- if( Tool.namespaceAntlr != null )
- namespaceAntlr = fixNameSpaceOption(Tool.namespaceAntlr);
-
- genHashLines = Tool.genHashLines;
-
- /* let grammar level options override filelevel ones...
- */
- if( g.hasOption("namespace") ) {
- Token t = g.getOption("namespace");
- if( t != null ) {
- nameSpace = new NameSpace(t.getText());
- }
- }
- if( g.hasOption("namespaceAntlr") ) {
- Token t = g.getOption("namespaceAntlr");
- if( t != null ) {
- String ns = Tool.stripFrontBack(t.getText(),"\"","\"");
- if ( ns != null ) {
- if( ns.length() > 2 &&
- !ns.substring(ns.length()-2, ns.length()).equals("::") )
- ns += "::";
- namespaceAntlr = ns;
- }
- }
- }
- if( g.hasOption("namespaceStd") ) {
- Token t = g.getOption("namespaceStd");
- if( t != null ) {
- String ns = Tool.stripFrontBack(t.getText(),"\"","\"");
- if ( ns != null ) {
- if( ns.length() > 2 &&
- !ns.substring(ns.length()-2, ns.length()).equals("::") )
- ns += "::";
- namespaceStd = ns;
- }
- }
- }
- if( g.hasOption("genHashLines") ) {
- Token t = g.getOption("genHashLines");
- if( t != null ) {
- String val = Tool.stripFrontBack(t.getText(),"\"","\"");
- genHashLines = val.equals("true");
- }
- }
- }
- if (g instanceof ParserGrammar) {
- labeledElementASTType = namespaceAntlr+"RefAST";
- labeledElementASTInit = namespaceAntlr+"nullAST";
- if ( g.hasOption("ASTLabelType") ) {
- Token tsuffix = g.getOption("ASTLabelType");
- if ( tsuffix != null ) {
- String suffix = Tool.stripFrontBack(tsuffix.getText(),"\"","\"");
- if ( suffix != null ) {
- usingCustomAST = true;
- labeledElementASTType = suffix;
- labeledElementASTInit = "static_cast<"+suffix+">("+namespaceAntlr+"nullAST)";
- }
- }
- }
- labeledElementType = namespaceAntlr+"RefToken ";
- labeledElementInit = namespaceAntlr+"nullToken";
- commonExtraArgs = "";
- commonExtraParams = "";
- commonLocalVars = "";
- lt1Value = "LT(1)";
- exceptionThrown = namespaceAntlr+"RecognitionException";
- throwNoViable = "throw "+namespaceAntlr+"NoViableAltException(LT(1), getFilename());";
- }
- else if (g instanceof LexerGrammar) {
- labeledElementType = "char ";
- labeledElementInit = "'\\0'";
- commonExtraArgs = "";
- commonExtraParams = "bool _createToken";
- commonLocalVars = "int _ttype; "+namespaceAntlr+"RefToken _token; int _begin=text.length();";
- lt1Value = "LA(1)";
- exceptionThrown = namespaceAntlr+"RecognitionException";
- throwNoViable = "throw "+namespaceAntlr+"NoViableAltForCharException(LA(1), getFilename(), getLine());";
- }
- else if (g instanceof TreeWalkerGrammar) {
- labeledElementInit = namespaceAntlr+"nullAST";
- labeledElementASTInit = namespaceAntlr+"nullAST";
- labeledElementASTType = namespaceAntlr+"RefAST";
- labeledElementType = namespaceAntlr+"RefAST";
- commonExtraParams = namespaceAntlr+"RefAST _t";
- throwNoViable = "throw "+namespaceAntlr+"NoViableAltException(_t);";
- lt1Value = "_t";
- if ( g.hasOption("ASTLabelType") ) {
- Token tsuffix = g.getOption("ASTLabelType");
- if ( tsuffix != null ) {
- String suffix = Tool.stripFrontBack(tsuffix.getText(),"\"","\"");
- if ( suffix != null ) {
- usingCustomAST = true;
- labeledElementASTType = suffix;
- labeledElementType = suffix;
- labeledElementInit = "static_cast<"+suffix+">("+namespaceAntlr+"nullAST)";
- labeledElementASTInit = labeledElementInit;
- commonExtraParams = suffix+" _t";
- throwNoViable = "throw "+namespaceAntlr+"NoViableAltException(static_cast<"+namespaceAntlr+"RefAST>(_t));";
- lt1Value = "_t";
- }
- }
- }
- if ( !g.hasOption("ASTLabelType") ) {
- g.setOption("ASTLabelType", new Token(ANTLRTokenTypes.STRING_LITERAL,namespaceAntlr+"RefAST"));
- }
- commonExtraArgs = "_t";
- commonLocalVars = "";
- exceptionThrown = namespaceAntlr+"RecognitionException";
- }
- else {
- tool.panic("Unknown grammar type");
- }
- }
- private String textOrChar(String text) {
- // check to see if the text is a single character
- if (text.startsWith("'")) {
- // assume it also ends with '
- return charFormatter.literalChar(ANTLRLexer.tokenTypeForCharLiteral(text));
- }
- else
- return text;
- }
+ // id does not map to anything -- return itself.
+ return id;
+ }
+ }
+
+ /** Given an element and the name of an associated AST variable,
+ * create a mapping between the element "name" and the variable name.
+ */
+ private void mapTreeVariable(AlternativeElement e, String name) {
+ // For tree elements, defer to the root
+ if (e instanceof TreeElement) {
+ mapTreeVariable(((TreeElement)e).root, name);
+ return;
+ }
+
+ // Determine the name of the element, if any, for mapping purposes
+ String elName = null;
+
+ // Don't map labeled items
+ if (e.getLabel() == null) {
+ if (e instanceof TokenRefElement) {
+ // use the token id
+ elName = ((TokenRefElement)e).atomText;
+ } else if (e instanceof RuleRefElement) {
+ // use the rule name
+ elName = ((RuleRefElement)e).targetRule;
+ }
+ }
+ // Add the element to the tree variable map if it has a name
+ if (elName != null) {
+ if (treeVariableMap.get(elName) != null) {
+ // Name is already in the map -- mark it as duplicate
+ treeVariableMap.remove(elName);
+ treeVariableMap.put(elName, NONUNIQUE);
+ } else {
+ treeVariableMap.put(elName, name);
+ }
+ }
+ }
+
+ /** Lexically process tree-specifiers in the action.
+ * This will replace #id and #(...) with the appropriate
+ * function calls and/or variables.
+ */
+ protected String processActionForTreeSpecifiers(String actionStr, int line, RuleBlock currentRule, ActionTransInfo tInfo) {
+ if (actionStr == null || actionStr.length() == 0)
+ return null;
+
+ // The action trans info tells us (at the moment) whether an
+ // assignment was done to the rule's tree root.
+ if (grammar == null)
+ return actionStr;
+
+ if ((grammar.buildAST && actionStr.indexOf('#') != -1) ||
+ grammar instanceof TreeWalkerGrammar ||
+ (grammar instanceof LexerGrammar && actionStr.indexOf('$') != -1)) {
+ // Create a lexer to read an action and return the translated version
+ antlr.actions.cpp.ActionLexer lexer =
+ new antlr.actions.cpp.ActionLexer(actionStr, currentRule,
+ this, tInfo);
+ lexer.setLineOffset(line);
+ lexer.setTool(tool);
+
+ try {
+ lexer.mACTION(true);
+ actionStr = lexer.getTokenObject().getText();
+ // System.out.println("action translated: "+actionStr);
+ // System.out.println("trans info is "+tInfo);
+ } catch (RecognitionException ex) {
+ lexer.reportError(ex);
+ return actionStr;
+ } catch (TokenStreamException tex) {
+ antlr.Tool.panic("Error reading action:" + actionStr);
+ return actionStr;
+ } catch (CharStreamException io) {
+ antlr.Tool.panic("Error reading action:" + actionStr);
+ return actionStr;
+ }
+ }
+ return actionStr;
+ }
+
+ private String fixNameSpaceOption(String ns) {
+ ns = Tool.stripFrontBack(ns, "\"", "\"");
+ if (ns.length() > 2 &&
+ !ns.substring(ns.length() - 2, ns.length()).equals("::"))
+ ns += "::";
+ return ns;
+ }
+
+ private void setupGrammarParameters(Grammar g) {
+ if (g instanceof ParserGrammar ||
+ g instanceof LexerGrammar ||
+ g instanceof TreeWalkerGrammar
+ ) {
+ /* RK: options also have to be added to Grammar.java and for options
+ * on the file level entries have to be defined in
+ * DefineGrammarSymbols.java and passed around via 'globals' in Tool.java
+ */
+ if (Tool.nameSpace != null)
+ nameSpace = Tool.nameSpace;
+
+ if (Tool.namespaceStd != null)
+ namespaceStd = fixNameSpaceOption(Tool.namespaceStd);
+
+ if (Tool.namespaceAntlr != null)
+ namespaceAntlr = fixNameSpaceOption(Tool.namespaceAntlr);
+
+ genHashLines = Tool.genHashLines;
+
+ /* let grammar level options override filelevel ones...
+ */
+ if (g.hasOption("namespace")) {
+ Token t = g.getOption("namespace");
+ if (t != null) {
+ nameSpace = new NameSpace(t.getText());
+ }
+ }
+ if (g.hasOption("namespaceAntlr")) {
+ Token t = g.getOption("namespaceAntlr");
+ if (t != null) {
+ String ns = Tool.stripFrontBack(t.getText(), "\"", "\"");
+ if (ns != null) {
+ if (ns.length() > 2 &&
+ !ns.substring(ns.length() - 2, ns.length()).equals("::"))
+ ns += "::";
+ namespaceAntlr = ns;
+ }
+ }
+ }
+ if (g.hasOption("namespaceStd")) {
+ Token t = g.getOption("namespaceStd");
+ if (t != null) {
+ String ns = Tool.stripFrontBack(t.getText(), "\"", "\"");
+ if (ns != null) {
+ if (ns.length() > 2 &&
+ !ns.substring(ns.length() - 2, ns.length()).equals("::"))
+ ns += "::";
+ namespaceStd = ns;
+ }
+ }
+ }
+ if (g.hasOption("genHashLines")) {
+ Token t = g.getOption("genHashLines");
+ if (t != null) {
+ String val = Tool.stripFrontBack(t.getText(), "\"", "\"");
+ genHashLines = val.equals("true");
+ }
+ }
+ }
+ if (g instanceof ParserGrammar) {
+ labeledElementASTType = namespaceAntlr + "RefAST";
+ labeledElementASTInit = namespaceAntlr + "nullAST";
+ if (g.hasOption("ASTLabelType")) {
+ Token tsuffix = g.getOption("ASTLabelType");
+ if (tsuffix != null) {
+ String suffix = Tool.stripFrontBack(tsuffix.getText(), "\"", "\"");
+ if (suffix != null) {
+ usingCustomAST = true;
+ labeledElementASTType = suffix;
+ labeledElementASTInit = "static_cast<" + suffix + ">(" + namespaceAntlr + "nullAST)";
+ }
+ }
+ }
+ labeledElementType = namespaceAntlr + "RefToken ";
+ labeledElementInit = namespaceAntlr + "nullToken";
+ commonExtraArgs = "";
+ commonExtraParams = "";
+ commonLocalVars = "";
+ lt1Value = "LT(1)";
+ exceptionThrown = namespaceAntlr + "RecognitionException";
+ throwNoViable = "throw " + namespaceAntlr + "NoViableAltException(LT(1), getFilename());";
+ } else if (g instanceof LexerGrammar) {
+ labeledElementType = "char ";
+ labeledElementInit = "'\\0'";
+ commonExtraArgs = "";
+ commonExtraParams = "bool _createToken";
+ commonLocalVars = "int _ttype; " + namespaceAntlr + "RefToken _token; int _begin=text.length();";
+ lt1Value = "LA(1)";
+ exceptionThrown = namespaceAntlr + "RecognitionException";
+ throwNoViable = "throw " + namespaceAntlr + "NoViableAltForCharException(LA(1), getFilename(), getLine());";
+ } else if (g instanceof TreeWalkerGrammar) {
+ labeledElementInit = namespaceAntlr + "nullAST";
+ labeledElementASTInit = namespaceAntlr + "nullAST";
+ labeledElementASTType = namespaceAntlr + "RefAST";
+ labeledElementType = namespaceAntlr + "RefAST";
+ commonExtraParams = namespaceAntlr + "RefAST _t";
+ throwNoViable = "throw " + namespaceAntlr + "NoViableAltException(_t);";
+ lt1Value = "_t";
+ if (g.hasOption("ASTLabelType")) {
+ Token tsuffix = g.getOption("ASTLabelType");
+ if (tsuffix != null) {
+ String suffix = Tool.stripFrontBack(tsuffix.getText(), "\"", "\"");
+ if (suffix != null) {
+ usingCustomAST = true;
+ labeledElementASTType = suffix;
+ labeledElementType = suffix;
+ labeledElementInit = "static_cast<" + suffix + ">(" + namespaceAntlr + "nullAST)";
+ labeledElementASTInit = labeledElementInit;
+ commonExtraParams = suffix + " _t";
+ throwNoViable = "throw " + namespaceAntlr + "NoViableAltException(static_cast<" + namespaceAntlr + "RefAST>(_t));";
+ lt1Value = "_t";
+ }
+ }
+ }
+ if (!g.hasOption("ASTLabelType")) {
+ g.setOption("ASTLabelType", new Token(ANTLRTokenTypes.STRING_LITERAL, namespaceAntlr + "RefAST"));
+ }
+ commonExtraArgs = "_t";
+ commonLocalVars = "";
+ exceptionThrown = namespaceAntlr + "RecognitionException";
+ } else {
+ tool.panic("Unknown grammar type");
+ }
+ }
+
+ private String textOrChar(String text) {
+ // check to see if the text is a single character
+ if (text.startsWith("'")) {
+ // assume it also ends with '
+ return charFormatter.literalChar(ANTLRLexer.tokenTypeForCharLiteral(text));
+ } else
+ return text;
+ }
}
1.2 +8 -7 e/src/jsrc/antlr/DefaultFileLineFormatter.java
Index: DefaultFileLineFormatter.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/DefaultFileLineFormatter.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- DefaultFileLineFormatter.java 2001/10/03 21:04:13 1.1
+++ DefaultFileLineFormatter.java 2001/10/04 01:09:14 1.2
@@ -4,16 +4,17 @@
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
- * $Id: DefaultFileLineFormatter.java,v 1.1 2001/10/03 21:04:13 markm Exp $
+ * $Id: DefaultFileLineFormatter.java,v 1.2 2001/10/04 01:09:14 markm Exp $
*/
public class DefaultFileLineFormatter extends FileLineFormatter {
+
public String getFormatString(String fileName, int line) {
- if ( fileName != null ) {
- return fileName+":"+line+": ";
- }
- else {
- return "line "+line+": ";
- }
+ if (fileName != null) {
+ return fileName + ":" + line + ": ";
+ } else {
+ return "line " + line + ": ";
+ }
}
-};
+}
+;
1.2 +61 -64 e/src/jsrc/antlr/DefaultToolErrorHandler.java
Index: DefaultToolErrorHandler.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/DefaultToolErrorHandler.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- DefaultToolErrorHandler.java 2001/10/03 21:04:13 1.1
+++ DefaultToolErrorHandler.java 2001/10/04 01:09:14 1.2
@@ -4,12 +4,13 @@
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
- * $Id: DefaultToolErrorHandler.java,v 1.1 2001/10/03 21:04:13 markm Exp $
+ * $Id: DefaultToolErrorHandler.java,v 1.2 2001/10/04 01:09:14 markm Exp $
*/
-import antlr.collections.impl.BitSet;
+
class DefaultToolErrorHandler implements ToolErrorHandler {
+
CharFormatter javaCharFormatter = new JavaCharFormatter();
/** Dump token/character sets to System.out
@@ -18,27 +19,25 @@
* @param sets An array of bitsets containing the ambiguities
*/
private void dumpSets(Grammar grammar,
- boolean lexicalAnalysis,
- int depth,
- Lookahead[] sets,
- String linePrefix)
- {
- for (int i = 1; i <= depth; i++) {
- System.out.print(linePrefix+"\tk==" + i + ":");
- if (lexicalAnalysis) {
- String bits = sets[i].fset.toStringWithRanges(",", javaCharFormatter);
- if ( sets[i].containsEpsilon() ) {
- System.out.print("<end-of-token>");
- if ( bits.length()>0 ) {
- System.out.print(",");
- }
- }
- System.out.println(bits);
- }
- else {
- System.out.println(sets[i].fset.toString(",", grammar.tokenManager.getVocabulary()));
- }
- }
+ boolean lexicalAnalysis,
+ int depth,
+ Lookahead[] sets,
+ String linePrefix) {
+ for (int i = 1; i <= depth; i++) {
+ System.out.print(linePrefix + "\tk==" + i + ":");
+ if (lexicalAnalysis) {
+ String bits = sets[i].fset.toStringWithRanges(",", javaCharFormatter);
+ if (sets[i].containsEpsilon()) {
+ System.out.print("<end-of-token>");
+ if (bits.length() > 0) {
+ System.out.print(",");
+ }
+ }
+ System.out.println(bits);
+ } else {
+ System.out.println(sets[i].fset.toString(",", grammar.tokenManager.getVocabulary()));
+ }
+ }
}
/** Issue a warning about ambiguity between a alternates
@@ -50,33 +49,32 @@
* @param altIdx2 The zero-based index of the second ambiguous alternative
*/
public void warnAltAmbiguity(Grammar grammar,
- AlternativeBlock blk,
- boolean lexicalAnalysis,
- int depth,
- Lookahead[] sets,
- int altIdx1,
- int altIdx2)
- {
- String fileline = FileLineFormatter.getFormatter().getFormatString(grammar.getFilename(),blk.getLine());
- if ( blk instanceof RuleBlock && ((RuleBlock)blk).isLexerAutoGenRule() ) {
- System.out.print("warning: lexical nondeterminism between rules ");
- Alternative ai = blk.getAlternativeAt(altIdx1);
- Alternative aj = blk.getAlternativeAt(altIdx2);
- RuleRefElement rri = (RuleRefElement)ai.head;
- RuleRefElement rrj = (RuleRefElement)aj.head;
- String ri = CodeGenerator.reverseLexerRuleName(rri.targetRule);
- String rj = CodeGenerator.reverseLexerRuleName(rrj.targetRule);
- System.out.println(ri+" and "+rj+" upon");
- dumpSets(grammar, lexicalAnalysis, depth, sets, fileline);
- return;
- }
- System.out.println(
- // "warning: line " + blk.getLine() + ": " +
- fileline+"warning: "+
- (lexicalAnalysis ? "lexical " : "") + "nondeterminism upon"
- );
- dumpSets(grammar, lexicalAnalysis, depth, sets, fileline);
- System.out.println(fileline+"\tbetween alts " + (altIdx1+1) + " and " + (altIdx2+1) + " of block");
+ AlternativeBlock blk,
+ boolean lexicalAnalysis,
+ int depth,
+ Lookahead[] sets,
+ int altIdx1,
+ int altIdx2) {
+ String fileline = FileLineFormatter.getFormatter().getFormatString(grammar.getFilename(), blk.getLine());
+ if (blk instanceof RuleBlock && ((RuleBlock)blk).isLexerAutoGenRule()) {
+ System.out.print("warning: lexical nondeterminism between rules ");
+ Alternative ai = blk.getAlternativeAt(altIdx1);
+ Alternative aj = blk.getAlternativeAt(altIdx2);
+ RuleRefElement rri = (RuleRefElement)ai.head;
+ RuleRefElement rrj = (RuleRefElement)aj.head;
+ String ri = CodeGenerator.reverseLexerRuleName(rri.targetRule);
+ String rj = CodeGenerator.reverseLexerRuleName(rrj.targetRule);
+ System.out.println(ri + " and " + rj + " upon");
+ dumpSets(grammar, lexicalAnalysis, depth, sets, fileline);
+ return;
+ }
+ System.out.println(
+ // "warning: line " + blk.getLine() + ": " +
+ fileline + "warning: " +
+ (lexicalAnalysis ? "lexical " : "") + "nondeterminism upon"
+ );
+ dumpSets(grammar, lexicalAnalysis, depth, sets, fileline);
+ System.out.println(fileline + "\tbetween alts " + (altIdx1 + 1) + " and " + (altIdx2 + 1) + " of block");
}
/** Issue a warning about ambiguity between an alternate and exit path.
@@ -87,20 +85,19 @@
* @param altIdx The zero-based index of the ambiguous alternative
*/
public void warnAltExitAmbiguity(Grammar grammar,
- BlockWithImpliedExitPath blk,
- boolean lexicalAnalysis,
- int depth,
- Lookahead[] sets,
- int altIdx
- )
- {
- String fileline = FileLineFormatter.getFormatter().getFormatString(grammar.getFilename(),blk.getLine());
- System.out.println(
- // "warning: line " + blk.getLine() + ": " +
- fileline+"warning: "+
- (lexicalAnalysis ? "lexical " : "") + "nondeterminism upon"
- );
- dumpSets(grammar, lexicalAnalysis, depth, sets, fileline);
- System.out.println(fileline+"\tbetween alt " + (altIdx+1) + " and exit branch of block");
+ BlockWithImpliedExitPath blk,
+ boolean lexicalAnalysis,
+ int depth,
+ Lookahead[] sets,
+ int altIdx
+ ) {
+ String fileline = FileLineFormatter.getFormatter().getFormatString(grammar.getFilename(), blk.getLine());
+ System.out.println(
+ // "warning: line " + blk.getLine() + ": " +
+ fileline + "warning: " +
+ (lexicalAnalysis ? "lexical " : "") + "nondeterminism upon"
+ );
+ dumpSets(grammar, lexicalAnalysis, depth, sets, fileline);
+ System.out.println(fileline + "\tbetween alt " + (altIdx + 1) + " and exit branch of block");
}
}
1.2 +694 -817 e/src/jsrc/antlr/DefineGrammarSymbols.java
Index: DefineGrammarSymbols.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/DefineGrammarSymbols.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- DefineGrammarSymbols.java 2001/10/03 21:04:13 1.1
+++ DefineGrammarSymbols.java 2001/10/04 01:09:14 1.2
@@ -4,851 +4,728 @@
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
- * $Id: DefineGrammarSymbols.java,v 1.1 2001/10/03 21:04:13 markm Exp $
+ * $Id: DefineGrammarSymbols.java,v 1.2 2001/10/04 01:09:14 markm Exp $
*/
-import java.util.Hashtable;
import antlr.collections.impl.BitSet;
+import java.util.Hashtable;
+
/**DefineGrammarSymbols is a behavior for the ANTLRParser that adds all
* the token and rule symbols to the grammar symbol table.
*
* Token types are assigned to token symbols in this class also.
* The token type for a token is done in the order seen (lexically).
*/
-public class DefineGrammarSymbols implements ANTLRGrammarParseBehavior
-{
- // Contains all of the defined parser and lexer Grammar's indexed by name
- protected Hashtable grammars = new Hashtable();
- // Contains all the TokenManagers indexed by name
- protected Hashtable tokenManagers = new Hashtable();
- // Current grammar (parser or lexer)
- protected Grammar grammar;
- // The tool under which this is invoked
- protected Tool tool;
- // The grammar analyzer object
- LLkAnalyzer analyzer;
- // The command-line arguments passed to the tool.
- // This allows each grammar to parse the arguments as it is created
- String[] args;
- // Name for default token manager does not match any valid name
- static final String DEFAULT_TOKENMANAGER_NAME = "*default";
- // Header actions apply to all parsers unless redefined
- // Contains all of the header actions indexed by name
- protected Hashtable headerActions = new Hashtable();
- // Place where preamble is stored until a grammar is defined
- Token thePreambleAction = new CommonToken(Token.INVALID_TYPE, ""); // init to empty token
- // The target language
- String language = "Java";
-
- protected int numLexers = 0;
- protected int numParsers = 0;
- protected int numTreeParsers = 0;
-
- public DefineGrammarSymbols(Tool tool_, String[] args_, LLkAnalyzer analyzer_)
- {
- tool = tool_;
- args = args_;
- analyzer = analyzer_;
- }
- public void _refStringLiteral(Token lit, Token label, int autoGenType, boolean lastInRule)
- {
- if (!(grammar instanceof LexerGrammar))
- {
- // String literals are treated like tokens except by the lexer
- String str = lit.getText();
- if ( grammar.tokenManager.getTokenSymbol(str) != null )
- {
- // string symbol is already defined
- return;
- }
- StringLiteralSymbol sl = new StringLiteralSymbol(str);
- int tt = grammar.tokenManager.nextTokenType();
- sl.setTokenType(tt);
- grammar.tokenManager.define(sl);
- }
- }
+public class DefineGrammarSymbols implements ANTLRGrammarParseBehavior {
+
+ // Contains all of the defined parser and lexer Grammar's indexed by name
+ protected Hashtable grammars = new Hashtable();
+ // Contains all the TokenManagers indexed by name
+ protected Hashtable tokenManagers = new Hashtable();
+ // Current grammar (parser or lexer)
+ protected Grammar grammar;
+ // The tool under which this is invoked
+ protected Tool tool;
+ // The grammar analyzer object
+ LLkAnalyzer analyzer;
+ // The command-line arguments passed to the tool.
+ // This allows each grammar to parse the arguments as it is created
+ String[] args;
+ // Name for default token manager does not match any valid name
+ static final String DEFAULT_TOKENMANAGER_NAME = "*default";
+ // Header actions apply to all parsers unless redefined
+ // Contains all of the header actions indexed by name
+ protected Hashtable headerActions = new Hashtable();
+ // Place where preamble is stored until a grammar is defined
+ Token thePreambleAction = new CommonToken(Token.INVALID_TYPE, ""); // init to empty token
+ // The target language
+ String language = "Java";
+
+ protected int numLexers = 0;
+
+ protected int numParsers = 0;
+
+ protected int numTreeParsers = 0;
+
+ public DefineGrammarSymbols(Tool tool_, String[] args_, LLkAnalyzer analyzer_) {
+ tool = tool_;
+ args = args_;
+ analyzer = analyzer_;
+ }
+
+ public void _refStringLiteral(Token lit, Token label, int autoGenType, boolean lastInRule) {
+ if (!(grammar instanceof LexerGrammar)) {
+ // String literals are treated like tokens except by the lexer
+ String str = lit.getText();
+ if (grammar.tokenManager.getTokenSymbol(str) != null) {
+ // string symbol is already defined
+ return;
+ }
+ StringLiteralSymbol sl = new StringLiteralSymbol(str);
+ int tt = grammar.tokenManager.nextTokenType();
+ sl.setTokenType(tt);
+ grammar.tokenManager.define(sl);
+ }
+ }
+
/** Reference a token */
- public void _refToken(Token assignId,
- Token t,
- Token label,
- Token args,
- boolean inverted,
- int autoGenType,
- boolean lastInRule)
- {
- String id = t.getText();
- if (!grammar.tokenManager.tokenDefined(id))
- {
- int tt = grammar.tokenManager.nextTokenType();
- TokenSymbol ts = new TokenSymbol(id);
- ts.setTokenType(tt);
- grammar.tokenManager.define(ts);
- }
- }
+ public void _refToken(Token assignId,
+ Token t,
+ Token label,
+ Token args,
+ boolean inverted,
+ int autoGenType,
+ boolean lastInRule) {
+ String id = t.getText();
+ if (!grammar.tokenManager.tokenDefined(id)) {
+ int tt = grammar.tokenManager.nextTokenType();
+ TokenSymbol ts = new TokenSymbol(id);
+ ts.setTokenType(tt);
+ grammar.tokenManager.define(ts);
+ }
+ }
/** Abort the processing of a grammar due to syntax errors */
- public void abortGrammar()
- {
- if (grammar != null && grammar.getClassName() != null)
- {
- grammars.remove(grammar.getClassName());
- }
- grammar = null;
- }
-
- public void beginAlt(boolean doAST_)
- {
- }
-
- public void beginChildList()
- {
- }
-
- // Exception handling
- public void beginExceptionGroup() {
- }
- public void beginExceptionSpec(Token label) {
- }
- public void beginSubRule(Token label, int line, boolean not)
- {
- }
- public void beginTree(int line) throws SemanticException
- {
- }
+ public void abortGrammar() {
+ if (grammar != null && grammar.getClassName() != null) {
+ grammars.remove(grammar.getClassName());
+ }
+ grammar = null;
+ }
+
+ public void beginAlt(boolean doAST_) {
+ }
+
+ public void beginChildList() {
+ }
+
+ // Exception handling
+ public void beginExceptionGroup() {
+ }
+
+ public void beginExceptionSpec(Token label) {
+ }
+
+ public void beginSubRule(Token label, int line, boolean not) {
+ }
+
+ public void beginTree(int line) throws SemanticException {
+ }
+
/** Define a lexer or parser rule */
- public void defineRuleName(Token r,
- String access,
- boolean ruleAutoGen,
- String docComment)
- throws SemanticException
- {
- String id = r.getText();
-
- // if ( Character.isUpperCase(id.charAt(0)) ) {
- if ( r.type == ANTLRTokenTypes.TOKEN_REF )
- {
- // lexer rule
- id = CodeGenerator.lexerRuleName(id);
- // make sure we define it as token identifier also
- if ( !grammar.tokenManager.tokenDefined(r.getText()) )
- {
- int tt = grammar.tokenManager.nextTokenType();
- TokenSymbol ts = new TokenSymbol(r.getText());
- ts.setTokenType(tt);
- grammar.tokenManager.define(ts);
- }
- }
-
- RuleSymbol rs;
- if ( grammar.isDefined(id) )
- {
- // symbol seen before?
- rs = (RuleSymbol) grammar.getSymbol(id);
- // rule just referenced or has it been defined yet?
- if ( rs.isDefined() )
- {
- tool.error("redefinition of rule "+id, grammar.getFilename(), r.getLine());
- }
- }
- else
- {
- rs = new RuleSymbol(id);
- grammar.define(rs);
- }
- rs.setDefined();
- rs.access = access;
- rs.comment = docComment;
- }
+ public void defineRuleName(Token r,
+ String access,
+ boolean ruleAutoGen,
+ String docComment)
+ throws SemanticException {
+ String id = r.getText();
+
+ // if ( Character.isUpperCase(id.charAt(0)) ) {
+ if (r.type == ANTLRTokenTypes.TOKEN_REF) {
+ // lexer rule
+ id = CodeGenerator.lexerRuleName(id);
+ // make sure we define it as token identifier also
+ if (!grammar.tokenManager.tokenDefined(r.getText())) {
+ int tt = grammar.tokenManager.nextTokenType();
+ TokenSymbol ts = new TokenSymbol(r.getText());
+ ts.setTokenType(tt);
+ grammar.tokenManager.define(ts);
+ }
+ }
+
+ RuleSymbol rs;
+ if (grammar.isDefined(id)) {
+ // symbol seen before?
+ rs = (RuleSymbol)grammar.getSymbol(id);
+ // rule just referenced or has it been defined yet?
+ if (rs.isDefined()) {
+ tool.error("redefinition of rule " + id, grammar.getFilename(), r.getLine());
+ }
+ } else {
+ rs = new RuleSymbol(id);
+ grammar.define(rs);
+ }
+ rs.setDefined();
+ rs.access = access;
+ rs.comment = docComment;
+ }
/** Define a token from tokens {...}.
- * Must be label and literal or just label or just a literal.
- */
- public void defineToken(Token tokname, Token tokliteral)
- {
- String name = null;
- String literal = null;
- if (tokname != null)
- {
- name = tokname.getText();
- }
- if (tokliteral != null)
- {
- literal = tokliteral.getText();
- }
- // System.out.println("defining " + name + " with literal " + literal);
- //
- if (literal != null)
- {
- StringLiteralSymbol sl=(StringLiteralSymbol)grammar.tokenManager.getTokenSymbol(literal);
- if ( sl != null)
- {
- // This literal is known already.
- // If the literal has no label already, but we can provide
- // one here, then no problem, just map the label to the literal
- // and don't change anything else.
- // Otherwise, labels conflict: error.
- if ( name==null || sl.getLabel()!=null )
- {
- tool.warning("Redefinition of literal in tokens {...}: " + literal, grammar.getFilename(), tokliteral.getLine());
- return;
- }
- else if ( name!=null )
- {
- // The literal had no label, but new def does. Set it.
- sl.setLabel(name);
- // Also, map the label to the literal.
- grammar.tokenManager.mapToTokenSymbol(name, sl);
- }
- }
- // if they provide a name/label and that name/label already
- // exists, just hook this literal onto old token.
- if (name != null)
- {
- TokenSymbol ts = (TokenSymbol) grammar.tokenManager.getTokenSymbol(name);
- if (ts != null)
- {
- // watch out that the label is not more than just a token.
- // If it already has a literal attached, then: conflict.
- if ( ts instanceof StringLiteralSymbol )
- {
- tool.warning("Redefinition of token in tokens {...}: " + name, grammar.getFilename(), tokliteral.getLine());
- return;
- }
- // a simple token symbol such as DECL is defined
- // must convert it to a StringLiteralSymbol with a
- // label by co-opting token type and killing old
- // TokenSymbol. Kill mapping and entry in vector
- // of token manager.
- // First, claim token type.
- int ttype = ts.getTokenType();
- // now, create string literal with label
- sl = new StringLiteralSymbol(literal);
- sl.setTokenType(ttype);
- sl.setLabel(name);
- // redefine this critter as a string literal
- grammar.tokenManager.define(sl);
- // make sure the label can be used also.
- grammar.tokenManager.mapToTokenSymbol(name, sl);
- return;
- }
- // here, literal was labeled but not by a known token symbol.
- }
- sl = new StringLiteralSymbol(literal);
- int tt = grammar.tokenManager.nextTokenType();
- sl.setTokenType(tt);
- sl.setLabel(name);
- grammar.tokenManager.define(sl);
- if (name != null)
- {
- // make the label point at token symbol too
- grammar.tokenManager.mapToTokenSymbol(name, sl);
- }
- }
-
- // create a token in the token manager not a literal
- else
- {
- if (grammar.tokenManager.tokenDefined(name))
- {
- tool.warning("Redefinition of token in tokens {...}: " + name, grammar.getFilename(), tokname.getLine());
- return;
- }
- int tt = grammar.tokenManager.nextTokenType();
- TokenSymbol ts = new TokenSymbol(name);
- ts.setTokenType(tt);
- grammar.tokenManager.define(ts);
- }
- }
- public void endAlt()
- {
- }
- public void endChildList()
- {
- }
- public void endExceptionGroup() {
- }
- public void endExceptionSpec() {
- }
- public void endGrammar()
- {
- }
+ * Must be label and literal or just label or just a literal.
+ */
+ public void defineToken(Token tokname, Token tokliteral) {
+ String name = null;
+ String literal = null;
+ if (tokname != null) {
+ name = tokname.getText();
+ }
+ if (tokliteral != null) {
+ literal = tokliteral.getText();
+ }
+ // System.out.println("defining " + name + " with literal " + literal);
+ //
+ if (literal != null) {
+ StringLiteralSymbol sl = (StringLiteralSymbol)grammar.tokenManager.getTokenSymbol(literal);
+ if (sl != null) {
+ // This literal is known already.
+ // If the literal has no label already, but we can provide
+ // one here, then no problem, just map the label to the literal
+ // and don't change anything else.
+ // Otherwise, labels conflict: error.
+ if (name == null || sl.getLabel() != null) {
+ tool.warning("Redefinition of literal in tokens {...}: " + literal, grammar.getFilename(), tokliteral.getLine());
+ return;
+ } else if (name != null) {
+ // The literal had no label, but new def does. Set it.
+ sl.setLabel(name);
+ // Also, map the label to the literal.
+ grammar.tokenManager.mapToTokenSymbol(name, sl);
+ }
+ }
+ // if they provide a name/label and that name/label already
+ // exists, just hook this literal onto old token.
+ if (name != null) {
+ TokenSymbol ts = (TokenSymbol)grammar.tokenManager.getTokenSymbol(name);
+ if (ts != null) {
+ // watch out that the label is not more than just a token.
+ // If it already has a literal attached, then: conflict.
+ if (ts instanceof StringLiteralSymbol) {
+ tool.warning("Redefinition of token in tokens {...}: " + name, grammar.getFilename(), tokliteral.getLine());
+ return;
+ }
+ // a simple token symbol such as DECL is defined
+ // must convert it to a StringLiteralSymbol with a
+ // label by co-opting token type and killing old
+ // TokenSymbol. Kill mapping and entry in vector
+ // of token manager.
+ // First, claim token type.
+ int ttype = ts.getTokenType();
+ // now, create string literal with label
+ sl = new StringLiteralSymbol(literal);
+ sl.setTokenType(ttype);
+ sl.setLabel(name);
+ // redefine this critter as a string literal
+ grammar.tokenManager.define(sl);
+ // make sure the label can be used also.
+ grammar.tokenManager.mapToTokenSymbol(name, sl);
+ return;
+ }
+ // here, literal was labeled but not by a known token symbol.
+ }
+ sl = new StringLiteralSymbol(literal);
+ int tt = grammar.tokenManager.nextTokenType();
+ sl.setTokenType(tt);
+ sl.setLabel(name);
+ grammar.tokenManager.define(sl);
+ if (name != null) {
+ // make the label point at token symbol too
+ grammar.tokenManager.mapToTokenSymbol(name, sl);
+ }
+ }
+
+ // create a token in the token manager not a literal
+ else {
+ if (grammar.tokenManager.tokenDefined(name)) {
+ tool.warning("Redefinition of token in tokens {...}: " + name, grammar.getFilename(), tokname.getLine());
+ return;
+ }
+ int tt = grammar.tokenManager.nextTokenType();
+ TokenSymbol ts = new TokenSymbol(name);
+ ts.setTokenType(tt);
+ grammar.tokenManager.define(ts);
+ }
+ }
+
+ public void endAlt() {
+ }
+
+ public void endChildList() {
+ }
+
+ public void endExceptionGroup() {
+ }
+
+ public void endExceptionSpec() {
+ }
+
+ public void endGrammar() {
+ }
+
/** Called after the optional options section, to compensate for
- * options that may not have been set.
- * This method is bigger than it needs to be, but is much more
- * clear if I delineate all the cases.
- */
- public void endOptions()
- {
- // NO VOCAB OPTIONS
- if ( grammar.exportVocab==null && grammar.importVocab==null )
- {
- grammar.exportVocab = grammar.getClassName();
- // Can we get initial vocab from default shared vocab?
- if (tokenManagers.containsKey(DEFAULT_TOKENMANAGER_NAME))
- {
- // Use the already-defined token manager
- grammar.exportVocab = DEFAULT_TOKENMANAGER_NAME;
- TokenManager tm = (TokenManager) tokenManagers.get(DEFAULT_TOKENMANAGER_NAME);
- // System.out.println("No tokenVocabulary for '" + grammar.getClassName() + "', using default '" + tm.getName() + "'");
- grammar.setTokenManager(tm);
- return;
- }
- // no shared vocab for file, make new one
- // System.out.println("No exportVocab for '" + grammar.getClassName() + "', creating default '" + grammar.exportVocab + "'");
- TokenManager tm = new SimpleTokenManager(grammar.exportVocab, tool);
- grammar.setTokenManager(tm);
- // Add the token manager to the list of token managers
- tokenManagers.put(grammar.exportVocab, tm);
- // no default vocab, so make this the default vocab
- tokenManagers.put(DEFAULT_TOKENMANAGER_NAME, tm);
- return;
- }
-
- // NO OUTPUT, BUT HAS INPUT VOCAB
- if ( grammar.exportVocab==null && grammar.importVocab!=null )
- {
- grammar.exportVocab = grammar.getClassName();
- // first make sure input!=output
- if ( grammar.importVocab.equals(grammar.exportVocab) )
- {
- tool.warning("Grammar " + grammar.getClassName() +
- " cannot have importVocab same as default output vocab (grammar name); ignored.");
- // kill importVocab option and try again: use default vocab
- grammar.importVocab = null;
- endOptions();
- return;
- }
- // check to see if the vocab is already in memory
- // (defined by another grammar in the file). Not normal situation.
- if (tokenManagers.containsKey(grammar.importVocab))
- {
- // make a copy since we'll be generating a new output vocab
- // and we don't want to affect this one. Set the name to
- // the default output vocab==classname.
- TokenManager tm = (TokenManager) tokenManagers.get(grammar.importVocab);
- // System.out.println("Duping importVocab of " + grammar.importVocab);
- TokenManager dup = (TokenManager)tm.clone();
- dup.setName(grammar.exportVocab);
- // System.out.println("Setting name to " + grammar.exportVocab);
- dup.setReadOnly(false);
- grammar.setTokenManager(dup);
- tokenManagers.put(grammar.exportVocab, dup);
- return;
- }
- // System.out.println("reading in vocab "+grammar.importVocab);
- // Must be a file, go get it.
- ImportVocabTokenManager tm =
- new ImportVocabTokenManager(grammar,
- grammar.importVocab + CodeGenerator.TokenTypesFileSuffix + CodeGenerator.TokenTypesFileExt,
- grammar.exportVocab,
- tool);
- tm.setReadOnly(false); // since renamed, can write out
- // Add this token manager to the list so its tokens will be generated
- tokenManagers.put(grammar.exportVocab, tm);
- // System.out.println("vocab renamed to default output vocab of "+tm.getName());
- // Assign the token manager to this grammar.
- grammar.setTokenManager(tm);
-
- // set default vocab if none
- if (!tokenManagers.containsKey(DEFAULT_TOKENMANAGER_NAME))
- {
- tokenManagers.put(DEFAULT_TOKENMANAGER_NAME, tm);
- }
-
- return;
- }
-
- // OUTPUT VOCAB, BUT NO INPUT VOCAB
- if ( grammar.exportVocab!=null && grammar.importVocab==null )
- {
- // share with previous vocab if it exists
- if (tokenManagers.containsKey(grammar.exportVocab))
- {
- // Use the already-defined token manager
- TokenManager tm = (TokenManager) tokenManagers.get(grammar.exportVocab);
- // System.out.println("Sharing exportVocab of " + grammar.exportVocab);
- grammar.setTokenManager(tm);
- return;
- }
- // create new output vocab
- // System.out.println("Creating exportVocab " + grammar.exportVocab);
- TokenManager tm = new SimpleTokenManager(grammar.exportVocab, tool);
- grammar.setTokenManager(tm);
- // Add the token manager to the list of token managers
- tokenManagers.put(grammar.exportVocab, tm);
- // set default vocab if none
- if (!tokenManagers.containsKey(DEFAULT_TOKENMANAGER_NAME))
- {
- tokenManagers.put(DEFAULT_TOKENMANAGER_NAME, tm);
- }
- return;
- }
-
- // BOTH INPUT AND OUTPUT VOCAB
- if ( grammar.exportVocab!=null && grammar.importVocab!=null )
- {
- // don't want input==output
- if (grammar.importVocab.equals(grammar.exportVocab))
- {
- tool.error("exportVocab of " + grammar.exportVocab + " same as importVocab; probably not what you want");
- }
- // does the input vocab already exist in memory?
- if (tokenManagers.containsKey(grammar.importVocab))
- {
- // make a copy since we'll be generating a new output vocab
- // and we don't want to affect this one.
- TokenManager tm = (TokenManager) tokenManagers.get(grammar.importVocab);
- // System.out.println("Duping importVocab of " + grammar.importVocab);
- TokenManager dup = (TokenManager)tm.clone();
- dup.setName(grammar.exportVocab);
- // System.out.println("Setting name to " + grammar.exportVocab);
- dup.setReadOnly(false);
- grammar.setTokenManager(dup);
- tokenManagers.put(grammar.exportVocab, dup);
- return;
- }
- // Must be a file, go get it.
- ImportVocabTokenManager tm =
- new ImportVocabTokenManager(grammar,
- grammar.importVocab + CodeGenerator.TokenTypesFileSuffix + CodeGenerator.TokenTypesFileExt,
- grammar.exportVocab,
- tool);
- tm.setReadOnly(false); // write it out as we've changed name
- // Add this token manager to the list so its tokens will be generated
- tokenManagers.put(grammar.exportVocab, tm);
- // Assign the token manager to this grammar.
- grammar.setTokenManager(tm);
-
- // set default vocab if none
- if (!tokenManagers.containsKey(DEFAULT_TOKENMANAGER_NAME))
- {
- tokenManagers.put(DEFAULT_TOKENMANAGER_NAME, tm);
- }
-
- return;
- }
- }
- public void endRule(String r)
- {
- }
- public void endSubRule()
- {
- }
- public void endTree()
- {
- }
- public void hasError()
- {
- }
- public void noASTSubRule()
- {
- }
- public void oneOrMoreSubRule()
- {
- }
- public void optionalSubRule()
- {
- }
- public void setUserExceptions(String thr) {}
- public void refAction(Token action)
- {
- }
- public void refArgAction(Token action)
- {
- }
- public void refCharLiteral(Token lit, Token label, boolean inverted, int autoGenType, boolean lastInRule)
- {
- }
- public void refCharRange(Token t1, Token t2, Token label, int autoGenType, boolean lastInRule)
- {
- }
-
- public void refElementOption(Token option, Token value) {
- }
-
- public void refTokensSpecElementOption(Token tok, Token option, Token value) {
- }
-
- public void refExceptionHandler(Token exTypeAndName, Token action) {
- }
-
- // Header action applies to all parsers and lexers.
- public void refHeaderAction(Token name,Token act)
- {
- headerActions.put((name==null) ? "" : Tool.stripFrontBack(name.getText(),"\"","\""),
- act);
- // headerAction = act.getText();
- }
-
- public String getHeaderAction(String name)
- {
- Token t = (Token)headerActions.get(name);
- if ( t==null )
- {
- return "";
- }
- return t.getText();
- }
-
- public void refInitAction(Token action)
- {
- }
- public void refMemberAction(Token act)
- {
- }
- public void refPreambleAction(Token act)
- {
- thePreambleAction = act;
- }
- public void refReturnAction(Token returnAction)
- {
- }
-
- public void refRule(Token idAssign,
- Token r,
- Token label,
- Token args,
- int autoGenType)
- {
- String id = r.getText();
- // if ( Character.isUpperCase(id.charAt(0)) ) { // lexer rule?
- if ( r.type == ANTLRTokenTypes.TOKEN_REF )
- {
- // lexer rule?
- id = CodeGenerator.lexerRuleName(id);
- }
- if ( !grammar.isDefined(id) )
- {
- grammar.define(new RuleSymbol(id));
- }
- }
-
- public void refSemPred(Token pred)
- {
- }
-
- public void refStringLiteral(Token lit,
- Token label,
- int autoGenType,
- boolean lastInRule)
- {
- _refStringLiteral(lit, label, autoGenType, lastInRule);
- }
+ * options that may not have been set.
+ * This method is bigger than it needs to be, but is much more
+ * clear if I delineate all the cases.
+ */
+ public void endOptions() {
+ // NO VOCAB OPTIONS
+ if (grammar.exportVocab == null && grammar.importVocab == null) {
+ grammar.exportVocab = grammar.getClassName();
+ // Can we get initial vocab from default shared vocab?
+ if (tokenManagers.containsKey(DEFAULT_TOKENMANAGER_NAME)) {
+ // Use the already-defined token manager
+ grammar.exportVocab = DEFAULT_TOKENMANAGER_NAME;
+ TokenManager tm = (TokenManager)tokenManagers.get(DEFAULT_TOKENMANAGER_NAME);
+ // System.out.println("No tokenVocabulary for '" + grammar.getClassName() + "', using default '" + tm.getName() + "'");
+ grammar.setTokenManager(tm);
+ return;
+ }
+ // no shared vocab for file, make new one
+ // System.out.println("No exportVocab for '" + grammar.getClassName() + "', creating default '" + grammar.exportVocab + "'");
+ TokenManager tm = new SimpleTokenManager(grammar.exportVocab, tool);
+ grammar.setTokenManager(tm);
+ // Add the token manager to the list of token managers
+ tokenManagers.put(grammar.exportVocab, tm);
+ // no default vocab, so make this the default vocab
+ tokenManagers.put(DEFAULT_TOKENMANAGER_NAME, tm);
+ return;
+ }
+
+ // NO OUTPUT, BUT HAS INPUT VOCAB
+ if (grammar.exportVocab == null && grammar.importVocab != null) {
+ grammar.exportVocab = grammar.getClassName();
+ // first make sure input!=output
+ if (grammar.importVocab.equals(grammar.exportVocab)) {
+ tool.warning("Grammar " + grammar.getClassName() +
+ " cannot have importVocab same as default output vocab (grammar name); ignored.");
+ // kill importVocab option and try again: use default vocab
+ grammar.importVocab = null;
+ endOptions();
+ return;
+ }
+ // check to see if the vocab is already in memory
+ // (defined by another grammar in the file). Not normal situation.
+ if (tokenManagers.containsKey(grammar.importVocab)) {
+ // make a copy since we'll be generating a new output vocab
+ // and we don't want to affect this one. Set the name to
+ // the default output vocab==classname.
+ TokenManager tm = (TokenManager)tokenManagers.get(grammar.importVocab);
+ // System.out.println("Duping importVocab of " + grammar.importVocab);
+ TokenManager dup = (TokenManager)tm.clone();
+ dup.setName(grammar.exportVocab);
+ // System.out.println("Setting name to " + grammar.exportVocab);
+ dup.setReadOnly(false);
+ grammar.setTokenManager(dup);
+ tokenManagers.put(grammar.exportVocab, dup);
+ return;
+ }
+ // System.out.println("reading in vocab "+grammar.importVocab);
+ // Must be a file, go get it.
+ ImportVocabTokenManager tm =
+ new ImportVocabTokenManager(grammar,
+ grammar.importVocab + CodeGenerator.TokenTypesFileSuffix + CodeGenerator.TokenTypesFileExt,
+ grammar.exportVocab,
+ tool);
+ tm.setReadOnly(false); // since renamed, can write out
+ // Add this token manager to the list so its tokens will be generated
+ tokenManagers.put(grammar.exportVocab, tm);
+ // System.out.println("vocab renamed to default output vocab of "+tm.getName());
+ // Assign the token manager to this grammar.
+ grammar.setTokenManager(tm);
+
+ // set default vocab if none
+ if (!tokenManagers.containsKey(DEFAULT_TOKENMANAGER_NAME)) {
+ tokenManagers.put(DEFAULT_TOKENMANAGER_NAME, tm);
+ }
+
+ return;
+ }
+
+ // OUTPUT VOCAB, BUT NO INPUT VOCAB
+ if (grammar.exportVocab != null && grammar.importVocab == null) {
+ // share with previous vocab if it exists
+ if (tokenManagers.containsKey(grammar.exportVocab)) {
+ // Use the already-defined token manager
+ TokenManager tm = (TokenManager)tokenManagers.get(grammar.exportVocab);
+ // System.out.println("Sharing exportVocab of " + grammar.exportVocab);
+ grammar.setTokenManager(tm);
+ return;
+ }
+ // create new output vocab
+ // System.out.println("Creating exportVocab " + grammar.exportVocab);
+ TokenManager tm = new SimpleTokenManager(grammar.exportVocab, tool);
+ grammar.setTokenManager(tm);
+ // Add the token manager to the list of token managers
+ tokenManagers.put(grammar.exportVocab, tm);
+ // set default vocab if none
+ if (!tokenManagers.containsKey(DEFAULT_TOKENMANAGER_NAME)) {
+ tokenManagers.put(DEFAULT_TOKENMANAGER_NAME, tm);
+ }
+ return;
+ }
+
+ // BOTH INPUT AND OUTPUT VOCAB
+ if (grammar.exportVocab != null && grammar.importVocab != null) {
+ // don't want input==output
+ if (grammar.importVocab.equals(grammar.exportVocab)) {
+ tool.error("exportVocab of " + grammar.exportVocab + " same as importVocab; probably not what you want");
+ }
+ // does the input vocab already exist in memory?
+ if (tokenManagers.containsKey(grammar.importVocab)) {
+ // make a copy since we'll be generating a new output vocab
+ // and we don't want to affect this one.
+ TokenManager tm = (TokenManager)tokenManagers.get(grammar.importVocab);
+ // System.out.println("Duping importVocab of " + grammar.importVocab);
+ TokenManager dup = (TokenManager)tm.clone();
+ dup.setName(grammar.exportVocab);
+ // System.out.println("Setting name to " + grammar.exportVocab);
+ dup.setReadOnly(false);
+ grammar.setTokenManager(dup);
+ tokenManagers.put(grammar.exportVocab, dup);
+ return;
+ }
+ // Must be a file, go get it.
+ ImportVocabTokenManager tm =
+ new ImportVocabTokenManager(grammar,
+ grammar.importVocab + CodeGenerator.TokenTypesFileSuffix + CodeGenerator.TokenTypesFileExt,
+ grammar.exportVocab,
+ tool);
+ tm.setReadOnly(false); // write it out as we've changed name
+ // Add this token manager to the list so its tokens will be generated
+ tokenManagers.put(grammar.exportVocab, tm);
+ // Assign the token manager to this grammar.
+ grammar.setTokenManager(tm);
+
+ // set default vocab if none
+ if (!tokenManagers.containsKey(DEFAULT_TOKENMANAGER_NAME)) {
+ tokenManagers.put(DEFAULT_TOKENMANAGER_NAME, tm);
+ }
+
+ return;
+ }
+ }
+
+ public void endRule(String r) {
+ }
+
+ public void endSubRule() {
+ }
+
+ public void endTree() {
+ }
+
+ public void hasError() {
+ }
+
+ public void noASTSubRule() {
+ }
+
+ public void oneOrMoreSubRule() {
+ }
+
+ public void optionalSubRule() {
+ }
+
+ public void setUserExceptions(String thr) {
+ }
+
+ public void refAction(Token action) {
+ }
+
+ public void refArgAction(Token action) {
+ }
+
+ public void refCharLiteral(Token lit, Token label, boolean inverted, int autoGenType, boolean lastInRule) {
+ }
+
+ public void refCharRange(Token t1, Token t2, Token label, int autoGenType, boolean lastInRule) {
+ }
+
+ public void refElementOption(Token option, Token value) {
+ }
+
+ public void refTokensSpecElementOption(Token tok, Token option, Token value) {
+ }
+
+ public void refExceptionHandler(Token exTypeAndName, Token action) {
+ }
+
+ // Header action applies to all parsers and lexers.
+ public void refHeaderAction(Token name, Token act) {
+ headerActions.put((name == null) ? "" : Tool.stripFrontBack(name.getText(), "\"", "\""),
+ act);
+ // headerAction = act.getText();
+ }
+
+ public String getHeaderAction(String name) {
+ Token t = (Token)headerActions.get(name);
+ if (t == null) {
+ return "";
+ }
+ return t.getText();
+ }
+
+ public void refInitAction(Token action) {
+ }
+
+ public void refMemberAction(Token act) {
+ }
+
+ public void refPreambleAction(Token act) {
+ thePreambleAction = act;
+ }
+
+ public void refReturnAction(Token returnAction) {
+ }
+
+ public void refRule(Token idAssign,
+ Token r,
+ Token label,
+ Token args,
+ int autoGenType) {
+ String id = r.getText();
+ // if ( Character.isUpperCase(id.charAt(0)) ) { // lexer rule?
+ if (r.type == ANTLRTokenTypes.TOKEN_REF) {
+ // lexer rule?
+ id = CodeGenerator.lexerRuleName(id);
+ }
+ if (!grammar.isDefined(id)) {
+ grammar.define(new RuleSymbol(id));
+ }
+ }
+
+ public void refSemPred(Token pred) {
+ }
+
+ public void refStringLiteral(Token lit,
+ Token label,
+ int autoGenType,
+ boolean lastInRule) {
+ _refStringLiteral(lit, label, autoGenType, lastInRule);
+ }
/** Reference a token */
- public void refToken(Token assignId, Token t, Token label, Token args,
- boolean inverted, int autoGenType, boolean lastInRule)
- {
- _refToken(assignId, t, label, args, inverted, autoGenType, lastInRule);
- }
-
- public void refTokenRange(Token t1, Token t2, Token label, int autoGenType, boolean lastInRule)
- {
- // ensure that the DefineGrammarSymbols methods are called; otherwise a range addes more
- // token refs to the alternative by calling MakeGrammar.refToken etc...
- if ( t1.getText().charAt(0) == '"' )
- {
- refStringLiteral(t1, null, GrammarElement.AUTO_GEN_NONE, lastInRule);
- }
- else
- {
- _refToken(null, t1, null, null, false, GrammarElement.AUTO_GEN_NONE, lastInRule);
- }
- if ( t2.getText().charAt(0) == '"' )
- {
- _refStringLiteral(t2, null, GrammarElement.AUTO_GEN_NONE, lastInRule);
- }
- else
- {
- _refToken(null, t2, null, null, false, GrammarElement.AUTO_GEN_NONE, lastInRule);
- }
- }
-
- public void refTreeSpecifier(Token treeSpec) {
- }
-
- public void refWildcard(Token t, Token label, int autoGenType)
- {
- }
+ public void refToken(Token assignId, Token t, Token label, Token args,
+ boolean inverted, int autoGenType, boolean lastInRule) {
+ _refToken(assignId, t, label, args, inverted, autoGenType, lastInRule);
+ }
+
+ public void refTokenRange(Token t1, Token t2, Token label, int autoGenType, boolean lastInRule) {
+ // ensure that the DefineGrammarSymbols methods are called; otherwise a range addes more
+ // token refs to the alternative by calling MakeGrammar.refToken etc...
+ if (t1.getText().charAt(0) == '"') {
+ refStringLiteral(t1, null, GrammarElement.AUTO_GEN_NONE, lastInRule);
+ } else {
+ _refToken(null, t1, null, null, false, GrammarElement.AUTO_GEN_NONE, lastInRule);
+ }
+ if (t2.getText().charAt(0) == '"') {
+ _refStringLiteral(t2, null, GrammarElement.AUTO_GEN_NONE, lastInRule);
+ } else {
+ _refToken(null, t2, null, null, false, GrammarElement.AUTO_GEN_NONE, lastInRule);
+ }
+ }
+
+ public void refTreeSpecifier(Token treeSpec) {
+ }
+
+ public void refWildcard(Token t, Token label, int autoGenType) {
+ }
/** Get ready to process a new grammar */
- public void reset()
- {
- grammar = null;
- }
-
- public void setArgOfRuleRef(Token argaction)
- {
- }
+ public void reset() {
+ grammar = null;
+ }
+ public void setArgOfRuleRef(Token argaction) {
+ }
+
/** Set the character vocabulary for a lexer */
- public void setCharVocabulary(BitSet b)
- {
- // grammar should enforce that this is only called for lexer
- ((LexerGrammar)grammar).setCharVocabulary(b);
- }
+ public void setCharVocabulary(BitSet b) {
+ // grammar should enforce that this is only called for lexer
+ ((LexerGrammar)grammar).setCharVocabulary(b);
+ }
/** setFileOption: Associate an option value with a key.
- * This applies to options for an entire grammar file.
- * @param key The token containing the option name
- * @param value The token containing the option value.
- */
- public void setFileOption(Token key, Token value, String filename)
- {
- if (key.getText().equals("language"))
- {
- if (value.getType() == ANTLRParser.STRING_LITERAL)
- {
- language = Tool.stripBack(Tool.stripFront(value.getText(), '"'), '"');
- }
- else if (value.getType() == ANTLRParser.TOKEN_REF || value.getType() == ANTLRParser.RULE_REF)
- {
- language = value.getText();
- }
- else
- {
- tool.error("language option must be string or identifier", filename, value.getLine());
- }
- }
- else if (key.getText().equals("mangleLiteralPrefix"))
- {
- if (value.getType() == ANTLRParser.STRING_LITERAL)
- {
- tool.literalsPrefix = tool.stripFrontBack(value.getText(), "\"","\"");
- }
- else
- {
- tool.error("mangleLiteralPrefix option must be string", filename, value.getLine());
- }
- }
- else if (key.getText().equals("upperCaseMangledLiterals"))
- {
- if (value.getText().equals("true"))
- {
- tool.upperCaseMangledLiterals = true;
- }
- else if (value.getText().equals("false"))
- {
- tool.upperCaseMangledLiterals = false;
- }
- else
- {
- grammar.tool.error("Value for upperCaseMangledLiterals must be true or false", filename, key.getLine());
- }
- }
- else if (key.getText().equals("namespace") ||
- key.getText().equals("namespaceStd") ||
- key.getText().equals("namespaceAntlr") ||
- key.getText().equals("genHashLines") )
- {
- if (!language.equals("Cpp"))
- {
- tool.error(key.getText() + " option only valid for C++", filename, key.getLine());
- }
- else
- {
- if( key.getText().equals("genHashLines") )
- {
- if(!( value.getText().equals("true") || value.getText().equals("false") ))
- tool.error("genHashLines option must be true or false", filename, value.getLine());
- tool.genHashLines = value.getText().equals("true");
- }
- else
- {
- if ( value.getType() != ANTLRParser.STRING_LITERAL )
- {
- tool.error(key.getText()+" option must be a string", filename, value.getLine());
- }
- else
- {
- if ( key.getText().equals("namespace") )
- tool.setNameSpace(value.getText());
- else if( key.getText().equals("namespaceStd") )
- tool.namespaceStd = value.getText();
- else if( key.getText().equals("namespaceAntlr") )
- tool.namespaceAntlr = value.getText();
- }
- }
- }
- }
- else
- {
- tool.error("Invalid file-level option: " + key.getText(), filename, key.getLine());
- }
- }
+ * This applies to options for an entire grammar file.
+ * @param key The token containing the option name
+ * @param value The token containing the option value.
+ */
+ public void setFileOption(Token key, Token value, String filename) {
+ if (key.getText().equals("language")) {
+ if (value.getType() == ANTLRParser.STRING_LITERAL) {
+ language = Tool.stripBack(Tool.stripFront(value.getText(), '"'), '"');
+ } else if (value.getType() == ANTLRParser.TOKEN_REF || value.getType() == ANTLRParser.RULE_REF) {
+ language = value.getText();
+ } else {
+ tool.error("language option must be string or identifier", filename, value.getLine());
+ }
+ } else if (key.getText().equals("mangleLiteralPrefix")) {
+ if (value.getType() == ANTLRParser.STRING_LITERAL) {
+ tool.literalsPrefix = tool.stripFrontBack(value.getText(), "\"", "\"");
+ } else {
+ tool.error("mangleLiteralPrefix option must be string", filename, value.getLine());
+ }
+ } else if (key.getText().equals("upperCaseMangledLiterals")) {
+ if (value.getText().equals("true")) {
+ tool.upperCaseMangledLiterals = true;
+ } else if (value.getText().equals("false")) {
+ tool.upperCaseMangledLiterals = false;
+ } else {
+ grammar.tool.error("Value for upperCaseMangledLiterals must be true or false", filename, key.getLine());
+ }
+ } else if (key.getText().equals("namespace") ||
+ key.getText().equals("namespaceStd") ||
+ key.getText().equals("namespaceAntlr") ||
+ key.getText().equals("genHashLines")) {
+ if (!language.equals("Cpp")) {
+ tool.error(key.getText() + " option only valid for C++", filename, key.getLine());
+ } else {
+ if (key.getText().equals("genHashLines")) {
+ if (!(value.getText().equals("true") || value.getText().equals("false")))
+ tool.error("genHashLines option must be true or false", filename, value.getLine());
+ tool.genHashLines = value.getText().equals("true");
+ } else {
+ if (value.getType() != ANTLRParser.STRING_LITERAL) {
+ tool.error(key.getText() + " option must be a string", filename, value.getLine());
+ } else {
+ if (key.getText().equals("namespace"))
+ tool.setNameSpace(value.getText());
+ else if (key.getText().equals("namespaceStd"))
+ tool.namespaceStd = value.getText();
+ else if (key.getText().equals("namespaceAntlr"))
+ tool.namespaceAntlr = value.getText();
+ }
+ }
+ }
+ } else {
+ tool.error("Invalid file-level option: " + key.getText(), filename, key.getLine());
+ }
+ }
+
/** setGrammarOption: Associate an option value with a key.
- * This function forwards to Grammar.setOption for some options.
- * @param key The token containing the option name
- * @param value The token containing the option value.
- */
- public void setGrammarOption(Token key, Token value)
- {
- if (key.getText().equals("tokdef")||key.getText().equals("tokenVocabulary"))
- {
- tool.error("tokdef/tokenVocabulary options are invalid >= ANTLR 2.6.0.\n"+
- " Use importVocab/exportVocab instead. Please see the documentation.\n"+
- " The previous options were so heinous that Terence changed the whole\n"+
- " vocabulary mechanism; it was better to change the names rather than\n"+
- " subtly change the functionality of the known options. Sorry!", grammar.getFilename(), value.getLine());
- }
- else if (key.getText().equals("literal") &&
- grammar instanceof LexerGrammar )
- {
- tool.error("the literal option is invalid >= ANTLR 2.6.0.\n"+
- " Use the \"tokens {...}\" mechanism instead.",
- grammar.getFilename(), value.getLine());
- }
- else if (key.getText().equals("exportVocab"))
- {
- // Set the token manager associated with the parser
- if (value.getType() == ANTLRParser.RULE_REF || value.getType() == ANTLRParser.TOKEN_REF)
- {
- grammar.exportVocab = value.getText();
- }
- else
- {
- tool.error("exportVocab must be an identifier", grammar.getFilename(), value.getLine());
- }
- }
- else if (key.getText().equals("importVocab"))
- {
- if (value.getType() == ANTLRParser.RULE_REF || value.getType() == ANTLRParser.TOKEN_REF)
- {
- grammar.importVocab = value.getText();
- }
- else
- {
- tool.error("importVocab must be an identifier", grammar.getFilename(), value.getLine());
- }
- }
-
- else
- {
- // Forward all unrecognized options to the grammar
- grammar.setOption(key.getText(), value);
- }
- }
- public void setRuleOption(Token key, Token value)
- {
- }
- public void setSubruleOption(Token key, Token value)
- {
- }
+ * This function forwards to Grammar.setOption for some options.
+ * @param key The token containing the option name
+ * @param value The token containing the option value.
+ */
+ public void setGrammarOption(Token key, Token value) {
+ if (key.getText().equals("tokdef") || key.getText().equals("tokenVocabulary")) {
+ tool.error("tokdef/tokenVocabulary options are invalid >= ANTLR 2.6.0.\n" +
+ " Use importVocab/exportVocab instead. Please see the documentation.\n" +
+ " The previous options were so heinous that Terence changed the whole\n" +
+ " vocabulary mechanism; it was better to change the names rather than\n" +
+ " subtly change the functionality of the known options. Sorry!", grammar.getFilename(), value.getLine());
+ } else if (key.getText().equals("literal") &&
+ grammar instanceof LexerGrammar) {
+ tool.error("the literal option is invalid >= ANTLR 2.6.0.\n" +
+ " Use the \"tokens {...}\" mechanism instead.",
+ grammar.getFilename(), value.getLine());
+ } else if (key.getText().equals("exportVocab")) {
+ // Set the token manager associated with the parser
+ if (value.getType() == ANTLRParser.RULE_REF || value.getType() == ANTLRParser.TOKEN_REF) {
+ grammar.exportVocab = value.getText();
+ } else {
+ tool.error("exportVocab must be an identifier", grammar.getFilename(), value.getLine());
+ }
+ } else if (key.getText().equals("importVocab")) {
+ if (value.getType() == ANTLRParser.RULE_REF || value.getType() == ANTLRParser.TOKEN_REF) {
+ grammar.importVocab = value.getText();
+ } else {
+ tool.error("importVocab must be an identifier", grammar.getFilename(), value.getLine());
+ }
+ } else {
+ // Forward all unrecognized options to the grammar
+ grammar.setOption(key.getText(), value);
+ }
+ }
+
+ public void setRuleOption(Token key, Token value) {
+ }
+
+ public void setSubruleOption(Token key, Token value) {
+ }
+
/** Start a new lexer */
- public void startLexer(String file, Token name, String superClass, String doc)
- {
- if ( numLexers>0 )
- {
- tool.panic("You may only have one lexer per grammar file: class "+ name.getText());
- }
- numLexers++;
- reset();
- //System.out.println("Processing lexer '" + name.getText() + "'");
- // Does the lexer already exist?
- Grammar g = (Grammar)grammars.get(name);
- if (g != null)
- {
- if (!(g instanceof LexerGrammar))
- {
- tool.panic("'" + name.getText() + "' is already defined as a non-lexer");
- }
- else
- {
- tool.panic("Lexer '" + name.getText() + "' is already defined");
- }
- }
- else
- {
- // Create a new lexer grammar
- LexerGrammar lg = new LexerGrammar(name.getText(), tool, superClass);
- lg.comment = doc;
- lg.processArguments(args);
- lg.setFilename(file);
- grammars.put(lg.getClassName(), lg);
- // Use any preamble action
- lg.preambleAction = thePreambleAction;
- thePreambleAction = new CommonToken(Token.INVALID_TYPE, "");
- // This is now the current grammar
- grammar = lg;
- }
- }
+ public void startLexer(String file, Token name, String superClass, String doc) {
+ if (numLexers > 0) {
+ tool.panic("You may only have one lexer per grammar file: class " + name.getText());
+ }
+ numLexers++;
+ reset();
+ //System.out.println("Processing lexer '" + name.getText() + "'");
+ // Does the lexer already exist?
+ Grammar g = (Grammar)grammars.get(name);
+ if (g != null) {
+ if (!(g instanceof LexerGrammar)) {
+ tool.panic("'" + name.getText() + "' is already defined as a non-lexer");
+ } else {
+ tool.panic("Lexer '" + name.getText() + "' is already defined");
+ }
+ } else {
+ // Create a new lexer grammar
+ LexerGrammar lg = new LexerGrammar(name.getText(), tool, superClass);
+ lg.comment = doc;
+ lg.processArguments(args);
+ lg.setFilename(file);
+ grammars.put(lg.getClassName(), lg);
+ // Use any preamble action
+ lg.preambleAction = thePreambleAction;
+ thePreambleAction = new CommonToken(Token.INVALID_TYPE, "");
+ // This is now the current grammar
+ grammar = lg;
+ }
+ }
+
/** Start a new parser */
- public void startParser(String file, Token name, String superClass, String doc)
- {
- if ( numParsers>0 )
- {
- tool.panic("You may only have one parser per grammar file: class "+ name.getText());
- }
- numParsers++;
- reset();
- //System.out.println("Processing parser '" + name.getText() + "'");
- // Is this grammar already defined?
- Grammar g = (Grammar)grammars.get(name);
- if (g != null)
- {
- if (!(g instanceof ParserGrammar))
- {
- tool.panic("'" + name.getText() + "' is already defined as a non-parser");
- }
- else
- {
- tool.panic("Parser '" + name.getText() + "' is already defined");
- }
- }
- else
- {
- // Create a new grammar
- grammar = new ParserGrammar(name.getText(), tool, superClass);
- grammar.comment = doc;
- grammar.processArguments(args);
- grammar.setFilename(file);
- grammars.put(grammar.getClassName(), grammar);
- // Use any preamble action
- grammar.preambleAction = thePreambleAction;
- thePreambleAction = new CommonToken(Token.INVALID_TYPE, "");
- }
- }
+ public void startParser(String file, Token name, String superClass, String doc) {
+ if (numParsers > 0) {
+ tool.panic("You may only have one parser per grammar file: class " + name.getText());
+ }
+ numParsers++;
+ reset();
+ //System.out.println("Processing parser '" + name.getText() + "'");
+ // Is this grammar already defined?
+ Grammar g = (Grammar)grammars.get(name);
+ if (g != null) {
+ if (!(g instanceof ParserGrammar)) {
+ tool.panic("'" + name.getText() + "' is already defined as a non-parser");
+ } else {
+ tool.panic("Parser '" + name.getText() + "' is already defined");
+ }
+ } else {
+ // Create a new grammar
+ grammar = new ParserGrammar(name.getText(), tool, superClass);
+ grammar.comment = doc;
+ grammar.processArguments(args);
+ grammar.setFilename(file);
+ grammars.put(grammar.getClassName(), grammar);
+ // Use any preamble action
+ grammar.preambleAction = thePreambleAction;
+ thePreambleAction = new CommonToken(Token.INVALID_TYPE, "");
+ }
+ }
+
/** Start a new tree-walker */
- public void startTreeWalker(String file, Token name, String superClass, String doc)
- {
- if ( numTreeParsers>0 )
- {
- tool.panic("You may only have one tree parser per grammar file: class "+ name.getText());
- }
- numTreeParsers++;
- reset();
- //System.out.println("Processing tree-walker '" + name.getText() + "'");
- // Is this grammar already defined?
- Grammar g = (Grammar)grammars.get(name);
- if (g != null)
- {
- if (!(g instanceof TreeWalkerGrammar))
- {
- tool.panic("'" + name.getText() + "' is already defined as a non-tree-walker");
- }
- else
- {
- tool.panic("Tree-walker '" + name.getText() + "' is already defined");
- }
- }
- else
- {
- // Create a new grammar
- grammar = new TreeWalkerGrammar(name.getText(), tool, superClass);
- grammar.comment = doc;
- grammar.processArguments(args);
- grammar.setFilename(file);
- grammars.put(grammar.getClassName(), grammar);
- // Use any preamble action
- grammar.preambleAction = thePreambleAction;
- thePreambleAction = new CommonToken(Token.INVALID_TYPE, "");
- }
- }
- public void synPred()
- {
- }
- public void zeroOrMoreSubRule()
- {
- }
+ public void startTreeWalker(String file, Token name, String superClass, String doc) {
+ if (numTreeParsers > 0) {
+ tool.panic("You may only have one tree parser per grammar file: class " + name.getText());
+ }
+ numTreeParsers++;
+ reset();
+ //System.out.println("Processing tree-walker '" + name.getText() + "'");
+ // Is this grammar already defined?
+ Grammar g = (Grammar)grammars.get(name);
+ if (g != null) {
+ if (!(g instanceof TreeWalkerGrammar)) {
+ tool.panic("'" + name.getText() + "' is already defined as a non-tree-walker");
+ } else {
+ tool.panic("Tree-walker '" + name.getText() + "' is already defined");
+ }
+ } else {
+ // Create a new grammar
+ grammar = new TreeWalkerGrammar(name.getText(), tool, superClass);
+ grammar.comment = doc;
+ grammar.processArguments(args);
+ grammar.setFilename(file);
+ grammars.put(grammar.getClassName(), grammar);
+ // Use any preamble action
+ grammar.preambleAction = thePreambleAction;
+ thePreambleAction = new CommonToken(Token.INVALID_TYPE, "");
+ }
+ }
+
+ public void synPred() {
+ }
+
+ public void zeroOrMoreSubRule() {
+ }
}
1.2 +882 -873 e/src/jsrc/antlr/DiagnosticCodeGenerator.java
Index: DiagnosticCodeGenerator.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/DiagnosticCodeGenerator.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- DiagnosticCodeGenerator.java 2001/10/03 21:04:13 1.1
+++ DiagnosticCodeGenerator.java 2001/10/04 01:09:14 1.2
@@ -4,888 +4,897 @@
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
- * $Id: DiagnosticCodeGenerator.java,v 1.1 2001/10/03 21:04:13 markm Exp $
+ * $Id: DiagnosticCodeGenerator.java,v 1.2 2001/10/04 01:09:14 markm Exp $
*/
-import java.util.Enumeration;
-import antlr.collections.impl.BitSet;
import antlr.collections.impl.Vector;
-import java.io.PrintWriter; //SAS: changed for proper text file io
+
import java.io.IOException;
-import java.io.FileWriter;
+import java.util.Enumeration;
/**Generate MyParser.txt, MyLexer.txt and MyParserTokenTypes.txt */
public class DiagnosticCodeGenerator extends CodeGenerator {
- /** non-zero if inside syntactic predicate generation */
- protected int syntacticPredLevel = 0;
+
+ /** non-zero if inside syntactic predicate generation */
+ protected int syntacticPredLevel = 0;
- /** true during lexer generation, false during parser generation */
- protected boolean doingLexRules = false;
+ /** true during lexer generation, false during parser generation */
+ protected boolean doingLexRules = false;
- /** Create a Diagnostic code-generator using the given Grammar
- * The caller must still call setTool, setBehavior, and setAnalyzer
- * before generating code.
- */
- public DiagnosticCodeGenerator() {
- super();
- charFormatter = new JavaCharFormatter();
- }
- /**Generate the parser, lexer, and token types documentation */
- public void gen() {
-
- // Do the code generation
- try {
- // Loop over all grammars
- Enumeration grammarIter = behavior.grammars.elements();
- while (grammarIter.hasMoreElements()) {
- Grammar g = (Grammar)grammarIter.nextElement();
-
- // Connect all the components to each other
- g.setGrammarAnalyzer(analyzer);
- g.setCodeGenerator(this);
- analyzer.setGrammar(g);
-
- // To get right overloading behavior across hetrogeneous grammars
- g.generate();
-
- if (tool.hasError) {
- System.out.println("Exiting due to errors.");
- System.exit(1);
- }
-
- }
-
- // Loop over all token managers (some of which are lexers)
- Enumeration tmIter = behavior.tokenManagers.elements();
- while (tmIter.hasMoreElements()) {
- TokenManager tm = (TokenManager)tmIter.nextElement();
- if (!tm.isReadOnly()) {
- // Write the token manager tokens as Java
- genTokenTypes(tm);
- }
- }
- }
- catch (IOException e) {
- System.out.println(e.getMessage());
- }
- }
- /** Generate code for the given grammar element.
- * @param blk The {...} action to generate
- */
- public void gen(ActionElement action) {
- if (action.isSemPred) {
- // handled elsewhere
- }
- else {
- print("ACTION: ");
- _printAction(action.actionText);
- }
- }
- /** Generate code for the given grammar element.
- * @param blk The "x|y|z|..." block to generate
- */
- public void gen(AlternativeBlock blk) {
- println("Start of alternative block.");
- tabs++;
- genBlockPreamble(blk);
-
- boolean ok = grammar.theLLkAnalyzer.deterministic(blk);
- if (!ok) {
- println("Warning: This alternative block is non-deterministic");
- }
- genCommonBlock(blk);
- tabs--;
- }
- /** Generate code for the given grammar element.
- * @param blk The block-end element to generate. Block-end
- * elements are synthesized by the grammar parser to represent
- * the end of a block.
- */
- public void gen(BlockEndElement end) {
- // no-op
- }
- /** Generate code for the given grammar element.
- * @param blk The character literal reference to generate
- */
- public void gen(CharLiteralElement atom) {
- print("Match character ");
- if (atom.not) {
- _print("NOT ");
- }
- _print(atom.atomText);
- if (atom.label != null) {
- _print(", label=" + atom.label);
- }
- _println("");
- }
- /** Generate code for the given grammar element.
- * @param blk The character-range reference to generate
- */
- public void gen(CharRangeElement r) {
- print("Match character range: " + r.beginText + ".." + r.endText);
- if ( r.label!=null ) {
- _print(", label = " + r.label);
- }
- _println("");
- }
- /** Generate the lexer TXT file */
- public void gen(LexerGrammar g) throws IOException {
- setGrammar(g);
- System.out.println("Generating " + grammar.getClassName() + TokenTypesFileExt);
- currentOutput = antlr.Tool.openOutputFile(grammar.getClassName() + TokenTypesFileExt);
- //SAS: changed for proper text file io
-
- tabs=0;
- doingLexRules = true;
-
- // Generate header common to all TXT output files
- genHeader();
-
- // Output the user-defined lexer premamble
- println("");
- println("*** Lexer Preamble Action.");
- println("This action will appear before the declaration of your lexer class:");
- tabs++;
- println(grammar.preambleAction.getText());
- tabs--;
- println("*** End of Lexer Preamble Action");
-
- // Generate lexer class definition
- println("");
- println("*** Your lexer class is called '" + grammar.getClassName() + "' and is a subclass of '" + grammar.getSuperClass() + "'.");
-
- // Generate user-defined parser class members
- println("");
- println("*** User-defined lexer class members:");
- println("These are the member declarations that you defined for your class:");
- tabs++;
- printAction(grammar.classMemberAction.getText());
- tabs--;
- println("*** End of user-defined lexer class members");
-
- // Generate string literals
- println("");
- println("*** String literals used in the parser");
- println("The following string literals were used in the parser.");
- println("An actual code generator would arrange to place these literals");
- println("into a table in the generated lexer, so that actions in the");
- println("generated lexer could match token text against the literals.");
- println("String literals used in the lexer are not listed here, as they");
- println("are incorporated into the mainstream lexer processing.");
- tabs++;
- // Enumerate all of the symbols and look for string literal symbols
- Enumeration ids = grammar.getSymbols();
- while ( ids.hasMoreElements() ) {
- GrammarSymbol sym = (GrammarSymbol)ids.nextElement();
- // Only processing string literals -- reject other symbol entries
- if ( sym instanceof StringLiteralSymbol ) {
- StringLiteralSymbol s = (StringLiteralSymbol)sym;
- println(s.getId() + " = " + s.getTokenType());
- }
- }
- tabs--;
- println("*** End of string literals used by the parser");
-
- // Generate nextToken() rule.
- // nextToken() is a synthetic lexer rule that is the implicit OR of all
- // user-defined lexer rules.
- genNextToken();
-
- // Generate code for each rule in the lexer
- println("");
- println("*** User-defined Lexer rules:");
- tabs++;
-
- ids = grammar.rules.elements();
- while ( ids.hasMoreElements() ) {
- RuleSymbol rs = (RuleSymbol)ids.nextElement();
- if (!rs.id.equals("mnextToken")) {
- genRule(rs);
- }
- }
-
- tabs--;
- println("");
- println("*** End User-defined Lexer rules:");
-
- // Close the lexer output file
- currentOutput.close();
- currentOutput = null;
- doingLexRules = false;
- }
- /** Generate code for the given grammar element.
- * @param blk The (...)+ block to generate
- */
- public void gen(OneOrMoreBlock blk) {
- println("Start ONE-OR-MORE (...)+ block:");
- tabs++;
- genBlockPreamble(blk);
- boolean ok = grammar.theLLkAnalyzer.deterministic(blk);
- if (!ok) {
- println("Warning: This one-or-more block is non-deterministic");
- }
- genCommonBlock(blk);
- tabs--;
- println("End ONE-OR-MORE block.");
- }
- /** Generate the parser TXT file */
- public void gen(ParserGrammar g) throws IOException {
- setGrammar(g);
- // Open the output stream for the parser and set the currentOutput
- System.out.println("Generating " + grammar.getClassName() + TokenTypesFileExt);
- currentOutput = antlr.Tool.openOutputFile(grammar.getClassName()+TokenTypesFileExt);
- //SAS: changed for proper text file io
-
- tabs = 0;
-
- // Generate the header common to all output files.
- genHeader();
-
- // Output the user-defined parser premamble
- println("");
- println("*** Parser Preamble Action.");
- println("This action will appear before the declaration of your parser class:");
- tabs++;
- println(grammar.preambleAction.getText());
- tabs--;
- println("*** End of Parser Preamble Action");
-
- // Generate parser class definition
- println("");
- println("*** Your parser class is called '" + grammar.getClassName() + "' and is a subclass of '" + grammar.getSuperClass() + "'.");
-
- // Generate user-defined parser class members
- println("");
- println("*** User-defined parser class members:");
- println("These are the member declarations that you defined for your class:");
- tabs++;
- printAction(grammar.classMemberAction.getText());
- tabs--;
- println("*** End of user-defined parser class members");
-
- // Generate code for each rule in the grammar
- println("");
- println("*** Parser rules:");
- tabs++;
-
- // Enumerate the parser rules
- Enumeration rules = grammar.rules.elements();
- while ( rules.hasMoreElements() ) {
- println("");
- // Get the rules from the list and downcast it to proper type
- GrammarSymbol sym = (GrammarSymbol) rules.nextElement();
- // Only process parser rules
- if ( sym instanceof RuleSymbol) {
- genRule((RuleSymbol)sym);
- }
- }
- tabs--;
- println("");
- println("*** End of parser rules");
-
- println("");
- println("*** End of parser");
-
- // Close the parser output stream
- currentOutput.close();
- currentOutput = null;
- }
- /** Generate code for the given grammar element.
- * @param blk The rule-reference to generate
- */
- public void gen(RuleRefElement rr) {
- RuleSymbol rs = (RuleSymbol)grammar.getSymbol(rr.targetRule);
-
- // Generate the actual rule description
- print("Rule Reference: " + rr.targetRule);
- if (rr.idAssign != null) {
- _print(", assigned to '" + rr.idAssign + "'");
- }
- if (rr.args != null) {
- _print(", arguments = " + rr.args);
- }
- _println("");
-
- // Perform diagnostics
- if (rs == null || !rs.isDefined())
- {
- println("Rule '" + rr.targetRule + "' is referenced, but that rule is not defined.");
- println("\tPerhaps the rule is misspelled, or you forgot to define it.");
- return;
- }
- if (!(rs instanceof RuleSymbol))
- {
- // Should this ever happen??
- println("Rule '" + rr.targetRule + "' is referenced, but that is not a grammar rule.");
- return;
- }
- if (rr.idAssign != null)
- {
- // Warn if the rule has no return type
- if (rs.block.returnAction == null)
- {
- println("Error: You assigned from Rule '" + rr.targetRule + "', but that rule has no return type.");
- }
- } else {
- // Warn about return value if any, but not inside syntactic predicate
- if (!(grammar instanceof LexerGrammar) && syntacticPredLevel == 0 && rs.block.returnAction != null)
- {
- println("Warning: Rule '" + rr.targetRule + "' returns a value");
- }
- }
- if (rr.args != null && rs.block.argAction == null) {
- println("Error: Rule '" + rr.targetRule + "' accepts no arguments.");
- }
- }
- /** Generate code for the given grammar element.
- * @param blk The string-literal reference to generate
- */
- public void gen(StringLiteralElement atom) {
- print("Match string literal ");
- _print(atom.atomText);
- if (atom.label != null) {
- _print(", label=" + atom.label);
- }
- _println("");
- }
- /** Generate code for the given grammar element.
- * @param blk The token-range reference to generate
- */
- public void gen(TokenRangeElement r) {
- print("Match token range: " + r.beginText + ".." + r.endText);
- if ( r.label!=null ) {
- _print(", label = " + r.label);
- }
- _println("");
- }
- /** Generate code for the given grammar element.
- * @param blk The token-reference to generate
- */
- public void gen(TokenRefElement atom) {
- print("Match token ");
- if (atom.not) {
- _print("NOT ");
- }
- _print(atom.atomText);
- if (atom.label != null) {
- _print(", label=" + atom.label);
- }
- _println("");
- }
- public void gen(TreeElement t) {
- print("Tree reference: "+t);
- }
- /** Generate the tree-walker TXT file */
- public void gen(TreeWalkerGrammar g) throws IOException {
- setGrammar(g);
- // Open the output stream for the parser and set the currentOutput
- System.out.println("Generating " + grammar.getClassName() + TokenTypesFileExt);
- currentOutput = antlr.Tool.openOutputFile(grammar.getClassName()+TokenTypesFileExt);
- //SAS: changed for proper text file io
-
- tabs = 0;
-
- // Generate the header common to all output files.
- genHeader();
-
- // Output the user-defined parser premamble
- println("");
- println("*** Tree-walker Preamble Action.");
- println("This action will appear before the declaration of your tree-walker class:");
- tabs++;
- println(grammar.preambleAction.getText());
- tabs--;
- println("*** End of tree-walker Preamble Action");
-
- // Generate tree-walker class definition
- println("");
- println("*** Your tree-walker class is called '" + grammar.getClassName() + "' and is a subclass of '" + grammar.getSuperClass() + "'.");
-
- // Generate user-defined tree-walker class members
- println("");
- println("*** User-defined tree-walker class members:");
- println("These are the member declarations that you defined for your class:");
- tabs++;
- printAction(grammar.classMemberAction.getText());
- tabs--;
- println("*** End of user-defined tree-walker class members");
-
- // Generate code for each rule in the grammar
- println("");
- println("*** tree-walker rules:");
- tabs++;
-
- // Enumerate the tree-walker rules
- Enumeration rules = grammar.rules.elements();
- while ( rules.hasMoreElements() ) {
- println("");
- // Get the rules from the list and downcast it to proper type
- GrammarSymbol sym = (GrammarSymbol) rules.nextElement();
- // Only process tree-walker rules
- if ( sym instanceof RuleSymbol) {
- genRule((RuleSymbol)sym);
- }
- }
- tabs--;
- println("");
- println("*** End of tree-walker rules");
-
- println("");
- println("*** End of tree-walker");
-
- // Close the tree-walker output stream
- currentOutput.close();
- currentOutput = null;
- }
- /** Generate a wildcard element */
- public void gen(WildcardElement wc) {
- print("Match wildcard");
- if ( wc.getLabel()!=null ) {
- _print(", label = " + wc.getLabel());
- }
- _println("");
- }
- /** Generate code for the given grammar element.
- * @param blk The (...)* block to generate
- */
- public void gen(ZeroOrMoreBlock blk) {
- println("Start ZERO-OR-MORE (...)+ block:");
- tabs++;
- genBlockPreamble(blk);
- boolean ok = grammar.theLLkAnalyzer.deterministic(blk);
- if (!ok) {
- println("Warning: This zero-or-more block is non-deterministic");
- }
- genCommonBlock(blk);
- tabs--;
- println("End ZERO-OR-MORE block.");
- }
- protected void genAlt(Alternative alt) {
- for (
- AlternativeElement elem = alt.head;
- !(elem instanceof BlockEndElement);
- elem = elem.next
- )
- {
- elem.generate();
- }
- if (alt.getTreeSpecifier() != null)
- {
- println("AST will be built as: " + alt.getTreeSpecifier().getText());
- }
- }
- /** Generate the header for a block, which may be a RuleBlock or a
- * plain AlternativeBLock. This generates any variable declarations,
- * init-actions, and syntactic-predicate-testing variables.
- * @blk The block for which the preamble is to be generated.
- */
- protected void genBlockPreamble(AlternativeBlock blk) {
- // dump out init action
- if ( blk.initAction!=null ) {
- printAction("Init action: " + blk.initAction);
- }
- }
- /**Generate common code for a block of alternatives; return a postscript
- * that needs to be generated at the end of the block. Other routines
- * may append else-clauses and such for error checking before the postfix
- * is generated.
- */
- public void genCommonBlock(AlternativeBlock blk) {
- boolean singleAlt = (blk.alternatives.size() == 1);
-
- println("Start of an alternative block.");
- tabs++;
- println("The lookahead set for this block is:");
- tabs++;
- genLookaheadSetForBlock(blk);
- tabs--;
-
- if (singleAlt) {
- println("This block has a single alternative");
- if (blk.getAlternativeAt(0).synPred != null)
- {
- // Generate a warning if there is one alt and it has a synPred
- println("Warning: you specified a syntactic predicate for this alternative,");
- println("and it is the only alternative of a block and will be ignored.");
- }
- }
- else {
- println("This block has multiple alternatives:");
- tabs++;
- }
-
- for (int i=0; i<blk.alternatives.size(); i++) {
- Alternative alt = blk.getAlternativeAt(i);
- AlternativeElement elem = alt.head;
-
- // Print lookahead set for alternate
- println("");
- if (i != 0) {
- print("Otherwise, ");
- } else {
- print("");
- }
- _println("Alternate(" + (i+1) + ") will be taken IF:");
- println("The lookahead set: ");
- tabs++;
- genLookaheadSetForAlt(alt);
- tabs--;
- if ( alt.semPred != null || alt.synPred != null ) {
- print("is matched, AND ");
- } else {
- println("is matched.");
- }
-
- // Dump semantic predicates
- if ( alt.semPred != null ) {
- _println("the semantic predicate:");
- tabs++;
- println(alt.semPred);
- if ( alt.synPred != null ) {
- print("is true, AND ");
- } else {
- println("is true.");
- }
- }
-
- // Dump syntactic predicate
- if ( alt.synPred != null ) {
- _println("the syntactic predicate:");
- tabs++;
- genSynPred( alt.synPred );
- tabs--;
- println("is matched.");
- }
-
- // Dump the alternative
- genAlt(alt);
- }
- println("");
- println("OTHERWISE, a NoViableAlt exception will be thrown");
- println("");
-
- if (!singleAlt) {
- tabs--;
- println("End of alternatives");
- }
- tabs--;
- println("End of alternative block.");
- }
- /** Generate a textual representation of the follow set
- * for a block.
- * @param blk The rule block of interest
- */
- public void genFollowSetForRuleBlock(RuleBlock blk)
- {
- Lookahead follow = grammar.theLLkAnalyzer.FOLLOW(1, blk.endNode);
- printSet(grammar.maxk, 1, follow);
- }
- /** Generate a header that is common to all TXT files */
- protected void genHeader()
- {
- println("ANTLR-generated file resulting from grammar " + tool.grammarFile);
- println("Diagnostic output");
- println("");
- println("Terence Parr, MageLang Institute");
- println("with John Lilley, Empathy Software");
- println("ANTLR Version "+Tool.version+"; 1996,1997");
- println("");
- println("*** Header Action.");
- println("This action will appear at the top of all generated files.");
- tabs++;
- printAction(behavior.getHeaderAction(""));
- tabs--;
- println("*** End of Header Action");
- println("");
- }
- /**Generate the lookahead set for an alternate. */
- protected void genLookaheadSetForAlt(Alternative alt) {
- if ( doingLexRules && alt.cache[1].containsEpsilon() ) {
- println("MATCHES ALL");
- return;
- }
- int depth = alt.lookaheadDepth;
- if ( depth == GrammarAnalyzer.NONDETERMINISTIC ) {
- // if the decision is nondeterministic, do the best we can: LL(k)
- // any predicates that are around will be generated later.
- depth = grammar.maxk;
- }
- for (int i = 1; i <= depth; i++)
- {
- Lookahead lookahead = alt.cache[i];
- printSet(depth, i, lookahead);
- }
- }
- /** Generate a textual representation of the lookahead set
- * for a block.
- * @param blk The block of interest
- */
- public void genLookaheadSetForBlock(AlternativeBlock blk)
- {
- // Find the maximal lookahead depth over all alternatives
- int depth = 0;
- for (int i=0; i<blk.alternatives.size(); i++) {
- Alternative alt = blk.getAlternativeAt(i);
- if (alt.lookaheadDepth == GrammarAnalyzer.NONDETERMINISTIC) {
- depth = grammar.maxk;
- break;
- }
- else if (depth < alt.lookaheadDepth) {
- depth = alt.lookaheadDepth;
- }
- }
-
- for (int i = 1; i <= depth; i++)
- {
- Lookahead lookahead = grammar.theLLkAnalyzer.look(i, blk);
- printSet(depth, i, lookahead);
- }
- }
- /** Generate the nextToken rule.
- * nextToken is a synthetic lexer rule that is the implicit OR of all
- * user-defined lexer rules.
- */
- public void genNextToken() {
- println("");
- println("*** Lexer nextToken rule:");
- println("The lexer nextToken rule is synthesized from all of the user-defined");
- println("lexer rules. It logically consists of one big alternative block with");
- println("each user-defined rule being an alternative.");
- println("");
-
- // Create the synthesized rule block for nextToken consisting
- // of an alternate block containing all the user-defined lexer rules.
- RuleBlock blk = MakeGrammar.createNextTokenRule(grammar, grammar.rules, "nextToken");
-
- // Define the nextToken rule symbol
- RuleSymbol nextTokenRs = new RuleSymbol("mnextToken");
- nextTokenRs.setDefined();
- nextTokenRs.setBlock(blk);
- nextTokenRs.access = "private";
- grammar.define(nextTokenRs);
-
- // Analyze the synthesized block
- if (!grammar.theLLkAnalyzer.deterministic(blk))
- {
- println("The grammar analyzer has determined that the synthesized");
- println("nextToken rule is non-deterministic (i.e., it has ambiguities)");
- println("This means that there is some overlap of the character");
- println("lookahead for two or more of your lexer rules.");
- }
-
- genCommonBlock(blk);
-
- println("*** End of nextToken lexer rule.");
- }
- /** Generate code for a named rule block
- * @param s The RuleSymbol describing the rule to generate
- */
- public void genRule(RuleSymbol s) {
- println("");
- String ruleType = (doingLexRules ? "Lexer" : "Parser");
- println("*** " + ruleType + " Rule: " + s.getId());
- if (!s.isDefined() ) {
- println("This rule is undefined.");
- println("This means that the rule was referenced somewhere in the grammar,");
- println("but a definition for the rule was not encountered.");
- println("It is also possible that syntax errors during the parse of");
- println("your grammar file prevented correct processing of the rule.");
- println("*** End " + ruleType + " Rule: " + s.getId());
- return;
- }
- tabs++;
-
- if (s.access.length() != 0) {
- println("Access: " + s.access);
- }
-
- // Get rule return type and arguments
- RuleBlock rblk = s.getBlock();
-
- // Gen method return value(s)
- if (rblk.returnAction != null) {
- println("Return value(s): " + rblk.returnAction);
- if ( doingLexRules ) {
- println("Error: you specified return value(s) for a lexical rule.");
- println("\tLexical rules have an implicit return type of 'int'.");
- }
- } else {
- if ( doingLexRules ) {
- println("Return value: lexical rule returns an implicit token type");
- } else {
- println("Return value: none");
- }
- }
-
- // Gen arguments
- if (rblk.argAction != null)
- {
- println("Arguments: " + rblk.argAction);
- }
-
- // Dump any init-action
- genBlockPreamble(rblk);
-
- // Analyze the rule
- boolean ok = grammar.theLLkAnalyzer.deterministic(rblk);
- if (!ok) {
- println("Error: This rule is non-deterministic");
- }
-
- // Dump the alternates of the rule
- genCommonBlock(rblk);
-
- // Search for an unlabeled exception specification attached to the rule
- ExceptionSpec unlabeledUserSpec = rblk.findExceptionSpec("");
-
- // Generate user-defined or default catch phrases
- if (unlabeledUserSpec != null) {
- println("You specified error-handler(s) for this rule:");
- tabs++;
- for (int i = 0; i < unlabeledUserSpec.handlers.size(); i++)
- {
- if (i != 0) {
- println("");
- }
-
- ExceptionHandler handler = (ExceptionHandler)unlabeledUserSpec.handlers.elementAt(i);
- println("Error-handler(" + (i+1) + ") catches [" + handler.exceptionTypeAndName.getText() + "] and executes:");
- printAction(handler.action.getText());
- }
- tabs--;
- println("End error-handlers.");
- }
- else if (!doingLexRules) {
- println("Default error-handling will be generated, which catches all");
- println("parser exceptions and consumes tokens until the follow-set is seen.");
- }
-
-
- // Dump the follow set
- // Doesn't seem to work for lexical rules...
- if (!doingLexRules) {
- println("The follow set for this rule is:");
- tabs++;
- genFollowSetForRuleBlock(rblk);
- tabs--;
- }
-
- tabs--;
- println("*** End " + ruleType + " Rule: " + s.getId());
- }
- /** Generate the syntactic predicate. This basically generates
- * the alternative block, buts tracks if we are inside a synPred
- * @param blk The syntactic predicate block
- */
- protected void genSynPred(SynPredBlock blk) {
- syntacticPredLevel++;
- gen((AlternativeBlock)blk);
- syntacticPredLevel--;
- }
- /** Generate the token types TXT file */
- protected void genTokenTypes(TokenManager tm) throws IOException {
- // Open the token output TXT file and set the currentOutput stream
- System.out.println("Generating " + tm.getName() + TokenTypesFileSuffix+TokenTypesFileExt);
- currentOutput = antlr.Tool.openOutputFile(tm.getName() + TokenTypesFileSuffix+TokenTypesFileExt);
- //SAS: changed for proper text file io
- tabs = 0;
-
- // Generate the header common to all diagnostic files
- genHeader();
-
- // Generate a string for each token. This creates a static
- // array of Strings indexed by token type.
- println("");
- println("*** Tokens used by the parser");
- println("This is a list of the token numeric values and the corresponding");
- println("token identifiers. Some tokens are literals, and because of that");
- println("they have no identifiers. Literals are double-quoted.");
- tabs++;
-
- // Enumerate all the valid token types
- Vector v = tm.getVocabulary();
- for (int i = Token.MIN_USER_TYPE; i < v.size(); i++) {
- String s = (String)v.elementAt(i);
- if (s != null) {
- println(s + " = " + i);
- }
- }
-
- // Close the interface
- tabs--;
- println("*** End of tokens used by the parser");
-
- // Close the tokens output file
- currentOutput.close();
- currentOutput = null;
- }
- /** Get a string for an expression to generate creation of an AST subtree.
- * @param v A Vector of String, where each element is an expression in the target language yielding an AST node.
- */
- public String getASTCreateString(Vector v) {
- return "***Create an AST from a vector here***"+System.getProperty("line.separator");
- }
- /** Get a string for an expression to generate creating of an AST node
- * @param str The arguments to the AST constructor
- */
- public String getASTCreateString(GrammarAtom atom, String str) {
- return "[" + str + "]";
- }
- /** Map an identifier to it's corresponding tree-node variable.
- * This is context-sensitive, depending on the rule and alternative
- * being generated
- * @param id The identifier name to map
- * @param forInput true if the input tree node variable is to be returned, otherwise the output variable is returned.
- */
- public String mapTreeId(String id, ActionTransInfo tInfo) {
- return id;
- }
- /** Format a lookahead or follow set.
- * @param depth The depth of the entire lookahead/follow
- * @param k The lookahead level to print
- * @param lookahead The lookahead/follow set to print
- */
- public void printSet(int depth, int k, Lookahead lookahead) {
- int numCols = 5;
-
- int[] elems = lookahead.fset.toArray();
-
- if (depth != 1) {
- print("k==" + k + ": {");
- } else {
- print("{ ");
- }
- if (elems.length > numCols) {
- _println("");
- tabs++;
- print("");
- }
-
- int column = 0;
- for (int i = 0; i < elems.length; i++)
- {
- column++;
- if (column > numCols) {
- _println("");
- print("");
- column = 0;
- }
- if (doingLexRules) {
- _print(charFormatter.literalChar(elems[i]));
- } else {
- _print((String)grammar.tokenManager.getVocabulary().elementAt(elems[i]));
- }
- if (i != elems.length-1) {
- _print(", ");
- }
- }
-
- if (elems.length > numCols) {
- _println("");
- tabs--;
- print("");
- }
- _println(" }");
- }
+ /** Create a Diagnostic code-generator using the given Grammar
+ * The caller must still call setTool, setBehavior, and setAnalyzer
+ * before generating code.
+ */
+ public DiagnosticCodeGenerator() {
+ super();
+ charFormatter = new JavaCharFormatter();
+ }
+
+ /**Generate the parser, lexer, and token types documentation */
+ public void gen() {
+
+ // Do the code generation
+ try {
+ // Loop over all grammars
+ Enumeration grammarIter = behavior.grammars.elements();
+ while (grammarIter.hasMoreElements()) {
+ Grammar g = (Grammar)grammarIter.nextElement();
+
+ // Connect all the components to each other
+ g.setGrammarAnalyzer(analyzer);
+ g.setCodeGenerator(this);
+ analyzer.setGrammar(g);
+
+ // To get right overloading behavior across hetrogeneous grammars
+ g.generate();
+
+ if (tool.hasError) {
+ System.out.println("Exiting due to errors.");
+ System.exit(1);
+ }
+
+ }
+
+ // Loop over all token managers (some of which are lexers)
+ Enumeration tmIter = behavior.tokenManagers.elements();
+ while (tmIter.hasMoreElements()) {
+ TokenManager tm = (TokenManager)tmIter.nextElement();
+ if (!tm.isReadOnly()) {
+ // Write the token manager tokens as Java
+ genTokenTypes(tm);
+ }
+ }
+ } catch (IOException e) {
+ System.out.println(e.getMessage());
+ }
+ }
+
+ /** Generate code for the given grammar element.
+ * @param blk The {...} action to generate
+ */
+ public void gen(ActionElement action) {
+ if (action.isSemPred) {
+ // handled elsewhere
+ } else {
+ print("ACTION: ");
+ _printAction(action.actionText);
+ }
+ }
+
+ /** Generate code for the given grammar element.
+ * @param blk The "x|y|z|..." block to generate
+ */
+ public void gen(AlternativeBlock blk) {
+ println("Start of alternative block.");
+ tabs++;
+ genBlockPreamble(blk);
+
+ boolean ok = grammar.theLLkAnalyzer.deterministic(blk);
+ if (!ok) {
+ println("Warning: This alternative block is non-deterministic");
+ }
+ genCommonBlock(blk);
+ tabs--;
+ }
+
+ /** Generate code for the given grammar element.
+ * @param blk The block-end element to generate. Block-end
+ * elements are synthesized by the grammar parser to represent
+ * the end of a block.
+ */
+ public void gen(BlockEndElement end) {
+ // no-op
+ }
+
+ /** Generate code for the given grammar element.
+ * @param blk The character literal reference to generate
+ */
+ public void gen(CharLiteralElement atom) {
+ print("Match character ");
+ if (atom.not) {
+ _print("NOT ");
+ }
+ _print(atom.atomText);
+ if (atom.label != null) {
+ _print(", label=" + atom.label);
+ }
+ _println("");
+ }
+
+ /** Generate code for the given grammar element.
+ * @param blk The character-range reference to generate
+ */
+ public void gen(CharRangeElement r) {
+ print("Match character range: " + r.beginText + ".." + r.endText);
+ if (r.label != null) {
+ _print(", label = " + r.label);
+ }
+ _println("");
+ }
+
+ /** Generate the lexer TXT file */
+ public void gen(LexerGrammar g) throws IOException {
+ setGrammar(g);
+ System.out.println("Generating " + grammar.getClassName() + TokenTypesFileExt);
+ currentOutput = antlr.Tool.openOutputFile(grammar.getClassName() + TokenTypesFileExt);
+ //SAS: changed for proper text file io
+
+ tabs = 0;
+ doingLexRules = true;
+
+ // Generate header common to all TXT output files
+ genHeader();
+
+ // Output the user-defined lexer premamble
+ println("");
+ println("*** Lexer Preamble Action.");
+ println("This action will appear before the declaration of your lexer class:");
+ tabs++;
+ println(grammar.preambleAction.getText());
+ tabs--;
+ println("*** End of Lexer Preamble Action");
+
+ // Generate lexer class definition
+ println("");
+ println("*** Your lexer class is called '" + grammar.getClassName() + "' and is a subclass of '" + grammar.getSuperClass() + "'.");
+
+ // Generate user-defined parser class members
+ println("");
+ println("*** User-defined lexer class members:");
+ println("These are the member declarations that you defined for your class:");
+ tabs++;
+ printAction(grammar.classMemberAction.getText());
+ tabs--;
+ println("*** End of user-defined lexer class members");
+
+ // Generate string literals
+ println("");
+ println("*** String literals used in the parser");
+ println("The following string literals were used in the parser.");
+ println("An actual code generator would arrange to place these literals");
+ println("into a table in the generated lexer, so that actions in the");
+ println("generated lexer could match token text against the literals.");
+ println("String literals used in the lexer are not listed here, as they");
+ println("are incorporated into the mainstream lexer processing.");
+ tabs++;
+ // Enumerate all of the symbols and look for string literal symbols
+ Enumeration ids = grammar.getSymbols();
+ while (ids.hasMoreElements()) {
+ GrammarSymbol sym = (GrammarSymbol)ids.nextElement();
+ // Only processing string literals -- reject other symbol entries
+ if (sym instanceof StringLiteralSymbol) {
+ StringLiteralSymbol s = (StringLiteralSymbol)sym;
+ println(s.getId() + " = " + s.getTokenType());
+ }
+ }
+ tabs--;
+ println("*** End of string literals used by the parser");
+
+ // Generate nextToken() rule.
+ // nextToken() is a synthetic lexer rule that is the implicit OR of all
+ // user-defined lexer rules.
+ genNextToken();
+
+ // Generate code for each rule in the lexer
+ println("");
+ println("*** User-defined Lexer rules:");
+ tabs++;
+
+ ids = grammar.rules.elements();
+ while (ids.hasMoreElements()) {
+ RuleSymbol rs = (RuleSymbol)ids.nextElement();
+ if (!rs.id.equals("mnextToken")) {
+ genRule(rs);
+ }
+ }
+
+ tabs--;
+ println("");
+ println("*** End User-defined Lexer rules:");
+
+ // Close the lexer output file
+ currentOutput.close();
+ currentOutput = null;
+ doingLexRules = false;
+ }
+
+ /** Generate code for the given grammar element.
+ * @param blk The (...)+ block to generate
+ */
+ public void gen(OneOrMoreBlock blk) {
+ println("Start ONE-OR-MORE (...)+ block:");
+ tabs++;
+ genBlockPreamble(blk);
+ boolean ok = grammar.theLLkAnalyzer.deterministic(blk);
+ if (!ok) {
+ println("Warning: This one-or-more block is non-deterministic");
+ }
+ genCommonBlock(blk);
+ tabs--;
+ println("End ONE-OR-MORE block.");
+ }
+
+ /** Generate the parser TXT file */
+ public void gen(ParserGrammar g) throws IOException {
+ setGrammar(g);
+ // Open the output stream for the parser and set the currentOutput
+ System.out.println("Generating " + grammar.getClassName() + TokenTypesFileExt);
+ currentOutput = antlr.Tool.openOutputFile(grammar.getClassName() + TokenTypesFileExt);
+ //SAS: changed for proper text file io
+
+ tabs = 0;
+
+ // Generate the header common to all output files.
+ genHeader();
+
+ // Output the user-defined parser premamble
+ println("");
+ println("*** Parser Preamble Action.");
+ println("This action will appear before the declaration of your parser class:");
+ tabs++;
+ println(grammar.preambleAction.getText());
+ tabs--;
+ println("*** End of Parser Preamble Action");
+
+ // Generate parser class definition
+ println("");
+ println("*** Your parser class is called '" + grammar.getClassName() + "' and is a subclass of '" + grammar.getSuperClass() + "'.");
+
+ // Generate user-defined parser class members
+ println("");
+ println("*** User-defined parser class members:");
+ println("These are the member declarations that you defined for your class:");
+ tabs++;
+ printAction(grammar.classMemberAction.getText());
+ tabs--;
+ println("*** End of user-defined parser class members");
+
+ // Generate code for each rule in the grammar
+ println("");
+ println("*** Parser rules:");
+ tabs++;
+
+ // Enumerate the parser rules
+ Enumeration rules = grammar.rules.elements();
+ while (rules.hasMoreElements()) {
+ println("");
+ // Get the rules from the list and downcast it to proper type
+ GrammarSymbol sym = (GrammarSymbol)rules.nextElement();
+ // Only process parser rules
+ if (sym instanceof RuleSymbol) {
+ genRule((RuleSymbol)sym);
+ }
+ }
+ tabs--;
+ println("");
+ println("*** End of parser rules");
+
+ println("");
+ println("*** End of parser");
+
+ // Close the parser output stream
+ currentOutput.close();
+ currentOutput = null;
+ }
+
+ /** Generate code for the given grammar element.
+ * @param blk The rule-reference to generate
+ */
+ public void gen(RuleRefElement rr) {
+ RuleSymbol rs = (RuleSymbol)grammar.getSymbol(rr.targetRule);
+
+ // Generate the actual rule description
+ print("Rule Reference: " + rr.targetRule);
+ if (rr.idAssign != null) {
+ _print(", assigned to '" + rr.idAssign + "'");
+ }
+ if (rr.args != null) {
+ _print(", arguments = " + rr.args);
+ }
+ _println("");
+
+ // Perform diagnostics
+ if (rs == null || !rs.isDefined()) {
+ println("Rule '" + rr.targetRule + "' is referenced, but that rule is not defined.");
+ println("\tPerhaps the rule is misspelled, or you forgot to define it.");
+ return;
+ }
+ if (!(rs instanceof RuleSymbol)) {
+ // Should this ever happen??
+ println("Rule '" + rr.targetRule + "' is referenced, but that is not a grammar rule.");
+ return;
+ }
+ if (rr.idAssign != null) {
+ // Warn if the rule has no return type
+ if (rs.block.returnAction == null) {
+ println("Error: You assigned from Rule '" + rr.targetRule + "', but that rule has no return type.");
+ }
+ } else {
+ // Warn about return value if any, but not inside syntactic predicate
+ if (!(grammar instanceof LexerGrammar) && syntacticPredLevel == 0 && rs.block.returnAction != null) {
+ println("Warning: Rule '" + rr.targetRule + "' returns a value");
+ }
+ }
+ if (rr.args != null && rs.block.argAction == null) {
+ println("Error: Rule '" + rr.targetRule + "' accepts no arguments.");
+ }
+ }
+
+ /** Generate code for the given grammar element.
+ * @param blk The string-literal reference to generate
+ */
+ public void gen(StringLiteralElement atom) {
+ print("Match string literal ");
+ _print(atom.atomText);
+ if (atom.label != null) {
+ _print(", label=" + atom.label);
+ }
+ _println("");
+ }
+
+ /** Generate code for the given grammar element.
+ * @param blk The token-range reference to generate
+ */
+ public void gen(TokenRangeElement r) {
+ print("Match token range: " + r.beginText + ".." + r.endText);
+ if (r.label != null) {
+ _print(", label = " + r.label);
+ }
+ _println("");
+ }
+
+ /** Generate code for the given grammar element.
+ * @param blk The token-reference to generate
+ */
+ public void gen(TokenRefElement atom) {
+ print("Match token ");
+ if (atom.not) {
+ _print("NOT ");
+ }
+ _print(atom.atomText);
+ if (atom.label != null) {
+ _print(", label=" + atom.label);
+ }
+ _println("");
+ }
+
+ public void gen(TreeElement t) {
+ print("Tree reference: " + t);
+ }
+
+ /** Generate the tree-walker TXT file */
+ public void gen(TreeWalkerGrammar g) throws IOException {
+ setGrammar(g);
+ // Open the output stream for the parser and set the currentOutput
+ System.out.println("Generating " + grammar.getClassName() + TokenTypesFileExt);
+ currentOutput = antlr.Tool.openOutputFile(grammar.getClassName() + TokenTypesFileExt);
+ //SAS: changed for proper text file io
+
+ tabs = 0;
+
+ // Generate the header common to all output files.
+ genHeader();
+
+ // Output the user-defined parser premamble
+ println("");
+ println("*** Tree-walker Preamble Action.");
+ println("This action will appear before the declaration of your tree-walker class:");
+ tabs++;
+ println(grammar.preambleAction.getText());
+ tabs--;
+ println("*** End of tree-walker Preamble Action");
+
+ // Generate tree-walker class definition
+ println("");
+ println("*** Your tree-walker class is called '" + grammar.getClassName() + "' and is a subclass of '" + grammar.getSuperClass() + "'.");
+
+ // Generate user-defined tree-walker class members
+ println("");
+ println("*** User-defined tree-walker class members:");
+ println("These are the member declarations that you defined for your class:");
+ tabs++;
+ printAction(grammar.classMemberAction.getText());
+ tabs--;
+ println("*** End of user-defined tree-walker class members");
+
+ // Generate code for each rule in the grammar
+ println("");
+ println("*** tree-walker rules:");
+ tabs++;
+
+ // Enumerate the tree-walker rules
+ Enumeration rules = grammar.rules.elements();
+ while (rules.hasMoreElements()) {
+ println("");
+ // Get the rules from the list and downcast it to proper type
+ GrammarSymbol sym = (GrammarSymbol)rules.nextElement();
+ // Only process tree-walker rules
+ if (sym instanceof RuleSymbol) {
+ genRule((RuleSymbol)sym);
+ }
+ }
+ tabs--;
+ println("");
+ println("*** End of tree-walker rules");
+
+ println("");
+ println("*** End of tree-walker");
+
+ // Close the tree-walker output stream
+ currentOutput.close();
+ currentOutput = null;
+ }
+
+ /** Generate a wildcard element */
+ public void gen(WildcardElement wc) {
+ print("Match wildcard");
+ if (wc.getLabel() != null) {
+ _print(", label = " + wc.getLabel());
+ }
+ _println("");
+ }
+
+ /** Generate code for the given grammar element.
+ * @param blk The (...)* block to generate
+ */
+ public void gen(ZeroOrMoreBlock blk) {
+ println("Start ZERO-OR-MORE (...)+ block:");
+ tabs++;
+ genBlockPreamble(blk);
+ boolean ok = grammar.theLLkAnalyzer.deterministic(blk);
+ if (!ok) {
+ println("Warning: This zero-or-more block is non-deterministic");
+ }
+ genCommonBlock(blk);
+ tabs--;
+ println("End ZERO-OR-MORE block.");
+ }
+
+ protected void genAlt(Alternative alt) {
+ for (
+ AlternativeElement elem = alt.head;
+ !(elem instanceof BlockEndElement);
+ elem = elem.next
+ ) {
+ elem.generate();
+ }
+ if (alt.getTreeSpecifier() != null) {
+ println("AST will be built as: " + alt.getTreeSpecifier().getText());
+ }
+ }
+
+ /** Generate the header for a block, which may be a RuleBlock or a
+ * plain AlternativeBLock. This generates any variable declarations,
+ * init-actions, and syntactic-predicate-testing variables.
+ * @blk The block for which the preamble is to be generated.
+ */
+ protected void genBlockPreamble(AlternativeBlock blk) {
+ // dump out init action
+ if (blk.initAction != null) {
+ printAction("Init action: " + blk.initAction);
+ }
+ }
+
+ /**Generate common code for a block of alternatives; return a postscript
+ * that needs to be generated at the end of the block. Other routines
+ * may append else-clauses and such for error checking before the postfix
+ * is generated.
+ */
+ public void genCommonBlock(AlternativeBlock blk) {
+ boolean singleAlt = (blk.alternatives.size() == 1);
+
+ println("Start of an alternative block.");
+ tabs++;
+ println("The lookahead set for this block is:");
+ tabs++;
+ genLookaheadSetForBlock(blk);
+ tabs--;
+
+ if (singleAlt) {
+ println("This block has a single alternative");
+ if (blk.getAlternativeAt(0).synPred != null) {
+ // Generate a warning if there is one alt and it has a synPred
+ println("Warning: you specified a syntactic predicate for this alternative,");
+ println("and it is the only alternative of a block and will be ignored.");
+ }
+ } else {
+ println("This block has multiple alternatives:");
+ tabs++;
+ }
+
+ for (int i = 0; i < blk.alternatives.size(); i++) {
+ Alternative alt = blk.getAlternativeAt(i);
+ AlternativeElement elem = alt.head;
+
+ // Print lookahead set for alternate
+ println("");
+ if (i != 0) {
+ print("Otherwise, ");
+ } else {
+ print("");
+ }
+ _println("Alternate(" + (i + 1) + ") will be taken IF:");
+ println("The lookahead set: ");
+ tabs++;
+ genLookaheadSetForAlt(alt);
+ tabs--;
+ if (alt.semPred != null || alt.synPred != null) {
+ print("is matched, AND ");
+ } else {
+ println("is matched.");
+ }
+
+ // Dump semantic predicates
+ if (alt.semPred != null) {
+ _println("the semantic predicate:");
+ tabs++;
+ println(alt.semPred);
+ if (alt.synPred != null) {
+ print("is true, AND ");
+ } else {
+ println("is true.");
+ }
+ }
+
+ // Dump syntactic predicate
+ if (alt.synPred != null) {
+ _println("the syntactic predicate:");
+ tabs++;
+ genSynPred(alt.synPred);
+ tabs--;
+ println("is matched.");
+ }
+
+ // Dump the alternative
+ genAlt(alt);
+ }
+ println("");
+ println("OTHERWISE, a NoViableAlt exception will be thrown");
+ println("");
+
+ if (!singleAlt) {
+ tabs--;
+ println("End of alternatives");
+ }
+ tabs--;
+ println("End of alternative block.");
+ }
+
+ /** Generate a textual representation of the follow set
+ * for a block.
+ * @param blk The rule block of interest
+ */
+ public void genFollowSetForRuleBlock(RuleBlock blk) {
+ Lookahead follow = grammar.theLLkAnalyzer.FOLLOW(1, blk.endNode);
+ printSet(grammar.maxk, 1, follow);
+ }
+
+ /** Generate a header that is common to all TXT files */
+ protected void genHeader() {
+ println("ANTLR-generated file resulting from grammar " + tool.grammarFile);
+ println("Diagnostic output");
+ println("");
+ println("Terence Parr, MageLang Institute");
+ println("with John Lilley, Empathy Software");
+ println("ANTLR Version " + Tool.version + "; 1996,1997");
+ println("");
+ println("*** Header Action.");
+ println("This action will appear at the top of all generated files.");
+ tabs++;
+ printAction(behavior.getHeaderAction(""));
+ tabs--;
+ println("*** End of Header Action");
+ println("");
+ }
+
+ /**Generate the lookahead set for an alternate. */
+ protected void genLookaheadSetForAlt(Alternative alt) {
+ if (doingLexRules && alt.cache[1].containsEpsilon()) {
+ println("MATCHES ALL");
+ return;
+ }
+ int depth = alt.lookaheadDepth;
+ if (depth == GrammarAnalyzer.NONDETERMINISTIC) {
+ // if the decision is nondeterministic, do the best we can: LL(k)
+ // any predicates that are around will be generated later.
+ depth = grammar.maxk;
+ }
+ for (int i = 1; i <= depth; i++) {
+ Lookahead lookahead = alt.cache[i];
+ printSet(depth, i, lookahead);
+ }
+ }
+
+ /** Generate a textual representation of the lookahead set
+ * for a block.
+ * @param blk The block of interest
+ */
+ public void genLookaheadSetForBlock(AlternativeBlock blk) {
+ // Find the maximal lookahead depth over all alternatives
+ int depth = 0;
+ for (int i = 0; i < blk.alternatives.size(); i++) {
+ Alternative alt = blk.getAlternativeAt(i);
+ if (alt.lookaheadDepth == GrammarAnalyzer.NONDETERMINISTIC) {
+ depth = grammar.maxk;
+ break;
+ } else if (depth < alt.lookaheadDepth) {
+ depth = alt.lookaheadDepth;
+ }
+ }
+
+ for (int i = 1; i <= depth; i++) {
+ Lookahead lookahead = grammar.theLLkAnalyzer.look(i, blk);
+ printSet(depth, i, lookahead);
+ }
+ }
+
+ /** Generate the nextToken rule.
+ * nextToken is a synthetic lexer rule that is the implicit OR of all
+ * user-defined lexer rules.
+ */
+ public void genNextToken() {
+ println("");
+ println("*** Lexer nextToken rule:");
+ println("The lexer nextToken rule is synthesized from all of the user-defined");
+ println("lexer rules. It logically consists of one big alternative block with");
+ println("each user-defined rule being an alternative.");
+ println("");
+
+ // Create the synthesized rule block for nextToken consisting
+ // of an alternate block containing all the user-defined lexer rules.
+ RuleBlock blk = MakeGrammar.createNextTokenRule(grammar, grammar.rules, "nextToken");
+
+ // Define the nextToken rule symbol
+ RuleSymbol nextTokenRs = new RuleSymbol("mnextToken");
+ nextTokenRs.setDefined();
+ nextTokenRs.setBlock(blk);
+ nextTokenRs.access = "private";
+ grammar.define(nextTokenRs);
+
+ // Analyze the synthesized block
+ if (!grammar.theLLkAnalyzer.deterministic(blk)) {
+ println("The grammar analyzer has determined that the synthesized");
+ println("nextToken rule is non-deterministic (i.e., it has ambiguities)");
+ println("This means that there is some overlap of the character");
+ println("lookahead for two or more of your lexer rules.");
+ }
+
+ genCommonBlock(blk);
+
+ println("*** End of nextToken lexer rule.");
+ }
+
+ /** Generate code for a named rule block
+ * @param s The RuleSymbol describing the rule to generate
+ */
+ public void genRule(RuleSymbol s) {
+ println("");
+ String ruleType = (doingLexRules ? "Lexer" : "Parser");
+ println("*** " + ruleType + " Rule: " + s.getId());
+ if (!s.isDefined()) {
+ println("This rule is undefined.");
+ println("This means that the rule was referenced somewhere in the grammar,");
+ println("but a definition for the rule was not encountered.");
+ println("It is also possible that syntax errors during the parse of");
+ println("your grammar file prevented correct processing of the rule.");
+ println("*** End " + ruleType + " Rule: " + s.getId());
+ return;
+ }
+ tabs++;
+
+ if (s.access.length() != 0) {
+ println("Access: " + s.access);
+ }
+
+ // Get rule return type and arguments
+ RuleBlock rblk = s.getBlock();
+
+ // Gen method return value(s)
+ if (rblk.returnAction != null) {
+ println("Return value(s): " + rblk.returnAction);
+ if (doingLexRules) {
+ println("Error: you specified return value(s) for a lexical rule.");
+ println("\tLexical rules have an implicit return type of 'int'.");
+ }
+ } else {
+ if (doingLexRules) {
+ println("Return value: lexical rule returns an implicit token type");
+ } else {
+ println("Return value: none");
+ }
+ }
+
+ // Gen arguments
+ if (rblk.argAction != null) {
+ println("Arguments: " + rblk.argAction);
+ }
+
+ // Dump any init-action
+ genBlockPreamble(rblk);
+
+ // Analyze the rule
+ boolean ok = grammar.theLLkAnalyzer.deterministic(rblk);
+ if (!ok) {
+ println("Error: This rule is non-deterministic");
+ }
+
+ // Dump the alternates of the rule
+ genCommonBlock(rblk);
+
+ // Search for an unlabeled exception specification attached to the rule
+ ExceptionSpec unlabeledUserSpec = rblk.findExceptionSpec("");
+
+ // Generate user-defined or default catch phrases
+ if (unlabeledUserSpec != null) {
+ println("You specified error-handler(s) for this rule:");
+ tabs++;
+ for (int i = 0; i < unlabeledUserSpec.handlers.size(); i++) {
+ if (i != 0) {
+ println("");
+ }
+
+ ExceptionHandler handler = (ExceptionHandler)unlabeledUserSpec.handlers.elementAt(i);
+ println("Error-handler(" + (i + 1) + ") catches [" + handler.exceptionTypeAndName.getText() + "] and executes:");
+ printAction(handler.action.getText());
+ }
+ tabs--;
+ println("End error-handlers.");
+ } else if (!doingLexRules) {
+ println("Default error-handling will be generated, which catches all");
+ println("parser exceptions and consumes tokens until the follow-set is seen.");
+ }
+
+
+ // Dump the follow set
+ // Doesn't seem to work for lexical rules...
+ if (!doingLexRules) {
+ println("The follow set for this rule is:");
+ tabs++;
+ genFollowSetForRuleBlock(rblk);
+ tabs--;
+ }
+
+ tabs--;
+ println("*** End " + ruleType + " Rule: " + s.getId());
+ }
+
+ /** Generate the syntactic predicate. This basically generates
+ * the alternative block, buts tracks if we are inside a synPred
+ * @param blk The syntactic predicate block
+ */
+ protected void genSynPred(SynPredBlock blk) {
+ syntacticPredLevel++;
+ gen((AlternativeBlock)blk);
+ syntacticPredLevel--;
+ }
+
+ /** Generate the token types TXT file */
+ protected void genTokenTypes(TokenManager tm) throws IOException {
+ // Open the token output TXT file and set the currentOutput stream
+ System.out.println("Generating " + tm.getName() + TokenTypesFileSuffix + TokenTypesFileExt);
+ currentOutput = antlr.Tool.openOutputFile(tm.getName() + TokenTypesFileSuffix + TokenTypesFileExt);
+ //SAS: changed for proper text file io
+ tabs = 0;
+
+ // Generate the header common to all diagnostic files
+ genHeader();
+
+ // Generate a string for each token. This creates a static
+ // array of Strings indexed by token type.
+ println("");
+ println("*** Tokens used by the parser");
+ println("This is a list of the token numeric values and the corresponding");
+ println("token identifiers. Some tokens are literals, and because of that");
+ println("they have no identifiers. Literals are double-quoted.");
+ tabs++;
+
+ // Enumerate all the valid token types
+ Vector v = tm.getVocabulary();
+ for (int i = Token.MIN_USER_TYPE; i < v.size(); i++) {
+ String s = (String)v.elementAt(i);
+ if (s != null) {
+ println(s + " = " + i);
+ }
+ }
+
+ // Close the interface
+ tabs--;
+ println("*** End of tokens used by the parser");
+
+ // Close the tokens output file
+ currentOutput.close();
+ currentOutput = null;
+ }
+
+ /** Get a string for an expression to generate creation of an AST subtree.
+ * @param v A Vector of String, where each element is an expression in the target language yielding an AST node.
+ */
+ public String getASTCreateString(Vector v) {
+ return "***Create an AST from a vector here***" + System.getProperty("line.separator");
+ }
+
+ /** Get a string for an expression to generate creating of an AST node
+ * @param str The arguments to the AST constructor
+ */
+ public String getASTCreateString(GrammarAtom atom, String str) {
+ return "[" + str + "]";
+ }
+
+ /** Map an identifier to it's corresponding tree-node variable.
+ * This is context-sensitive, depending on the rule and alternative
+ * being generated
+ * @param id The identifier name to map
+ * @param forInput true if the input tree node variable is to be returned, otherwise the output variable is returned.
+ */
+ public String mapTreeId(String id, ActionTransInfo tInfo) {
+ return id;
+ }
+
+ /** Format a lookahead or follow set.
+ * @param depth The depth of the entire lookahead/follow
+ * @param k The lookahead level to print
+ * @param lookahead The lookahead/follow set to print
+ */
+ public void printSet(int depth, int k, Lookahead lookahead) {
+ int numCols = 5;
+
+ int[] elems = lookahead.fset.toArray();
+
+ if (depth != 1) {
+ print("k==" + k + ": {");
+ } else {
+ print("{ ");
+ }
+ if (elems.length > numCols) {
+ _println("");
+ tabs++;
+ print("");
+ }
+
+ int column = 0;
+ for (int i = 0; i < elems.length; i++) {
+ column++;
+ if (column > numCols) {
+ _println("");
+ print("");
+ column = 0;
+ }
+ if (doingLexRules) {
+ _print(charFormatter.literalChar(elems[i]));
+ } else {
+ _print((String)grammar.tokenManager.getVocabulary().elementAt(elems[i]));
+ }
+ if (i != elems.length - 1) {
+ _print(", ");
+ }
+ }
+
+ if (elems.length > numCols) {
+ _println("");
+ tabs--;
+ print("");
+ }
+ _println(" }");
+ }
}
1.2 +42 -44 e/src/jsrc/antlr/DumpASTVisitor.java
Index: DumpASTVisitor.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/DumpASTVisitor.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- DumpASTVisitor.java 2001/10/03 21:04:13 1.1
+++ DumpASTVisitor.java 2001/10/04 01:09:14 1.2
@@ -4,63 +4,61 @@
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
- * $Id: DumpASTVisitor.java,v 1.1 2001/10/03 21:04:13 markm Exp $
+ * $Id: DumpASTVisitor.java,v 1.2 2001/10/04 01:09:14 markm Exp $
*/
-import java.io.*;
import antlr.collections.AST;
/** Simple class to dump the contents of an AST to the output */
public class DumpASTVisitor implements ASTVisitor {
+
protected int level = 0;
private void tabs() {
- for (int i = 0; i < level; i++) {
- System.out.print(" ");
- }
+ for (int i = 0; i < level; i++) {
+ System.out.print(" ");
+ }
}
public void visit(AST node) {
- // Flatten this level of the tree if it has no children
- boolean flatten = /*true*/ false;
- AST node2;
- for (node2 = node; node2 != null ; node2 = node2.getNextSibling()) {
- if (node2.getFirstChild() != null) {
- flatten = false;
- break;
- }
- }
-
- for (node2 = node; node2 != null; node2 = node2.getNextSibling()) {
- if (!flatten || node2 == node) {
- tabs();
- }
- if ( node2.getText()==null ) {
- System.out.print("nil");
- }
- else {
- System.out.print(node2.getText());
- }
-
- System.out.print(" [" + node2.getType() + "] ");
-
- if (flatten) {
- System.out.print(" ");
- }
- else {
- System.out.println("");
- }
-
- if ( node2.getFirstChild() != null ) {
- level++;
- visit(node2.getFirstChild());
- level--;
- }
- }
-
- if (flatten) {
- System.out.println("");
- }
+ // Flatten this level of the tree if it has no children
+ boolean flatten = /*true*/ false;
+ AST node2;
+ for (node2 = node; node2 != null; node2 = node2.getNextSibling()) {
+ if (node2.getFirstChild() != null) {
+ flatten = false;
+ break;
+ }
+ }
+
+ for (node2 = node; node2 != null; node2 = node2.getNextSibling()) {
+ if (!flatten || node2 == node) {
+ tabs();
+ }
+ if (node2.getText() == null) {
+ System.out.print("nil");
+ } else {
+ System.out.print(node2.getText());
+ }
+
+ System.out.print(" [" + node2.getType() + "] ");
+
+ if (flatten) {
+ System.out.print(" ");
+ } else {
+ System.out.println("");
+ }
+
+ if (node2.getFirstChild() != null) {
+ level++;
+ visit(node2.getFirstChild());
+ level--;
+ }
+ }
+
+ if (flatten) {
+ System.out.println("");
+ }
}
}
1.2 +10 -9 e/src/jsrc/antlr/ExceptionHandler.java
Index: ExceptionHandler.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/ExceptionHandler.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ExceptionHandler.java 2001/10/03 21:04:13 1.1
+++ ExceptionHandler.java 2001/10/04 01:09:14 1.2
@@ -4,19 +4,20 @@
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
- * $Id: ExceptionHandler.java,v 1.1 2001/10/03 21:04:13 markm Exp $
+ * $Id: ExceptionHandler.java,v 1.2 2001/10/04 01:09:14 markm Exp $
*/
class ExceptionHandler {
- // Type of the ANTLR exception class to catch and the variable decl
- protected Token exceptionTypeAndName;
- // The action to be executed when the exception is caught
- protected Token action;
+ // Type of the ANTLR exception class to catch and the variable decl
+ protected Token exceptionTypeAndName;
+ // The action to be executed when the exception is caught
+ protected Token action;
- public ExceptionHandler(Token exceptionTypeAndName_,
- Token action_) {
- exceptionTypeAndName = exceptionTypeAndName_;
- action = action_;
- }
+
+ public ExceptionHandler(Token exceptionTypeAndName_,
+ Token action_) {
+ exceptionTypeAndName = exceptionTypeAndName_;
+ action = action_;
+ }
}
1.2 +15 -14 e/src/jsrc/antlr/ExceptionSpec.java
Index: ExceptionSpec.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/ExceptionSpec.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ExceptionSpec.java 2001/10/03 21:04:13 1.1
+++ ExceptionSpec.java 2001/10/04 01:09:14 1.2
@@ -4,26 +4,27 @@
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
- * $Id: ExceptionSpec.java,v 1.1 2001/10/03 21:04:13 markm Exp $
+ * $Id: ExceptionSpec.java,v 1.2 2001/10/04 01:09:14 markm Exp $
*/
import antlr.collections.impl.Vector;
-class ExceptionSpec
-{
- // Non-null if this refers to a labeled rule
- // Use a token instead of a string to get the line information
- protected Token label;
-
- // List of ExceptionHandler (catch phrases)
- protected Vector handlers;
-
-
- public ExceptionSpec(Token label_) {
- label = label_;
- handlers = new Vector();
- }
- public void addHandler(ExceptionHandler handler) {
- handlers.appendElement(handler);
- }
+class ExceptionSpec {
+
+ // Non-null if this refers to a labeled rule
+ // Use a token instead of a string to get the line information
+ protected Token label;
+
+ // List of ExceptionHandler (catch phrases)
+ protected Vector handlers;
+
+
+ public ExceptionSpec(Token label_) {
+ label = label_;
+ handlers = new Vector();
+ }
+
+ public void addHandler(ExceptionHandler handler) {
+ handlers.appendElement(handler);
+ }
}
1.2 +5 -2 e/src/jsrc/antlr/FileCopyException.java
Index: FileCopyException.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/FileCopyException.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- FileCopyException.java 2001/10/03 21:04:13 1.1
+++ FileCopyException.java 2001/10/04 01:09:14 1.2
@@ -4,9 +4,12 @@
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
- * $Id: FileCopyException.java,v 1.1 2001/10/03 21:04:13 markm Exp $
+ * $Id: FileCopyException.java,v 1.2 2001/10/04 01:09:14 markm Exp $
*/
-class FileCopyException extends java.io.IOException {
- public FileCopyException(String msg) { super(msg); }
+class FileCopyException extends java.io.IOException {
+
+ public FileCopyException(String msg) {
+ super(msg);
+ }
}
1.2 +6 -6 e/src/jsrc/antlr/FileLineFormatter.java
Index: FileLineFormatter.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/FileLineFormatter.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- FileLineFormatter.java 2001/10/03 21:04:13 1.1
+++ FileLineFormatter.java 2001/10/04 01:09:14 1.2
@@ -4,20 +4,20 @@
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
- * $Id: FileLineFormatter.java,v 1.1 2001/10/03 21:04:13 markm Exp $
+ * $Id: FileLineFormatter.java,v 1.2 2001/10/04 01:09:14 markm Exp $
*/
public abstract class FileLineFormatter {
-
+
private static FileLineFormatter formatter = new DefaultFileLineFormatter();
-
+
public static FileLineFormatter getFormatter() {
- return formatter;
+ return formatter;
}
-
+
public static void setFormatter(FileLineFormatter f) {
- formatter = f;
+ formatter = f;
}
-
+
public abstract String getFormatString(String fileName, int line);
}
1.2 +147 -135 e/src/jsrc/antlr/Grammar.java
Index: Grammar.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/Grammar.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- Grammar.java 2001/10/03 21:04:13 1.1
+++ Grammar.java 2001/10/04 01:09:14 1.2
@@ -4,43 +4,51 @@
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
- * $Id: Grammar.java,v 1.1 2001/10/03 21:04:13 markm Exp $
+ * $Id: Grammar.java,v 1.2 2001/10/04 01:09:14 markm Exp $
*/
-import java.util.Hashtable;
-import java.util.Enumeration;
-import java.io.IOException;
-import antlr.collections.impl.BitSet;
import antlr.collections.impl.Vector;
+import java.io.IOException;
+import java.util.Enumeration;
+import java.util.Hashtable;
+
/**A Grammar holds a set of rules (which are stored
* in a symbol table). Most of the time a grammar
* needs a code generator and an LLkAnalyzer too.
*/
public abstract class Grammar {
+
protected Tool tool;
+
protected CodeGenerator generator;
+
protected LLkGrammarAnalyzer theLLkAnalyzer;
+
protected Hashtable symbols;
+
protected boolean buildAST = false;
+
protected boolean analyzerDebug = false;
+
protected boolean interactive = false;
+
protected String superClass = null;
/** The token manager associated with the grammar, if any.
- // The token manager is responsible for maintaining the set of valid tokens, and
- // is conceptually shared between the lexer and parser. This may be either a
- // LexerGrammar or a ImportVocabTokenManager.
- */
+ // The token manager is responsible for maintaining the set of valid tokens, and
+ // is conceptually shared between the lexer and parser. This may be either a
+ // LexerGrammar or a ImportVocabTokenManager.
+ */
protected TokenManager tokenManager;
/** The name of the export vocabulary...used to generate the output
- * token types interchange file.
- */
+ * token types interchange file.
+ */
protected String exportVocab = null;
/** The name of the import vocabulary. "Initial conditions"
- */
+ */
protected String importVocab = null;
// Mapping from String keys to Token option values
@@ -49,66 +57,74 @@
protected Vector rules;
protected Token preambleAction = new CommonToken(Token.INVALID_TYPE, "");
+
protected String className = null;
+
protected String fileName = null;
+
protected Token classMemberAction = new CommonToken(Token.INVALID_TYPE, "");
+
protected boolean hasSyntacticPredicate = false;
+
protected boolean hasUserErrorHandling = false;
// max lookahead that can be attempted for this parser.
- protected int maxk=1;
+ protected int maxk = 1;
// options
protected boolean traceRules = false;
+
protected boolean debuggingOutput = false;
+
protected boolean defaultErrorHandler = true;
protected String comment = null; // javadoc comment
public Grammar(String className_, Tool tool_, String superClass) {
- className = className_;
- tool = tool_;
- symbols = new Hashtable();
- options = new Hashtable();
- rules = new Vector(100);
- this.superClass = superClass;
+ className = className_;
+ tool = tool_;
+ symbols = new Hashtable();
+ options = new Hashtable();
+ rules = new Vector(100);
+ this.superClass = superClass;
}
/** Define a rule */
public void define(RuleSymbol rs) {
- rules.appendElement(rs);
- // add the symbol to the rules hash table
- symbols.put(rs.getId(), rs);
+ rules.appendElement(rs);
+ // add the symbol to the rules hash table
+ symbols.put(rs.getId(), rs);
}
/** Top-level call to generate the code for this grammar */
public abstract void generate() throws IOException;
- protected String getClassName() { return className; }
+ protected String getClassName() {
+ return className;
+ }
/* Does this grammar have a default error handler? */
public boolean getDefaultErrorHandler() {
- return defaultErrorHandler;
+ return defaultErrorHandler;
}
public String getFilename() {
- return fileName;
+ return fileName;
}
- /** Get an integer option. Given the name of the option find its
+ /** Get an integer option. Given the name of the option find its
* associated integer value. If the associated value is not an integer or
* is not in the table, then throw an exception of type NumberFormatException.
* @param key The name of the option
* @return The value associated with the key.
*/
public int getIntegerOption(String key) throws NumberFormatException {
- Token t = (Token)options.get(key);
- if (t == null || t.getType() != ANTLRTokenTypes.INT) {
- throw new NumberFormatException();
- }
- else {
- return Integer.parseInt(t.getText());
- }
+ Token t = (Token)options.get(key);
+ if (t == null || t.getType() != ANTLRTokenTypes.INT) {
+ throw new NumberFormatException();
+ } else {
+ return Integer.parseInt(t.getText());
+ }
}
/** Get an option. Given the name of the option find its associated value.
@@ -116,152 +132,148 @@
* @return The value associated with the key, or null if the key has not been set.
*/
public Token getOption(String key) {
- return (Token)options.get(key);
+ return (Token)options.get(key);
}
// Get name of class from which generated parser/lexer inherits
protected abstract String getSuperClass();
public GrammarSymbol getSymbol(String s) {
- return (GrammarSymbol) symbols.get(s);
+ return (GrammarSymbol)symbols.get(s);
}
public Enumeration getSymbols() {
- return symbols.elements();
+ return symbols.elements();
}
/** Check the existence of an option in the table
- * @param key The name of the option
- * @return true if the option is in the table
- */
+ * @param key The name of the option
+ * @return true if the option is in the table
+ */
public boolean hasOption(String key) {
- return options.containsKey(key);
+ return options.containsKey(key);
}
/** Is a rule symbol defined? (not used for tokens) */
public boolean isDefined(String s) {
- return symbols.containsKey(s);
+ return symbols.containsKey(s);
}
/**Process command line arguments. Implemented in subclasses */
public abstract void processArguments(String[] args);
public void setCodeGenerator(CodeGenerator gen) {
- generator = gen;
+ generator = gen;
}
public void setFilename(String s) {
- fileName = s;
+ fileName = s;
}
public void setGrammarAnalyzer(LLkGrammarAnalyzer a) {
- theLLkAnalyzer = a;
+ theLLkAnalyzer = a;
}
/** Set a generic option.
- * This associates a generic option key with a Token value.
- * No validation is performed by this method, although users of the value
- * (code generation and/or analysis) may require certain formats.
- * The value is stored as a token so that the location of an error
- * can be reported.
- * @param key The name of the option.
- * @param value The value to associate with the key.
- * @return true if the option was a valid generic grammar option, false o/w
- */
+ * This associates a generic option key with a Token value.
+ * No validation is performed by this method, although users of the value
+ * (code generation and/or analysis) may require certain formats.
+ * The value is stored as a token so that the location of an error
+ * can be reported.
+ * @param key The name of the option.
+ * @param value The value to associate with the key.
+ * @return true if the option was a valid generic grammar option, false o/w
+ */
public boolean setOption(String key, Token value) {
- options.put(key, value);
- String s = value.getText();
- int i;
- if (key.equals("k")) {
- try {
- maxk = getIntegerOption("k");
- //System.out.println("setting lookahead to " + maxk);
- } catch (NumberFormatException e) {
- tool.error("option 'k' must be an integer (was "+value.getText()+")", getFilename(), value.getLine());
- }
- return true;
- }
- if (key.equals("codeGenMakeSwitchThreshold")) {
- try {
- i = getIntegerOption("codeGenMakeSwitchThreshold");
- } catch (NumberFormatException e) {
- tool.error("option 'codeGenMakeSwitchThreshold' must be an integer", getFilename(), value.getLine());
- }
- return true;
- }
- if (key.equals("codeGenBitsetTestThreshold")) {
- try {
- i = getIntegerOption("codeGenBitsetTestThreshold");
- } catch (NumberFormatException e) {
- tool.error("option 'codeGenBitsetTestThreshold' must be an integer", getFilename(), value.getLine());
- }
- return true;
- }
- if (key.equals("defaultErrorHandler")) {
- if (s.equals("true")) {
- defaultErrorHandler = true;
- } else if (s.equals("false")) {
- defaultErrorHandler = false;
- } else {
- tool.error("Value for defaultErrorHandler must be true or false", getFilename(), value.getLine());
- }
- return true;
- }
- if (key.equals("analyzerDebug")) {
- if (s.equals("true")) {
- analyzerDebug = true;
- }
- else if (s.equals("false")) {
- analyzerDebug = false;
- }
- else {
- tool.error("option 'analyzerDebug' must be true or false", getFilename(), value.getLine());
- }
- return true;
- }
- if (key.equals("codeGenDebug")) {
- if (s.equals("true")) {
- analyzerDebug = true;
- }
- else if (s.equals("false")) {
- analyzerDebug = false;
- }
- else {
- tool.error("option 'codeGenDebug' must be true or false", getFilename(), value.getLine());
- }
- return true;
- }
- if (key.equals("classHeaderSuffix")) {
- return true;
- }
- if (key.equals("namespaceAntlr")) {
- return true;
- }
- if (key.equals("namespaceStd")) {
- return true;
- }
- if (key.equals("genHashLines")) {
- return true;
- }
- return false;
+ options.put(key, value);
+ String s = value.getText();
+ int i;
+ if (key.equals("k")) {
+ try {
+ maxk = getIntegerOption("k");
+ //System.out.println("setting lookahead to " + maxk);
+ } catch (NumberFormatException e) {
+ tool.error("option 'k' must be an integer (was " + value.getText() + ")", getFilename(), value.getLine());
+ }
+ return true;
+ }
+ if (key.equals("codeGenMakeSwitchThreshold")) {
+ try {
+ i = getIntegerOption("codeGenMakeSwitchThreshold");
+ } catch (NumberFormatException e) {
+ tool.error("option 'codeGenMakeSwitchThreshold' must be an integer", getFilename(), value.getLine());
+ }
+ return true;
+ }
+ if (key.equals("codeGenBitsetTestThreshold")) {
+ try {
+ i = getIntegerOption("codeGenBitsetTestThreshold");
+ } catch (NumberFormatException e) {
+ tool.error("option 'codeGenBitsetTestThreshold' must be an integer", getFilename(), value.getLine());
+ }
+ return true;
+ }
+ if (key.equals("defaultErrorHandler")) {
+ if (s.equals("true")) {
+ defaultErrorHandler = true;
+ } else if (s.equals("false")) {
+ defaultErrorHandler = false;
+ } else {
+ tool.error("Value for defaultErrorHandler must be true or false", getFilename(), value.getLine());
+ }
+ return true;
+ }
+ if (key.equals("analyzerDebug")) {
+ if (s.equals("true")) {
+ analyzerDebug = true;
+ } else if (s.equals("false")) {
+ analyzerDebug = false;
+ } else {
+ tool.error("option 'analyzerDebug' must be true or false", getFilename(), value.getLine());
+ }
+ return true;
+ }
+ if (key.equals("codeGenDebug")) {
+ if (s.equals("true")) {
+ analyzerDebug = true;
+ } else if (s.equals("false")) {
+ analyzerDebug = false;
+ } else {
+ tool.error("option 'codeGenDebug' must be true or false", getFilename(), value.getLine());
+ }
+ return true;
+ }
+ if (key.equals("classHeaderSuffix")) {
+ return true;
+ }
+ if (key.equals("namespaceAntlr")) {
+ return true;
+ }
+ if (key.equals("namespaceStd")) {
+ return true;
+ }
+ if (key.equals("genHashLines")) {
+ return true;
+ }
+ return false;
}
-
+
public void setTokenManager(TokenManager tokenManager_) {
- tokenManager = tokenManager_;
+ tokenManager = tokenManager_;
}
/** Print out the grammar without actions */
public String toString() {
- StringBuffer buf = new StringBuffer(20000);
+ StringBuffer buf = new StringBuffer(20000);
Enumeration ids = rules.elements();
- while ( ids.hasMoreElements() ) {
+ while (ids.hasMoreElements()) {
RuleSymbol rs = (RuleSymbol)ids.nextElement();
- if (!rs.id.equals("mnextToken")) {
- buf.append(rs.getBlock().toString());
- buf.append("\n\n");
+ if (!rs.id.equals("mnextToken")) {
+ buf.append(rs.getBlock().toString());
+ buf.append("\n\n");
}
}
- return buf.toString();
+ return buf.toString();
}
}
1.2 +10 -8 e/src/jsrc/antlr/GrammarAnalyzer.java
Index: GrammarAnalyzer.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/GrammarAnalyzer.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- GrammarAnalyzer.java 2001/10/03 21:04:13 1.1
+++ GrammarAnalyzer.java 2001/10/04 01:09:14 1.2
@@ -4,7 +4,7 @@
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
- * $Id: GrammarAnalyzer.java,v 1.1 2001/10/03 21:04:13 markm Exp $
+ * $Id: GrammarAnalyzer.java,v 1.2 2001/10/04 01:09:14 markm Exp $
*/
/**A GrammarAnalyzer computes lookahead from Grammar (which contains
@@ -25,12 +25,14 @@
* versus "does this specific k-sequence match?".
*/
public interface GrammarAnalyzer {
- /**The epsilon token type is an imaginary type used
- * during analysis. It indicates an incomplete look() computation.
- * Must be kept consistent with Token constants to be between
- * MIN_USER_TYPE and INVALID_TYPE.
- */
- // public static final int EPSILON_TYPE = 2;
- public static final int NONDETERMINISTIC = Integer.MAX_VALUE; // lookahead depth
- public static final int LOOKAHEAD_DEPTH_INIT = -1;
+
+ /**The epsilon token type is an imaginary type used
+ * during analysis. It indicates an incomplete look() computation.
+ * Must be kept consistent with Token constants to be between
+ * MIN_USER_TYPE and INVALID_TYPE.
+ */
+ // public static final int EPSILON_TYPE = 2;
+ public static final int NONDETERMINISTIC = Integer.MAX_VALUE; // lookahead depth
+
+ public static final int LOOKAHEAD_DEPTH_INIT = -1;
}
1.2 +56 -52 e/src/jsrc/antlr/GrammarAtom.java
Index: GrammarAtom.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/GrammarAtom.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- GrammarAtom.java 2001/10/03 21:04:13 1.1
+++ GrammarAtom.java 2001/10/04 01:09:14 1.2
@@ -4,65 +4,69 @@
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
- * $Id: GrammarAtom.java,v 1.1 2001/10/03 21:04:13 markm Exp $
+ * $Id: GrammarAtom.java,v 1.2 2001/10/04 01:09:14 markm Exp $
*/
/**A GrammarAtom is either a token ref, a character ref, or string.
* The analysis doesn't care.
*/
abstract class GrammarAtom extends AlternativeElement {
- protected String label;
- protected String atomText;
- protected int tokenType = Token.INVALID_TYPE;
- protected boolean not = false; // ~T or ~'c' or ~"foo"
- /** Set to type of AST node to create during parse. Defaults to what is
- * set in the TokenSymbol.
- */
+
+ protected String label;
+
+ protected String atomText;
+
+ protected int tokenType = Token.INVALID_TYPE;
+
+ protected boolean not = false; // ~T or ~'c' or ~"foo"
+
+ /** Set to type of AST node to create during parse. Defaults to what is
+ * set in the TokenSymbol.
+ */
protected String ASTNodeType = null;
- public GrammarAtom(Grammar g, Token t, int autoGenType) {
- super(g, autoGenType);
- atomText = t.getText();
- }
-
- public String getLabel() {
- return label;
- }
-
- public String getText() {
- return atomText;
- }
-
- public int getType() {
- return tokenType;
- }
-
- public void setLabel(String label_) {
- label = label_;
- }
-
- public String getASTNodeType() {
- return ASTNodeType;
- }
-
- public void setASTNodeType(String type) {
- ASTNodeType = type;
- }
-
- public void setOption(Token option, Token value) {
- if ( option.getText().equals("AST") ) {
- setASTNodeType(value.getText());
- }
- else {
- grammar.tool.error("Invalid element option:"+option.getText(),
- grammar.getFilename(), option.getLine());
- }
- }
-
- public String toString() {
- String s = " ";
- if ( label!=null ) s += label+":";
- if ( not ) s += "~";
- return s+atomText;
- }
+ public GrammarAtom(Grammar g, Token t, int autoGenType) {
+ super(g, autoGenType);
+ atomText = t.getText();
+ }
+
+ public String getLabel() {
+ return label;
+ }
+
+ public String getText() {
+ return atomText;
+ }
+
+ public int getType() {
+ return tokenType;
+ }
+
+ public void setLabel(String label_) {
+ label = label_;
+ }
+
+ public String getASTNodeType() {
+ return ASTNodeType;
+ }
+
+ public void setASTNodeType(String type) {
+ ASTNodeType = type;
+ }
+
+ public void setOption(Token option, Token value) {
+ if (option.getText().equals("AST")) {
+ setASTNodeType(value.getText());
+ } else {
+ grammar.tool.error("Invalid element option:" + option.getText(),
+ grammar.getFilename(), option.getLine());
+ }
+ }
+
+ public String toString() {
+ String s = " ";
+ if (label != null) s += label + ":";
+ if (not) s += "~";
+ return s + atomText;
+ }
}
1.2 +37 -25 e/src/jsrc/antlr/GrammarElement.java
Index: GrammarElement.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/GrammarElement.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- GrammarElement.java 2001/10/03 21:04:13 1.1
+++ GrammarElement.java 2001/10/04 01:09:14 1.2
@@ -4,7 +4,7 @@
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
- * $Id: GrammarElement.java,v 1.1 2001/10/03 21:04:13 markm Exp $
+ * $Id: GrammarElement.java,v 1.2 2001/10/04 01:09:14 markm Exp $
*/
/**A GrammarElement is a generic node in our
@@ -15,29 +15,41 @@
* to generate code, and how to do analysis.
*/
abstract class GrammarElement {
- public static final int AUTO_GEN_NONE = 1;
- public static final int AUTO_GEN_CARET = 2;
- public static final int AUTO_GEN_BANG = 3;
-
- /*
- * Note that Java does static argument type matching to
- * determine which function to execute on the receiver.
- * Here, that implies that we cannot simply say
- * grammar.generator.gen(this) in GrammarElement or
- * only CodeGenerator.gen(GrammarElement ge) would
- * ever be called.
- */
- protected Grammar grammar;
- protected int line;
-
-
- public GrammarElement(Grammar g) {
- grammar = g;
- }
- public void generate() {;}
- public int getLine() {
- return line;
- }
- public Lookahead look(int k) { return null; }
- public abstract String toString();
+
+ public static final int AUTO_GEN_NONE = 1;
+
+ public static final int AUTO_GEN_CARET = 2;
+
+ public static final int AUTO_GEN_BANG = 3;
+
+ /*
+ * Note that Java does static argument type matching to
+ * determine which function to execute on the receiver.
+ * Here, that implies that we cannot simply say
+ * grammar.generator.gen(this) in GrammarElement or
+ * only CodeGenerator.gen(GrammarElement ge) would
+ * ever be called.
+ */
+ protected Grammar grammar;
+
+ protected int line;
+
+
+ public GrammarElement(Grammar g) {
+ grammar = g;
+ }
+
+ public void generate() {
+ ;
+ }
+
+ public int getLine() {
+ return line;
+ }
+
+ public Lookahead look(int k) {
+ return null;
+ }
+
+ public abstract String toString();
}
1.2 +16 -5 e/src/jsrc/antlr/GrammarSymbol.java
Index: GrammarSymbol.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/GrammarSymbol.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- GrammarSymbol.java 2001/10/03 21:04:13 1.1
+++ GrammarSymbol.java 2001/10/04 01:09:14 1.2
@@ -4,17 +4,28 @@
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
- * $Id: GrammarSymbol.java,v 1.1 2001/10/03 21:04:13 markm Exp $
+ * $Id: GrammarSymbol.java,v 1.2 2001/10/04 01:09:14 markm Exp $
*/
/**A GrammarSymbol is a generic symbol that can be
* added to the symbol table for a grammar.
*/
abstract class GrammarSymbol {
- protected String id;
- public GrammarSymbol() {}
- public GrammarSymbol(String s) { id = s; }
- public String getId() { return id; }
- public void setId(String s) { id = s; }
+ protected String id;
+
+ public GrammarSymbol() {
+ }
+
+ public GrammarSymbol(String s) {
+ id = s;
+ }
+
+ public String getId() {
+ return id;
+ }
+
+ public void setId(String s) {
+ id = s;
+ }
}
1.2 +783 -762 e/src/jsrc/antlr/HTMLCodeGenerator.java
Index: HTMLCodeGenerator.java
===================================================================
RCS file: /cvs/e/src/jsrc/antlr/HTMLCodeGenerator.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- HTMLCodeGenerator.java 2001/10/03 21:04:13 1.1
+++ HTMLCodeGenerator.java 2001/10/04 01:09:14 1.2
@@ -4,777 +4,798 @@
* Project led by Terence Parr at http://www.jGuru.com
* Software rights: http://www.antlr.org/RIGHTS.html
*
- * $Id: HTMLCodeGenerator.java,v 1.1 2001/10/03 21:04:13 markm Exp $
+ * $Id: HTMLCodeGenerator.java,v 1.2 2001/10/04 01:09:14 markm Exp $
*/
-import java.util.Enumeration;
-import antlr.collections.impl.BitSet;
import antlr.collections.impl.Vector;
-import java.io.PrintWriter; //SAS: changed for proper text file io
+
import java.io.IOException;
-import java.io.FileWriter;
+import java.util.Enumeration;
/**Generate P.html, a cross-linked representation of P with or without actions */
public class HTMLCodeGenerator extends CodeGenerator {
- /** non-zero if inside syntactic predicate generation */
- protected int syntacticPredLevel = 0;
- /** true during lexer generation, false during parser generation */
- protected boolean doingLexRules = false;
+ /** non-zero if inside syntactic predicate generation */
+ protected int syntacticPredLevel = 0;
- protected boolean firstElementInAlt;
+ /** true during lexer generation, false during parser generation */
+ protected boolean doingLexRules = false;
- protected AlternativeElement prevAltElem = null; // what was generated last?
-
- /** Create a Diagnostic code-generator using the given Grammar
- * The caller must still call setTool, setBehavior, and setAnalyzer
- * before generating code.
- */
- public HTMLCodeGenerator() {
- super();
- charFormatter = new JavaCharFormatter();
- }
- public void gen() {
-
- // Do the code generation
- try {
- // Loop over all grammars
- Enumeration grammarIter = behavior.grammars.elements();
- while (grammarIter.hasMoreElements()) {
- Grammar g = (Grammar)grammarIter.nextElement();
-
- // Connect all the components to each other
- /*
- g.setGrammarAnalyzer(analyzer);
- analyzer.setGrammar(g);
- */
- g.setCodeGenerator(this);
-
- // To get right overloading behavior across hetrogeneous grammars
- g.generate();
-
- if (tool.hasError) {
- System.out.println("Exiting due to errors.");
- System.exit(1);
- }
-
- }
-
- }
- catch (IOException e) {
- System.out.println(e.getMessage());
- }
- }
- /** Generate code for the given grammar element.
- * @param blk The {...} action to generate
- */
- public void gen(ActionElement action) {
- /*
- _print("{");
- _printAction(action.actionText);
- _print("} ");
- */
- }
-/** Generate code for the given grammar element.
- * @param blk The "x|y|z|..." block to generate
- */
-public void gen(AlternativeBlock blk) {
- genGenericBlock(blk, "");
-}
- /** Generate code for the given grammar element.
- * @param blk The block-end element to generate. Block-end
- * elements are synthesized by the grammar parser to represent
- * the end of a block.
- */
- public void gen(BlockEndElement end) {
- // no-op
- }
- /** Generate code for the given grammar element.
- * @param blk The character literal reference to generate
- */
- public void gen(CharLiteralElement atom) {
- /*
- if (atom.label != null) {
- _print(atom.label+"=");
- }
- */
- if (atom.not) {
- _print("~");
- }
- _print(atom.atomText+" ");
- }
- /** Generate code for the given grammar element.
- * @param blk The character-range reference to generate
- */
- public void gen(CharRangeElement r) {
- /*
- if ( r.label!=null ) {
- _print(r.label+"=");
- }
- */
- print(r.beginText + ".." + r.endText+" ");
- }
- /** Generate the lexer TXT file */
- public void gen(LexerGrammar g) throws IOException {
- setGrammar(g);
- System.out.println("Generating " + grammar.getClassName() + TokenTypesFileExt);
- currentOutput = antlr.Tool.openOutputFile(grammar.getClassName() + TokenTypesFileExt);
- //SAS: changed for proper text file io
-
- tabs=0;
- doingLexRules = true;
-
- // Generate header common to all TXT output files
- genHeader();
-
- /*
- // Output the user-defined lexer premamble
- println(grammar.preambleAction.getText());
- */
-
- // Generate lexer class definition
- println("");
-
- // print javadoc comment if any
- if ( grammar.comment!=null ) {
- _println(grammar.comment);
- }
-
- println("class " + grammar.getClassName() + " extends " + grammar.getSuperClass() + " {");
-
- // Generate user-defined parser class members
- // printAction(grammar.classMemberAction.getText());
-
- /*
- // Generate string literals
- println("");
- println("*** String literals used in the parser");
- println("The following string literals were used in the parser.");
- println("An actual code generator would arrange to place these literals");
- println("into a table in the generated lexer, so that actions in the");
- println("generated lexer could match token text against the literals.");
- println("String literals used in the lexer are not listed here, as they");
- println("are incorporated into the mainstream lexer processing.");
- tabs++;
- // Enumerate all of the symbols and look for string literal symbols
- Enumeration ids = grammar.getSymbols();
- while ( ids.hasMoreElements() ) {
- GrammarSymbol sym = (GrammarSymbol)ids.nextElement();
- // Only processing string literals -- reject other symbol entries
- if ( sym instanceof StringLiteralSymbol ) {
- StringLiteralSymbol s = (StringLiteralSymbol)sym;
- println(s.getId() + " = " + s.getTokenType());
- }
- }
- tabs--;
- println("*** End of string literals used by the parser");
- */
-
- // Generate nextToken() rule.
- // nextToken() is a synthetic lexer rule that is the implicit OR of all
- // user-defined lexer rules.
- genNextToken();
-
- // Generate code for each rule in the lexer
-
- Enumeration ids = grammar.rules.elements();
- while ( ids.hasMoreElements() ) {
- RuleSymbol rs = (RuleSymbol)ids.nextElement();
- if (!rs.id.equals("mnextToken")) {
- genRule(rs);
- }
- }
-
- // Close the lexer output file
- currentOutput.close();
- currentOutput = null;
- doingLexRules = false;
- }
-/** Generate code for the given grammar element.
- * @param blk The (...)+ block to generate
- */
-public void gen(OneOrMoreBlock blk) {
- genGenericBlock(blk, "+");
-}
- /** Generate the parser TXT file */
- public void gen(ParserGrammar g) throws IOException {
- setGrammar(g);
- // Open the output stream for the parser and set the currentOutput
- System.out.println("Generating " + grammar.getClassName() + ".html");
- currentOutput = antlr.Tool.openOutputFile(grammar.getClassName()+".html");
-
- tabs = 0;
-
- // Generate the header common to all output files.
- genHeader();
-
- /*
- _print("{");
- printAction(grammar.preambleAction.getText());
- _print("}");
- */
-
- // Generate parser class definition
- println("");
-
- // print javadoc comment if any
- if ( grammar.comment!=null ) {
- _println(grammar.comment);
- }
-
- println("class " + grammar.getClassName() + " extends " + grammar.getSuperClass());
-
- /*
- // Generate user-defined parser class members
- println("");
- _print("{");
- printAction(grammar.classMemberAction.getText());
- _print("}");
- */
-
- // Enumerate the parser rules
- Enumeration rules = grammar.rules.elements();
- while ( rules.hasMoreElements() ) {
- println("");
- // Get the rules from the list and downcast it to proper type
- GrammarSymbol sym = (GrammarSymbol) rules.nextElement();
- // Only process parser rules
- if ( sym instanceof RuleSymbol) {
- genRule((RuleSymbol)sym);
- }
- }
- tabs--;
- println("");
-
- genTail();
-
- // Close the parser output stream
- currentOutput.close();
- currentOutput = null;
- }
- /** Generate code for the given grammar element.
- * @param blk The rule-reference to generate
- */
- public void gen(RuleRefElement rr) {
- RuleSymbol rs = (RuleSymbol)grammar.getSymbol(rr.targetRule);
-
- // Generate the actual rule description
- /*
- if ( rr.idAssign!=null ) {
- _print(rr.idAssign+"=");
- }
- */
- _print("<a href="+grammar.getClassName() + ".html#"+rr.targetRule+">");
- _print(rr.targetRule);
- _print("</a>");
- if (rr.args != null) {
- _print("["+rr.args+"]");
- }
- _print(" ");
- }
- /** Generate code for the given grammar element.
- * @param blk The string-literal reference to generate
- */
- public void gen(StringLiteralElement atom) {
- /*
- if (atom.label != null) {
- _print(atom.label+"=");
- }
- */
- if (atom.not) {
- _print("~");
- }
- _print(atom.atomText);
- _print(" ");
- }
- /** Generate code for the given grammar element.
- * @param blk The token-range reference to generate
- */
- public void gen(TokenRangeElement r) {
- /*
- if ( r.label!=null ) {
- _print(r.label+"=");
- }
- */
- print(r.beginText + ".." + r.endText+" ");
- }
- /** Generate code for the given grammar element.
- * @param blk The token-reference to generate
- */
- public void gen(TokenRefElement atom) {
- /*
- if (atom.label != null) {
- _print(atom.label+"=");
- }
- */
- if (atom.not) {
- _print("~");
- }
- _print(atom.atomText);
- _print(" ");
- }
- public void gen(TreeElement t) {
- print(t+" ");
- }
- /** Generate the tree-walker TXT file */
- public void gen(TreeWalkerGrammar g) throws IOException {
- setGrammar(g);
- // Open the output stream for the parser and set the currentOutput
- System.out.println("Generating " + grammar.getClassName() + ".txt");
- currentOutput = antlr.Tool.openOutputFile(grammar.getClassName()+".txt");
- //SAS: changed for proper text file io
-
- tabs = 0;
-
- // Generate the header common to all output files.
- genHeader();
-
- // Output the user-defined parser premamble
- println("");
- println("*** Tree-walker Preamble Action.");
- println("This action will appear before the declaration of your tree-walker class:");
- tabs++;
- println(grammar.preambleAction.getText());
- tabs--;
- println("*** End of tree-walker Preamble Action");
-
- // Generate tree-walker class definition
- println("");
-
- // print javadoc comment if any
- if ( grammar.comment!=null ) {
- _println(grammar.comment);
- }
-
- println("class " + grammar.getClassName() + " extends " + grammar.getSuperClass() + "{");
-
- // Generate user-defined tree-walker class members
- println("");
- println("*** User-defined tree-walker class members:");
- println("These are the member declarations that you defined for your class:");
- tabs++;
- printAction(grammar.classMemberAction.getText());
- tabs--;
- println("*** End of user-defined tree-walker class members");
-
- // Generate code for each rule in the grammar
- println("");
- println("*** tree-walker rules:");
- tabs++;
-
- // Enumerate the tree-walker rules
- Enumeration rules = grammar.rules.elements();
- while ( rules.hasMoreElements() ) {
- println("");
- // Get the rules from the list and downcast it to proper type
- GrammarSymbol sym = (GrammarSymbol) rules.nextElement();
- // Only process tree-walker rules
- if ( sym instanceof RuleSymbol) {
- genRule((RuleSymbol)sym);
- }
- }
- tabs--;
- println("");
- println("*** End of tree-walker rules");
-
- println("");
- println("*** End of tree-walker");
-
- // Close the tree-walker output stream
- currentOutput.close();
- currentOutput = null;
- }
- /** Generate a wildcard element */
- public void gen(WildcardElement wc) {
- /*
- if ( wc.getLabel()!=null ) {
- _print(wc.getLabel()+"=");
- }
- */
- _print(". ");
- }
-/** Generate code for the given grammar element.
- * @param blk The (...)* block to generate
- */
-public void gen(ZeroOrMoreBlock blk) {
- genGenericBlock(blk, "*");
-}
-protected void genAlt(Alternative alt) {
- if (alt.getTreeSpecifier() != null) {
- _print(alt.getTreeSpecifier().getText());
- }
- prevAltElem = null;
- for (AlternativeElement elem = alt.head; !(elem instanceof BlockEndElement); elem = elem.next) {
- elem.generate();
- firstElementInAlt = false;
- prevAltElem = elem;
- }
-}
- /** Generate the header for a block, which may be a RuleBlock or a
- * plain AlternativeBLock. This generates any variable declarations,
- * init-actions, and syntactic-predicate-testing variables.
- * @blk The block for which the preamble is to be generated.
- */
- protected void genBlockPreamble(AlternativeBlock blk) {
- // dump out init action
- if ( blk.initAction!=null ) {
- printAction("{" + blk.initAction + "}");
- }
- }
-/**Generate common code for a block of alternatives; return a postscript
- * that needs to be generated at the end of the block. Other routines
- * may append else-clauses and such for error checking before the postfix
- * is generated.
- */
-public void genCommonBlock(AlternativeBlock blk) {
- for (int i = 0; i < blk.alternatives.size(); i++) {
- Alternative alt = blk.getAlternativeAt(i);
- AlternativeElement elem = alt.head;
-
- // dump alt operator |
- if ( i>0 && blk.alternatives.size()>1 ) {
- /*
- // only do newline if the last element wasn't a multi-line block
- if ( prevAltElem==null ||
- !(prevAltElem instanceof AlternativeBlock) ||
- ((AlternativeBlock)prevAltElem).alternatives.size()==1 )
- {
- _println("");
- }
- */
- _println("");
- print("|\t");
- }
-
- // Dump the alternative, starting with predicates
-
- boolean save = firstElementInAlt;
- firstElementInAlt = true;
- tabs++; // in case we do a newline in alt, increase the tab indent
- // Dump semantic predicates
- if (alt.semPred != null) {
- println("{" + alt.semPred + "}?");
- }
- // Dump syntactic predicate
- if (alt.synPred != null) {
- genSynPred(alt.synPred);
- }
- genAlt(alt);
- tabs--;
- firstElementInAlt = save;
- }
-}
- /** Generate a textual representation of the follow set
- * for a block.
- * @param blk The rule block of interest
- */
- public void genFollowSetForRuleBlock(RuleBlock blk)
- {
- Lookahead follow = grammar.theLLkAnalyzer.FOLLOW(1, blk.endNode);
- printSet(grammar.maxk, 1, follow);
- }
-protected void genGenericBlock(AlternativeBlock blk, String blkOp) {
- if (blk.alternatives.size() > 1) {
- // make sure we start on a new line
- if (!firstElementInAlt) {
- // only do newline if the last element wasn't a multi-line block
- if ( prevAltElem==null ||
- !(prevAltElem instanceof AlternativeBlock) ||
- ((AlternativeBlock)prevAltElem).alternatives.size()==1 )
- {
- _println("");
- print("(\t");
- }
- else {
- _print("(\t");
- }
- // _println("");
- // print("(\t");
- }
- else {
- _print("(\t");
- }
- } else {
- _print("( ");
- }
- genBlockPreamble(blk);
- genCommonBlock(blk);
- if (blk.alternatives.size() > 1) {
- _println("");
- print(")" + blkOp + " ");
- // if not last element of alt, need newline & to indent
- if ( !(blk.next instanceof BlockEndElement) ) {
- _println("");
- print("");
- }
- } else {
- _print(")" + blkOp + " ");
- }
-}
- /** Generate a header that is common to all TXT files */
- protected void genHeader()
- {
- println("<HTML>");
- println("<HEAD>");
- println("<TITLE>Grammar "+tool.grammarFile+"</TITLE>");
- println("</HEAD>");
- println("<BODY>");
- println("<table border=1 cellpadding=5>");
- println("<tr>");
- println("<td>");
- println("<font size=+2>Grammar "+grammar.getClassName()+"</font><br>");
- println("<a href=http://www.ANTLR.org>ANTLR</a>-generated HTML file from "+tool.grammarFile);
- println("<p>");
- println("Terence Parr, <a href=http://www.magelang.com>MageLang Institute</a>");
- println("<br>ANTLR Version "+Tool.version+"; 1989-1999");
- println("</td>");
- println("</tr>");
- println("</table>");
- println("<PRE>");
- tabs++;
- printAction(behavior.getHeaderAction(""));
- tabs--;
- }
- /**Generate the lookahead set for an alternate. */
- protected void genLookaheadSetForAlt(Alternative alt) {
- if ( doingLexRules && alt.cache[1].containsEpsilon() ) {
- println("MATCHES ALL");
- return;
- }
- int depth = alt.lookaheadDepth;
- if ( depth == GrammarAnalyzer.NONDETERMINISTIC ) {
- // if the decision is nondeterministic, do the best we can: LL(k)
- // any predicates that are around will be generated later.
- depth = grammar.maxk;
- }
- for (int i = 1; i <= depth; i++)
- {
- Lookahead lookahead = alt.cache[i];
- printSet(depth, i, lookahead);
- }
- }
- /** Generate a textual representation of the lookahead set
- * for a block.
- * @param blk The block of interest
- */
- public void genLookaheadSetForBlock(AlternativeBlock blk)
- {
- // Find the maximal lookahead depth over all alternatives
- int depth = 0;
- for (int i=0; i<blk.alternatives.size(); i++) {
- Alternative alt = blk.getAlternativeAt(i);
- if (alt.lookaheadDepth == GrammarAnalyzer.NONDETERMINISTIC) {
- depth = grammar.maxk;
- break;
- }
- else if (depth < alt.lookaheadDepth) {
- depth = alt.lookaheadDepth;
- }
- }
-
- for (int i = 1; i <= depth; i++)
- {
- Lookahead lookahead = grammar.theLLkAnalyzer.look(i, blk);
- printSet(depth, i, lookahead);
- }
- }
- /** Generate the nextToken rule.
- * nextToken is a synthetic lexer rule that is the implicit OR of all
- * user-defined lexer rules.
- */
- public void genNextToken() {
- println("");
- println("/** Lexer nextToken rule:");
- println(" * The lexer nextToken rule is synthesized from all of the user-defined");
- println(" * lexer rules. It logically consists of one big alternative block with");
- println(" * each user-defined rule being an alternative.");
- println(" */");
-
- // Create the synthesized rule block for nextToken consisting
- // of an alternate block containing all the user-defined lexer rules.
- RuleBlock blk = MakeGrammar.createNextTokenRule(grammar, grammar.rules, "nextToken");
-
- // Define the nextToken rule symbol
- RuleSymbol nextTokenRs = new RuleSymbol("mnextToken");
- nextTokenRs.setDefined();
- nextTokenRs.setBlock(blk);
- nextTokenRs.access = "private";
- grammar.define(nextTokenRs);
-
- /*
- // Analyze the synthesized block
- if (!grammar.theLLkAnalyzer.deterministic(blk))
- {
- println("The grammar analyzer has determined that the synthesized");
- println("nextToken rule is non-deterministic (i.e., it has ambiguities)");
- println("This means that there is some overlap of the character");
- println("lookahead for two or more of your lexer rules.");
- }
- */
-
- genCommonBlock(blk);
- }
- /** Generate code for a named rule block
- * @param s The RuleSymbol describing the rule to generate
- */
- public void genRule(RuleSymbol s) {
- if ( s==null || !s.isDefined() ) return; // undefined rule
- println("");
- if ( s.comment!=null ) {
- _println(s.comment);
- }
- if (s.access.length() != 0) {
- if ( !s.access.equals("public") ) {
- _print(s.access+" ");
- }
- }
- _print("<a name="+s.getId()+">");
- _print(s.getId());
- _print("</a>");
-
- // Get rule return type and arguments
- RuleBlock rblk = s.getBlock();
-
- // Gen method return value(s)
- if (rblk.returnAction != null) {
- _print("["+rblk.returnAction+"]");
- }
- // Gen arguments
- if (rblk.argAction != null)
- {
- _print(" returns [" + rblk.argAction+"]");
- }
- _println("");
- tabs++;
- print(":\t");
-
- // Dump any init-action
- // genBlockPreamble(rblk);
-
- // Dump the alternates of the rule
- genCommonBlock(rblk);
-
- _println("");
- println(";");
- tabs--;
- }
- /** Generate the syntactic predicate. This basically generates
- * the alternative block, buts tracks if we are inside a synPred
- * @param blk The syntactic predicate block
- */
- protected void genSynPred(SynPredBlock blk) {
- syntacticPredLevel++;
- genGenericBlock(blk, " =>");
- syntacticPredLevel--;
- }
- public void genTail() {
- println("</PRE>");
- println("</BODY>");
- println("</HTML>");
- }
- /** Generate the token types TXT file */
- protected void genTokenTypes(TokenManager tm) throws IOException {
- // Open the token output TXT file and set the currentOutput stream
- System.out.println("Generating " + tm.getName() + TokenTypesFileSuffix+TokenTypesFileExt);
- currentOutput = antlr.Tool.openOutputFile(tm.getName() + TokenTypesFileSuffix+TokenTypesFileExt);
- //SAS: changed for proper text file io
- tabs = 0;
-
- // Generate the header common to all diagnostic files
- genHeader();
-
- // Generate a string for each token. This creates a static
- // array of Strings indexed by token type.
- println("");
- println("*** Tokens used by the parser");
- println("This is a list of the token numeric values and the corresponding");
- println("token identifiers. Some tokens are literals, and because of that");
- println("they have no identifiers. Literals are double-quoted.");
- tabs++;
-
- // Enumerate all the valid token types
- Vector v = tm.getVocabulary();
- for (int i = Token.MIN_USER_TYPE; i < v.size(); i++) {
- String s = (String)v.elementAt(i);
- if (s != null) {
- println(s + " = " + i);
- }
- }
-
- // Close the interface
- tabs--;
- println("*** End of tokens used by the parser");
-
- // Close the tokens output file
- currentOutput.close();
- currentOutput = null;
- }
- /** Get a string for an expression to generate creation of an AST subtree.
- * @param v A Vector of String, where each element is an expression in the target language yielding an AST node.
- */
- public String getASTCreateString(Vector v) {
- return null;
- }
- /** Get a string for an expression to generate creating of an AST node
- * @param str The arguments to the AST constructor
- */
- public String getASTCreateString(GrammarAtom atom, String str) {
- return null;
- }
- /** Map an identifier to it's corresponding tree-node variable.
- * This is context-sensitive, depending on the rule and alternative
- * being generated
- * @param id The identifier name to map
- * @param forInput true if the input tree node variable is to be returned, otherwise the output variable is returned.
- */
- public String mapTreeId(String id, ActionTransInfo tInfo) {
- return id;
- }
- /** Format a lookahead or follow set.
- * @param depth The depth of the entire lookahead/follow
- * @param k The lookahead level to print
- * @param lookahead The lookahead/follow set to print
- */
- public void printSet(int depth, int k, Lookahead lookahead) {
- int numCols = 5;
-
- int[] elems = lookahead.fset.toArray();
-
- if (depth != 1) {
- print("k==" + k + ": {");
- } else {
- print("{ ");
- }
- if (elems.length > numCols) {
- _println("");
- tabs++;
- print("");
- }
-
- int column = 0;
- for (int i = 0; i < elems.length; i++)
- {
- column++;
- if (column > numCols) {
- _println("");
- print("");
- column = 0;
- }
- if (doingLexRules) {
- _print(charFormatter.literalChar(elems[i]));
- } else {
- _print((String)grammar.tokenManager.getVocabulary().elementAt(elems[i]));
- }
- if (i != elems.length-1) {
- _print(", ");
- }
- }
-
- if (elems.length > numCols) {
- _println("");
- tabs--;
- print("");
- }
- _println(" }");
- }
+ protected boolean firstElementInAlt;
+
+ protected AlternativeElement prevAltElem = null; // what was generated last?
+
+ /** Create a Diagnostic code-generator using the given Grammar
+ * The caller must still call setTool, setBehavior, and setAnalyzer
+ * before generating code.
+ */
+ public HTMLCodeGenerator() {
+ super();
+ charFormatter = new JavaCharFormatter();
+ }
+
+ public void gen() {
+
+ // Do the code generation
+ try {
+ // Loop over all grammars
+ Enumeration grammarIter = behavior.grammars.elements();
+ while (grammarIter.hasMoreElements()) {
+ Grammar g = (Grammar)grammarIter.nextElement();
+
+ // Connect all the components to each other
+ /*
+ g.setGrammarAnalyzer(analyzer);
+ analyzer.setGrammar(g);
+ */
+ g.setCodeGenerator(this);
+
+ // To get right overloading behavior across hetrogeneous grammars
+ g.generate();
+
+ if (tool.hasError) {
+ System.out.println("Exiting due to errors.");
+ System.exit(1);
+ }
+
+ }
+
+ } catch (IOException e) {
+ System.out.println(e.getMessage());
+ }
+ }
+
+ /** Generate code for the given grammar element.
+ * @param blk The {...} action to generate
+ */
+ public void gen(ActionElement action) {
+ /*
+ _print("{");
+ _printAction(action.actionText);
+ _print("} ");
+ */
+ }
+
+ /** Generate code for the given grammar element.
+ * @param blk The "x|y|z|..." block to generate
+ */
+ public void gen(AlternativeBlock blk) {
+ genGenericBlock(blk, "");
+ }
+
+ /** Generate code for the given grammar element.
+ * @param blk The block-end element to generate. Block-end
+ * elements are synthesized by the grammar parser to represent
+ * the end of a block.
+ */
+ public void gen(BlockEndElement end) {
+ // no-op
+ }
+
+ /** Generate code for the given grammar element.
+ * @param blk The character literal reference to generate
+ */
+ public void gen(CharLiteralElement atom) {
+ /*
+ if (atom.label != null) {
+ _print(atom.label+"=");
+ }
+ */
+ if (atom.not) {
+ _print("~");
+ }
+ _print(atom.atomText + " ");
+ }
+
+ /** Generate code for the given grammar element.
+ * @param blk The character-range reference to generate
+ */
+ public void gen(CharRangeElement r) {
+ /*
+ if ( r.label!=null ) {
+ _print(r.label+"=");
+ }
+ */
+ print(r.beginText + ".." + r.endText + " ");
+ }
+
+ /** Generate the lexer TXT file */
+ public void gen(LexerGrammar g) throws IOException {
+ setGrammar(g);
+ System.out.println("Generating " + grammar.getClassName() + TokenTypesFileExt);
+ currentOutput = antlr.Tool.openOutputFile(grammar.getClassName() + TokenTypesFileExt);
+ //SAS: changed for proper text file io
+
+ tabs = 0;
+ doingLexRules = true;
+
+ // Generate header common to all TXT output files
+ genHeader();
+
+ /*
+ // Output the user-defined lexer premamble
+ println(grammar.preambleAction.getText());
+ */
+
+ // Generate lexer class definition
+ println("");
+
+ // print javadoc comment if any
+ if (grammar.comment != null) {
+ _println(grammar.comment);
+ }
+
+ println("class " + grammar.getClassName() + " extends " + grammar.getSuperClass() + " {");
+
+ // Generate user-defined parser class members
+ // printAction(grammar.classMemberAction.getText());
+
+ /*
+ // Generate string literals
+ println("");
+ println("*** String literals used in the parser");
+ println("The following string literals were used in the parser.");
+ println("An actual code generator would arrange to place these literals");
+ println("into a table in the generated lexer, so that actions in the");
+ println("generated lexer could match token text against the literals.");
+ println("String literals used in the lexer are not listed here, as they");
+ println("are incorporated into the mainstream lexer processing.");
+ tabs++;
+ // Enumerate all of the symbols and look for string literal symbols
+ Enumeration ids = grammar.getSymbols();
+ while ( ids.hasMoreElements() ) {
+ GrammarSymbol sym = (GrammarSymbol)ids.nextElement();
+ // Only processing string literals -- reject other symbol entries
+ if ( sym instanceof StringLiteralSymbol ) {
+ StringLiteralSymbol s = (StringLiteralSymbol)sym;
+ println(s.getId() + " = " + s.getTokenType());
+ }
+ }
+ tabs--;
+ println("*** End of string literals used by the parser");
+ */
+
+ // Generate nextToken() rule.
+ // nextToken() is a synthetic lexer rule that is the implicit OR of all
+ // user-defined lexer rules.
+ genNextToken();
+
+ // Generate code for each rule in the lexer
+
+ Enumeration ids = grammar.rules.elements();
+ while (ids.hasMoreElements()) {
+ RuleSymbol rs = (RuleSymbol)ids.nextElement();
+ if (!rs.id.equals("mnextToken")) {
+ genRule(rs);
+ }
+ }
+
+ // Close the lexer output file
+ currentOutput.close();
+ currentOutput = null;
+ doingLexRules = false;
+ }
+
+ /** Generate code for the given grammar element.
+ * @param blk The (...)+ block to generate
+ */
+ public void gen(OneOrMoreBlock blk) {
+ genGenericBlock(blk, "+");
+ }
+
+ /** Generate the parser TXT file */
+ public void gen(ParserGrammar g) throws IOException {
+ setGrammar(g);
+ // Open the output stream for the parser and set the currentOutput
+ System.out.println("Generating " + grammar.getClassName() + ".html");
+ currentOutput = antlr.Tool.openOutputFile(grammar.getClassName() + ".html");
+
+ tabs = 0;
+
+ // Generate the header common to all output files.
+ genHeader();
+
+ /*
+ _print("{");
+ printAction(grammar.preambleAction.getText());
+ _print("}");
+ */
+
+ // Generate parser class definition
+ println("");
+
+ // print javadoc comment if any
+ if (grammar.comment != null) {
+ _println(grammar.comment);
+ }
+
+ println("class " + grammar.getClassName() + " extends " + grammar.getSuperClass());
+
+ /*
+ // Generate user-defined parser class members
+ println("");
+ _print("{");
+ printAction(grammar.classMemberAction.getText());
+ _print("}");
+ */
+
+ // Enumerate the parser rules
+ Enumeration rules = grammar.rules.elements();
+ while (rules.hasMoreElements()) {
+ println("");
+ // Get the rules from the list and downcast it to proper type
+ GrammarSymbol sym = (GrammarSymbol)rules.nextElement();
+ // Only process parser rules
+ if (sym instanceof RuleSymbol) {
+ genRule((RuleSymbol)sym);
+ }
+ }
+ tabs--;
+ println("");
+
+ genTail();
+
+ // Close the parser output stream
+ currentOutput.close();
+