01: package persistence.antlr;
02:
03: /* ANTLR Translator Generator
04: * Project led by Terence Parr at http://www.jGuru.com
05: * Software rights: http://www.antlr.org/license.html
06: *
07: */
08:
09: class StringLiteralElement extends GrammarAtom {
10: // atomText with quotes stripped and escape codes processed
11: protected String processedAtomText;
12:
13: public StringLiteralElement(Grammar g, Token t, int autoGenType) {
14: super (g, t, autoGenType);
15: if (!(g instanceof LexerGrammar)) {
16: // lexer does not have token types for string literals
17: TokenSymbol ts = grammar.tokenManager
18: .getTokenSymbol(atomText);
19: if (ts == null) {
20: g.antlrTool.error("Undefined literal: " + atomText,
21: grammar.getFilename(), t.getLine(), t
22: .getColumn());
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: }
|