001: package persistence.antlr;
002:
003: /* ANTLR Translator Generator
004: * Project led by Terence Parr at http://www.jGuru.com
005: * Software rights: http://www.antlr.org/license.html
006: *
007: */
008:
009: import java.util.Hashtable;
010: import java.util.Enumeration;
011: import java.io.IOException;
012:
013: import persistence.antlr.collections.impl.BitSet;
014: import persistence.antlr.collections.impl.Vector;
015:
016: /** Lexer-specific grammar subclass */
017: class LexerGrammar extends Grammar {
018: // character set used by lexer
019: protected BitSet charVocabulary;
020: // true if the lexer generates literal testing code for nextToken
021: protected boolean testLiterals = true;
022: // true if the lexer generates case-sensitive LA(k) testing
023: protected boolean caseSensitiveLiterals = true;
024: /** true if the lexer generates case-sensitive literals testing */
025: protected boolean caseSensitive = true;
026: /** true if lexer is to ignore all unrecognized tokens */
027: protected boolean filterMode = false;
028:
029: /** if filterMode is true, then filterRule can indicate an optional
030: * rule to use as the scarf language. If null, programmer used
031: * plain "filter=true" not "filter=rule".
032: */
033: protected String filterRule = null;
034:
035: LexerGrammar(String className_, Tool tool_, String super Class) {
036: super (className_, tool_, super Class);
037: charVocabulary = new BitSet();
038:
039: // Lexer usually has no default error handling
040: defaultErrorHandler = false;
041: }
042:
043: /** Top-level call to generate the code */
044: public void generate() throws IOException {
045: generator.gen(this );
046: }
047:
048: public String getSuperClass() {
049: // If debugging, use debugger version of scanner
050: if (debuggingOutput)
051: return "debug.DebuggingCharScanner";
052: return "CharScanner";
053: }
054:
055: // Get the testLiterals option value
056: public boolean getTestLiterals() {
057: return testLiterals;
058: }
059:
060: /**Process command line arguments.
061: * -trace have all rules call traceIn/traceOut
062: * -traceLexer have lexical rules call traceIn/traceOut
063: * -debug generate debugging output for parser debugger
064: */
065: public void processArguments(String[] args) {
066: for (int i = 0; i < args.length; i++) {
067: if (args[i].equals("-trace")) {
068: traceRules = true;
069: antlrTool.setArgOK(i);
070: } else if (args[i].equals("-traceLexer")) {
071: traceRules = true;
072: antlrTool.setArgOK(i);
073: } else if (args[i].equals("-debug")) {
074: debuggingOutput = true;
075: antlrTool.setArgOK(i);
076: }
077: }
078: }
079:
080: /** Set the character vocabulary used by the lexer */
081: public void setCharVocabulary(BitSet b) {
082: charVocabulary = b;
083: }
084:
085: /** Set lexer options */
086: public boolean setOption(String key, Token value) {
087: String s = value.getText();
088: if (key.equals("buildAST")) {
089: antlrTool.warning("buildAST option is not valid for lexer",
090: getFilename(), value.getLine(), value.getColumn());
091: return true;
092: }
093: if (key.equals("testLiterals")) {
094: if (s.equals("true")) {
095: testLiterals = true;
096: } else if (s.equals("false")) {
097: testLiterals = false;
098: } else {
099: antlrTool.warning(
100: "testLiterals option must be true or false",
101: getFilename(), value.getLine(), value
102: .getColumn());
103: }
104: return true;
105: }
106: if (key.equals("interactive")) {
107: if (s.equals("true")) {
108: interactive = true;
109: } else if (s.equals("false")) {
110: interactive = false;
111: } else {
112: antlrTool.error(
113: "interactive option must be true or false",
114: getFilename(), value.getLine(), value
115: .getColumn());
116: }
117: return true;
118: }
119: if (key.equals("caseSensitive")) {
120: if (s.equals("true")) {
121: caseSensitive = true;
122: } else if (s.equals("false")) {
123: caseSensitive = false;
124: } else {
125: antlrTool.warning(
126: "caseSensitive option must be true or false",
127: getFilename(), value.getLine(), value
128: .getColumn());
129: }
130: return true;
131: }
132: if (key.equals("caseSensitiveLiterals")) {
133: if (s.equals("true")) {
134: caseSensitiveLiterals = true;
135: } else if (s.equals("false")) {
136: caseSensitiveLiterals = false;
137: } else {
138: antlrTool
139: .warning(
140: "caseSensitiveLiterals option must be true or false",
141: getFilename(), value.getLine(), value
142: .getColumn());
143: }
144: return true;
145: }
146: if (key.equals("filter")) {
147: if (s.equals("true")) {
148: filterMode = true;
149: } else if (s.equals("false")) {
150: filterMode = false;
151: } else if (value.getType() == ANTLRTokenTypes.TOKEN_REF) {
152: filterMode = true;
153: filterRule = s;
154: } else {
155: antlrTool
156: .warning(
157: "filter option must be true, false, or a lexer rule name",
158: getFilename(), value.getLine(), value
159: .getColumn());
160: }
161: return true;
162: }
163: if (key.equals("longestPossible")) {
164: antlrTool
165: .warning(
166: "longestPossible option has been deprecated; ignoring it...",
167: getFilename(), value.getLine(), value
168: .getColumn());
169: return true;
170: }
171: if (super .setOption(key, value)) {
172: return true;
173: }
174: antlrTool.error("Invalid option: " + key, getFilename(), value
175: .getLine(), value.getColumn());
176: return false;
177: }
178: }
|