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