KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > persistence > antlr > StringLiteralElement


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 class StringLiteralElement extends GrammarAtom {
10     // atomText with quotes stripped and escape codes processed
11
protected String JavaDoc processedAtomText;
12
13
14     public StringLiteralElement(Grammar g, Token t, int autoGenType) {
15         super(g, t, autoGenType);
16         if (!(g instanceof LexerGrammar)) {
17             // lexer does not have token types for string literals
18
TokenSymbol ts = grammar.tokenManager.getTokenSymbol(atomText);
19             if (ts == null) {
20                 g.antlrTool.error("Undefined literal: " + atomText, grammar.getFilename(), t.getLine(), t.getColumn());
21             }
22             else {
23                 tokenType = ts.getTokenType();
24             }
25         }
26         line = t.getLine();
27
28         // process the string literal text by removing quotes and escaping chars
29
// If a lexical grammar, add the characters to the char vocabulary
30
processedAtomText = new String JavaDoc();
31         for (int i = 1; i < atomText.length() - 1; i++) {
32             char c = atomText.charAt(i);
33             if (c == '\\') {
34                 if (i + 1 < atomText.length() - 1) {
35                     i++;
36                     c = atomText.charAt(i);
37                     switch (c) {
38                         case 'n':
39                             c = '\n';
40                             break;
41                         case 'r':
42                             c = '\r';
43                             break;
44                         case 't':
45                             c = '\t';
46                             break;
47                     }
48                 }
49             }
50             if (g instanceof LexerGrammar) {
51                 ((LexerGrammar)g).charVocabulary.add(c);
52             }
53             processedAtomText += c;
54         }
55     }
56
57     public void generate() {
58         grammar.generator.gen(this);
59     }
60
61     public Lookahead look(int k) {
62         return grammar.theLLkAnalyzer.look(k, this);
63     }
64 }
65
Popular Tags