001: // $ANTLR 3.0b7 ActionAnalysis.g 2007-04-03 12:25:48
002:
003: package org.antlr.tool;
004:
005: import org.antlr.runtime.*;
006: import org.antlr.tool.AttributeScope;
007: import org.antlr.tool.Grammar;
008: import org.antlr.tool.GrammarAST;
009: import org.antlr.tool.Rule;
010:
011: import java.util.HashMap;
012:
013: /** We need to set Rule.referencedPredefinedRuleAttributes before
014: * code generation. This filter looks at an action in context of
015: * its rule and outer alternative number and figures out which
016: * rules have predefined prefs referenced. I need this so I can
017: * remove unusued labels.
018: */
019: public class ActionAnalysisLexer extends Lexer {
020: public static final int X_Y = 5;
021: public static final int EOF = -1;
022: public static final int Tokens = 8;
023: public static final int Y = 7;
024: public static final int ID = 4;
025: public static final int X = 6;
026:
027: Rule enclosingRule;
028: Grammar grammar;
029: antlr.Token actionToken;
030: int outerAltNum = 0;
031:
032: public ActionAnalysisLexer(Grammar grammar, String ruleName,
033: GrammarAST actionAST) {
034: this (new ANTLRStringStream(actionAST.token.getText()));
035: this .grammar = grammar;
036: this .enclosingRule = grammar.getRule(ruleName);
037: this .actionToken = actionAST.token;
038: this .outerAltNum = actionAST.outerAltNum;
039: }
040:
041: public void analyze() {
042: // System.out.println("###\naction="+actionToken);
043: Token t;
044: do {
045: t = nextToken();
046: } while (t.getType() != Token.EOF);
047: }
048:
049: public ActionAnalysisLexer() {
050: ;
051: }
052:
053: public ActionAnalysisLexer(CharStream input) {
054: super (input);
055: ruleMemo = new HashMap[7 + 1];
056: }
057:
058: public String getGrammarFileName() {
059: return "ActionAnalysis.g";
060: }
061:
062: public Token nextToken() {
063: while (true) {
064: if (input.LA(1) == CharStream.EOF) {
065: return Token.EOF_TOKEN;
066: }
067: token = null;
068: channel = Token.DEFAULT_CHANNEL;
069: tokenStartCharIndex = input.index();
070: tokenStartCharPositionInLine = input
071: .getCharPositionInLine();
072: tokenStartLine = input.getLine();
073: text = null;
074: try {
075: int m = input.mark();
076: backtracking = 1;
077: failed = false;
078: mTokens();
079: backtracking = 0;
080:
081: if (failed) {
082: input.rewind(m);
083: input.consume();
084: } else {
085: emit();
086: return token;
087: }
088: } catch (RecognitionException re) {
089: // shouldn't happen in backtracking mode, but...
090: reportError(re);
091: recover(re);
092: }
093: }
094: }
095:
096: public void memoize(IntStream input, int ruleIndex,
097: int ruleStartIndex) {
098: if (backtracking > 1)
099: super .memoize(input, ruleIndex, ruleStartIndex);
100: }
101:
102: public boolean alreadyParsedRule(IntStream input, int ruleIndex) {
103: if (backtracking > 1)
104: return super .alreadyParsedRule(input, ruleIndex);
105: return false;
106: }// $ANTLR start X_Y
107:
108: public final void mX_Y() throws RecognitionException {
109: try {
110: int _type = X_Y;
111: // ActionAnalysis.g:73:7: ( '$' x= ID '.' y= ID {...}?)
112: // ActionAnalysis.g:73:7: '$' x= ID '.' y= ID {...}?
113: {
114: match('$');
115: if (failed)
116: return;
117: int xStart = getCharIndex();
118: mID();
119: if (failed)
120: return;
121: Token x = new CommonToken(input,
122: Token.INVALID_TOKEN_TYPE,
123: Token.DEFAULT_CHANNEL, xStart,
124: getCharIndex() - 1);
125: match('.');
126: if (failed)
127: return;
128: int yStart = getCharIndex();
129: mID();
130: if (failed)
131: return;
132: Token y = new CommonToken(input,
133: Token.INVALID_TOKEN_TYPE,
134: Token.DEFAULT_CHANNEL, yStart,
135: getCharIndex() - 1);
136: if (!(enclosingRule != null)) {
137: if (backtracking > 0) {
138: failed = true;
139: return;
140: }
141: throw new FailedPredicateException(input, "X_Y",
142: "enclosingRule!=null");
143: }
144: if (backtracking == 1) {
145:
146: AttributeScope scope = null;
147: String refdRuleName = null;
148: if (x.getText().equals(enclosingRule.name)) {
149: // ref to enclosing rule.
150: refdRuleName = x.getText();
151: scope = enclosingRule.getLocalAttributeScope(y
152: .getText());
153: } else if (enclosingRule.getRuleLabel(x.getText()) != null) {
154: // ref to rule label
155: Grammar.LabelElementPair pair = enclosingRule
156: .getRuleLabel(x.getText());
157: pair.actionReferencesLabel = true;
158: refdRuleName = pair.referencedRuleName;
159: Rule refdRule = grammar.getRule(refdRuleName);
160: scope = refdRule.getLocalAttributeScope(y
161: .getText());
162: } else if (enclosingRule.getRuleRefsInAlt(x
163: .getText(), outerAltNum) != null) {
164: // ref to rule referenced in this alt
165: refdRuleName = x.getText();
166: Rule refdRule = grammar.getRule(refdRuleName);
167: scope = refdRule.getLocalAttributeScope(y
168: .getText());
169: }
170: if (scope != null
171: && (scope.isPredefinedRuleScope || scope.isPredefinedLexerRuleScope)) {
172: grammar
173: .referenceRuleLabelPredefinedAttribute(refdRuleName);
174: //System.out.println("referenceRuleLabelPredefinedAttribute for "+refdRuleName);
175: }
176:
177: }
178:
179: }
180:
181: this .type = _type;
182: } finally {
183: }
184: }
185:
186: // $ANTLR end X_Y
187:
188: // $ANTLR start X
189: public final void mX() throws RecognitionException {
190: try {
191: int _type = X;
192: // ActionAnalysis.g:106:5: ( '$' x= ID {...}?)
193: // ActionAnalysis.g:106:5: '$' x= ID {...}?
194: {
195: match('$');
196: if (failed)
197: return;
198: int xStart = getCharIndex();
199: mID();
200: if (failed)
201: return;
202: Token x = new CommonToken(input,
203: Token.INVALID_TOKEN_TYPE,
204: Token.DEFAULT_CHANNEL, xStart,
205: getCharIndex() - 1);
206: if (!(enclosingRule != null && enclosingRule
207: .getRuleLabel(x.getText()) != null)) {
208: if (backtracking > 0) {
209: failed = true;
210: return;
211: }
212: throw new FailedPredicateException(input, "X",
213: "enclosingRule!=null && enclosingRule.getRuleLabel($x.text)!=null");
214: }
215: if (backtracking == 1) {
216:
217: Grammar.LabelElementPair pair = enclosingRule
218: .getRuleLabel(x.getText());
219: pair.actionReferencesLabel = true;
220:
221: }
222:
223: }
224:
225: this .type = _type;
226: } finally {
227: }
228: }
229:
230: // $ANTLR end X
231:
232: // $ANTLR start Y
233: public final void mY() throws RecognitionException {
234: try {
235: int _type = Y;
236: // ActionAnalysis.g:114:5: ( '$' ID {...}?)
237: // ActionAnalysis.g:114:5: '$' ID {...}?
238: {
239: match('$');
240: if (failed)
241: return;
242: int ID1Start = getCharIndex();
243: mID();
244: if (failed)
245: return;
246: Token ID1 = new CommonToken(input,
247: Token.INVALID_TOKEN_TYPE,
248: Token.DEFAULT_CHANNEL, ID1Start,
249: getCharIndex() - 1);
250: if (!(enclosingRule != null && enclosingRule
251: .getLocalAttributeScope(ID1.getText()) != null)) {
252: if (backtracking > 0) {
253: failed = true;
254: return;
255: }
256: throw new FailedPredicateException(input, "Y",
257: "enclosingRule!=null && enclosingRule.getLocalAttributeScope($ID.text)!=null");
258: }
259: if (backtracking == 1) {
260:
261: AttributeScope scope = enclosingRule
262: .getLocalAttributeScope(ID1.getText());
263: if (scope != null
264: && (scope.isPredefinedRuleScope || scope.isPredefinedLexerRuleScope)) {
265: grammar
266: .referenceRuleLabelPredefinedAttribute(enclosingRule.name);
267: //System.out.println("referenceRuleLabelPredefinedAttribute for "+ID1.getText());
268: }
269:
270: }
271:
272: }
273:
274: this .type = _type;
275: } finally {
276: }
277: }
278:
279: // $ANTLR end Y
280:
281: // $ANTLR start ID
282: public final void mID() throws RecognitionException {
283: try {
284: // ActionAnalysis.g:127:9: ( ( 'a' .. 'z' | 'A' .. 'Z' | '_' ) ( 'a' .. 'z' | 'A' .. 'Z' | '_' | '0' .. '9' )* )
285: // ActionAnalysis.g:127:9: ( 'a' .. 'z' | 'A' .. 'Z' | '_' ) ( 'a' .. 'z' | 'A' .. 'Z' | '_' | '0' .. '9' )*
286: {
287: if ((input.LA(1) >= 'A' && input.LA(1) <= 'Z')
288: || input.LA(1) == '_'
289: || (input.LA(1) >= 'a' && input.LA(1) <= 'z')) {
290: input.consume();
291: failed = false;
292: } else {
293: if (backtracking > 0) {
294: failed = true;
295: return;
296: }
297: MismatchedSetException mse = new MismatchedSetException(
298: null, input);
299: recover(mse);
300: throw mse;
301: }
302:
303: // ActionAnalysis.g:127:33: ( 'a' .. 'z' | 'A' .. 'Z' | '_' | '0' .. '9' )*
304: loop1: do {
305: int alt1 = 2;
306: int LA1_0 = input.LA(1);
307:
308: if (((LA1_0 >= '0' && LA1_0 <= '9')
309: || (LA1_0 >= 'A' && LA1_0 <= 'Z')
310: || LA1_0 == '_' || (LA1_0 >= 'a' && LA1_0 <= 'z'))) {
311: alt1 = 1;
312: }
313:
314: switch (alt1) {
315: case 1:
316: // ActionAnalysis.g:
317: {
318: if ((input.LA(1) >= '0' && input.LA(1) <= '9')
319: || (input.LA(1) >= 'A' && input.LA(1) <= 'Z')
320: || input.LA(1) == '_'
321: || (input.LA(1) >= 'a' && input.LA(1) <= 'z')) {
322: input.consume();
323: failed = false;
324: } else {
325: if (backtracking > 0) {
326: failed = true;
327: return;
328: }
329: MismatchedSetException mse = new MismatchedSetException(
330: null, input);
331: recover(mse);
332: throw mse;
333: }
334:
335: }
336: break;
337:
338: default:
339: break loop1;
340: }
341: } while (true);
342:
343: }
344:
345: } finally {
346: }
347: }
348:
349: // $ANTLR end ID
350:
351: public void mTokens() throws RecognitionException {
352: // ActionAnalysis.g:1:41: ( X_Y | X | Y )
353: int alt2 = 3;
354: int LA2_0 = input.LA(1);
355:
356: if ((LA2_0 == '$')) {
357: int LA2_1 = input.LA(2);
358:
359: if ((synpred1())) {
360: alt2 = 1;
361: } else if ((synpred2())) {
362: alt2 = 2;
363: } else if ((true)) {
364: alt2 = 3;
365: } else {
366: if (backtracking > 0) {
367: failed = true;
368: return;
369: }
370: NoViableAltException nvae = new NoViableAltException(
371: "1:1: Tokens options {k=1; backtrack=true; } : ( X_Y | X | Y );",
372: 2, 1, input);
373:
374: throw nvae;
375: }
376: } else {
377: if (backtracking > 0) {
378: failed = true;
379: return;
380: }
381: NoViableAltException nvae = new NoViableAltException(
382: "1:1: Tokens options {k=1; backtrack=true; } : ( X_Y | X | Y );",
383: 2, 0, input);
384:
385: throw nvae;
386: }
387: switch (alt2) {
388: case 1:
389: // ActionAnalysis.g:1:41: X_Y
390: {
391: mX_Y();
392: if (failed)
393: return;
394:
395: }
396: break;
397: case 2:
398: // ActionAnalysis.g:1:45: X
399: {
400: mX();
401: if (failed)
402: return;
403:
404: }
405: break;
406: case 3:
407: // ActionAnalysis.g:1:47: Y
408: {
409: mY();
410: if (failed)
411: return;
412:
413: }
414: break;
415:
416: }
417:
418: }
419:
420: // $ANTLR start synpred1
421: public final void synpred1_fragment() throws RecognitionException {
422: // ActionAnalysis.g:1:41: ( X_Y )
423: // ActionAnalysis.g:1:41: X_Y
424: {
425: mX_Y();
426: if (failed)
427: return;
428:
429: }
430: }
431:
432: // $ANTLR end synpred1
433:
434: // $ANTLR start synpred2
435: public final void synpred2_fragment() throws RecognitionException {
436: // ActionAnalysis.g:1:45: ( X )
437: // ActionAnalysis.g:1:45: X
438: {
439: mX();
440: if (failed)
441: return;
442:
443: }
444: }
445:
446: // $ANTLR end synpred2
447:
448: public final boolean synpred2() {
449: backtracking++;
450: int start = input.mark();
451: try {
452: synpred2_fragment(); // can never throw exception
453: } catch (RecognitionException re) {
454: System.err.println("impossible: " + re);
455: }
456: boolean success = !failed;
457: input.rewind(start);
458: backtracking--;
459: failed = false;
460: return success;
461: }
462:
463: public final boolean synpred1() {
464: backtracking++;
465: int start = input.mark();
466: try {
467: synpred1_fragment(); // can never throw exception
468: } catch (RecognitionException re) {
469: System.err.println("impossible: " + re);
470: }
471: boolean success = !failed;
472: input.rewind(start);
473: backtracking--;
474: failed = false;
475: return success;
476: }
477:
478: }
|