public TemplateRenderModel Render(FileModel fileModel) { TemplateRenderModel result = new TemplateRenderModel(); result.Structure = sourceStructure; result.FileModel = fileModel; StringBuilder templateBuilder = new StringBuilder(); templateBuilder.Append("group Group1;"); templateBuilder.Append("RenderFile(file) ::= <<"); templateBuilder.Append(sourceStructure.FileTemplate); templateBuilder.Append(">>"); templateBuilder.Append("RenderNamespace(namespace) ::= <<"); templateBuilder.Append(sourceStructure.NamespaceTemplate); templateBuilder.Append(">>"); templateBuilder.Append("RenderEntity(entity) ::= <<"); templateBuilder.Append(sourceStructure.EntityTemplate); templateBuilder.Append(">>"); using (TextReader sr = new StringReader(templateBuilder.ToString())) { Antlr3.ST.StringTemplateGroup groups = new Antlr3.ST.StringTemplateGroup(sr, typeof(Antlr3.ST.Language.TemplateLexer)); Antlr3.ST.StringTemplate template = groups.GetInstanceOf("RenderFile"); template.SetAttribute("file", fileModel); result.Result = template.ToString(); } return result; }
protected override void GenRecognizerHeaderFile(AntlrTool tool, CodeGenerator generator, Grammar grammar, StringTemplate headerFileST, string extName) { StringTemplateGroup templates = generator.Templates; generator.Write(headerFileST, grammar.name + extName); }
public StringTemplate GenExpr(CodeGenerator generator, StringTemplateGroup templates, DFA dfa) { if (templates != null) { return(templates.GetInstanceOf("false")); } return(new StringTemplate("false")); }
public override StringTemplate GenExpr(CodeGenerator generator, StringTemplateGroup templates, DFA dfa) { StringTemplate eST = null; if (templates != null) { if (_synpred) { eST = templates.GetInstanceOf("evalSynPredicate"); } else { eST = templates.GetInstanceOf("evalPredicate"); generator.grammar.decisionsWhoseDFAsUsesSemPreds.Add(dfa); } string predEnclosingRuleName = predicateAST.enclosingRuleName; /* * String decisionEnclosingRuleName = * dfa.getNFADecisionStartState().getEnclosingRule(); * // if these rulenames are diff, then pred was hoisted out of rule * // Currently I don't warn you about this as it could be annoying. * // I do the translation anyway. */ //eST.setAttribute("pred", this.toString()); if (generator != null) { eST.SetAttribute("pred", generator.TranslateAction(predEnclosingRuleName, predicateAST)); } } else { eST = new StringTemplate("$pred$"); eST.SetAttribute("pred", this.ToString()); return(eST); } if (generator != null) { string description = generator.target.GetTargetStringLiteralFromString(this.ToString()); eST.SetAttribute("description", description); } return(eST); }
public override StringTemplate GenExpr(CodeGenerator generator, StringTemplateGroup templates, DFA dfa) { StringTemplate eST = null; if (templates != null) { eST = templates.GetInstanceOf("notPredicate"); } else { eST = new StringTemplate("?!($pred$)"); } eST.SetAttribute("pred", ctx.GenExpr(generator, templates, dfa)); return(eST); }
public override StringTemplate GenExpr(CodeGenerator generator, StringTemplateGroup templates, DFA dfa) { StringTemplate eST = null; if (templates != null) { eST = templates.GetInstanceOf("andPredicates"); } else { eST = new StringTemplate("($left$&&$right$)"); } eST.SetAttribute("left", _left.GenExpr(generator, templates, dfa)); eST.SetAttribute("right", _right.GenExpr(generator, templates, dfa)); return(eST); }
public override StringTemplate GenExpr(CodeGenerator generator, StringTemplateGroup templates, DFA dfa) { StringTemplate eST = null; if (templates != null) { eST = templates.GetInstanceOf("orPredicates"); } else { eST = new StringTemplate("($first(operands)$$rest(operands):{o | ||$o$}$)"); } foreach (SemanticContext semctx in _operands) { eST.SetAttribute("operands", semctx.GenExpr(generator, templates, dfa)); } return(eST); }
public void TestTemplateConstructor() /*throws Exception*/ { string action = "x = %foo(name={$ID.text});"; string expecting = "x = templateLib.getInstanceOf(\"foo\"," + LINE_SEP + " new STAttrMap().put(\"name\", (ID1!=null?ID1.getText():null)));"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.SetErrorListener(equeue); Grammar g = new Grammar( "grammar t;\n" + "options {\n" + " output=template;\n" + "}\n" + "\n" + "a : ID {" + action + "}\n" + " ;\n" + "\n" + "ID : 'a';\n"); AntlrTool antlr = newTool(); CodeGenerator generator = new CodeGenerator(antlr, g, "Java"); g.CodeGenerator = generator; generator.GenRecognizer(); // forces load of templates ActionTranslator translator = new ActionTranslator(generator, "a", new CommonToken(ANTLRParser.ACTION, action), 1); string rawTranslation = translator.Translate(); StringTemplateGroup templates = new StringTemplateGroup(".", typeof(AngleBracketTemplateLexer)); StringTemplate actionST = new StringTemplate(templates, rawTranslation); string found = actionST.ToString(); assertNoErrors(equeue); assertEquals(expecting, found); }
public void TestSetAttr() /*throws Exception*/ { string action = "%x.y = z;"; string expecting = "(x).setAttribute(\"y\", z);"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.SetErrorListener(equeue); Grammar g = new Grammar( "grammar t;\n" + "options {\n" + " output=template;\n" + "}\n" + "\n" + "a : ID {" + action + "}\n" + " ;\n" + "\n" + "ID : 'a';\n"); AntlrTool antlr = newTool(); CodeGenerator generator = new CodeGenerator(antlr, g, "Java"); g.CodeGenerator = generator; generator.GenRecognizer(); // forces load of templates ActionTranslator translator = new ActionTranslator(generator, "a", new CommonToken(ANTLRParser.ACTION, action), 1); string rawTranslation = translator.Translate(); StringTemplateGroup templates = new StringTemplateGroup(".", typeof(AngleBracketTemplateLexer)); StringTemplate actionST = new StringTemplate(templates, rawTranslation); string found = actionST.ToString(); assertNoErrors(equeue); assertEquals(expecting, found); }
/** We really only need a single locale for entire running ANTLR code * in a single VM. Only pay attention to the language, not the country * so that French Canadians and French Frenchies all get the same * template file, fr.stg. Just easier this way. */ public static void SetLocale(CultureInfo locale) { ErrorManager.locale = locale; String language = locale.TwoLetterISOLanguageName; //String fileName = "org/antlr/tool/templates/messages/languages/"+language+".stg"; string fileName = @"Tool\Templates\messages\languages\" + language + ".stg"; string streamName = "Antlr3." + fileName.Replace('\\', '.'); fileName = System.IO.Path.Combine(AntlrTool.ToolPathRoot, fileName); //ClassLoader cl = Thread.currentThread().getContextClassLoader(); //InputStream @is = cl.getResourceAsStream(fileName); System.IO.Stream @is; if (System.IO.File.Exists(fileName)) { @is = new System.IO.MemoryStream(System.IO.File.ReadAllBytes(fileName)); } else { @is = typeof(ErrorManager).Assembly.GetManifestResourceStream(streamName); } //if ( @is==null ) { // cl = typeof(ErrorManager).getClassLoader(); // @is = cl.getResourceAsStream(fileName); //} if (@is == null && language.Equals(CultureInfo.GetCultureInfo("en-us").TwoLetterISOLanguageName)) { RawError("ANTLR installation corrupted; cannot find English messages file " + fileName); Panic(); } else if (@is == null) { //rawError("no such locale file "+fileName+" retrying with English locale"); SetLocale(CultureInfo.GetCultureInfo("en-us")); // recurse on this rule, trying the US locale return; } StreamReader br = null; try { br = new StreamReader(new System.IO.BufferedStream(@is)); messages = new StringTemplateGroup(br, typeof(AngleBracketTemplateLexer), initSTListener); br.Close(); } catch (IOException ioe) { RawError("error reading message file " + fileName, ioe); } finally { if (br != null) { try { br.Close(); } catch (IOException ioe) { RawError("cannot close message file " + fileName, ioe); } } } messages.ErrorListener = blankSTListener; bool messagesOK = VerifyMessages(); if (!messagesOK && language.Equals(CultureInfo.GetCultureInfo("en-us").TwoLetterISOLanguageName)) { RawError("ANTLR installation corrupted; English messages file " + language + ".stg incomplete"); Panic(); } else if (!messagesOK) { SetLocale(CultureInfo.GetCultureInfo("en-us")); // try US to see if that will work } }
public void TestReuseExistingListLabelWithImplicitTokenLabel() { string action = "$ID.text;"; string expecting = "(x!=null?x.getText():null);"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.SetErrorListener( equeue ); Grammar g = new Grammar( "grammar t;\n" + "a : x+=ID {" + action + "} ;" + "ID : 'a';\n" ); AntlrTool antlr = newTool(); antlr.SetOutputDirectory( null ); // write to /dev/null CodeGenerator generator = new CodeGenerator( antlr, g, "Java" ); g.CodeGenerator = generator; generator.GenRecognizer(); ActionTranslator translator = new ActionTranslator( generator, "a", new CommonToken( ANTLRParser.ACTION, action ), 1 ); string rawTranslation = translator.Translate(); StringTemplateGroup templates = new StringTemplateGroup( ".", typeof( AngleBracketTemplateLexer ) ); StringTemplate actionST = new StringTemplate( templates, rawTranslation ); string found = actionST.ToString(); assertEquals( expecting, found ); assertEquals( "unexpected errors: " + equeue, 0, equeue.errors.Count ); }
protected internal virtual StringTemplate GenSemanticPredicateExpr( StringTemplateGroup templates, Transition edge ) { DFA dfa = ( (DFAState)edge.target ).dfa; // which DFA are we in Label label = edge.label; SemanticContext semCtx = label.SemanticContext; return semCtx.GenExpr( this, templates, dfa ); }
/** load the main language.stg template group file */ public virtual void LoadTemplates( string language ) { // get a group loader containing main templates dir and target subdir string templateDirs = tool.TemplatesDirectory + ":" + Path.Combine(tool.TemplatesDirectory, language); //[email protected]("targets="+templateDirs.toString()); IStringTemplateGroupLoader loader = new CommonGroupLoader( templateDirs, ErrorManager.GetStringTemplateErrorListener() ); StringTemplateGroup.RegisterGroupLoader( loader ); StringTemplateGroup.RegisterDefaultLexer( typeof( AngleBracketTemplateLexer ) ); // first load main language template StringTemplateGroup coreTemplates = StringTemplateGroup.LoadGroup( language ); baseTemplates = coreTemplates; if ( coreTemplates == null ) { ErrorManager.Error( ErrorManager.MSG_MISSING_CODE_GEN_TEMPLATES, language ); return; } // dynamically add subgroups that act like filters to apply to // their supergroup. E.g., Java:Dbg:AST:ASTParser::ASTDbg. string outputOption = (string)grammar.GetOption( "output" ); if ( outputOption != null && outputOption.Equals( "AST" ) ) { if ( debug && grammar.type != GrammarType.Lexer ) { StringTemplateGroup dbgTemplates = StringTemplateGroup.LoadGroup( "Dbg", coreTemplates ); baseTemplates = dbgTemplates; StringTemplateGroup astTemplates = StringTemplateGroup.LoadGroup( "AST", dbgTemplates ); StringTemplateGroup astParserTemplates = astTemplates; //if ( !grammar.rewriteMode() ) { if ( grammar.type == GrammarType.TreeParser ) { astParserTemplates = StringTemplateGroup.LoadGroup( "ASTTreeParser", astTemplates ); } else { astParserTemplates = StringTemplateGroup.LoadGroup( "ASTParser", astTemplates ); } //} StringTemplateGroup astDbgTemplates = StringTemplateGroup.LoadGroup( "ASTDbg", astParserTemplates ); templates = astDbgTemplates; } else { StringTemplateGroup astTemplates = StringTemplateGroup.LoadGroup( "AST", coreTemplates ); StringTemplateGroup astParserTemplates = astTemplates; //if ( !grammar.rewriteMode() ) { if ( grammar.type == GrammarType.TreeParser ) { astParserTemplates = StringTemplateGroup.LoadGroup( "ASTTreeParser", astTemplates ); } else { astParserTemplates = StringTemplateGroup.LoadGroup( "ASTParser", astTemplates ); } //} templates = astParserTemplates; } } else if ( outputOption != null && outputOption.Equals( "template" ) ) { if ( debug && grammar.type != GrammarType.Lexer ) { StringTemplateGroup dbgTemplates = StringTemplateGroup.LoadGroup( "Dbg", coreTemplates ); baseTemplates = dbgTemplates; StringTemplateGroup stTemplates = StringTemplateGroup.LoadGroup( "ST", dbgTemplates ); templates = stTemplates; } else { templates = StringTemplateGroup.LoadGroup( "ST", coreTemplates ); } } else if ( debug && grammar.type != GrammarType.Lexer ) { templates = StringTemplateGroup.LoadGroup( "Dbg", coreTemplates ); baseTemplates = templates; } else { templates = coreTemplates; } if ( EmitTemplateDelimiters ) { templates.EmitDebugStartStopStrings( true ); templates.DoNotEmitDebugStringsForTemplate( "codeFileExtension" ); templates.DoNotEmitDebugStringsForTemplate( "headerFileExtension" ); } }
public void TestEscapedLessThanInAction() { Grammar g = new Grammar(); AntlrTool antlr = newTool(); CodeGenerator generator = new CodeGenerator( antlr, g, "Java" ); string action = "i<3; '<xmltag>'"; ActionTranslator translator = new ActionTranslator( generator, "a", new CommonToken( ANTLRParser.ACTION, action ), 0 ); string expecting = action; string rawTranslation = translator.Translate(); StringTemplateGroup templates = new StringTemplateGroup( ".", typeof( AngleBracketTemplateLexer ) ); StringTemplate actionST = new StringTemplate( templates, "<action>" ); actionST.SetAttribute( "action", rawTranslation ); string found = actionST.ToString(); assertEquals( expecting, found ); }
public void TestSetFullyQualifiedRefToCurrentRuleRetVal() { string action = "$a.i = 1;"; string expecting = "retval.i = 1;"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.SetErrorListener( equeue ); Grammar g = new Grammar( "grammar t;\n" + "a returns [int i, int j]: {" + action + "}\n" + " ;\n" ); AntlrTool antlr = newTool(); CodeGenerator generator = new CodeGenerator( antlr, g, "Java" ); g.CodeGenerator = generator; generator.GenRecognizer(); // forces load of templates ActionTranslator translator = new ActionTranslator( generator, "a", new CommonToken( ANTLRParser.ACTION, action ), 1 ); string rawTranslation = translator.Translate(); StringTemplateGroup templates = new StringTemplateGroup( ".", typeof( AngleBracketTemplateLexer ) ); StringTemplate actionST = new StringTemplate( templates, rawTranslation ); string found = actionST.ToString(); assertEquals( expecting, found ); assertEquals( "unexpected errors: " + equeue, 0, equeue.errors.Count ); }
public void TestTokenRefTreeProperty() { string action = "$ID.tree;"; string expecting = "ID1_tree;"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.SetErrorListener( equeue ); Grammar g = new Grammar( "grammar t;\n" + "a : ID {" + action + "} ;" + "ID : 'a';\n" ); AntlrTool antlr = newTool(); antlr.SetOutputDirectory( null ); // write to /dev/null CodeGenerator generator = new CodeGenerator( antlr, g, "Java" ); g.CodeGenerator = generator; generator.GenRecognizer(); ActionTranslator translator = new ActionTranslator( generator, "a", new CommonToken( ANTLRParser.ACTION, action ), 1 ); string rawTranslation = translator.Translate(); StringTemplateGroup templates = new StringTemplateGroup( ".", typeof( AngleBracketTemplateLexer ) ); StringTemplate actionST = new StringTemplate( templates, rawTranslation ); string found = actionST.ToString(); assertEquals( expecting, found ); }
public void TestTokenLabels() { string action = "$id; $f; $id.text; $id.getText(); $id.dork " + "$id.type; $id.line; $id.pos; " + "$id.channel; $id.index;"; string expecting = "id; f; (id!=null?id.getText():null); id.getText(); id.dork (id!=null?id.getType():0); (id!=null?id.getLine():0); (id!=null?id.getCharPositionInLine():0); (id!=null?id.getChannel():0); (id!=null?id.getTokenIndex():0);"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.SetErrorListener( equeue ); Grammar g = new Grammar( "parser grammar t;\n" + "a : id=ID f=FLOAT {" + action + "}\n" + " ;" ); AntlrTool antlr = newTool(); CodeGenerator generator = new CodeGenerator( antlr, g, "Java" ); g.CodeGenerator = generator; generator.GenRecognizer(); // forces load of templates ActionTranslator translator = new ActionTranslator( generator, "a", new CommonToken( ANTLRParser.ACTION, action ), 1 ); string rawTranslation = translator.Translate(); StringTemplateGroup templates = new StringTemplateGroup( ".", typeof( AngleBracketTemplateLexer ) ); StringTemplate actionST = new StringTemplate( templates, rawTranslation ); string found = actionST.ToString(); assertEquals( expecting, found ); assertEquals( "unexpected errors: " + equeue, 0, equeue.errors.Count ); }
public void TestTokenLabelFromMultipleAlts() { string action = "$ID.text;"; // must be qualified string action2 = "$INT.text;"; // must be qualified string expecting = "(ID1!=null?ID1.getText():null);"; string expecting2 = "(INT2!=null?INT2.getText():null);"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.SetErrorListener( equeue ); Grammar g = new Grammar( "grammar t;\n" + "a : ID {" + action + "}\n" + " | INT {" + action2 + "}\n" + " ;\n" + "ID : 'a';\n" + "INT : '0';\n" ); AntlrTool antlr = newTool(); CodeGenerator generator = new CodeGenerator( antlr, g, "Java" ); g.CodeGenerator = generator; generator.GenRecognizer(); // forces load of templates ActionTranslator translator = new ActionTranslator( generator, "a", new CommonToken( ANTLRParser.ACTION, action ), 1 ); string rawTranslation = translator.Translate(); StringTemplateGroup templates = new StringTemplateGroup( ".", typeof( AngleBracketTemplateLexer ) ); StringTemplate actionST = new StringTemplate( templates, rawTranslation ); string found = actionST.ToString(); assertEquals( expecting, found ); assertEquals( "unexpected errors: " + equeue, 0, equeue.errors.Count ); translator = new ActionTranslator( generator, "a", new CommonToken( ANTLRParser.ACTION, action2 ), 2 ); rawTranslation = translator.Translate(); templates = new StringTemplateGroup( ".", typeof( AngleBracketTemplateLexer ) ); actionST = new StringTemplate( templates, rawTranslation ); found = actionST.ToString(); assertEquals( expecting2, found ); assertEquals( "unexpected errors: " + equeue, 0, equeue.errors.Count ); }
public void TestSimplePlusEqualLabel() { string action = "$ids.size();"; // must be qualified string expecting = "list_ids.size();"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.SetErrorListener( equeue ); Grammar g = new Grammar( "parser grammar t;\n" + "a : ids+=ID ( COMMA ids+=ID {" + action + "})* ;\n" ); AntlrTool antlr = newTool(); CodeGenerator generator = new CodeGenerator( antlr, g, "Java" ); g.CodeGenerator = generator; generator.GenRecognizer(); // forces load of templates ActionTranslator translator = new ActionTranslator( generator, "a", new CommonToken( ANTLRParser.ACTION, action ), 1 ); string rawTranslation = translator.Translate(); StringTemplateGroup templates = new StringTemplateGroup( ".", typeof( AngleBracketTemplateLexer ) ); StringTemplate actionST = new StringTemplate( templates, rawTranslation ); string found = actionST.ToString(); assertEquals( expecting, found ); assertEquals( "unexpected errors: " + equeue, 0, equeue.errors.Count ); }
public void TestSharedGlobalScope() { string action = "$Symbols::x;"; string expecting = "((Symbols_scope)Symbols_stack.peek()).x;"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.SetErrorListener( equeue ); Grammar g = new Grammar( "grammar t;\n" + "scope Symbols {\n" + " String x;\n" + "}\n" + "a\n" + "scope { int y; }\n" + "scope Symbols;\n" + " : b {" + action + "}\n" + " ;\n" + "b : ID {$Symbols::x=$ID.text} ;\n" + "ID : 'a';\n" ); AntlrTool antlr = newTool(); CodeGenerator generator = new CodeGenerator( antlr, g, "Java" ); g.CodeGenerator = generator; generator.GenRecognizer(); // forces load of templates ActionTranslator translator = new ActionTranslator( generator, "a", new CommonToken( ANTLRParser.ACTION, action ), 1 ); string rawTranslation = translator.Translate(); StringTemplateGroup templates = new StringTemplateGroup( ".", typeof( AngleBracketTemplateLexer ) ); StringTemplate actionST = new StringTemplate( templates, rawTranslation ); string found = actionST.ToString(); assertEquals( expecting, found ); assertEquals( "unexpected errors: " + equeue, 0, equeue.errors.Count ); }
public void TestSettingLexerRulePropertyRefs() { string action = "$text $type=1 $line=1 $pos=1 $channel=1 $index"; string expecting = "getText() _type=1 state.tokenStartLine=1 state.tokenStartCharPositionInLine=1 _channel=1 -1"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.SetErrorListener( equeue ); Grammar g = new Grammar( "lexer grammar t;\n" + "R : 'r' {" + action + "};\n" ); AntlrTool antlr = newTool(); CodeGenerator generator = new CodeGenerator( antlr, g, "Java" ); g.CodeGenerator = generator; generator.GenRecognizer(); // forces load of templates ActionTranslator translator = new ActionTranslator( generator, "R", new CommonToken( ANTLRParser.ACTION, action ), 1 ); string rawTranslation = translator.Translate(); StringTemplateGroup templates = new StringTemplateGroup( ".", typeof( AngleBracketTemplateLexer ) ); StringTemplate actionST = new StringTemplate( templates, rawTranslation ); string found = actionST.ToString(); assertEquals( expecting, found ); assertEquals( "unexpected errors: " + equeue, 0, equeue.errors.Count ); }
public void TestReturnWithMultipleRuleRefs() { string action1 = "$obj = $rule2.obj;"; string action2 = "$obj = $rule3.obj;"; string expecting1 = "obj = rule21;"; string expecting2 = "obj = rule32;"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.SetErrorListener( equeue ); Grammar g = new Grammar( "grammar t;\n" + "rule1 returns [ Object obj ]\n" + ": rule2 { " + action1 + " }\n" + "| rule3 { " + action2 + " }\n" + ";\n" + "rule2 returns [ Object obj ]\n" + ": foo='foo' { $obj = $foo.text; }\n" + ";\n" + "rule3 returns [ Object obj ]\n" + ": bar='bar' { $obj = $bar.text; }\n" + ";" ); AntlrTool antlr = newTool(); CodeGenerator generator = new CodeGenerator( antlr, g, "Java" ); g.CodeGenerator = generator; generator.GenRecognizer(); // forces load of templates int i = 0; string action = action1; string expecting = expecting1; do { ActionTranslator translator = new ActionTranslator( generator, "rule1", new CommonToken( ANTLRParser.ACTION, action ), i + 1 ); string rawTranslation = translator.Translate(); StringTemplateGroup templates = new StringTemplateGroup( ".", typeof( AngleBracketTemplateLexer ) ); StringTemplate actionST = new StringTemplate( templates, rawTranslation ); string found = actionST.ToString(); assertEquals( expecting, found ); action = action2; expecting = expecting2; } while ( i++ < 1 ); assertEquals( "unexpected errors: " + equeue, 0, equeue.errors.Count ); }
protected virtual StringTemplate WalkFixedDFAGeneratingStateMachine( StringTemplateGroup templates, DFA dfa, DFAState s, int k) { //System.Console.Out.WriteLine( "walk " + s.stateNumber + " in dfa for decision " + dfa.decisionNumber ); if (s.IsAcceptState) { StringTemplate dfaST2 = templates.GetInstanceOf("dfaAcceptState"); dfaST2.SetAttribute("alt", s.GetUniquelyPredictedAlt()); return(dfaST2); } // the default templates for generating a state and its edges // can be an if-then-else structure or a switch string dfaStateName = "dfaState"; string dfaLoopbackStateName = "dfaLoopbackState"; string dfaOptionalBlockStateName = "dfaOptionalBlockState"; string dfaEdgeName = "dfaEdge"; if (parentGenerator.CanGenerateSwitch(s)) { dfaStateName = "dfaStateSwitch"; dfaLoopbackStateName = "dfaLoopbackStateSwitch"; dfaOptionalBlockStateName = "dfaOptionalBlockStateSwitch"; dfaEdgeName = "dfaEdgeSwitch"; } StringTemplate dfaST = templates.GetInstanceOf(dfaStateName); if (dfa.NFADecisionStartState.decisionStateType == NFAState.LOOPBACK) { dfaST = templates.GetInstanceOf(dfaLoopbackStateName); } else if (dfa.NFADecisionStartState.decisionStateType == NFAState.OPTIONAL_BLOCK_START) { dfaST = templates.GetInstanceOf(dfaOptionalBlockStateName); } dfaST.SetAttribute("k", k); dfaST.SetAttribute("stateNumber", s.stateNumber); dfaST.SetAttribute("semPredState", s.IsResolvedWithPredicates); /* * string description = dfa.getNFADecisionStartState().Description; * description = parentGenerator.target.getTargetStringLiteralFromString( description ); * //System.Console.Out.WriteLine( "DFA: " + description + " associated with AST " + dfa.getNFADecisionStartState() ); * if ( description != null ) * { * dfaST.SetAttribute( "description", description ); * } */ int EOTPredicts = NFA.INVALID_ALT_NUMBER; DFAState EOTTarget = null; //System.Console.Out.WriteLine( "DFA state " + s.stateNumber ); for (int i = 0; i < s.NumberOfTransitions; i++) { Transition edge = (Transition)s.Transition(i); //System.Console.Out.WriteLine( "edge " + s.stateNumber + "-" + edge.label.ToString() + "->" + edge.target.stateNumber ); if (edge.label.Atom == Label.EOT) { // don't generate a real edge for EOT; track alt EOT predicts // generate that prediction in the else clause as default case EOTTarget = (DFAState)edge.target; EOTPredicts = EOTTarget.GetUniquelyPredictedAlt(); /* * System.Console.Out.WriteLine("DFA s"+s.stateNumber+" EOT goes to s"+ * edge.target.stateNumber+" predicates alt "+ * EOTPredicts); */ continue; } StringTemplate edgeST = templates.GetInstanceOf(dfaEdgeName); // If the template wants all the label values delineated, do that if (edgeST.GetFormalArgument("labels") != null) { List <string> labels = edge.Label.Set.Select(value => parentGenerator.GetTokenTypeAsTargetLabel(value)).ToList(); edgeST.SetAttribute("labels", labels); } else { // else create an expression to evaluate (the general case) edgeST.SetAttribute("labelExpr", parentGenerator.GenLabelExpr(templates, edge, k)); } // stick in any gated predicates for any edge if not already a pred if (!edge.label.IsSemanticPredicate) { DFAState target = (DFAState)edge.target; SemanticContext preds = target.GetGatedPredicatesInNFAConfigurations(); if (preds != null) { //System.Console.Out.WriteLine( "preds=" + target.getGatedPredicatesInNFAConfigurations() ); StringTemplate predST = preds.GenExpr(parentGenerator, parentGenerator.Templates, dfa); edgeST.SetAttribute("predicates", predST); } } StringTemplate targetST = WalkFixedDFAGeneratingStateMachine(templates, dfa, (DFAState)edge.target, k + 1); edgeST.SetAttribute("targetState", targetST); dfaST.SetAttribute("edges", edgeST); //System.Console.Out.WriteLine( "back to DFA " + dfa.decisionNumber + "." + s.stateNumber ); } // HANDLE EOT EDGE if (EOTPredicts != NFA.INVALID_ALT_NUMBER) { // EOT unique predicts an alt dfaST.SetAttribute("eotPredictsAlt", EOTPredicts); } else if (EOTTarget != null && EOTTarget.NumberOfTransitions > 0) { // EOT state has transitions so must split on predicates. // Generate predicate else-if clauses and then generate // NoViableAlt exception as else clause. // Note: these predicates emanate from the EOT target state // rather than the current DFAState s so the error message // might be slightly misleading if you are looking at the // state number. Predicates emanating from EOT targets are // hoisted up to the state that has the EOT edge. for (int i = 0; i < EOTTarget.NumberOfTransitions; i++) { Transition predEdge = (Transition)EOTTarget.Transition(i); StringTemplate edgeST = templates.GetInstanceOf(dfaEdgeName); edgeST.SetAttribute("labelExpr", parentGenerator.GenSemanticPredicateExpr(templates, predEdge)); // the target must be an accept state //System.Console.Out.WriteLine( "EOT edge" ); StringTemplate targetST = WalkFixedDFAGeneratingStateMachine(templates, dfa, (DFAState)predEdge.target, k + 1); edgeST.SetAttribute("targetState", targetST); dfaST.SetAttribute("edges", edgeST); } } return(dfaST); }
public void TestUnknownGlobalDynamicAttribute() { string action = "$Symbols::x"; string expecting = action; ErrorQueue equeue = new ErrorQueue(); ErrorManager.SetErrorListener( equeue ); Grammar g = new Grammar( "grammar t;\n" + "scope Symbols {\n" + " int n;\n" + "}\n" + "a : {'+action+'}\n" + " ;\n" ); AntlrTool antlr = newTool(); CodeGenerator generator = new CodeGenerator( antlr, g, "Java" ); g.CodeGenerator = generator; generator.GenRecognizer(); // forces load of templates ActionTranslator translator = new ActionTranslator( generator, "a", new CommonToken( ANTLRParser.ACTION, action ), 1 ); string rawTranslation = translator.Translate(); StringTemplateGroup templates = new StringTemplateGroup( ".", typeof( AngleBracketTemplateLexer ) ); StringTemplate actionST = new StringTemplate( templates, rawTranslation ); string found = actionST.ToString(); assertEquals( expecting, found ); int expectedMsgID = ErrorManager.MSG_UNKNOWN_DYNAMIC_SCOPE_ATTRIBUTE; object expectedArg = "Symbols"; object expectedArg2 = "x"; GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage( expectedMsgID, g, null, expectedArg, expectedArg2 ); checkError( equeue, expectedMessage ); }
public void TestDynamicScopeRefOkEvenThoughRuleRefExists() { string action = "$b::n;"; string expecting = "((b_scope)b_stack.peek()).n;"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.SetErrorListener( equeue ); Grammar g = new Grammar( "grammar t;\n" + "s : b ;\n" + "b\n" + "scope {\n" + " int n;\n" + "} : '(' b ')' {" + action + "}\n" + // refers to current invocation's n " ;\n" ); AntlrTool antlr = newTool(); CodeGenerator generator = new CodeGenerator( antlr, g, "Java" ); g.CodeGenerator = generator; generator.GenRecognizer(); // forces load of templates ActionTranslator translator = new ActionTranslator( generator, "b", new CommonToken( ANTLRParser.ACTION, action ), 1 ); string rawTranslation = translator.Translate(); StringTemplateGroup templates = new StringTemplateGroup( ".", typeof( AngleBracketTemplateLexer ) ); StringTemplate actionST = new StringTemplate( templates, rawTranslation ); string found = actionST.ToString(); assertEquals( expecting, found ); assertEquals( "unexpected errors: " + equeue, 0, equeue.errors.Count ); }
public void TestArguments() { string action = "$i; $i.x; $u; $u.x"; string expecting = "i; i.x; u; u.x"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.SetErrorListener( equeue ); Grammar g = new Grammar( "parser grammar t;\n" + "a[User u, int i]\n" + " : {" + action + "}\n" + " ;" ); AntlrTool antlr = newTool(); CodeGenerator generator = new CodeGenerator( antlr, g, "Java" ); g.CodeGenerator = generator; generator.GenRecognizer(); // forces load of templates ActionTranslator translator = new ActionTranslator( generator, "a", new CommonToken( ANTLRParser.ACTION, action ), 1 ); string rawTranslation = translator.Translate(); StringTemplateGroup templates = new StringTemplateGroup( ".", typeof( AngleBracketTemplateLexer ) ); StringTemplate actionST = new StringTemplate( templates, rawTranslation ); string found = actionST.ToString(); assertEquals( expecting, found ); assertEquals( "unexpected errors: " + equeue, 0, equeue.errors.Count ); }
public void TestEscaped_InAction() { string action = "int \\$n; \"\\$in string\\$\""; string expecting = "int $n; \"$in string$\""; Grammar g = new Grammar( "parser grammar t;\n" + "@members {" + action + "}\n" + "a[User u, int i]\n" + " : {" + action + "}\n" + " ;" ); AntlrTool antlr = newTool(); CodeGenerator generator = new CodeGenerator( antlr, g, "Java" ); g.CodeGenerator = generator; generator.GenRecognizer(); // forces load of templates ActionTranslator translator = new ActionTranslator( generator, "a", new CommonToken( ANTLRParser.ACTION, action ), 0 ); string rawTranslation = translator.Translate(); StringTemplateGroup templates = new StringTemplateGroup( ".", typeof( AngleBracketTemplateLexer ) ); StringTemplate actionST = new StringTemplate( templates, rawTranslation ); string found = actionST.ToString(); assertEquals( expecting, found ); }
public void TestUnqualifiedRuleScopeAttribute() { string action = "$n;"; // must be qualified string expecting = "$n;"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.SetErrorListener( equeue ); Grammar g = new Grammar( "grammar t;\n" + "a\n" + "scope {\n" + " int n;\n" + "} : b\n" + " ;\n" + "b : {'+action+'}\n" + " ;\n" ); AntlrTool antlr = newTool(); CodeGenerator generator = new CodeGenerator( antlr, g, "Java" ); ActionTranslator translator = new ActionTranslator( generator, "b", new CommonToken( ANTLRParser.ACTION, action ), 1 ); string rawTranslation = translator.Translate(); StringTemplateGroup templates = new StringTemplateGroup( ".", typeof( AngleBracketTemplateLexer ) ); StringTemplate actionST = new StringTemplate( templates, rawTranslation ); string found = actionST.ToString(); assertEquals( expecting, found ); int expectedMsgID = ErrorManager.MSG_UNKNOWN_SIMPLE_ATTRIBUTE; object expectedArg = "n"; object expectedArg2 = null; GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage( expectedMsgID, g, null, expectedArg, expectedArg2 ); checkError( equeue, expectedMessage ); }
/** Generate an expression for traversing an edge. */ protected internal virtual StringTemplate GenLabelExpr( StringTemplateGroup templates, Transition edge, int k ) { Label label = edge.label; if ( label.IsSemanticPredicate ) { return GenSemanticPredicateExpr( templates, edge ); } if ( label.IsSet ) { return GenSetExpr( templates, label.Set, k, true ); } // must be simple label StringTemplate eST = templates.GetInstanceOf( "lookaheadTest" ); eST.SetAttribute( "atom", GetTokenTypeAsTargetLabel( label.Atom ) ); eST.SetAttribute( "atomAsInt", label.Atom ); eST.SetAttribute( "k", k ); return eST; }
public void TestAssignToOwnRulenameAttr() { string action = "$rule.tree = null;"; string expecting = "retval.tree = null;"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.SetErrorListener( equeue ); Grammar g = new Grammar( "grammar a;\n" + "rule\n" + " : 'y' {" + action + "}\n" + " ;" ); AntlrTool antlr = newTool(); CodeGenerator generator = new CodeGenerator( antlr, g, "Java" ); g.CodeGenerator = generator; generator.GenRecognizer(); // forces load of templates ActionTranslator translator = new ActionTranslator( generator, "rule", new CommonToken( ANTLRParser.ACTION, action ), 1 ); string rawTranslation = translator.Translate(); StringTemplateGroup templates = new StringTemplateGroup( ".", typeof( AngleBracketTemplateLexer ) ); StringTemplate actionST = new StringTemplate( templates, rawTranslation ); string found = actionST.ToString(); assertEquals( expecting, found ); assertEquals( "unexpected errors: " + equeue, 0, equeue.errors.Count ); }
/** For intervals such as [3..3, 30..35], generate an expression that * tests the lookahead similar to LA(1)==3 || (LA(1)>=30&&LA(1)<=35) */ public virtual StringTemplate GenSetExpr( StringTemplateGroup templates, IIntSet set, int k, bool partOfDFA ) { if ( !( set is IntervalSet ) ) { throw new ArgumentException( "unable to generate expressions for non IntervalSet objects" ); } IntervalSet iset = (IntervalSet)set; if ( iset.Intervals == null || iset.Intervals.Count == 0 ) { StringTemplate emptyST = new StringTemplate( templates, "" ); emptyST.Name = "empty-set-expr"; return emptyST; } string testSTName = "lookaheadTest"; string testRangeSTName = "lookaheadRangeTest"; if ( !partOfDFA ) { testSTName = "isolatedLookaheadTest"; testRangeSTName = "isolatedLookaheadRangeTest"; } StringTemplate setST = templates.GetInstanceOf( "setTest" ); int rangeNumber = 1; foreach ( Interval I in iset.GetIntervals() ) { int a = I.a; int b = I.b; StringTemplate eST; if ( a == b ) { eST = templates.GetInstanceOf( testSTName ); eST.SetAttribute( "atom", GetTokenTypeAsTargetLabel( a ) ); eST.SetAttribute( "atomAsInt", a ); //eST.setAttribute("k",Utils.integer(k)); } else { eST = templates.GetInstanceOf( testRangeSTName ); eST.SetAttribute( "lower", GetTokenTypeAsTargetLabel( a ) ); eST.SetAttribute( "lowerAsInt", a ); eST.SetAttribute( "upper", GetTokenTypeAsTargetLabel( b ) ); eST.SetAttribute( "upperAsInt", b ); eST.SetAttribute( "rangeNumber", rangeNumber ); } eST.SetAttribute( "k", k ); setST.SetAttribute( "ranges", eST ); rangeNumber++; } return setST; }
public void TestAssignToTreeNodeAttribute() { string action = "$tree.scope = localScope;"; string expecting = "(()retval.tree).scope = localScope;"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.SetErrorListener( equeue ); Grammar g = new Grammar( "grammar a;\n" + "options { output=AST; }" + "rule\n" + "@init {\n" + " Scope localScope=null;\n" + "}\n" + "@after {\n" + " $tree.scope = localScope;\n" + "}\n" + " : 'a' -> ^('a')\n" + ";" ); AntlrTool antlr = newTool(); CodeGenerator generator = new CodeGenerator( antlr, g, "Java" ); g.CodeGenerator = generator; generator.GenRecognizer(); // forces load of templates ActionTranslator translator = new ActionTranslator( generator, "rule", new CommonToken( ANTLRParser.ACTION, action ), 1 ); string rawTranslation = translator.Translate(); StringTemplateGroup templates = new StringTemplateGroup( ".", typeof( AngleBracketTemplateLexer ) ); StringTemplate actionST = new StringTemplate( templates, rawTranslation ); string found = actionST.ToString(); assertEquals( "unexpected errors: " + equeue, 0, equeue.errors.Count ); assertEquals( expecting, found ); }
public void TestBasicRuleScope() { string action = "$a::n;"; string expecting = "((a_scope)a_stack.peek()).n;"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.SetErrorListener( equeue ); Grammar g = new Grammar( "grammar t;\n" + "a\n" + "scope {\n" + " int n;\n" + "} : {" + action + "}\n" + " ;\n" ); AntlrTool antlr = newTool(); CodeGenerator generator = new CodeGenerator( antlr, g, "Java" ); g.CodeGenerator = generator; generator.GenRecognizer(); // forces load of templates ActionTranslator translator = new ActionTranslator( generator, "a", new CommonToken( ANTLRParser.ACTION, action ), 1 ); string rawTranslation = translator.Translate(); StringTemplateGroup templates = new StringTemplateGroup( ".", typeof( AngleBracketTemplateLexer ) ); StringTemplate actionST = new StringTemplate( templates, rawTranslation ); string found = actionST.ToString(); assertEquals( expecting, found ); assertEquals( "unexpected errors: " + equeue, 0, equeue.errors.Count ); }
public void TestRuleLabelBeforeRefToPredefinedAttr() { // As of Mar 2007, I'm removing unused labels. Unfortunately, // the action is not seen until code gen. Can't see $x.text // before stripping unused labels. We really need to translate // actions first so code gen logic can use info. string action = "$x.text"; string expecting = "(x!=null?input.toString(x.start,x.stop):null)"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.SetErrorListener( equeue ); Grammar g = new Grammar( "parser grammar t;\n" + "a : x=b {" + action + "} ;\n" + "b : B ;\n" ); AntlrTool antlr = newTool(); CodeGenerator generator = new CodeGenerator( antlr, g, "Java" ); g.CodeGenerator = generator; generator.GenRecognizer(); // forces load of templates ActionTranslator translator = new ActionTranslator( generator, "a", new CommonToken( ANTLRParser.ACTION, action ), 1 ); string rawTranslation = translator.Translate(); StringTemplateGroup templates = new StringTemplateGroup( ".", typeof( AngleBracketTemplateLexer ) ); StringTemplate actionST = new StringTemplate( templates, rawTranslation ); string found = actionST.ToString(); assertEquals( expecting, found ); assertEquals( "unexpected errors: " + equeue, 0, equeue.errors.Count ); }
public void Init( Grammar g ) { this.grammar = g; this.generator = grammar.CodeGenerator; this.templates = generator.Templates; }
public virtual StringTemplate GenFixedLookaheadDecision(StringTemplateGroup templates, DFA dfa) { return(WalkFixedDFAGeneratingStateMachine(templates, dfa, dfa.startState, 1)); }
/** The format gets reset either from the Tool if the user supplied a command line option to that effect * Otherwise we just use the default "antlr". */ public static void SetFormat(String formatName) { ErrorManager.formatName = formatName; //String fileName = "org/antlr/tool/templates/messages/formats/"+formatName+".stg"; string fileName = @"Tool\Templates\messages\formats\" + formatName + ".stg"; string streamName = "Antlr3." + fileName.Replace('\\', '.'); fileName = System.IO.Path.Combine(AntlrTool.ToolPathRoot, fileName); //ClassLoader cl = Thread.currentThread().getContextClassLoader(); //InputStream is = cl.getResourceAsStream(fileName); System.IO.Stream @is; if (System.IO.File.Exists(fileName)) { @is = new System.IO.MemoryStream(System.IO.File.ReadAllBytes(fileName)); } else { @is = typeof(ErrorManager).Assembly.GetManifestResourceStream(streamName); } //if ( is==null ) { // cl = ErrorManager.class.getClassLoader(); // is = cl.getResourceAsStream(fileName); //} if (@is == null && formatName.Equals("antlr")) { RawError("ANTLR installation corrupted; cannot find ANTLR messages format file " + fileName); Panic(); } else if (@is == null) { RawError("no such message format file " + fileName + " retrying with default ANTLR format"); SetFormat("antlr"); // recurse on this rule, trying the default message format return; } StreamReader br = null; try { br = new StreamReader(new System.IO.BufferedStream(@is)); format = new StringTemplateGroup(br, typeof(AngleBracketTemplateLexer), initSTListener); } finally { try { if (br != null) { br.Close(); } } catch (IOException ioe) { RawError("cannot close message format file " + fileName, ioe); } } format.ErrorListener = blankSTListener; bool formatOK = VerifyFormat(); if (!formatOK && formatName.Equals("antlr")) { RawError("ANTLR installation corrupted; ANTLR messages format file " + formatName + ".stg incomplete"); Panic(); } else if (!formatOK) { SetFormat("antlr"); // recurse on this rule, trying the default message format } }
public void Test0IndexedGlobalScope() { string action = "$Symbols[0]::names.add($id.text);"; string expecting = "((Symbols_scope)Symbols_stack.elementAt(0)).names.add((id!=null?id.getText():null));"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.SetErrorListener( equeue ); Grammar g = new Grammar( "grammar t;\n" + "scope Symbols {\n" + " int n;\n" + " List names;\n" + "}\n" + "a scope Symbols; : (id=ID ';' {" + action + "} )+\n" + " ;\n" + "ID : 'a';\n" ); AntlrTool antlr = newTool(); CodeGenerator generator = new CodeGenerator( antlr, g, "Java" ); g.CodeGenerator = generator; generator.GenRecognizer(); // forces load of templates ActionTranslator translator = new ActionTranslator( generator, "a", new CommonToken( ANTLRParser.ACTION, action ), 1 ); string rawTranslation = translator.Translate(); StringTemplateGroup templates = new StringTemplateGroup( ".", typeof( AngleBracketTemplateLexer ) ); assertEquals( expecting, rawTranslation ); assertEquals( "unexpected errors: " + equeue, 0, equeue.errors.Count ); }
public void Init(Grammar g) { this.grammar = g; this.generator = grammar.CodeGenerator; this.templates = generator.Templates; }
public void TestDoNotTranslateScopeAttributeCompare() { string action = "if ($rule::foo == \"foo\" || 1) { System.out.println(\"ouch\"); }"; string expecting = "if (((rule_scope)rule_stack.peek()).foo == \"foo\" || 1) { System.out.println(\"ouch\"); }"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.SetErrorListener( equeue ); Grammar g = new Grammar( "grammar a;\n" + "rule\n" + "scope {\n" + " String foo;" + "} :\n" + " twoIDs" + " ;\n" + "twoIDs:\n" + " ID ID {" + action + "}\n" + " ;\n" + "ID : 'id';" ); AntlrTool antlr = newTool(); CodeGenerator generator = new CodeGenerator( antlr, g, "Java" ); g.CodeGenerator = generator; generator.GenRecognizer(); ActionTranslator translator = new ActionTranslator( generator, "twoIDs", new CommonToken( ANTLRParser.ACTION, action ), 1 ); string rawTranslation = translator.Translate(); // check that we didn't use scopeSetAttributeRef int translation! bool foundScopeSetAttributeRef = false; for ( int i = 0; i < translator.chunks.Count; i++ ) { object chunk = translator.chunks[i]; if ( chunk is StringTemplate ) { if ( ( (StringTemplate)chunk ).Name.Equals( "scopeSetAttributeRef" ) ) { foundScopeSetAttributeRef = true; } } } assertFalse( "action translator used scopeSetAttributeRef template in comparison!", foundScopeSetAttributeRef ); StringTemplateGroup templates = new StringTemplateGroup( ".", typeof( AngleBracketTemplateLexer ) ); StringTemplate actionST = new StringTemplate( templates, rawTranslation ); string found = actionST.ToString(); assertEquals( "unexpected errors: " + equeue, 0, equeue.errors.Count ); assertEquals( expecting, found ); }
public void TestComplicatedArgParsingWithTranslation() { string action = "x, $A.text+\"3242\", (*$A).foo(21,33), 3.2+1, '\\n', " + "\"a,oo\\nick\", {bl, \"fdkj\"eck}"; string expecting = "x, (A1!=null?A1.getText():null)+\"3242\", (*A1).foo(21,33), 3.2+1, '\\n', \"a,oo\\nick\", {bl, \"fdkj\"eck}"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.SetErrorListener( equeue ); // now check in actual grammar. Grammar g = new Grammar( "parser grammar t;\n" + "a[User u, int i]\n" + " : A a[" + action + "] B\n" + " ;" ); AntlrTool antlr = newTool(); CodeGenerator generator = new CodeGenerator( antlr, g, "Java" ); g.CodeGenerator = generator; generator.GenRecognizer(); // forces load of templates ActionTranslator translator = new ActionTranslator( generator, "a", new CommonToken( ANTLRParser.ACTION, action ), 1 ); string rawTranslation = translator.Translate(); StringTemplateGroup templates = new StringTemplateGroup( ".", typeof( AngleBracketTemplateLexer ) ); StringTemplate actionST = new StringTemplate( templates, rawTranslation ); string found = actionST.ToString(); assertEquals( expecting, found ); assertEquals( "unexpected errors: " + equeue, 0, equeue.errors.Count ); }
public void TestRuleRefWithDynamicScope() { string action = "$field::x = $field.st;"; string expecting = "((field_scope)field_stack.peek()).x = retval.st;"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.SetErrorListener( equeue ); Grammar g = new Grammar( "grammar a;\n" + "field\n" + "scope { StringTemplate x; }\n" + " : 'y' {" + action + "}\n" + " ;\n" ); AntlrTool antlr = newTool(); CodeGenerator generator = new CodeGenerator( antlr, g, "Java" ); g.CodeGenerator = generator; generator.GenRecognizer(); // forces load of templates ActionTranslator translator = new ActionTranslator( generator, "field", new CommonToken( ANTLRParser.ACTION, action ), 1 ); string rawTranslation = translator.Translate(); StringTemplateGroup templates = new StringTemplateGroup( ".", typeof( AngleBracketTemplateLexer ) ); StringTemplate actionST = new StringTemplate( templates, rawTranslation ); string found = actionST.ToString(); assertEquals( expecting, found ); assertEquals( "unexpected errors: " + equeue, 0, equeue.errors.Count ); }
/** Generate an expression that will evaluate the semantic context, * given a set of output templates. */ public abstract StringTemplate GenExpr(CodeGenerator generator, StringTemplateGroup templates, DFA dfa);
public void TestDoNotTranslateAttributeCompare() { string action = "$a.line == $b.line"; string expecting = "(a!=null?a.getLine():0) == (b!=null?b.getLine():0)"; ErrorQueue equeue = new ErrorQueue(); ErrorManager.SetErrorListener( equeue ); Grammar g = new Grammar( "lexer grammar a;\n" + "RULE:\n" + " a=ID b=ID {" + action + "}" + " ;\n" + "ID : 'id';" ); AntlrTool antlr = newTool(); CodeGenerator generator = new CodeGenerator( antlr, g, "Java" ); g.CodeGenerator = generator; generator.GenRecognizer(); ActionTranslator translator = new ActionTranslator( generator, "RULE", new CommonToken( ANTLRParser.ACTION, action ), 1 ); string rawTranslation = translator.Translate(); StringTemplateGroup templates = new StringTemplateGroup( ".", typeof( AngleBracketTemplateLexer ) ); StringTemplate actionST = new StringTemplate( templates, rawTranslation ); string found = actionST.ToString(); assertEquals( "unexpected errors: " + equeue, 0, equeue.errors.Count ); assertEquals( expecting, found ); }