KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > antlr > StringLiteralElement


1 package antlr;
2
3 /* ANTLR Translator Generator
4  * Project led by Terence Parr at http://www.jGuru.com
5  * Software rights: http://www.antlr.org/RIGHTS.html
6  *
7  * $Id: //depot/code/org.antlr/main/main/antlr/StringLiteralElement.java#6 $
8  */

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