protected StringTemplate GetRuleElementST(string name, string ruleTargetName, GrammarAST elementAST, GrammarAST ast_suffix, string label) { string suffix = GetSTSuffix(elementAST, ast_suffix, label); name += suffix; // if we're building trees and there is no label, gen a label // unless we're in a synpred rule. Rule r = grammar.GetRule(currentRuleName); if ((grammar.BuildAST || suffix.Length > 0) && label == null && (r == null || !r.isSynPred)) { // we will need a label to do the AST or tracking, make one label = generator.CreateUniqueLabel(ruleTargetName); CommonToken labelTok = new CommonToken(ANTLRParser.ID, label); grammar.DefineRuleRefLabel(currentRuleName, labelTok, elementAST); } StringTemplate elementST = templates.GetInstanceOf(name); if (label != null) { elementST.SetAttribute("label", label); } return(elementST); }
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; }
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 virtual StringTemplate GetDependencies() { LoadDependencyTemplates(); StringTemplate dependenciesST = templates.GetInstanceOf("dependencies"); dependenciesST.SetAttribute("in", GetDependenciesFileList()); dependenciesST.SetAttribute("out", GetGeneratedFileList()); dependenciesST.SetAttribute("grammarFileName", grammar.fileName); return(dependenciesST); }
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) { 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 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 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; }
/** Return a StringTemplate that refers to the current format used for * emitting messages. */ public static StringTemplate GetLocationFormat() { return(format.GetInstanceOf("location")); }
/** Given a message ID, return a StringTemplate that somebody can fill * with data. We need to convert the int ID to the name of a template * in the messages ST group. */ public static StringTemplate GetMessage(int msgID) { String msgName = idToMessageTemplateName[msgID]; return(messages.GetInstanceOf(msgName)); }
/** 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; }
/** 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 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 StringTemplate GenExpr( CodeGenerator generator, StringTemplateGroup templates, DFA dfa ) { if ( templates != null ) { return templates.GetInstanceOf( "false" ); } return new StringTemplate( "false" ); }
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); }
/** Return a String containing a DOT description that, when displayed, * will show the incoming state machine visually. All nodes reachable * from startState will be included. */ public virtual string GenerateGraph(State startState) { if (startState == null) { return(null); } // The output DOT graph for visualization StringTemplate dot = null; markedStates = new HashSet <int>(); if (startState is DFAState) { dot = stlib.GetInstanceOf(Path.Combine(dfaTemplateDirectoryName, "dfa")); dot.SetAttribute("startState", startState.stateNumber); dot.SetAttribute("useBox", AntlrTool.internalOption_ShowNFAConfigsInDFA); WalkCreatingDFADOT(dot, (DFAState)startState); } else { dot = stlib.GetInstanceOf(Path.Combine(dfaTemplateDirectoryName, "nfa")); dot.SetAttribute("startState", startState.stateNumber); WalkRuleNFACreatingDOT(dot, startState); } dot.SetAttribute("rankdir", rankdir); return(dot.ToString()); }
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 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; }