KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > persistence > antlr > LexerGrammar


1 package persistence.antlr;
2
3 /* ANTLR Translator Generator
4  * Project led by Terence Parr at http://www.jGuru.com
5  * Software rights: http://www.antlr.org/license.html
6  *
7  */

8
9 import java.util.Hashtable JavaDoc;
10 import java.util.Enumeration JavaDoc;
11 import java.io.IOException JavaDoc;
12
13 import persistence.antlr.collections.impl.BitSet;
14 import persistence.antlr.collections.impl.Vector;
15
16 /** Lexer-specific grammar subclass */
17 class LexerGrammar extends Grammar {
18     // character set used by lexer
19
protected BitSet charVocabulary;
20     // true if the lexer generates literal testing code for nextToken
21
protected boolean testLiterals = true;
22     // true if the lexer generates case-sensitive LA(k) testing
23
protected boolean caseSensitiveLiterals = true;
24     /** true if the lexer generates case-sensitive literals testing */
25     protected boolean caseSensitive = true;
26     /** true if lexer is to ignore all unrecognized tokens */
27     protected boolean filterMode = false;
28
29     /** if filterMode is true, then filterRule can indicate an optional
30      * rule to use as the scarf language. If null, programmer used
31      * plain "filter=true" not "filter=rule".
32      */

33     protected String JavaDoc filterRule = null;
34
35     LexerGrammar(String JavaDoc className_, Tool tool_, String JavaDoc superClass) {
36         super(className_, tool_, superClass);
37         charVocabulary = new BitSet();
38
39         // Lexer usually has no default error handling
40
defaultErrorHandler = false;
41     }
42
43     /** Top-level call to generate the code */
44     public void generate() throws IOException JavaDoc {
45         generator.gen(this);
46     }
47
48     public String JavaDoc getSuperClass() {
49         // If debugging, use debugger version of scanner
50
if (debuggingOutput)
51             return "debug.DebuggingCharScanner";
52         return "CharScanner";
53     }
54
55     // Get the testLiterals option value
56
public boolean getTestLiterals() {
57         return testLiterals;
58     }
59
60     /**Process command line arguments.
61      * -trace have all rules call traceIn/traceOut
62      * -traceLexer have lexical rules call traceIn/traceOut
63      * -debug generate debugging output for parser debugger
64      */

65     public void processArguments(String JavaDoc[] args) {
66         for (int i = 0; i < args.length; i++) {
67             if (args[i].equals("-trace")) {
68                 traceRules = true;
69                 antlrTool.setArgOK(i);
70             }
71             else if (args[i].equals("-traceLexer")) {
72                 traceRules = true;
73                 antlrTool.setArgOK(i);
74             }
75             else if (args[i].equals("-debug")) {
76                 debuggingOutput = true;
77                 antlrTool.setArgOK(i);
78             }
79         }
80     }
81
82     /** Set the character vocabulary used by the lexer */
83     public void setCharVocabulary(BitSet b) {
84         charVocabulary = b;
85     }
86
87     /** Set lexer options */
88     public boolean setOption(String JavaDoc key, Token value) {
89         String JavaDoc s = value.getText();
90         if (key.equals("buildAST")) {
91             antlrTool.warning("buildAST option is not valid for lexer", getFilename(), value.getLine(), value.getColumn());
92             return true;
93         }
94         if (key.equals("testLiterals")) {
95             if (s.equals("true")) {
96                 testLiterals = true;
97             }
98             else if (s.equals("false")) {
99                 testLiterals = false;
100             }
101             else {
102                 antlrTool.warning("testLiterals option must be true or false", getFilename(), value.getLine(), value.getColumn());
103             }
104             return true;
105         }
106         if (key.equals("interactive")) {
107             if (s.equals("true")) {
108                 interactive = true;
109             }
110             else if (s.equals("false")) {
111                 interactive = false;
112             }
113             else {
114                 antlrTool.error("interactive option must be true or false", getFilename(), value.getLine(), value.getColumn());
115             }
116             return true;
117         }
118         if (key.equals("caseSensitive")) {
119             if (s.equals("true")) {
120                 caseSensitive = true;
121             }
122             else if (s.equals("false")) {
123                 caseSensitive = false;
124             }
125             else {
126                 antlrTool.warning("caseSensitive option must be true or false", getFilename(), value.getLine(), value.getColumn());
127             }
128             return true;
129         }
130         if (key.equals("caseSensitiveLiterals")) {
131             if (s.equals("true")) {
132                 caseSensitiveLiterals = true;
133             }
134             else if (s.equals("false")) {
135                 caseSensitiveLiterals = false;
136             }
137             else {
138                 antlrTool.warning("caseSensitiveLiterals option must be true or false", getFilename(), value.getLine(), value.getColumn());
139             }
140             return true;
141         }
142         if (key.equals("filter")) {
143             if (s.equals("true")) {
144                 filterMode = true;
145             }
146             else if (s.equals("false")) {
147                 filterMode = false;
148             }
149             else if (value.getType() == ANTLRTokenTypes.TOKEN_REF) {
150                 filterMode = true;
151                 filterRule = s;
152             }
153             else {
154                 antlrTool.warning("filter option must be true, false, or a lexer rule name", getFilename(), value.getLine(), value.getColumn());
155             }
156             return true;
157         }
158         if (key.equals("longestPossible")) {
159             antlrTool.warning("longestPossible option has been deprecated; ignoring it...", getFilename(), value.getLine(), value.getColumn());
160             return true;
161         }
162         if (super.setOption(key, value)) {
163             return true;
164         }
165         antlrTool.error("Invalid option: " + key, getFilename(), value.getLine(), value.getColumn());
166         return false;
167     }
168 }
169
Popular Tags