source: trunk/yao/share/antlr-2.7.7/antlr/StringLiteralElement.java @ 1

Last change on this file since 1 was 1, checked in by lnalod, 15 years ago

Initial import of YAO sources

File size: 2.1 KB
Line 
1package antlr;
2
3/* ANTLR Translator Generator
4 * Project led by Terence Parr at http://www.cs.usfca.edu
5 * Software rights: http://www.antlr.org/license.html
6 *
7 * $Id: //depot/code/org.antlr/release/antlr-2.7.7/antlr/StringLiteralElement.java#2 $
8 */
9
10class StringLiteralElement extends GrammarAtom {
11    // atomText with quotes stripped and escape codes processed
12    protected String 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();
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}
Note: See TracBrowser for help on using the repository browser.