0001: // $ANTLR 2.7.7 (2006-01-29): "codegen.g" -> "CodeGenTreeWalker.java"$
0002:
0003: /*
0004: [The "BSD licence"]
0005: Copyright (c) 2005-2006 Terence Parr
0006: All rights reserved.
0007:
0008: Redistribution and use in source and binary forms, with or without
0009: modification, are permitted provided that the following conditions
0010: are met:
0011: 1. Redistributions of source code must retain the above copyright
0012: notice, this list of conditions and the following disclaimer.
0013: 2. Redistributions in binary form must reproduce the above copyright
0014: notice, this list of conditions and the following disclaimer in the
0015: documentation and/or other materials provided with the distribution.
0016: 3. The name of the author may not be used to endorse or promote products
0017: derived from this software without specific prior written permission.
0018:
0019: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
0020: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0021: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
0022: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
0023: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
0024: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0025: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0026: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0027: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
0028: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0029: */
0030: package org.antlr.codegen;
0031:
0032: import org.antlr.tool.*;
0033: import org.antlr.analysis.*;
0034: import org.antlr.misc.*;
0035: import java.util.*;
0036: import org.antlr.stringtemplate.*;
0037: import antlr.TokenWithIndex;
0038: import antlr.CommonToken;
0039:
0040: import antlr.TreeParser;
0041: import antlr.Token;
0042: import antlr.collections.AST;
0043: import antlr.RecognitionException;
0044: import antlr.ANTLRException;
0045: import antlr.NoViableAltException;
0046: import antlr.MismatchedTokenException;
0047: import antlr.SemanticException;
0048: import antlr.collections.impl.BitSet;
0049: import antlr.ASTPair;
0050: import antlr.collections.impl.ASTArray;
0051:
0052: /** Walk a grammar and generate code by gradually building up
0053: * a bigger and bigger StringTemplate.
0054: *
0055: * Terence Parr
0056: * University of San Francisco
0057: * June 15, 2004
0058: */
0059: public class CodeGenTreeWalker extends antlr.TreeParser implements
0060: CodeGenTreeWalkerTokenTypes {
0061:
0062: protected static final int RULE_BLOCK_NESTING_LEVEL = 0;
0063: protected static final int OUTER_REWRITE_NESTING_LEVEL = 0;
0064:
0065: protected String currentRuleName = null;
0066: protected int blockNestingLevel = 0;
0067: protected int rewriteBlockNestingLevel = 0;
0068: protected int outerAltNum = 0;
0069: protected StringTemplate currentBlockST = null;
0070: protected boolean currentAltHasASTRewrite = false;
0071: protected int rewriteTreeNestingLevel = 0;
0072: protected Set rewriteRuleRefs = null;
0073:
0074: public void reportError(RecognitionException ex) {
0075: Token token = null;
0076: if (ex instanceof MismatchedTokenException) {
0077: token = ((MismatchedTokenException) ex).token;
0078: } else if (ex instanceof NoViableAltException) {
0079: token = ((NoViableAltException) ex).token;
0080: }
0081: ErrorManager.syntaxError(ErrorManager.MSG_SYNTAX_ERROR,
0082: grammar, token, "codegen: " + ex.toString(), ex);
0083: }
0084:
0085: public void reportError(String s) {
0086: System.out.println("codegen: error: " + s);
0087: }
0088:
0089: protected CodeGenerator generator;
0090: protected Grammar grammar;
0091: protected StringTemplateGroup templates;
0092:
0093: /** The overall lexer/parser template; simulate dynamically scoped
0094: * attributes by making this an instance var of the walker.
0095: */
0096: protected StringTemplate recognizerST;
0097:
0098: protected StringTemplate outputFileST;
0099: protected StringTemplate headerFileST;
0100:
0101: protected String outputOption = "";
0102:
0103: protected StringTemplate getWildcardST(GrammarAST elementAST,
0104: GrammarAST ast_suffix, String label) {
0105: String name = "wildcard";
0106: if (grammar.type == Grammar.LEXER) {
0107: name = "wildcardChar";
0108: }
0109: return getTokenElementST(name, name, elementAST, ast_suffix,
0110: label);
0111: }
0112:
0113: protected StringTemplate getRuleElementST(String name,
0114: String elementName, GrammarAST elementAST,
0115: GrammarAST ast_suffix, String label) {
0116: String suffix = getSTSuffix(ast_suffix, label);
0117: name += suffix;
0118: // if we're building trees and there is no label, gen a label
0119: // unless we're in a synpred rule.
0120: Rule r = grammar.getRule(currentRuleName);
0121: if ((grammar.buildAST() || suffix.length() > 0)
0122: && label == null && (r == null || !r.isSynPred)) {
0123: // we will need a label to do the AST or tracking, make one
0124: label = generator.createUniqueLabel(elementName);
0125: CommonToken labelTok = new CommonToken(ANTLRParser.ID,
0126: label);
0127: grammar.defineRuleRefLabel(currentRuleName, labelTok,
0128: elementAST);
0129: }
0130: StringTemplate elementST = templates.getInstanceOf(name);
0131: if (label != null) {
0132: elementST.setAttribute("label", label);
0133: }
0134: return elementST;
0135: }
0136:
0137: protected StringTemplate getTokenElementST(String name,
0138: String elementName, GrammarAST elementAST,
0139: GrammarAST ast_suffix, String label) {
0140: String suffix = getSTSuffix(ast_suffix, label);
0141: name += suffix;
0142: // if we're building trees and there is no label, gen a label
0143: // unless we're in a synpred rule.
0144: Rule r = grammar.getRule(currentRuleName);
0145: if ((grammar.buildAST() || suffix.length() > 0)
0146: && label == null && (r == null || !r.isSynPred)) {
0147: label = generator.createUniqueLabel(elementName);
0148: CommonToken labelTok = new CommonToken(ANTLRParser.ID,
0149: label);
0150: grammar.defineTokenRefLabel(currentRuleName, labelTok,
0151: elementAST);
0152: }
0153: StringTemplate elementST = templates.getInstanceOf(name);
0154: if (label != null) {
0155: elementST.setAttribute("label", label);
0156: }
0157: return elementST;
0158: }
0159:
0160: public boolean isListLabel(String label) {
0161: boolean hasListLabel = false;
0162: if (label != null) {
0163: Rule r = grammar.getRule(currentRuleName);
0164: String stName = null;
0165: if (r != null) {
0166: Grammar.LabelElementPair pair = r.getLabel(label);
0167: if (pair != null
0168: && (pair.type == Grammar.TOKEN_LIST_LABEL || pair.type == Grammar.RULE_LIST_LABEL)) {
0169: hasListLabel = true;
0170: }
0171: }
0172: }
0173: return hasListLabel;
0174: }
0175:
0176: /** Return a non-empty template name suffix if the token is to be
0177: * tracked, added to a tree, or both.
0178: */
0179: protected String getSTSuffix(GrammarAST ast_suffix, String label) {
0180: if (grammar.type == Grammar.LEXER) {
0181: return "";
0182: }
0183: // handle list label stuff; make element use "Track"
0184:
0185: String astPart = "";
0186: String operatorPart = "";
0187: String rewritePart = "";
0188: String listLabelPart = "";
0189: if (grammar.buildAST()) {
0190: astPart = "AST";
0191: }
0192: if (ast_suffix != null) {
0193: if (ast_suffix.getType() == ANTLRParser.ROOT) {
0194: operatorPart = "RuleRoot";
0195: } else if (ast_suffix.getType() == ANTLRParser.BANG) {
0196: operatorPart = "Bang";
0197: }
0198: }
0199: if (currentAltHasASTRewrite) {
0200: rewritePart = "Track";
0201: }
0202: if (isListLabel(label)) {
0203: listLabelPart = "AndListLabel";
0204: }
0205: String STsuffix = operatorPart + rewritePart + listLabelPart;
0206: //System.out.println("suffix = "+STsuffix);
0207:
0208: return STsuffix;
0209: }
0210:
0211: /** Convert rewrite AST lists to target labels list */
0212: protected List<String> getTokenTypesAsTargetLabels(
0213: Set<GrammarAST> refs) {
0214: if (refs == null || refs.size() == 0) {
0215: return null;
0216: }
0217: List<String> labels = new ArrayList<String>(refs.size());
0218: for (GrammarAST t : refs) {
0219: String label;
0220: if (t.getType() == ANTLRParser.RULE_REF) {
0221: label = t.getText();
0222: } else if (t.getType() == ANTLRParser.LABEL) {
0223: label = t.getText();
0224: } else {
0225: // must be char or string literal
0226: label = generator.getTokenTypeAsTargetLabel(grammar
0227: .getTokenType(t.getText()));
0228: }
0229: labels.add(label);
0230: }
0231: return labels;
0232: }
0233:
0234: protected void init(Grammar g) {
0235: this .grammar = g;
0236: this .generator = grammar.getCodeGenerator();
0237: this .templates = generator.getTemplates();
0238: }
0239:
0240: public CodeGenTreeWalker() {
0241: tokenNames = _tokenNames;
0242: }
0243:
0244: public final void grammar(AST _t, Grammar g,
0245: StringTemplate recognizerST, StringTemplate outputFileST,
0246: StringTemplate headerFileST) throws RecognitionException {
0247:
0248: GrammarAST grammar_AST_in = (_t == ASTNULL) ? null
0249: : (GrammarAST) _t;
0250:
0251: init(g);
0252: this .recognizerST = recognizerST;
0253: this .outputFileST = outputFileST;
0254: this .headerFileST = headerFileST;
0255: String super Class = (String) g.getOption("superClass");
0256: outputOption = (String) g.getOption("output");
0257: recognizerST.setAttribute("superClass", super Class);
0258: if (g.type != Grammar.LEXER) {
0259: recognizerST.setAttribute("ASTLabelType", g
0260: .getOption("ASTLabelType"));
0261: }
0262: if (g.type == Grammar.TREE_PARSER
0263: && g.getOption("ASTLabelType") == null) {
0264: ErrorManager.grammarWarning(
0265: ErrorManager.MSG_MISSING_AST_TYPE_IN_TREE_GRAMMAR,
0266: g, null, g.name);
0267: }
0268: if (g.type != Grammar.TREE_PARSER) {
0269: recognizerST.setAttribute("labelType", g
0270: .getOption("TokenLabelType"));
0271: }
0272: recognizerST
0273: .setAttribute("numRules", grammar.getRules().size());
0274: outputFileST
0275: .setAttribute("numRules", grammar.getRules().size());
0276: headerFileST
0277: .setAttribute("numRules", grammar.getRules().size());
0278:
0279: try { // for error handling
0280: {
0281: if (_t == null)
0282: _t = ASTNULL;
0283: switch (_t.getType()) {
0284: case LEXER_GRAMMAR: {
0285: AST __t3 = _t;
0286: GrammarAST tmp1_AST_in = (GrammarAST) _t;
0287: match(_t, LEXER_GRAMMAR);
0288: _t = _t.getFirstChild();
0289: grammarSpec(_t);
0290: _t = _retTree;
0291: _t = __t3;
0292: _t = _t.getNextSibling();
0293: break;
0294: }
0295: case PARSER_GRAMMAR: {
0296: AST __t4 = _t;
0297: GrammarAST tmp2_AST_in = (GrammarAST) _t;
0298: match(_t, PARSER_GRAMMAR);
0299: _t = _t.getFirstChild();
0300: grammarSpec(_t);
0301: _t = _retTree;
0302: _t = __t4;
0303: _t = _t.getNextSibling();
0304: break;
0305: }
0306: case TREE_GRAMMAR: {
0307: AST __t5 = _t;
0308: GrammarAST tmp3_AST_in = (GrammarAST) _t;
0309: match(_t, TREE_GRAMMAR);
0310: _t = _t.getFirstChild();
0311: grammarSpec(_t);
0312: _t = _retTree;
0313: _t = __t5;
0314: _t = _t.getNextSibling();
0315: break;
0316: }
0317: case COMBINED_GRAMMAR: {
0318: AST __t6 = _t;
0319: GrammarAST tmp4_AST_in = (GrammarAST) _t;
0320: match(_t, COMBINED_GRAMMAR);
0321: _t = _t.getFirstChild();
0322: grammarSpec(_t);
0323: _t = _retTree;
0324: _t = __t6;
0325: _t = _t.getNextSibling();
0326: break;
0327: }
0328: default: {
0329: throw new NoViableAltException(_t);
0330: }
0331: }
0332: }
0333: } catch (RecognitionException ex) {
0334: reportError(ex);
0335: if (_t != null) {
0336: _t = _t.getNextSibling();
0337: }
0338: }
0339: _retTree = _t;
0340: }
0341:
0342: public final void grammarSpec(AST _t) throws RecognitionException {
0343:
0344: GrammarAST grammarSpec_AST_in = (_t == ASTNULL) ? null
0345: : (GrammarAST) _t;
0346: GrammarAST name = null;
0347: GrammarAST cmt = null;
0348:
0349: try { // for error handling
0350: name = (GrammarAST) _t;
0351: match(_t, ID);
0352: _t = _t.getNextSibling();
0353: {
0354: if (_t == null)
0355: _t = ASTNULL;
0356: switch (_t.getType()) {
0357: case DOC_COMMENT: {
0358: cmt = (GrammarAST) _t;
0359: match(_t, DOC_COMMENT);
0360: _t = _t.getNextSibling();
0361:
0362: outputFileST.setAttribute("docComment", cmt
0363: .getText());
0364: headerFileST.setAttribute("docComment", cmt
0365: .getText());
0366:
0367: break;
0368: }
0369: case OPTIONS:
0370: case TOKENS:
0371: case RULE:
0372: case SCOPE:
0373: case AMPERSAND: {
0374: break;
0375: }
0376: default: {
0377: throw new NoViableAltException(_t);
0378: }
0379: }
0380: }
0381:
0382: String suffix = Grammar.grammarTypeToFileNameSuffix[grammar.type];
0383: String n = name.getText() + suffix;
0384: recognizerST.setAttribute("name", n);
0385: outputFileST.setAttribute("name", n);
0386: headerFileST.setAttribute("name", n);
0387: recognizerST.setAttribute("scopes", grammar
0388: .getGlobalScopes());
0389: headerFileST.setAttribute("scopes", grammar
0390: .getGlobalScopes());
0391:
0392: {
0393: if (_t == null)
0394: _t = ASTNULL;
0395: switch (_t.getType()) {
0396: case OPTIONS: {
0397: AST __t12 = _t;
0398: GrammarAST tmp5_AST_in = (GrammarAST) _t;
0399: match(_t, OPTIONS);
0400: _t = _t.getFirstChild();
0401: GrammarAST tmp6_AST_in = (GrammarAST) _t;
0402: if (_t == null)
0403: throw new MismatchedTokenException();
0404: _t = _t.getNextSibling();
0405: _t = __t12;
0406: _t = _t.getNextSibling();
0407: break;
0408: }
0409: case TOKENS:
0410: case RULE:
0411: case SCOPE:
0412: case AMPERSAND: {
0413: break;
0414: }
0415: default: {
0416: throw new NoViableAltException(_t);
0417: }
0418: }
0419: }
0420: {
0421: if (_t == null)
0422: _t = ASTNULL;
0423: switch (_t.getType()) {
0424: case TOKENS: {
0425: AST __t14 = _t;
0426: GrammarAST tmp7_AST_in = (GrammarAST) _t;
0427: match(_t, TOKENS);
0428: _t = _t.getFirstChild();
0429: GrammarAST tmp8_AST_in = (GrammarAST) _t;
0430: if (_t == null)
0431: throw new MismatchedTokenException();
0432: _t = _t.getNextSibling();
0433: _t = __t14;
0434: _t = _t.getNextSibling();
0435: break;
0436: }
0437: case RULE:
0438: case SCOPE:
0439: case AMPERSAND: {
0440: break;
0441: }
0442: default: {
0443: throw new NoViableAltException(_t);
0444: }
0445: }
0446: }
0447: {
0448: _loop16: do {
0449: if (_t == null)
0450: _t = ASTNULL;
0451: if ((_t.getType() == SCOPE)) {
0452: attrScope(_t);
0453: _t = _retTree;
0454: } else {
0455: break _loop16;
0456: }
0457:
0458: } while (true);
0459: }
0460: {
0461: _loop18: do {
0462: if (_t == null)
0463: _t = ASTNULL;
0464: if ((_t.getType() == AMPERSAND)) {
0465: GrammarAST tmp9_AST_in = (GrammarAST) _t;
0466: match(_t, AMPERSAND);
0467: _t = _t.getNextSibling();
0468: } else {
0469: break _loop18;
0470: }
0471:
0472: } while (true);
0473: }
0474: rules(_t, recognizerST);
0475: _t = _retTree;
0476: } catch (RecognitionException ex) {
0477: reportError(ex);
0478: if (_t != null) {
0479: _t = _t.getNextSibling();
0480: }
0481: }
0482: _retTree = _t;
0483: }
0484:
0485: public final void attrScope(AST _t) throws RecognitionException {
0486:
0487: GrammarAST attrScope_AST_in = (_t == ASTNULL) ? null
0488: : (GrammarAST) _t;
0489:
0490: try { // for error handling
0491: AST __t8 = _t;
0492: GrammarAST tmp10_AST_in = (GrammarAST) _t;
0493: match(_t, SCOPE);
0494: _t = _t.getFirstChild();
0495: GrammarAST tmp11_AST_in = (GrammarAST) _t;
0496: match(_t, ID);
0497: _t = _t.getNextSibling();
0498: GrammarAST tmp12_AST_in = (GrammarAST) _t;
0499: match(_t, ACTION);
0500: _t = _t.getNextSibling();
0501: _t = __t8;
0502: _t = _t.getNextSibling();
0503: } catch (RecognitionException ex) {
0504: reportError(ex);
0505: if (_t != null) {
0506: _t = _t.getNextSibling();
0507: }
0508: }
0509: _retTree = _t;
0510: }
0511:
0512: public final void rules(AST _t, StringTemplate recognizerST)
0513: throws RecognitionException {
0514:
0515: GrammarAST rules_AST_in = (_t == ASTNULL) ? null
0516: : (GrammarAST) _t;
0517:
0518: StringTemplate rST;
0519:
0520: try { // for error handling
0521: {
0522: int _cnt22 = 0;
0523: _loop22: do {
0524: if (_t == null)
0525: _t = ASTNULL;
0526: if ((_t.getType() == RULE)) {
0527: {
0528:
0529: String ruleName = _t.getFirstChild()
0530: .getText();
0531: Rule r = grammar.getRule(ruleName);
0532:
0533: if (_t == null)
0534: _t = ASTNULL;
0535: if (((_t.getType() == RULE))
0536: && (!r.isSynPred || grammar.synPredNamesUsedInDFA
0537: .contains(ruleName))) {
0538: rST = rule(_t);
0539: _t = _retTree;
0540:
0541: if (rST != null) {
0542: recognizerST.setAttribute("rules",
0543: rST);
0544: outputFileST.setAttribute("rules",
0545: rST);
0546: headerFileST.setAttribute("rules",
0547: rST);
0548: }
0549:
0550: } else if ((_t.getType() == RULE)) {
0551: GrammarAST tmp13_AST_in = (GrammarAST) _t;
0552: match(_t, RULE);
0553: _t = _t.getNextSibling();
0554: } else {
0555: throw new NoViableAltException(_t);
0556: }
0557:
0558: }
0559: } else {
0560: if (_cnt22 >= 1) {
0561: break _loop22;
0562: } else {
0563: throw new NoViableAltException(_t);
0564: }
0565: }
0566:
0567: _cnt22++;
0568: } while (true);
0569: }
0570: } catch (RecognitionException ex) {
0571: reportError(ex);
0572: if (_t != null) {
0573: _t = _t.getNextSibling();
0574: }
0575: }
0576: _retTree = _t;
0577: }
0578:
0579: public final StringTemplate rule(AST _t)
0580: throws RecognitionException {
0581: StringTemplate code = null;
0582:
0583: GrammarAST rule_AST_in = (_t == ASTNULL) ? null
0584: : (GrammarAST) _t;
0585: GrammarAST id = null;
0586: GrammarAST mod = null;
0587:
0588: String r;
0589: String initAction = null;
0590: StringTemplate b;
0591: // get the dfa for the BLOCK
0592: GrammarAST block = rule_AST_in.getFirstChildWithType(BLOCK);
0593: DFA dfa = block.getLookaheadDFA();
0594: // init blockNestingLevel so it's block level RULE_BLOCK_NESTING_LEVEL
0595: // for alts of rule
0596: blockNestingLevel = RULE_BLOCK_NESTING_LEVEL - 1;
0597: Rule ruleDescr = grammar.getRule(rule_AST_in.getFirstChild()
0598: .getText());
0599:
0600: // For syn preds, we don't want any AST code etc... in there.
0601: // Save old templates ptr and restore later. Base templates include Dbg.
0602: StringTemplateGroup saveGroup = templates;
0603: if (ruleDescr.isSynPred) {
0604: templates = generator.getBaseTemplates();
0605: }
0606:
0607: try { // for error handling
0608: AST __t24 = _t;
0609: GrammarAST tmp14_AST_in = (GrammarAST) _t;
0610: match(_t, RULE);
0611: _t = _t.getFirstChild();
0612: id = (GrammarAST) _t;
0613: match(_t, ID);
0614: _t = _t.getNextSibling();
0615: r = id.getText();
0616: currentRuleName = r;
0617: {
0618: if (_t == null)
0619: _t = ASTNULL;
0620: switch (_t.getType()) {
0621: case FRAGMENT:
0622: case LITERAL_protected:
0623: case LITERAL_public:
0624: case LITERAL_private: {
0625: mod = _t == ASTNULL ? null : (GrammarAST) _t;
0626: modifier(_t);
0627: _t = _retTree;
0628: break;
0629: }
0630: case ARG: {
0631: break;
0632: }
0633: default: {
0634: throw new NoViableAltException(_t);
0635: }
0636: }
0637: }
0638: AST __t26 = _t;
0639: GrammarAST tmp15_AST_in = (GrammarAST) _t;
0640: match(_t, ARG);
0641: _t = _t.getFirstChild();
0642: {
0643: if (_t == null)
0644: _t = ASTNULL;
0645: switch (_t.getType()) {
0646: case ARG_ACTION: {
0647: GrammarAST tmp16_AST_in = (GrammarAST) _t;
0648: match(_t, ARG_ACTION);
0649: _t = _t.getNextSibling();
0650: break;
0651: }
0652: case 3: {
0653: break;
0654: }
0655: default: {
0656: throw new NoViableAltException(_t);
0657: }
0658: }
0659: }
0660: _t = __t26;
0661: _t = _t.getNextSibling();
0662: AST __t28 = _t;
0663: GrammarAST tmp17_AST_in = (GrammarAST) _t;
0664: match(_t, RET);
0665: _t = _t.getFirstChild();
0666: {
0667: if (_t == null)
0668: _t = ASTNULL;
0669: switch (_t.getType()) {
0670: case ARG_ACTION: {
0671: GrammarAST tmp18_AST_in = (GrammarAST) _t;
0672: match(_t, ARG_ACTION);
0673: _t = _t.getNextSibling();
0674: break;
0675: }
0676: case 3: {
0677: break;
0678: }
0679: default: {
0680: throw new NoViableAltException(_t);
0681: }
0682: }
0683: }
0684: _t = __t28;
0685: _t = _t.getNextSibling();
0686: {
0687: if (_t == null)
0688: _t = ASTNULL;
0689: switch (_t.getType()) {
0690: case OPTIONS: {
0691: AST __t31 = _t;
0692: GrammarAST tmp19_AST_in = (GrammarAST) _t;
0693: match(_t, OPTIONS);
0694: _t = _t.getFirstChild();
0695: GrammarAST tmp20_AST_in = (GrammarAST) _t;
0696: if (_t == null)
0697: throw new MismatchedTokenException();
0698: _t = _t.getNextSibling();
0699: _t = __t31;
0700: _t = _t.getNextSibling();
0701: break;
0702: }
0703: case BLOCK:
0704: case SCOPE:
0705: case AMPERSAND: {
0706: break;
0707: }
0708: default: {
0709: throw new NoViableAltException(_t);
0710: }
0711: }
0712: }
0713: {
0714: if (_t == null)
0715: _t = ASTNULL;
0716: switch (_t.getType()) {
0717: case SCOPE: {
0718: ruleScopeSpec(_t);
0719: _t = _retTree;
0720: break;
0721: }
0722: case BLOCK:
0723: case AMPERSAND: {
0724: break;
0725: }
0726: default: {
0727: throw new NoViableAltException(_t);
0728: }
0729: }
0730: }
0731: {
0732: _loop34: do {
0733: if (_t == null)
0734: _t = ASTNULL;
0735: if ((_t.getType() == AMPERSAND)) {
0736: GrammarAST tmp21_AST_in = (GrammarAST) _t;
0737: match(_t, AMPERSAND);
0738: _t = _t.getNextSibling();
0739: } else {
0740: break _loop34;
0741: }
0742:
0743: } while (true);
0744: }
0745: b = block(_t, "ruleBlock", dfa);
0746: _t = _retTree;
0747:
0748: String description = grammar.grammarTreeToString(
0749: rule_AST_in.getFirstChildWithType(BLOCK), false);
0750: description = generator.target
0751: .getTargetStringLiteralFromString(description);
0752: b.setAttribute("description", description);
0753: // do not generate lexer rules in combined grammar
0754: String stName = null;
0755: if (ruleDescr.isSynPred) {
0756: stName = "synpredRule";
0757: } else if (grammar.type == Grammar.LEXER) {
0758: if (r.equals(Grammar.ARTIFICIAL_TOKENS_RULENAME)) {
0759: stName = "tokensRule";
0760: } else {
0761: stName = "lexerRule";
0762: }
0763: } else {
0764: if (!(grammar.type == Grammar.COMBINED && Character
0765: .isUpperCase(r.charAt(0)))) {
0766: stName = "rule";
0767: }
0768: }
0769: code = templates.getInstanceOf(stName);
0770: if (code.getName().equals("rule")) {
0771: code.setAttribute("emptyRule", Boolean.valueOf(grammar
0772: .isEmptyRule(block)));
0773: }
0774: code.setAttribute("ruleDescriptor", ruleDescr);
0775: String memo = (String) rule_AST_in.getOption("memoize");
0776: if (memo == null) {
0777: memo = (String) grammar.getOption("memoize");
0778: }
0779: if (memo != null
0780: && memo.equals("true")
0781: && (stName.equals("rule") || stName
0782: .equals("lexerRule"))) {
0783: code.setAttribute("memoize", Boolean
0784: .valueOf(memo != null && memo.equals("true")));
0785: }
0786:
0787: {
0788: if (_t == null)
0789: _t = ASTNULL;
0790: switch (_t.getType()) {
0791: case LITERAL_catch:
0792: case LITERAL_finally: {
0793: exceptionGroup(_t, code);
0794: _t = _retTree;
0795: break;
0796: }
0797: case EOR: {
0798: break;
0799: }
0800: default: {
0801: throw new NoViableAltException(_t);
0802: }
0803: }
0804: }
0805: GrammarAST tmp22_AST_in = (GrammarAST) _t;
0806: match(_t, EOR);
0807: _t = _t.getNextSibling();
0808: _t = __t24;
0809: _t = _t.getNextSibling();
0810:
0811: if (code != null) {
0812: if (grammar.type == Grammar.LEXER) {
0813: boolean naked = r
0814: .equals(Grammar.ARTIFICIAL_TOKENS_RULENAME)
0815: || (mod != null && mod.getText().equals(
0816: Grammar.FRAGMENT_RULE_MODIFIER));
0817: code.setAttribute("nakedBlock", Boolean
0818: .valueOf(naked));
0819: } else {
0820: description = grammar.grammarTreeToString(
0821: rule_AST_in, false);
0822: description = generator.target
0823: .getTargetStringLiteralFromString(description);
0824: code.setAttribute("description", description);
0825: }
0826: Rule theRule = grammar.getRule(r);
0827: generator
0828: .translateActionAttributeReferencesForSingleScope(
0829: theRule, theRule.getActions());
0830: code.setAttribute("ruleName", r);
0831: code.setAttribute("block", b);
0832: if (initAction != null) {
0833: code.setAttribute("initAction", initAction);
0834: }
0835: }
0836: templates = saveGroup;
0837:
0838: } catch (RecognitionException ex) {
0839: reportError(ex);
0840: if (_t != null) {
0841: _t = _t.getNextSibling();
0842: }
0843: }
0844: _retTree = _t;
0845: return code;
0846: }
0847:
0848: public final void modifier(AST _t) throws RecognitionException {
0849:
0850: GrammarAST modifier_AST_in = (_t == ASTNULL) ? null
0851: : (GrammarAST) _t;
0852:
0853: try { // for error handling
0854: if (_t == null)
0855: _t = ASTNULL;
0856: switch (_t.getType()) {
0857: case LITERAL_protected: {
0858: GrammarAST tmp23_AST_in = (GrammarAST) _t;
0859: match(_t, LITERAL_protected);
0860: _t = _t.getNextSibling();
0861: break;
0862: }
0863: case LITERAL_public: {
0864: GrammarAST tmp24_AST_in = (GrammarAST) _t;
0865: match(_t, LITERAL_public);
0866: _t = _t.getNextSibling();
0867: break;
0868: }
0869: case LITERAL_private: {
0870: GrammarAST tmp25_AST_in = (GrammarAST) _t;
0871: match(_t, LITERAL_private);
0872: _t = _t.getNextSibling();
0873: break;
0874: }
0875: case FRAGMENT: {
0876: GrammarAST tmp26_AST_in = (GrammarAST) _t;
0877: match(_t, FRAGMENT);
0878: _t = _t.getNextSibling();
0879: break;
0880: }
0881: default: {
0882: throw new NoViableAltException(_t);
0883: }
0884: }
0885: } catch (RecognitionException ex) {
0886: reportError(ex);
0887: if (_t != null) {
0888: _t = _t.getNextSibling();
0889: }
0890: }
0891: _retTree = _t;
0892: }
0893:
0894: public final void ruleScopeSpec(AST _t) throws RecognitionException {
0895:
0896: GrammarAST ruleScopeSpec_AST_in = (_t == ASTNULL) ? null
0897: : (GrammarAST) _t;
0898:
0899: try { // for error handling
0900: AST __t38 = _t;
0901: GrammarAST tmp27_AST_in = (GrammarAST) _t;
0902: match(_t, SCOPE);
0903: _t = _t.getFirstChild();
0904: {
0905: if (_t == null)
0906: _t = ASTNULL;
0907: switch (_t.getType()) {
0908: case ACTION: {
0909: GrammarAST tmp28_AST_in = (GrammarAST) _t;
0910: match(_t, ACTION);
0911: _t = _t.getNextSibling();
0912: break;
0913: }
0914: case 3:
0915: case ID: {
0916: break;
0917: }
0918: default: {
0919: throw new NoViableAltException(_t);
0920: }
0921: }
0922: }
0923: {
0924: _loop41: do {
0925: if (_t == null)
0926: _t = ASTNULL;
0927: if ((_t.getType() == ID)) {
0928: GrammarAST tmp29_AST_in = (GrammarAST) _t;
0929: match(_t, ID);
0930: _t = _t.getNextSibling();
0931: } else {
0932: break _loop41;
0933: }
0934:
0935: } while (true);
0936: }
0937: _t = __t38;
0938: _t = _t.getNextSibling();
0939: } catch (RecognitionException ex) {
0940: reportError(ex);
0941: if (_t != null) {
0942: _t = _t.getNextSibling();
0943: }
0944: }
0945: _retTree = _t;
0946: }
0947:
0948: public final StringTemplate block(AST _t, String blockTemplateName,
0949: DFA dfa) throws RecognitionException {
0950: StringTemplate code = null;
0951:
0952: GrammarAST block_AST_in = (_t == ASTNULL) ? null
0953: : (GrammarAST) _t;
0954:
0955: StringTemplate decision = null;
0956: if (dfa != null) {
0957: code = templates.getInstanceOf(blockTemplateName);
0958: decision = generator
0959: .genLookaheadDecision(recognizerST, dfa);
0960: code.setAttribute("decision", decision);
0961: code
0962: .setAttribute("decisionNumber", dfa
0963: .getDecisionNumber());
0964: code.setAttribute("maxK", dfa.getMaxLookaheadDepth());
0965: code.setAttribute("maxAlt", dfa.getNumberOfAlts());
0966: } else {
0967: code = templates.getInstanceOf(blockTemplateName
0968: + "SingleAlt");
0969: }
0970: blockNestingLevel++;
0971: code.setAttribute("blockLevel", blockNestingLevel);
0972: code.setAttribute("enclosingBlockLevel", blockNestingLevel - 1);
0973: StringTemplate alt = null;
0974: StringTemplate rew = null;
0975: StringTemplate sb = null;
0976: GrammarAST r = null;
0977: int altNum = 1;
0978: if (this .blockNestingLevel == RULE_BLOCK_NESTING_LEVEL) {
0979: this .outerAltNum = 1;
0980: }
0981:
0982: try { // for error handling
0983: if (_t == null)
0984: _t = ASTNULL;
0985: if (((_t.getType() == BLOCK))
0986: && (block_AST_in.getSetValue() != null)) {
0987: sb = setBlock(_t);
0988: _t = _retTree;
0989:
0990: code.setAttribute("alts", sb);
0991: blockNestingLevel--;
0992:
0993: } else if ((_t.getType() == BLOCK)) {
0994: AST __t43 = _t;
0995: GrammarAST tmp30_AST_in = (GrammarAST) _t;
0996: match(_t, BLOCK);
0997: _t = _t.getFirstChild();
0998: {
0999: if (_t == null)
1000: _t = ASTNULL;
1001: switch (_t.getType()) {
1002: case OPTIONS: {
1003: GrammarAST tmp31_AST_in = (GrammarAST) _t;
1004: match(_t, OPTIONS);
1005: _t = _t.getNextSibling();
1006: break;
1007: }
1008: case ALT: {
1009: break;
1010: }
1011: default: {
1012: throw new NoViableAltException(_t);
1013: }
1014: }
1015: }
1016: {
1017: int _cnt46 = 0;
1018: _loop46: do {
1019: if (_t == null)
1020: _t = ASTNULL;
1021: if ((_t.getType() == ALT)) {
1022: alt = alternative(_t);
1023: _t = _retTree;
1024: r = (GrammarAST) _t;
1025: rew = rewrite(_t);
1026: _t = _retTree;
1027:
1028: if (this .blockNestingLevel == RULE_BLOCK_NESTING_LEVEL) {
1029: this .outerAltNum++;
1030: }
1031: // add the rewrite code as just another element in the alt :)
1032: if (rew != null) {
1033: alt.setAttribute(
1034: "elements.{el,line,pos}", rew,
1035: Utils.integer(r.getLine()),
1036: Utils.integer(r.getColumn()));
1037: }
1038: // add this alt to the list of alts for this block
1039: code.setAttribute("alts", alt);
1040: alt.setAttribute("altNum", Utils
1041: .integer(altNum));
1042: alt
1043: .setAttribute(
1044: "outerAlt",
1045: Boolean
1046: .valueOf(blockNestingLevel == RULE_BLOCK_NESTING_LEVEL));
1047: altNum++;
1048:
1049: } else {
1050: if (_cnt46 >= 1) {
1051: break _loop46;
1052: } else {
1053: throw new NoViableAltException(_t);
1054: }
1055: }
1056:
1057: _cnt46++;
1058: } while (true);
1059: }
1060: GrammarAST tmp32_AST_in = (GrammarAST) _t;
1061: match(_t, EOB);
1062: _t = _t.getNextSibling();
1063: _t = __t43;
1064: _t = _t.getNextSibling();
1065: blockNestingLevel--;
1066: } else {
1067: throw new NoViableAltException(_t);
1068: }
1069:
1070: } catch (RecognitionException ex) {
1071: reportError(ex);
1072: if (_t != null) {
1073: _t = _t.getNextSibling();
1074: }
1075: }
1076: _retTree = _t;
1077: return code;
1078: }
1079:
1080: public final void exceptionGroup(AST _t, StringTemplate ruleST)
1081: throws RecognitionException {
1082:
1083: GrammarAST exceptionGroup_AST_in = (_t == ASTNULL) ? null
1084: : (GrammarAST) _t;
1085:
1086: try { // for error handling
1087: if (_t == null)
1088: _t = ASTNULL;
1089: switch (_t.getType()) {
1090: case LITERAL_catch: {
1091: {
1092: int _cnt50 = 0;
1093: _loop50: do {
1094: if (_t == null)
1095: _t = ASTNULL;
1096: if ((_t.getType() == LITERAL_catch)) {
1097: exceptionHandler(_t, ruleST);
1098: _t = _retTree;
1099: } else {
1100: if (_cnt50 >= 1) {
1101: break _loop50;
1102: } else {
1103: throw new NoViableAltException(_t);
1104: }
1105: }
1106:
1107: _cnt50++;
1108: } while (true);
1109: }
1110: {
1111: if (_t == null)
1112: _t = ASTNULL;
1113: switch (_t.getType()) {
1114: case LITERAL_finally: {
1115: finallyClause(_t, ruleST);
1116: _t = _retTree;
1117: break;
1118: }
1119: case EOR: {
1120: break;
1121: }
1122: default: {
1123: throw new NoViableAltException(_t);
1124: }
1125: }
1126: }
1127: break;
1128: }
1129: case LITERAL_finally: {
1130: finallyClause(_t, ruleST);
1131: _t = _retTree;
1132: break;
1133: }
1134: default: {
1135: throw new NoViableAltException(_t);
1136: }
1137: }
1138: } catch (RecognitionException ex) {
1139: reportError(ex);
1140: if (_t != null) {
1141: _t = _t.getNextSibling();
1142: }
1143: }
1144: _retTree = _t;
1145: }
1146:
1147: public final StringTemplate setBlock(AST _t)
1148: throws RecognitionException {
1149: StringTemplate code = null;
1150:
1151: GrammarAST setBlock_AST_in = (_t == ASTNULL) ? null
1152: : (GrammarAST) _t;
1153: GrammarAST s = null;
1154:
1155: if (blockNestingLevel == RULE_BLOCK_NESTING_LEVEL
1156: && grammar.buildAST()) {
1157: Rule r = grammar.getRule(currentRuleName);
1158: currentAltHasASTRewrite = r.hasRewrite(outerAltNum);
1159: if (currentAltHasASTRewrite) {
1160: r
1161: .trackTokenReferenceInAlt(setBlock_AST_in,
1162: outerAltNum);
1163: }
1164: }
1165:
1166: try { // for error handling
1167: s = (GrammarAST) _t;
1168: match(_t, BLOCK);
1169: _t = _t.getNextSibling();
1170:
1171: StringTemplate setcode = getTokenElementST("matchSet",
1172: "set", s, null, null);
1173: int i = ((TokenWithIndex) s.getToken()).getIndex();
1174: setcode.setAttribute("elementIndex", i);
1175: if (grammar.type != Grammar.LEXER) {
1176: generator.generateLocalFOLLOW(s, "set",
1177: currentRuleName, i);
1178: }
1179: setcode.setAttribute("s", generator.genSetExpr(templates, s
1180: .getSetValue(), 1, false));
1181: StringTemplate altcode = templates.getInstanceOf("alt");
1182: altcode.setAttribute("elements.{el,line,pos}", setcode,
1183: Utils.integer(s.getLine()), Utils.integer(s
1184: .getColumn()));
1185: altcode.setAttribute("altNum", Utils.integer(1));
1186: altcode
1187: .setAttribute(
1188: "outerAlt",
1189: Boolean
1190: .valueOf(blockNestingLevel == RULE_BLOCK_NESTING_LEVEL));
1191: if (!currentAltHasASTRewrite && grammar.buildAST()) {
1192: altcode.setAttribute("autoAST", Boolean.valueOf(true));
1193: }
1194: code = altcode;
1195:
1196: } catch (RecognitionException ex) {
1197: reportError(ex);
1198: if (_t != null) {
1199: _t = _t.getNextSibling();
1200: }
1201: }
1202: _retTree = _t;
1203: return code;
1204: }
1205:
1206: public final StringTemplate alternative(AST _t)
1207: throws RecognitionException {
1208: StringTemplate code = templates.getInstanceOf("alt");
1209:
1210: GrammarAST alternative_AST_in = (_t == ASTNULL) ? null
1211: : (GrammarAST) _t;
1212: GrammarAST a = null;
1213:
1214: /*
1215: // TODO: can we use Rule.altsWithRewrites???
1216: if ( blockNestingLevel==RULE_BLOCK_NESTING_LEVEL ) {
1217: GrammarAST aRewriteNode = #alternative.findFirstType(REWRITE);
1218: if ( grammar.buildAST() &&
1219: (aRewriteNode!=null||
1220: (#alternative.getNextSibling()!=null &&
1221: #alternative.getNextSibling().getType()==REWRITE)) )
1222: {
1223: currentAltHasASTRewrite = true;
1224: }
1225: else {
1226: currentAltHasASTRewrite = false;
1227: }
1228: }
1229: */
1230: if (blockNestingLevel == RULE_BLOCK_NESTING_LEVEL
1231: && grammar.buildAST()) {
1232: Rule r = grammar.getRule(currentRuleName);
1233: currentAltHasASTRewrite = r.hasRewrite(outerAltNum);
1234: }
1235: String description = grammar.grammarTreeToString(
1236: alternative_AST_in, false);
1237: description = generator.target
1238: .getTargetStringLiteralFromString(description);
1239: code.setAttribute("description", description);
1240: if (!currentAltHasASTRewrite && grammar.buildAST()) {
1241: code.setAttribute("autoAST", Boolean.valueOf(true));
1242: }
1243: StringTemplate e;
1244:
1245: try { // for error handling
1246: AST __t57 = _t;
1247: a = _t == ASTNULL ? null : (GrammarAST) _t;
1248: match(_t, ALT);
1249: _t = _t.getFirstChild();
1250: {
1251: int _cnt59 = 0;
1252: _loop59: do {
1253: if (_t == null)
1254: _t = ASTNULL;
1255: if ((_t.getType() == BLOCK
1256: || _t.getType() == OPTIONAL
1257: || _t.getType() == CLOSURE
1258: || _t.getType() == POSITIVE_CLOSURE
1259: || _t.getType() == CHAR_RANGE
1260: || _t.getType() == EPSILON
1261: || _t.getType() == GATED_SEMPRED
1262: || _t.getType() == SYN_SEMPRED
1263: || _t.getType() == BACKTRACK_SEMPRED
1264: || _t.getType() == ACTION
1265: || _t.getType() == ASSIGN
1266: || _t.getType() == STRING_LITERAL
1267: || _t.getType() == CHAR_LITERAL
1268: || _t.getType() == TOKEN_REF
1269: || _t.getType() == BANG
1270: || _t.getType() == PLUS_ASSIGN
1271: || _t.getType() == SEMPRED
1272: || _t.getType() == ROOT
1273: || _t.getType() == RULE_REF
1274: || _t.getType() == NOT
1275: || _t.getType() == TREE_BEGIN || _t
1276: .getType() == WILDCARD)) {
1277: GrammarAST elAST = (GrammarAST) _t;
1278: e = element(_t, null, null);
1279: _t = _retTree;
1280:
1281: if (e != null) {
1282: code.setAttribute("elements.{el,line,pos}",
1283: e, Utils.integer(elAST.getLine()),
1284: Utils.integer(elAST.getColumn()));
1285: }
1286:
1287: } else {
1288: if (_cnt59 >= 1) {
1289: break _loop59;
1290: } else {
1291: throw new NoViableAltException(_t);
1292: }
1293: }
1294:
1295: _cnt59++;
1296: } while (true);
1297: }
1298: GrammarAST tmp33_AST_in = (GrammarAST) _t;
1299: match(_t, EOA);
1300: _t = _t.getNextSibling();
1301: _t = __t57;
1302: _t = _t.getNextSibling();
1303: } catch (RecognitionException ex) {
1304: reportError(ex);
1305: if (_t != null) {
1306: _t = _t.getNextSibling();
1307: }
1308: }
1309: _retTree = _t;
1310: return code;
1311: }
1312:
1313: public final StringTemplate rewrite(AST _t)
1314: throws RecognitionException {
1315: StringTemplate code = null;
1316:
1317: GrammarAST rewrite_AST_in = (_t == ASTNULL) ? null
1318: : (GrammarAST) _t;
1319: GrammarAST r = null;
1320: GrammarAST pred = null;
1321:
1322: StringTemplate alt;
1323: if (rewrite_AST_in.getType() == REWRITE) {
1324: if (generator.grammar.buildTemplate()) {
1325: code = templates.getInstanceOf("rewriteTemplate");
1326: } else {
1327: code = templates.getInstanceOf("rewriteCode");
1328: code.setAttribute("treeLevel", Utils
1329: .integer(OUTER_REWRITE_NESTING_LEVEL));
1330: code.setAttribute("rewriteBlockLevel", Utils
1331: .integer(OUTER_REWRITE_NESTING_LEVEL));
1332: code
1333: .setAttribute(
1334: "referencedElementsDeep",
1335: getTokenTypesAsTargetLabels(rewrite_AST_in.rewriteRefsDeep));
1336: Set<String> tokenLabels = grammar.getLabels(
1337: rewrite_AST_in.rewriteRefsDeep,
1338: Grammar.TOKEN_LABEL);
1339: Set<String> tokenListLabels = grammar.getLabels(
1340: rewrite_AST_in.rewriteRefsDeep,
1341: Grammar.TOKEN_LIST_LABEL);
1342: Set<String> ruleLabels = grammar.getLabels(
1343: rewrite_AST_in.rewriteRefsDeep,
1344: Grammar.RULE_LABEL);
1345: Set<String> ruleListLabels = grammar.getLabels(
1346: rewrite_AST_in.rewriteRefsDeep,
1347: Grammar.RULE_LIST_LABEL);
1348: // just in case they ref $r for "previous value", make a stream
1349: // from retval.tree
1350: StringTemplate retvalST = templates
1351: .getInstanceOf("prevRuleRootRef");
1352: ruleLabels.add(retvalST.toString());
1353: code.setAttribute("referencedTokenLabels", tokenLabels);
1354: code.setAttribute("referencedTokenListLabels",
1355: tokenListLabels);
1356: code.setAttribute("referencedRuleLabels", ruleLabels);
1357: code.setAttribute("referencedRuleListLabels",
1358: ruleListLabels);
1359: }
1360: }
1361:
1362: try { // for error handling
1363: {
1364: _loop95: do {
1365: if (_t == null)
1366: _t = ASTNULL;
1367: if ((_t.getType() == REWRITE)) {
1368: rewriteRuleRefs = new HashSet();
1369: AST __t93 = _t;
1370: r = _t == ASTNULL ? null : (GrammarAST) _t;
1371: match(_t, REWRITE);
1372: _t = _t.getFirstChild();
1373: {
1374: if (_t == null)
1375: _t = ASTNULL;
1376: switch (_t.getType()) {
1377: case SEMPRED: {
1378: pred = (GrammarAST) _t;
1379: match(_t, SEMPRED);
1380: _t = _t.getNextSibling();
1381: break;
1382: }
1383: case ALT:
1384: case TEMPLATE:
1385: case ACTION: {
1386: break;
1387: }
1388: default: {
1389: throw new NoViableAltException(_t);
1390: }
1391: }
1392: }
1393: alt = rewrite_alternative(_t);
1394: _t = _retTree;
1395: _t = __t93;
1396: _t = _t.getNextSibling();
1397:
1398: rewriteBlockNestingLevel = OUTER_REWRITE_NESTING_LEVEL;
1399: List predChunks = null;
1400: if (pred != null) {
1401: //predText = #pred.getText();
1402: predChunks = generator.translateAction(
1403: currentRuleName, pred);
1404: }
1405: String description = grammar
1406: .grammarTreeToString(r, false);
1407: description = generator.target
1408: .getTargetStringLiteralFromString(description);
1409: code.setAttribute(
1410: "alts.{pred,alt,description}",
1411: predChunks, alt, description);
1412: pred = null;
1413:
1414: } else {
1415: break _loop95;
1416: }
1417:
1418: } while (true);
1419: }
1420: } catch (RecognitionException ex) {
1421: reportError(ex);
1422: if (_t != null) {
1423: _t = _t.getNextSibling();
1424: }
1425: }
1426: _retTree = _t;
1427: return code;
1428: }
1429:
1430: public final void exceptionHandler(AST _t, StringTemplate ruleST)
1431: throws RecognitionException {
1432:
1433: GrammarAST exceptionHandler_AST_in = (_t == ASTNULL) ? null
1434: : (GrammarAST) _t;
1435:
1436: try { // for error handling
1437: AST __t53 = _t;
1438: GrammarAST tmp34_AST_in = (GrammarAST) _t;
1439: match(_t, LITERAL_catch);
1440: _t = _t.getFirstChild();
1441: GrammarAST tmp35_AST_in = (GrammarAST) _t;
1442: match(_t, ARG_ACTION);
1443: _t = _t.getNextSibling();
1444: GrammarAST tmp36_AST_in = (GrammarAST) _t;
1445: match(_t, ACTION);
1446: _t = _t.getNextSibling();
1447: _t = __t53;
1448: _t = _t.getNextSibling();
1449:
1450: List chunks = generator.translateAction(currentRuleName,
1451: tmp36_AST_in);
1452: ruleST.setAttribute("exceptions.{decl,action}",
1453: tmp35_AST_in.getText(), chunks);
1454:
1455: } catch (RecognitionException ex) {
1456: reportError(ex);
1457: if (_t != null) {
1458: _t = _t.getNextSibling();
1459: }
1460: }
1461: _retTree = _t;
1462: }
1463:
1464: public final void finallyClause(AST _t, StringTemplate ruleST)
1465: throws RecognitionException {
1466:
1467: GrammarAST finallyClause_AST_in = (_t == ASTNULL) ? null
1468: : (GrammarAST) _t;
1469:
1470: try { // for error handling
1471: AST __t55 = _t;
1472: GrammarAST tmp37_AST_in = (GrammarAST) _t;
1473: match(_t, LITERAL_finally);
1474: _t = _t.getFirstChild();
1475: GrammarAST tmp38_AST_in = (GrammarAST) _t;
1476: match(_t, ACTION);
1477: _t = _t.getNextSibling();
1478: _t = __t55;
1479: _t = _t.getNextSibling();
1480:
1481: List chunks = generator.translateAction(currentRuleName,
1482: tmp38_AST_in);
1483: ruleST.setAttribute("finally", chunks);
1484:
1485: } catch (RecognitionException ex) {
1486: reportError(ex);
1487: if (_t != null) {
1488: _t = _t.getNextSibling();
1489: }
1490: }
1491: _retTree = _t;
1492: }
1493:
1494: public final StringTemplate element(AST _t, GrammarAST label,
1495: GrammarAST astSuffix) throws RecognitionException {
1496: StringTemplate code = null;
1497:
1498: GrammarAST element_AST_in = (_t == ASTNULL) ? null
1499: : (GrammarAST) _t;
1500: GrammarAST n = null;
1501: GrammarAST alabel = null;
1502: GrammarAST label2 = null;
1503: GrammarAST a = null;
1504: GrammarAST b = null;
1505: GrammarAST sp = null;
1506: GrammarAST gsp = null;
1507:
1508: IntSet elements = null;
1509: GrammarAST ast = null;
1510:
1511: try { // for error handling
1512: if (_t == null)
1513: _t = ASTNULL;
1514: switch (_t.getType()) {
1515: case ROOT: {
1516: AST __t61 = _t;
1517: GrammarAST tmp39_AST_in = (GrammarAST) _t;
1518: match(_t, ROOT);
1519: _t = _t.getFirstChild();
1520: code = element(_t, label, tmp39_AST_in);
1521: _t = _retTree;
1522: _t = __t61;
1523: _t = _t.getNextSibling();
1524: break;
1525: }
1526: case BANG: {
1527: AST __t62 = _t;
1528: GrammarAST tmp40_AST_in = (GrammarAST) _t;
1529: match(_t, BANG);
1530: _t = _t.getFirstChild();
1531: code = element(_t, label, tmp40_AST_in);
1532: _t = _retTree;
1533: _t = __t62;
1534: _t = _t.getNextSibling();
1535: break;
1536: }
1537: case NOT: {
1538: AST __t63 = _t;
1539: n = _t == ASTNULL ? null : (GrammarAST) _t;
1540: match(_t, NOT);
1541: _t = _t.getFirstChild();
1542: code = notElement(_t, n, label, astSuffix);
1543: _t = _retTree;
1544: _t = __t63;
1545: _t = _t.getNextSibling();
1546: break;
1547: }
1548: case ASSIGN: {
1549: AST __t64 = _t;
1550: GrammarAST tmp41_AST_in = (GrammarAST) _t;
1551: match(_t, ASSIGN);
1552: _t = _t.getFirstChild();
1553: alabel = (GrammarAST) _t;
1554: match(_t, ID);
1555: _t = _t.getNextSibling();
1556: code = element(_t, alabel, astSuffix);
1557: _t = _retTree;
1558: _t = __t64;
1559: _t = _t.getNextSibling();
1560: break;
1561: }
1562: case PLUS_ASSIGN: {
1563: AST __t65 = _t;
1564: GrammarAST tmp42_AST_in = (GrammarAST) _t;
1565: match(_t, PLUS_ASSIGN);
1566: _t = _t.getFirstChild();
1567: label2 = (GrammarAST) _t;
1568: match(_t, ID);
1569: _t = _t.getNextSibling();
1570: code = element(_t, label2, astSuffix);
1571: _t = _retTree;
1572: _t = __t65;
1573: _t = _t.getNextSibling();
1574: break;
1575: }
1576: case CHAR_RANGE: {
1577: AST __t66 = _t;
1578: GrammarAST tmp43_AST_in = (GrammarAST) _t;
1579: match(_t, CHAR_RANGE);
1580: _t = _t.getFirstChild();
1581: a = (GrammarAST) _t;
1582: match(_t, CHAR_LITERAL);
1583: _t = _t.getNextSibling();
1584: b = (GrammarAST) _t;
1585: match(_t, CHAR_LITERAL);
1586: _t = _t.getNextSibling();
1587: _t = __t66;
1588: _t = _t.getNextSibling();
1589: code = templates.getInstanceOf("charRangeRef");
1590: String low = generator.target
1591: .getTargetCharLiteralFromANTLRCharLiteral(
1592: generator, a.getText());
1593: String high = generator.target
1594: .getTargetCharLiteralFromANTLRCharLiteral(
1595: generator, b.getText());
1596: code.setAttribute("a", low);
1597: code.setAttribute("b", high);
1598: if (label != null) {
1599: code.setAttribute("label", label.getText());
1600: }
1601:
1602: break;
1603: }
1604: case TREE_BEGIN: {
1605: code = tree(_t);
1606: _t = _retTree;
1607: break;
1608: }
1609: case ACTION: {
1610: code = element_action(_t);
1611: _t = _retTree;
1612: break;
1613: }
1614: case GATED_SEMPRED:
1615: case SEMPRED: {
1616: {
1617: if (_t == null)
1618: _t = ASTNULL;
1619: switch (_t.getType()) {
1620: case SEMPRED: {
1621: sp = (GrammarAST) _t;
1622: match(_t, SEMPRED);
1623: _t = _t.getNextSibling();
1624: break;
1625: }
1626: case GATED_SEMPRED: {
1627: gsp = (GrammarAST) _t;
1628: match(_t, GATED_SEMPRED);
1629: _t = _t.getNextSibling();
1630: sp = gsp;
1631: break;
1632: }
1633: default: {
1634: throw new NoViableAltException(_t);
1635: }
1636: }
1637: }
1638:
1639: code = templates
1640: .getInstanceOf("validateSemanticPredicate");
1641: code.setAttribute("pred", generator.translateAction(
1642: currentRuleName, sp));
1643: String description = generator.target
1644: .getTargetStringLiteralFromString(sp.getText());
1645: code.setAttribute("description", description);
1646:
1647: break;
1648: }
1649: case SYN_SEMPRED: {
1650: GrammarAST tmp44_AST_in = (GrammarAST) _t;
1651: match(_t, SYN_SEMPRED);
1652: _t = _t.getNextSibling();
1653: break;
1654: }
1655: case BACKTRACK_SEMPRED: {
1656: GrammarAST tmp45_AST_in = (GrammarAST) _t;
1657: match(_t, BACKTRACK_SEMPRED);
1658: _t = _t.getNextSibling();
1659: break;
1660: }
1661: case EPSILON: {
1662: GrammarAST tmp46_AST_in = (GrammarAST) _t;
1663: match(_t, EPSILON);
1664: _t = _t.getNextSibling();
1665: break;
1666: }
1667: default:
1668: if (_t == null)
1669: _t = ASTNULL;
1670: if ((((_t.getType() >= BLOCK && _t.getType() <= POSITIVE_CLOSURE)))
1671: && (element_AST_in.getSetValue() == null)) {
1672: code = ebnf(_t);
1673: _t = _retTree;
1674: } else if ((_t.getType() == BLOCK
1675: || _t.getType() == STRING_LITERAL
1676: || _t.getType() == CHAR_LITERAL
1677: || _t.getType() == TOKEN_REF
1678: || _t.getType() == RULE_REF || _t.getType() == WILDCARD)) {
1679: code = atom(_t, label, astSuffix);
1680: _t = _retTree;
1681: } else {
1682: throw new NoViableAltException(_t);
1683: }
1684: }
1685: } catch (RecognitionException ex) {
1686: reportError(ex);
1687: if (_t != null) {
1688: _t = _t.getNextSibling();
1689: }
1690: }
1691: _retTree = _t;
1692: return code;
1693: }
1694:
1695: public final StringTemplate notElement(AST _t, GrammarAST n,
1696: GrammarAST label, GrammarAST astSuffix)
1697: throws RecognitionException {
1698: StringTemplate code = null;
1699:
1700: GrammarAST notElement_AST_in = (_t == ASTNULL) ? null
1701: : (GrammarAST) _t;
1702: GrammarAST assign_c = null;
1703: GrammarAST assign_s = null;
1704: GrammarAST assign_t = null;
1705: GrammarAST assign_st = null;
1706:
1707: IntSet elements = null;
1708: String labelText = null;
1709: if (label != null) {
1710: labelText = label.getText();
1711: }
1712:
1713: try { // for error handling
1714: {
1715: if (_t == null)
1716: _t = ASTNULL;
1717: switch (_t.getType()) {
1718: case CHAR_LITERAL: {
1719: assign_c = (GrammarAST) _t;
1720: match(_t, CHAR_LITERAL);
1721: _t = _t.getNextSibling();
1722:
1723: int ttype = 0;
1724: if (grammar.type == Grammar.LEXER) {
1725: ttype = Grammar
1726: .getCharValueFromGrammarCharLiteral(assign_c
1727: .getText());
1728: } else {
1729: ttype = grammar
1730: .getTokenType(assign_c.getText());
1731: }
1732: elements = grammar.complement(ttype);
1733:
1734: break;
1735: }
1736: case STRING_LITERAL: {
1737: assign_s = (GrammarAST) _t;
1738: match(_t, STRING_LITERAL);
1739: _t = _t.getNextSibling();
1740:
1741: int ttype = 0;
1742: if (grammar.type == Grammar.LEXER) {
1743: // TODO: error!
1744: } else {
1745: ttype = grammar
1746: .getTokenType(assign_s.getText());
1747: }
1748: elements = grammar.complement(ttype);
1749:
1750: break;
1751: }
1752: case TOKEN_REF: {
1753: assign_t = (GrammarAST) _t;
1754: match(_t, TOKEN_REF);
1755: _t = _t.getNextSibling();
1756:
1757: int ttype = grammar
1758: .getTokenType(assign_t.getText());
1759: elements = grammar.complement(ttype);
1760:
1761: break;
1762: }
1763: case BLOCK: {
1764: assign_st = (GrammarAST) _t;
1765: match(_t, BLOCK);
1766: _t = _t.getNextSibling();
1767:
1768: elements = assign_st.getSetValue();
1769: elements = grammar.complement(elements);
1770:
1771: break;
1772: }
1773: default: {
1774: throw new NoViableAltException(_t);
1775: }
1776: }
1777: }
1778:
1779: code = getTokenElementST("matchSet", "set", (GrammarAST) n
1780: .getFirstChild(), astSuffix, labelText);
1781: code.setAttribute("s", generator.genSetExpr(templates,
1782: elements, 1, false));
1783: int i = ((TokenWithIndex) n.getToken()).getIndex();
1784: code.setAttribute("elementIndex", i);
1785: if (grammar.type != Grammar.LEXER) {
1786: generator.generateLocalFOLLOW(n, "set",
1787: currentRuleName, i);
1788: }
1789:
1790: } catch (RecognitionException ex) {
1791: reportError(ex);
1792: if (_t != null) {
1793: _t = _t.getNextSibling();
1794: }
1795: }
1796: _retTree = _t;
1797: return code;
1798: }
1799:
1800: public final StringTemplate ebnf(AST _t)
1801: throws RecognitionException {
1802: StringTemplate code = null;
1803:
1804: GrammarAST ebnf_AST_in = (_t == ASTNULL) ? null
1805: : (GrammarAST) _t;
1806:
1807: DFA dfa = null;
1808: GrammarAST b = (GrammarAST) ebnf_AST_in.getFirstChild();
1809: GrammarAST eob = (GrammarAST) b.getLastChild(); // loops will use EOB DFA
1810:
1811: try { // for error handling
1812: {
1813: if (_t == null)
1814: _t = ASTNULL;
1815: switch (_t.getType()) {
1816: case BLOCK: {
1817: dfa = ebnf_AST_in.getLookaheadDFA();
1818: code = block(_t, "block", dfa);
1819: _t = _retTree;
1820: break;
1821: }
1822: case OPTIONAL: {
1823: dfa = ebnf_AST_in.getLookaheadDFA();
1824: AST __t73 = _t;
1825: GrammarAST tmp47_AST_in = (GrammarAST) _t;
1826: match(_t, OPTIONAL);
1827: _t = _t.getFirstChild();
1828: code = block(_t, "optionalBlock", dfa);
1829: _t = _retTree;
1830: _t = __t73;
1831: _t = _t.getNextSibling();
1832: break;
1833: }
1834: case CLOSURE: {
1835: dfa = eob.getLookaheadDFA();
1836: AST __t74 = _t;
1837: GrammarAST tmp48_AST_in = (GrammarAST) _t;
1838: match(_t, CLOSURE);
1839: _t = _t.getFirstChild();
1840: code = block(_t, "closureBlock", dfa);
1841: _t = _retTree;
1842: _t = __t74;
1843: _t = _t.getNextSibling();
1844: break;
1845: }
1846: case POSITIVE_CLOSURE: {
1847: dfa = eob.getLookaheadDFA();
1848: AST __t75 = _t;
1849: GrammarAST tmp49_AST_in = (GrammarAST) _t;
1850: match(_t, POSITIVE_CLOSURE);
1851: _t = _t.getFirstChild();
1852: code = block(_t, "positiveClosureBlock", dfa);
1853: _t = _retTree;
1854: _t = __t75;
1855: _t = _t.getNextSibling();
1856: break;
1857: }
1858: default: {
1859: throw new NoViableAltException(_t);
1860: }
1861: }
1862: }
1863:
1864: String description = grammar.grammarTreeToString(
1865: ebnf_AST_in, false);
1866: description = generator.target
1867: .getTargetStringLiteralFromString(description);
1868: code.setAttribute("description", description);
1869:
1870: } catch (RecognitionException ex) {
1871: reportError(ex);
1872: if (_t != null) {
1873: _t = _t.getNextSibling();
1874: }
1875: }
1876: _retTree = _t;
1877: return code;
1878: }
1879:
1880: public final StringTemplate atom(AST _t, GrammarAST label,
1881: GrammarAST astSuffix) throws RecognitionException {
1882: StringTemplate code = null;
1883:
1884: GrammarAST atom_AST_in = (_t == ASTNULL) ? null
1885: : (GrammarAST) _t;
1886: GrammarAST r = null;
1887: GrammarAST rarg = null;
1888: GrammarAST t = null;
1889: GrammarAST targ = null;
1890: GrammarAST c = null;
1891: GrammarAST s = null;
1892: GrammarAST w = null;
1893:
1894: String labelText = null;
1895: if (label != null) {
1896: labelText = label.getText();
1897: }
1898:
1899: try { // for error handling
1900: if (_t == null)
1901: _t = ASTNULL;
1902: switch (_t.getType()) {
1903: case RULE_REF: {
1904: AST __t83 = _t;
1905: r = _t == ASTNULL ? null : (GrammarAST) _t;
1906: match(_t, RULE_REF);
1907: _t = _t.getFirstChild();
1908: {
1909: if (_t == null)
1910: _t = ASTNULL;
1911: switch (_t.getType()) {
1912: case ARG_ACTION: {
1913: rarg = (GrammarAST) _t;
1914: match(_t, ARG_ACTION);
1915: _t = _t.getNextSibling();
1916: break;
1917: }
1918: case 3: {
1919: break;
1920: }
1921: default: {
1922: throw new NoViableAltException(_t);
1923: }
1924: }
1925: }
1926: _t = __t83;
1927: _t = _t.getNextSibling();
1928:
1929: grammar.checkRuleReference(r, rarg, currentRuleName);
1930: Rule rdef = grammar.getRule(r.getText());
1931: // don't insert label=r() if $label.attr not used, no ret value, ...
1932: if (!rdef.getHasReturnValue()) {
1933: labelText = null;
1934: }
1935: code = getRuleElementST("ruleRef", r.getText(), r,
1936: astSuffix, labelText);
1937: code.setAttribute("rule", r.getText());
1938:
1939: if (rarg != null) {
1940: List args = generator.translateAction(
1941: currentRuleName, rarg);
1942: code.setAttribute("args", args);
1943: }
1944: int i = ((TokenWithIndex) r.getToken()).getIndex();
1945: code.setAttribute("elementIndex", i);
1946: generator.generateLocalFOLLOW(r, r.getText(),
1947: currentRuleName, i);
1948: r.code = code;
1949:
1950: break;
1951: }
1952: case TOKEN_REF: {
1953: AST __t85 = _t;
1954: t = _t == ASTNULL ? null : (GrammarAST) _t;
1955: match(_t, TOKEN_REF);
1956: _t = _t.getFirstChild();
1957: {
1958: if (_t == null)
1959: _t = ASTNULL;
1960: switch (_t.getType()) {
1961: case ARG_ACTION: {
1962: targ = (GrammarAST) _t;
1963: match(_t, ARG_ACTION);
1964: _t = _t.getNextSibling();
1965: break;
1966: }
1967: case 3: {
1968: break;
1969: }
1970: default: {
1971: throw new NoViableAltException(_t);
1972: }
1973: }
1974: }
1975: _t = __t85;
1976: _t = _t.getNextSibling();
1977:
1978: grammar.checkRuleReference(t, targ, currentRuleName);
1979: if (grammar.type == Grammar.LEXER) {
1980: if (grammar.getTokenType(t.getText()) == Label.EOF) {
1981: code = templates.getInstanceOf("lexerMatchEOF");
1982: } else {
1983: code = templates.getInstanceOf("lexerRuleRef");
1984: if (isListLabel(labelText)) {
1985: code = templates
1986: .getInstanceOf("lexerRuleRefAndListLabel");
1987: }
1988: code.setAttribute("rule", t.getText());
1989: if (targ != null) {
1990: List args = generator.translateAction(
1991: currentRuleName, targ);
1992: code.setAttribute("args", args);
1993: }
1994: }
1995: int i = ((TokenWithIndex) t.getToken()).getIndex();
1996: code.setAttribute("elementIndex", i);
1997: if (label != null)
1998: code.setAttribute("label", labelText);
1999: } else {
2000: code = getTokenElementST("tokenRef", t.getText(),
2001: t, astSuffix, labelText);
2002: String tokenLabel = generator
2003: .getTokenTypeAsTargetLabel(grammar
2004: .getTokenType(t.getText()));
2005: code.setAttribute("token", tokenLabel);
2006: int i = ((TokenWithIndex) t.getToken()).getIndex();
2007: code.setAttribute("elementIndex", i);
2008: generator.generateLocalFOLLOW(t, tokenLabel,
2009: currentRuleName, i);
2010: }
2011: t.code = code;
2012:
2013: break;
2014: }
2015: case CHAR_LITERAL: {
2016: c = (GrammarAST) _t;
2017: match(_t, CHAR_LITERAL);
2018: _t = _t.getNextSibling();
2019:
2020: if (grammar.type == Grammar.LEXER) {
2021: code = templates.getInstanceOf("charRef");
2022: code.setAttribute("char", generator.target
2023: .getTargetCharLiteralFromANTLRCharLiteral(
2024: generator, c.getText()));
2025: if (label != null) {
2026: code.setAttribute("label", labelText);
2027: }
2028: } else { // else it's a token type reference
2029: code = getTokenElementST("tokenRef",
2030: "char_literal", c, astSuffix, labelText);
2031: String tokenLabel = generator
2032: .getTokenTypeAsTargetLabel(grammar
2033: .getTokenType(c.getText()));
2034: code.setAttribute("token", tokenLabel);
2035: int i = ((TokenWithIndex) c.getToken()).getIndex();
2036: code.setAttribute("elementIndex", i);
2037: generator.generateLocalFOLLOW(c, tokenLabel,
2038: currentRuleName, i);
2039: }
2040:
2041: break;
2042: }
2043: case STRING_LITERAL: {
2044: s = (GrammarAST) _t;
2045: match(_t, STRING_LITERAL);
2046: _t = _t.getNextSibling();
2047:
2048: if (grammar.type == Grammar.LEXER) {
2049: code = templates.getInstanceOf("lexerStringRef");
2050: code
2051: .setAttribute(
2052: "string",
2053: generator.target
2054: .getTargetStringLiteralFromANTLRStringLiteral(
2055: generator, s
2056: .getText()));
2057: if (label != null) {
2058: code.setAttribute("label", labelText);
2059: }
2060: } else { // else it's a token type reference
2061: code = getTokenElementST("tokenRef",
2062: "string_literal", s, astSuffix, labelText);
2063: String tokenLabel = generator
2064: .getTokenTypeAsTargetLabel(grammar
2065: .getTokenType(s.getText()));
2066: code.setAttribute("token", tokenLabel);
2067: int i = ((TokenWithIndex) s.getToken()).getIndex();
2068: code.setAttribute("elementIndex", i);
2069: generator.generateLocalFOLLOW(s, tokenLabel,
2070: currentRuleName, i);
2071: }
2072:
2073: break;
2074: }
2075: case WILDCARD: {
2076: w = (GrammarAST) _t;
2077: match(_t, WILDCARD);
2078: _t = _t.getNextSibling();
2079:
2080: code = getWildcardST(w, astSuffix, labelText);
2081: code.setAttribute("elementIndex", ((TokenWithIndex) w
2082: .getToken()).getIndex());
2083:
2084: break;
2085: }
2086: case BLOCK: {
2087: code = set(_t, label, astSuffix);
2088: _t = _retTree;
2089: break;
2090: }
2091: default: {
2092: throw new NoViableAltException(_t);
2093: }
2094: }
2095: } catch (RecognitionException ex) {
2096: reportError(ex);
2097: if (_t != null) {
2098: _t = _t.getNextSibling();
2099: }
2100: }
2101: _retTree = _t;
2102: return code;
2103: }
2104:
2105: public final StringTemplate tree(AST _t)
2106: throws RecognitionException {
2107: StringTemplate code = templates.getInstanceOf("tree");
2108:
2109: GrammarAST tree_AST_in = (_t == ASTNULL) ? null
2110: : (GrammarAST) _t;
2111:
2112: StringTemplate el = null, act = null;
2113: GrammarAST elAST = null, actAST = null;
2114: NFAState afterDOWN = (NFAState) tree_AST_in.NFATreeDownState
2115: .transition(0).target;
2116: LookaheadSet s = grammar.LOOK(afterDOWN);
2117: if (s.member(Label.UP)) {
2118: // nullable child list if we can see the UP as the next token
2119: // we need an "if ( input.LA(1)==Token.DOWN )" gate around
2120: // the child list.
2121: code.setAttribute("nullableChildList", "true");
2122: }
2123:
2124: try { // for error handling
2125: AST __t77 = _t;
2126: GrammarAST tmp50_AST_in = (GrammarAST) _t;
2127: match(_t, TREE_BEGIN);
2128: _t = _t.getFirstChild();
2129: elAST = (GrammarAST) _t;
2130: el = element(_t, null, null);
2131: _t = _retTree;
2132:
2133: code.setAttribute("root.{el,line,pos}", el, Utils
2134: .integer(elAST.getLine()), Utils.integer(elAST
2135: .getColumn()));
2136:
2137: {
2138: _loop79: do {
2139: if (_t == null)
2140: _t = ASTNULL;
2141: if ((_t.getType() == ACTION)) {
2142: actAST = (GrammarAST) _t;
2143: act = element_action(_t);
2144: _t = _retTree;
2145:
2146: code.setAttribute(
2147: "actionsAfterRoot.{el,line,pos}", act,
2148: Utils.integer(actAST.getLine()), Utils
2149: .integer(actAST.getColumn()));
2150:
2151: } else {
2152: break _loop79;
2153: }
2154:
2155: } while (true);
2156: }
2157: {
2158: _loop81: do {
2159: if (_t == null)
2160: _t = ASTNULL;
2161: if ((_t.getType() == BLOCK
2162: || _t.getType() == OPTIONAL
2163: || _t.getType() == CLOSURE
2164: || _t.getType() == POSITIVE_CLOSURE
2165: || _t.getType() == CHAR_RANGE
2166: || _t.getType() == EPSILON
2167: || _t.getType() == GATED_SEMPRED
2168: || _t.getType() == SYN_SEMPRED
2169: || _t.getType() == BACKTRACK_SEMPRED
2170: || _t.getType() == ACTION
2171: || _t.getType() == ASSIGN
2172: || _t.getType() == STRING_LITERAL
2173: || _t.getType() == CHAR_LITERAL
2174: || _t.getType() == TOKEN_REF
2175: || _t.getType() == BANG
2176: || _t.getType() == PLUS_ASSIGN
2177: || _t.getType() == SEMPRED
2178: || _t.getType() == ROOT
2179: || _t.getType() == RULE_REF
2180: || _t.getType() == NOT
2181: || _t.getType() == TREE_BEGIN || _t
2182: .getType() == WILDCARD)) {
2183: elAST = (GrammarAST) _t;
2184: el = element(_t, null, null);
2185: _t = _retTree;
2186:
2187: code.setAttribute("children.{el,line,pos}", el,
2188: Utils.integer(elAST.getLine()), Utils
2189: .integer(elAST.getColumn()));
2190:
2191: } else {
2192: break _loop81;
2193: }
2194:
2195: } while (true);
2196: }
2197: _t = __t77;
2198: _t = _t.getNextSibling();
2199: } catch (RecognitionException ex) {
2200: reportError(ex);
2201: if (_t != null) {
2202: _t = _t.getNextSibling();
2203: }
2204: }
2205: _retTree = _t;
2206: return code;
2207: }
2208:
2209: public final StringTemplate element_action(AST _t)
2210: throws RecognitionException {
2211: StringTemplate code = null;
2212:
2213: GrammarAST element_action_AST_in = (_t == ASTNULL) ? null
2214: : (GrammarAST) _t;
2215: GrammarAST act = null;
2216:
2217: try { // for error handling
2218: act = (GrammarAST) _t;
2219: match(_t, ACTION);
2220: _t = _t.getNextSibling();
2221:
2222: code = templates.getInstanceOf("execAction");
2223: code.setAttribute("action", generator.translateAction(
2224: currentRuleName, act));
2225:
2226: } catch (RecognitionException ex) {
2227: reportError(ex);
2228: if (_t != null) {
2229: _t = _t.getNextSibling();
2230: }
2231: }
2232: _retTree = _t;
2233: return code;
2234: }
2235:
2236: public final StringTemplate set(AST _t, GrammarAST label,
2237: GrammarAST astSuffix) throws RecognitionException {
2238: StringTemplate code = null;
2239:
2240: GrammarAST set_AST_in = (_t == ASTNULL) ? null
2241: : (GrammarAST) _t;
2242: GrammarAST s = null;
2243:
2244: String labelText = null;
2245: if (label != null) {
2246: labelText = label.getText();
2247: }
2248:
2249: try { // for error handling
2250: s = (GrammarAST) _t;
2251: match(_t, BLOCK);
2252: _t = _t.getNextSibling();
2253:
2254: code = getTokenElementST("matchSet", "set", s, astSuffix,
2255: labelText);
2256: int i = ((TokenWithIndex) s.getToken()).getIndex();
2257: code.setAttribute("elementIndex", i);
2258: if (grammar.type != Grammar.LEXER) {
2259: generator.generateLocalFOLLOW(s, "set",
2260: currentRuleName, i);
2261: }
2262: code.setAttribute("s", generator.genSetExpr(templates, s
2263: .getSetValue(), 1, false));
2264:
2265: } catch (RecognitionException ex) {
2266: reportError(ex);
2267: if (_t != null) {
2268: _t = _t.getNextSibling();
2269: }
2270: }
2271: _retTree = _t;
2272: return code;
2273: }
2274:
2275: public final void ast_suffix(AST _t) throws RecognitionException {
2276:
2277: GrammarAST ast_suffix_AST_in = (_t == ASTNULL) ? null
2278: : (GrammarAST) _t;
2279:
2280: try { // for error handling
2281: if (_t == null)
2282: _t = ASTNULL;
2283: switch (_t.getType()) {
2284: case ROOT: {
2285: GrammarAST tmp51_AST_in = (GrammarAST) _t;
2286: match(_t, ROOT);
2287: _t = _t.getNextSibling();
2288: break;
2289: }
2290: case BANG: {
2291: GrammarAST tmp52_AST_in = (GrammarAST) _t;
2292: match(_t, BANG);
2293: _t = _t.getNextSibling();
2294: break;
2295: }
2296: default: {
2297: throw new NoViableAltException(_t);
2298: }
2299: }
2300: } catch (RecognitionException ex) {
2301: reportError(ex);
2302: if (_t != null) {
2303: _t = _t.getNextSibling();
2304: }
2305: }
2306: _retTree = _t;
2307: }
2308:
2309: public final void setElement(AST _t) throws RecognitionException {
2310:
2311: GrammarAST setElement_AST_in = (_t == ASTNULL) ? null
2312: : (GrammarAST) _t;
2313: GrammarAST c = null;
2314: GrammarAST t = null;
2315: GrammarAST s = null;
2316: GrammarAST c1 = null;
2317: GrammarAST c2 = null;
2318:
2319: try { // for error handling
2320: if (_t == null)
2321: _t = ASTNULL;
2322: switch (_t.getType()) {
2323: case CHAR_LITERAL: {
2324: c = (GrammarAST) _t;
2325: match(_t, CHAR_LITERAL);
2326: _t = _t.getNextSibling();
2327: break;
2328: }
2329: case TOKEN_REF: {
2330: t = (GrammarAST) _t;
2331: match(_t, TOKEN_REF);
2332: _t = _t.getNextSibling();
2333: break;
2334: }
2335: case STRING_LITERAL: {
2336: s = (GrammarAST) _t;
2337: match(_t, STRING_LITERAL);
2338: _t = _t.getNextSibling();
2339: break;
2340: }
2341: case CHAR_RANGE: {
2342: AST __t90 = _t;
2343: GrammarAST tmp53_AST_in = (GrammarAST) _t;
2344: match(_t, CHAR_RANGE);
2345: _t = _t.getFirstChild();
2346: c1 = (GrammarAST) _t;
2347: match(_t, CHAR_LITERAL);
2348: _t = _t.getNextSibling();
2349: c2 = (GrammarAST) _t;
2350: match(_t, CHAR_LITERAL);
2351: _t = _t.getNextSibling();
2352: _t = __t90;
2353: _t = _t.getNextSibling();
2354: break;
2355: }
2356: default: {
2357: throw new NoViableAltException(_t);
2358: }
2359: }
2360: } catch (RecognitionException ex) {
2361: reportError(ex);
2362: if (_t != null) {
2363: _t = _t.getNextSibling();
2364: }
2365: }
2366: _retTree = _t;
2367: }
2368:
2369: public final StringTemplate rewrite_alternative(AST _t)
2370: throws RecognitionException {
2371: StringTemplate code = null;
2372:
2373: GrammarAST rewrite_alternative_AST_in = (_t == ASTNULL) ? null
2374: : (GrammarAST) _t;
2375: GrammarAST a = null;
2376:
2377: StringTemplate el, st;
2378:
2379: try { // for error handling
2380: if (_t == null)
2381: _t = ASTNULL;
2382: if (((_t.getType() == ALT))
2383: && (generator.grammar.buildAST())) {
2384: AST __t99 = _t;
2385: a = _t == ASTNULL ? null : (GrammarAST) _t;
2386: match(_t, ALT);
2387: _t = _t.getFirstChild();
2388: code = templates.getInstanceOf("rewriteElementList");
2389: {
2390: if (_t == null)
2391: _t = ASTNULL;
2392: switch (_t.getType()) {
2393: case OPTIONAL:
2394: case CLOSURE:
2395: case POSITIVE_CLOSURE:
2396: case LABEL:
2397: case ACTION:
2398: case STRING_LITERAL:
2399: case CHAR_LITERAL:
2400: case TOKEN_REF:
2401: case RULE_REF:
2402: case TREE_BEGIN: {
2403: {
2404: int _cnt102 = 0;
2405: _loop102: do {
2406: if (_t == null)
2407: _t = ASTNULL;
2408: if ((_t.getType() == OPTIONAL
2409: || _t.getType() == CLOSURE
2410: || _t.getType() == POSITIVE_CLOSURE
2411: || _t.getType() == LABEL
2412: || _t.getType() == ACTION
2413: || _t.getType() == STRING_LITERAL
2414: || _t.getType() == CHAR_LITERAL
2415: || _t.getType() == TOKEN_REF
2416: || _t.getType() == RULE_REF || _t
2417: .getType() == TREE_BEGIN)) {
2418: GrammarAST elAST = (GrammarAST) _t;
2419: el = rewrite_element(_t);
2420: _t = _retTree;
2421: code.setAttribute(
2422: "elements.{el,line,pos}",
2423: el, Utils.integer(elAST
2424: .getLine()),
2425: Utils.integer(elAST
2426: .getColumn()));
2427:
2428: } else {
2429: if (_cnt102 >= 1) {
2430: break _loop102;
2431: } else {
2432: throw new NoViableAltException(
2433: _t);
2434: }
2435: }
2436:
2437: _cnt102++;
2438: } while (true);
2439: }
2440: break;
2441: }
2442: case EPSILON: {
2443: GrammarAST tmp54_AST_in = (GrammarAST) _t;
2444: match(_t, EPSILON);
2445: _t = _t.getNextSibling();
2446: code
2447: .setAttribute(
2448: "elements.{el,line,pos}",
2449: templates
2450: .getInstanceOf("rewriteEmptyAlt"),
2451: Utils.integer(a.getLine()),
2452: Utils.integer(a.getColumn()));
2453:
2454: break;
2455: }
2456: default: {
2457: throw new NoViableAltException(_t);
2458: }
2459: }
2460: }
2461: GrammarAST tmp55_AST_in = (GrammarAST) _t;
2462: match(_t, EOA);
2463: _t = _t.getNextSibling();
2464: _t = __t99;
2465: _t = _t.getNextSibling();
2466: } else if (((_t.getType() == ALT
2467: || _t.getType() == TEMPLATE || _t.getType() == ACTION))
2468: && (generator.grammar.buildTemplate())) {
2469: code = rewrite_template(_t);
2470: _t = _retTree;
2471: } else {
2472: throw new NoViableAltException(_t);
2473: }
2474:
2475: } catch (RecognitionException ex) {
2476: reportError(ex);
2477: if (_t != null) {
2478: _t = _t.getNextSibling();
2479: }
2480: }
2481: _retTree = _t;
2482: return code;
2483: }
2484:
2485: public final StringTemplate rewrite_block(AST _t,
2486: String blockTemplateName) throws RecognitionException {
2487: StringTemplate code = null;
2488:
2489: GrammarAST rewrite_block_AST_in = (_t == ASTNULL) ? null
2490: : (GrammarAST) _t;
2491:
2492: rewriteBlockNestingLevel++;
2493: code = templates.getInstanceOf(blockTemplateName);
2494: StringTemplate save_currentBlockST = currentBlockST;
2495: currentBlockST = code;
2496: code
2497: .setAttribute("rewriteBlockLevel",
2498: rewriteBlockNestingLevel);
2499: StringTemplate alt = null;
2500:
2501: try { // for error handling
2502: AST __t97 = _t;
2503: GrammarAST tmp56_AST_in = (GrammarAST) _t;
2504: match(_t, BLOCK);
2505: _t = _t.getFirstChild();
2506:
2507: currentBlockST
2508: .setAttribute(
2509: "referencedElementsDeep",
2510: getTokenTypesAsTargetLabels(tmp56_AST_in.rewriteRefsDeep));
2511: currentBlockST
2512: .setAttribute(
2513: "referencedElements",
2514: getTokenTypesAsTargetLabels(tmp56_AST_in.rewriteRefsShallow));
2515:
2516: alt = rewrite_alternative(_t);
2517: _t = _retTree;
2518: GrammarAST tmp57_AST_in = (GrammarAST) _t;
2519: match(_t, EOB);
2520: _t = _t.getNextSibling();
2521: _t = __t97;
2522: _t = _t.getNextSibling();
2523:
2524: code.setAttribute("alt", alt);
2525: rewriteBlockNestingLevel--;
2526: currentBlockST = save_currentBlockST;
2527:
2528: } catch (RecognitionException ex) {
2529: reportError(ex);
2530: if (_t != null) {
2531: _t = _t.getNextSibling();
2532: }
2533: }
2534: _retTree = _t;
2535: return code;
2536: }
2537:
2538: public final StringTemplate rewrite_element(AST _t)
2539: throws RecognitionException {
2540: StringTemplate code = null;
2541:
2542: GrammarAST rewrite_element_AST_in = (_t == ASTNULL) ? null
2543: : (GrammarAST) _t;
2544:
2545: IntSet elements = null;
2546: GrammarAST ast = null;
2547:
2548: try { // for error handling
2549: if (_t == null)
2550: _t = ASTNULL;
2551: switch (_t.getType()) {
2552: case LABEL:
2553: case ACTION:
2554: case STRING_LITERAL:
2555: case CHAR_LITERAL:
2556: case TOKEN_REF:
2557: case RULE_REF: {
2558: code = rewrite_atom(_t, false);
2559: _t = _retTree;
2560: break;
2561: }
2562: case OPTIONAL:
2563: case CLOSURE:
2564: case POSITIVE_CLOSURE: {
2565: code = rewrite_ebnf(_t);
2566: _t = _retTree;
2567: break;
2568: }
2569: case TREE_BEGIN: {
2570: code = rewrite_tree(_t);
2571: _t = _retTree;
2572: break;
2573: }
2574: default: {
2575: throw new NoViableAltException(_t);
2576: }
2577: }
2578: } catch (RecognitionException ex) {
2579: reportError(ex);
2580: if (_t != null) {
2581: _t = _t.getNextSibling();
2582: }
2583: }
2584: _retTree = _t;
2585: return code;
2586: }
2587:
2588: public final StringTemplate rewrite_template(AST _t)
2589: throws RecognitionException {
2590: StringTemplate code = null;
2591:
2592: GrammarAST rewrite_template_AST_in = (_t == ASTNULL) ? null
2593: : (GrammarAST) _t;
2594: GrammarAST id = null;
2595: GrammarAST ind = null;
2596: GrammarAST arg = null;
2597: GrammarAST a = null;
2598: GrammarAST act = null;
2599:
2600: try { // for error handling
2601: if (_t == null)
2602: _t = ASTNULL;
2603: switch (_t.getType()) {
2604: case ALT: {
2605: AST __t117 = _t;
2606: GrammarAST tmp58_AST_in = (GrammarAST) _t;
2607: match(_t, ALT);
2608: _t = _t.getFirstChild();
2609: GrammarAST tmp59_AST_in = (GrammarAST) _t;
2610: match(_t, EPSILON);
2611: _t = _t.getNextSibling();
2612: GrammarAST tmp60_AST_in = (GrammarAST) _t;
2613: match(_t, EOA);
2614: _t = _t.getNextSibling();
2615: _t = __t117;
2616: _t = _t.getNextSibling();
2617: code = templates.getInstanceOf("rewriteEmptyTemplate");
2618: break;
2619: }
2620: case TEMPLATE: {
2621: AST __t118 = _t;
2622: GrammarAST tmp61_AST_in = (GrammarAST) _t;
2623: match(_t, TEMPLATE);
2624: _t = _t.getFirstChild();
2625: {
2626: if (_t == null)
2627: _t = ASTNULL;
2628: switch (_t.getType()) {
2629: case ID: {
2630: id = (GrammarAST) _t;
2631: match(_t, ID);
2632: _t = _t.getNextSibling();
2633: break;
2634: }
2635: case ACTION: {
2636: ind = (GrammarAST) _t;
2637: match(_t, ACTION);
2638: _t = _t.getNextSibling();
2639: break;
2640: }
2641: default: {
2642: throw new NoViableAltException(_t);
2643: }
2644: }
2645: }
2646:
2647: if (id != null && id.getText().equals("template")) {
2648: code = templates
2649: .getInstanceOf("rewriteInlineTemplate");
2650: } else if (id != null) {
2651: code = templates
2652: .getInstanceOf("rewriteExternalTemplate");
2653: code.setAttribute("name", id.getText());
2654: } else if (ind != null) { // must be %({expr})(args)
2655: code = templates
2656: .getInstanceOf("rewriteIndirectTemplate");
2657: List chunks = generator.translateAction(
2658: currentRuleName, ind);
2659: code.setAttribute("expr", chunks);
2660: }
2661:
2662: AST __t120 = _t;
2663: GrammarAST tmp62_AST_in = (GrammarAST) _t;
2664: match(_t, ARGLIST);
2665: _t = _t.getFirstChild();
2666: {
2667: _loop123: do {
2668: if (_t == null)
2669: _t = ASTNULL;
2670: if ((_t.getType() == ARG)) {
2671: AST __t122 = _t;
2672: GrammarAST tmp63_AST_in = (GrammarAST) _t;
2673: match(_t, ARG);
2674: _t = _t.getFirstChild();
2675: arg = (GrammarAST) _t;
2676: match(_t, ID);
2677: _t = _t.getNextSibling();
2678: a = (GrammarAST) _t;
2679: match(_t, ACTION);
2680: _t = _t.getNextSibling();
2681:
2682: // must set alt num here rather than in define.g
2683: // because actions like %foo(name={$ID.text}) aren't
2684: // broken up yet into trees.
2685: a.outerAltNum = this .outerAltNum;
2686: List chunks = generator.translateAction(
2687: currentRuleName, a);
2688: code.setAttribute("args.{name,value}", arg
2689: .getText(), chunks);
2690:
2691: _t = __t122;
2692: _t = _t.getNextSibling();
2693: } else {
2694: break _loop123;
2695: }
2696:
2697: } while (true);
2698: }
2699: _t = __t120;
2700: _t = _t.getNextSibling();
2701: {
2702: if (_t == null)
2703: _t = ASTNULL;
2704: switch (_t.getType()) {
2705: case DOUBLE_QUOTE_STRING_LITERAL: {
2706: GrammarAST tmp64_AST_in = (GrammarAST) _t;
2707: match(_t, DOUBLE_QUOTE_STRING_LITERAL);
2708: _t = _t.getNextSibling();
2709:
2710: String sl = tmp64_AST_in.getText();
2711: String t = sl.substring(1, sl.length() - 1); // strip quotes
2712: t = generator.target
2713: .getTargetStringLiteralFromString(t);
2714: code.setAttribute("template", t);
2715:
2716: break;
2717: }
2718: case DOUBLE_ANGLE_STRING_LITERAL: {
2719: GrammarAST tmp65_AST_in = (GrammarAST) _t;
2720: match(_t, DOUBLE_ANGLE_STRING_LITERAL);
2721: _t = _t.getNextSibling();
2722:
2723: String sl = tmp65_AST_in.getText();
2724: String t = sl.substring(2, sl.length() - 2); // strip double angle quotes
2725: t = generator.target
2726: .getTargetStringLiteralFromString(t);
2727: code.setAttribute("template", t);
2728:
2729: break;
2730: }
2731: case 3: {
2732: break;
2733: }
2734: default: {
2735: throw new NoViableAltException(_t);
2736: }
2737: }
2738: }
2739: _t = __t118;
2740: _t = _t.getNextSibling();
2741: break;
2742: }
2743: case ACTION: {
2744: act = (GrammarAST) _t;
2745: match(_t, ACTION);
2746: _t = _t.getNextSibling();
2747:
2748: // set alt num for same reason as ARGLIST above
2749: act.outerAltNum = this .outerAltNum;
2750: code = templates.getInstanceOf("rewriteAction");
2751: code.setAttribute("action", generator.translateAction(
2752: currentRuleName, act));
2753:
2754: break;
2755: }
2756: default: {
2757: throw new NoViableAltException(_t);
2758: }
2759: }
2760: } catch (RecognitionException ex) {
2761: reportError(ex);
2762: if (_t != null) {
2763: _t = _t.getNextSibling();
2764: }
2765: }
2766: _retTree = _t;
2767: return code;
2768: }
2769:
2770: public final StringTemplate rewrite_atom(AST _t, boolean isRoot)
2771: throws RecognitionException {
2772: StringTemplate code = null;
2773:
2774: GrammarAST rewrite_atom_AST_in = (_t == ASTNULL) ? null
2775: : (GrammarAST) _t;
2776: GrammarAST r = null;
2777: GrammarAST arg = null;
2778:
2779: try { // for error handling
2780: if (_t == null)
2781: _t = ASTNULL;
2782: switch (_t.getType()) {
2783: case RULE_REF: {
2784: r = (GrammarAST) _t;
2785: match(_t, RULE_REF);
2786: _t = _t.getNextSibling();
2787:
2788: String ruleRefName = r.getText();
2789: String stName = "rewriteRuleRef";
2790: if (isRoot) {
2791: stName += "Root";
2792: }
2793: code = templates.getInstanceOf(stName);
2794: code.setAttribute("rule", ruleRefName);
2795: if (grammar.getRule(ruleRefName) == null) {
2796: ErrorManager.grammarError(
2797: ErrorManager.MSG_UNDEFINED_RULE_REF,
2798: grammar, ((GrammarAST) (r)).getToken(),
2799: ruleRefName);
2800: code = new StringTemplate(); // blank; no code gen
2801: } else if (grammar.getRule(currentRuleName)
2802: .getRuleRefsInAlt(ruleRefName, outerAltNum) == null) {
2803: ErrorManager
2804: .grammarError(
2805: ErrorManager.MSG_REWRITE_ELEMENT_NOT_PRESENT_ON_LHS,
2806: grammar, ((GrammarAST) (r))
2807: .getToken(), ruleRefName);
2808: code = new StringTemplate(); // blank; no code gen
2809: } else {
2810: // track all rule refs as we must copy 2nd ref to rule and beyond
2811: if (!rewriteRuleRefs.contains(ruleRefName)) {
2812: rewriteRuleRefs.add(ruleRefName);
2813: }
2814: }
2815:
2816: break;
2817: }
2818: case STRING_LITERAL:
2819: case CHAR_LITERAL:
2820: case TOKEN_REF: {
2821: {
2822: if (_t == null)
2823: _t = ASTNULL;
2824: switch (_t.getType()) {
2825: case TOKEN_REF: {
2826: AST __t114 = _t;
2827: GrammarAST tmp66_AST_in = (GrammarAST) _t;
2828: match(_t, TOKEN_REF);
2829: _t = _t.getFirstChild();
2830: {
2831: if (_t == null)
2832: _t = ASTNULL;
2833: switch (_t.getType()) {
2834: case ARG_ACTION: {
2835: arg = (GrammarAST) _t;
2836: match(_t, ARG_ACTION);
2837: _t = _t.getNextSibling();
2838: break;
2839: }
2840: case 3: {
2841: break;
2842: }
2843: default: {
2844: throw new NoViableAltException(_t);
2845: }
2846: }
2847: }
2848: _t = __t114;
2849: _t = _t.getNextSibling();
2850: break;
2851: }
2852: case CHAR_LITERAL: {
2853: GrammarAST tmp67_AST_in = (GrammarAST) _t;
2854: match(_t, CHAR_LITERAL);
2855: _t = _t.getNextSibling();
2856: break;
2857: }
2858: case STRING_LITERAL: {
2859: GrammarAST tmp68_AST_in = (GrammarAST) _t;
2860: match(_t, STRING_LITERAL);
2861: _t = _t.getNextSibling();
2862: break;
2863: }
2864: default: {
2865: throw new NoViableAltException(_t);
2866: }
2867: }
2868: }
2869:
2870: String tokenName = rewrite_atom_AST_in.getText();
2871: String stName = "rewriteTokenRef";
2872: Rule rule = grammar.getRule(currentRuleName);
2873: Set tokenRefsInAlt = rule
2874: .getTokenRefsInAlt(outerAltNum);
2875: boolean imaginary = !tokenRefsInAlt.contains(tokenName);
2876: if (imaginary) {
2877: stName = "rewriteImaginaryTokenRef";
2878: }
2879: if (isRoot) {
2880: stName += "Root";
2881: }
2882: code = templates.getInstanceOf(stName);
2883: if (arg != null) {
2884: List args = generator.translateAction(
2885: currentRuleName, arg);
2886: code.setAttribute("args", args);
2887: }
2888: code.setAttribute("elementIndex",
2889: ((TokenWithIndex) rewrite_atom_AST_in
2890: .getToken()).getIndex());
2891: int ttype = grammar.getTokenType(tokenName);
2892: String tok = generator.getTokenTypeAsTargetLabel(ttype);
2893: code.setAttribute("token", tok);
2894: if (grammar.getTokenType(tokenName) == Label.INVALID) {
2895: ErrorManager
2896: .grammarError(
2897: ErrorManager.MSG_UNDEFINED_TOKEN_REF_IN_REWRITE,
2898: grammar,
2899: ((GrammarAST) (rewrite_atom_AST_in))
2900: .getToken(), tokenName);
2901: code = new StringTemplate(); // blank; no code gen
2902: }
2903:
2904: break;
2905: }
2906: case LABEL: {
2907: GrammarAST tmp69_AST_in = (GrammarAST) _t;
2908: match(_t, LABEL);
2909: _t = _t.getNextSibling();
2910:
2911: String labelName = tmp69_AST_in.getText();
2912: Rule rule = grammar.getRule(currentRuleName);
2913: Grammar.LabelElementPair pair = rule
2914: .getLabel(labelName);
2915: if (labelName.equals(currentRuleName)) {
2916: // special case; ref to old value via $rule
2917: StringTemplate labelST = templates
2918: .getInstanceOf("prevRuleRootRef");
2919: code = templates
2920: .getInstanceOf("rewriteRuleLabelRef"
2921: + (isRoot ? "Root" : ""));
2922: code.setAttribute("label", labelST);
2923: } else if (pair == null) {
2924: ErrorManager
2925: .grammarError(
2926: ErrorManager.MSG_UNDEFINED_LABEL_REF_IN_REWRITE,
2927: grammar,
2928: ((GrammarAST) (tmp69_AST_in))
2929: .getToken(), labelName);
2930: code = new StringTemplate();
2931: } else {
2932: String stName = null;
2933: switch (pair.type) {
2934: case Grammar.TOKEN_LABEL:
2935: stName = "rewriteTokenLabelRef";
2936: break;
2937: case Grammar.RULE_LABEL:
2938: stName = "rewriteRuleLabelRef";
2939: break;
2940: case Grammar.TOKEN_LIST_LABEL:
2941: stName = "rewriteTokenListLabelRef";
2942: break;
2943: case Grammar.RULE_LIST_LABEL:
2944: stName = "rewriteRuleListLabelRef";
2945: break;
2946: }
2947: if (isRoot) {
2948: stName += "Root";
2949: }
2950: code = templates.getInstanceOf(stName);
2951: code.setAttribute("label", labelName);
2952: }
2953:
2954: break;
2955: }
2956: case ACTION: {
2957: GrammarAST tmp70_AST_in = (GrammarAST) _t;
2958: match(_t, ACTION);
2959: _t = _t.getNextSibling();
2960:
2961: // actions in rewrite rules yield a tree object
2962: String actText = tmp70_AST_in.getText();
2963: List chunks = generator.translateAction(
2964: currentRuleName, tmp70_AST_in);
2965: code = templates.getInstanceOf("rewriteNodeAction"
2966: + (isRoot ? "Root" : ""));
2967: code.setAttribute("action", chunks);
2968:
2969: break;
2970: }
2971: default: {
2972: throw new NoViableAltException(_t);
2973: }
2974: }
2975: } catch (RecognitionException ex) {
2976: reportError(ex);
2977: if (_t != null) {
2978: _t = _t.getNextSibling();
2979: }
2980: }
2981: _retTree = _t;
2982: return code;
2983: }
2984:
2985: public final StringTemplate rewrite_ebnf(AST _t)
2986: throws RecognitionException {
2987: StringTemplate code = null;
2988:
2989: GrammarAST rewrite_ebnf_AST_in = (_t == ASTNULL) ? null
2990: : (GrammarAST) _t;
2991:
2992: try { // for error handling
2993: if (_t == null)
2994: _t = ASTNULL;
2995: switch (_t.getType()) {
2996: case OPTIONAL: {
2997: AST __t105 = _t;
2998: GrammarAST tmp71_AST_in = (GrammarAST) _t;
2999: match(_t, OPTIONAL);
3000: _t = _t.getFirstChild();
3001: code = rewrite_block(_t, "rewriteOptionalBlock");
3002: _t = _retTree;
3003: _t = __t105;
3004: _t = _t.getNextSibling();
3005:
3006: String description = grammar.grammarTreeToString(
3007: rewrite_ebnf_AST_in, false);
3008: description = generator.target
3009: .getTargetStringLiteralFromString(description);
3010: code.setAttribute("description", description);
3011:
3012: break;
3013: }
3014: case CLOSURE: {
3015: AST __t106 = _t;
3016: GrammarAST tmp72_AST_in = (GrammarAST) _t;
3017: match(_t, CLOSURE);
3018: _t = _t.getFirstChild();
3019: code = rewrite_block(_t, "rewriteClosureBlock");
3020: _t = _retTree;
3021: _t = __t106;
3022: _t = _t.getNextSibling();
3023:
3024: String description = grammar.grammarTreeToString(
3025: rewrite_ebnf_AST_in, false);
3026: description = generator.target
3027: .getTargetStringLiteralFromString(description);
3028: code.setAttribute("description", description);
3029:
3030: break;
3031: }
3032: case POSITIVE_CLOSURE: {
3033: AST __t107 = _t;
3034: GrammarAST tmp73_AST_in = (GrammarAST) _t;
3035: match(_t, POSITIVE_CLOSURE);
3036: _t = _t.getFirstChild();
3037: code = rewrite_block(_t, "rewritePositiveClosureBlock");
3038: _t = _retTree;
3039: _t = __t107;
3040: _t = _t.getNextSibling();
3041:
3042: String description = grammar.grammarTreeToString(
3043: rewrite_ebnf_AST_in, false);
3044: description = generator.target
3045: .getTargetStringLiteralFromString(description);
3046: code.setAttribute("description", description);
3047:
3048: break;
3049: }
3050: default: {
3051: throw new NoViableAltException(_t);
3052: }
3053: }
3054: } catch (RecognitionException ex) {
3055: reportError(ex);
3056: if (_t != null) {
3057: _t = _t.getNextSibling();
3058: }
3059: }
3060: _retTree = _t;
3061: return code;
3062: }
3063:
3064: public final StringTemplate rewrite_tree(AST _t)
3065: throws RecognitionException {
3066: StringTemplate code = templates.getInstanceOf("rewriteTree");
3067:
3068: GrammarAST rewrite_tree_AST_in = (_t == ASTNULL) ? null
3069: : (GrammarAST) _t;
3070:
3071: rewriteTreeNestingLevel++;
3072: code.setAttribute("treeLevel", rewriteTreeNestingLevel);
3073: code.setAttribute("enclosingTreeLevel",
3074: rewriteTreeNestingLevel - 1);
3075: StringTemplate r, el;
3076: GrammarAST elAST = null;
3077:
3078: try { // for error handling
3079: AST __t109 = _t;
3080: GrammarAST tmp74_AST_in = (GrammarAST) _t;
3081: match(_t, TREE_BEGIN);
3082: _t = _t.getFirstChild();
3083: elAST = (GrammarAST) _t;
3084: r = rewrite_atom(_t, true);
3085: _t = _retTree;
3086: code.setAttribute("root.{el,line,pos}", r, Utils
3087: .integer(elAST.getLine()), Utils.integer(elAST
3088: .getColumn()));
3089:
3090: {
3091: _loop111: do {
3092: if (_t == null)
3093: _t = ASTNULL;
3094: if ((_t.getType() == OPTIONAL
3095: || _t.getType() == CLOSURE
3096: || _t.getType() == POSITIVE_CLOSURE
3097: || _t.getType() == LABEL
3098: || _t.getType() == ACTION
3099: || _t.getType() == STRING_LITERAL
3100: || _t.getType() == CHAR_LITERAL
3101: || _t.getType() == TOKEN_REF
3102: || _t.getType() == RULE_REF || _t.getType() == TREE_BEGIN)) {
3103: elAST = (GrammarAST) _t;
3104: el = rewrite_element(_t);
3105: _t = _retTree;
3106:
3107: code.setAttribute("children.{el,line,pos}", el,
3108: Utils.integer(elAST.getLine()), Utils
3109: .integer(elAST.getColumn()));
3110:
3111: } else {
3112: break _loop111;
3113: }
3114:
3115: } while (true);
3116: }
3117: _t = __t109;
3118: _t = _t.getNextSibling();
3119:
3120: String description = grammar.grammarTreeToString(
3121: rewrite_tree_AST_in, false);
3122: description = generator.target
3123: .getTargetStringLiteralFromString(description);
3124: code.setAttribute("description", description);
3125: rewriteTreeNestingLevel--;
3126:
3127: } catch (RecognitionException ex) {
3128: reportError(ex);
3129: if (_t != null) {
3130: _t = _t.getNextSibling();
3131: }
3132: }
3133: _retTree = _t;
3134: return code;
3135: }
3136:
3137: public static final String[] _tokenNames = { "<0>", "EOF", "<2>",
3138: "NULL_TREE_LOOKAHEAD", "\"options\"", "\"tokens\"",
3139: "\"parser\"", "LEXER", "RULE", "BLOCK", "OPTIONAL",
3140: "CLOSURE", "POSITIVE_CLOSURE", "SYNPRED", "RANGE",
3141: "CHAR_RANGE", "EPSILON", "ALT", "EOR", "EOB", "EOA", "ID",
3142: "ARG", "ARGLIST", "RET", "LEXER_GRAMMAR", "PARSER_GRAMMAR",
3143: "TREE_GRAMMAR", "COMBINED_GRAMMAR", "INITACTION", "LABEL",
3144: "TEMPLATE", "\"scope\"", "GATED_SEMPRED", "SYN_SEMPRED",
3145: "BACKTRACK_SEMPRED", "\"fragment\"", "ACTION",
3146: "DOC_COMMENT", "SEMI", "\"lexer\"", "\"tree\"",
3147: "\"grammar\"", "AMPERSAND", "COLON", "RCURLY", "ASSIGN",
3148: "STRING_LITERAL", "CHAR_LITERAL", "INT", "STAR",
3149: "TOKEN_REF", "\"protected\"", "\"public\"", "\"private\"",
3150: "BANG", "ARG_ACTION", "\"returns\"", "\"throws\"", "COMMA",
3151: "LPAREN", "OR", "RPAREN", "\"catch\"", "\"finally\"",
3152: "PLUS_ASSIGN", "SEMPRED", "IMPLIES", "ROOT", "RULE_REF",
3153: "NOT", "TREE_BEGIN", "QUESTION", "PLUS", "WILDCARD",
3154: "REWRITE", "DOLLAR", "DOUBLE_QUOTE_STRING_LITERAL",
3155: "DOUBLE_ANGLE_STRING_LITERAL", "WS", "COMMENT",
3156: "SL_COMMENT", "ML_COMMENT", "OPEN_ELEMENT_OPTION",
3157: "CLOSE_ELEMENT_OPTION", "ESC", "DIGIT", "XDIGIT",
3158: "NESTED_ARG_ACTION", "NESTED_ACTION",
3159: "ACTION_CHAR_LITERAL", "ACTION_STRING_LITERAL",
3160: "ACTION_ESC", "WS_LOOP", "INTERNAL_RULE_REF", "WS_OPT",
3161: "SRC" };
3162:
3163: }
|