public virtual string GetOptionString(string key) { GrammarAST value = GetOptionAST(key); if (value == null) { return(null); } if (value is ActionAST) { return(value.Text); } else { string v = value.Text; if (v.StartsWith("'") || v.StartsWith("\"")) { v = CharSupport.GetStringFromGrammarStringLiteral(v); if (v == null) { g.tool.errMgr.GrammarError(ErrorType.INVALID_ESCAPE_SEQUENCE, g.fileName, value.Token); v = ""; } } return(v); } }
public AltAST(AltAST node) : base(node) { this.alt = node.alt; this.altLabel = node.altLabel; this.leftRecursiveAltInfo = node.leftRecursiveAltInfo; }
public GrammarAST(GrammarAST node) : base(node) { this.g = node.g; this.atnState = node.atnState; this.textOverride = node.textOverride; }
// TODO: don't include this node!! public virtual CommonTree GetFirstDescendantWithType(Antlr.Runtime.BitSet types) { if (types.Member(Type)) { return(this); } if (Children == null) { return(null); } foreach (object c in Children) { GrammarAST t = (GrammarAST)c; if (types.Member(t.Type)) { return(t); } CommonTree d = t.GetFirstDescendantWithType(types); if (d != null) { return(d); } } return(null); }
// TODO: move to basetree when i settle on how runtime works // TODO: don't include this node!! // TODO: reuse other method public virtual CommonTree GetFirstDescendantWithType(int type) { if (Type == type) { return(this); } if (Children == null) { return(null); } foreach (object c in Children) { GrammarAST t = (GrammarAST)c; if (t.Type == type) { return(t); } CommonTree d = t.GetFirstDescendantWithType(type); if (d != null) { return(d); } } return(null); }
/** Walk ancestors of this node until we find ALT with * alt!=null or leftRecursiveAltInfo!=null. Then grab label if any. * If not a rule element, just returns null. */ public virtual string GetAltLabel() { IList <ITree> ancestors = this.GetAncestors(); if (ancestors == null) { return(null); } for (int i = ancestors.Count - 1; i >= 0; i--) { GrammarAST p = (GrammarAST)ancestors[i]; if (p.Type == ANTLRParser.ALT) { AltAST a = (AltAST)p; if (a.altLabel != null) { return(a.altLabel.Text); } if (a.leftRecursiveAltInfo != null) { return(a.leftRecursiveAltInfo.altLabel); } } } return(null); }
public Choice(OutputModelFactory factory, GrammarAST blkOrEbnfRootAST, IList<CodeBlockForAlt> alts) : base(factory, blkOrEbnfRootAST) { this.alts = alts; }
public LabelElementPair(Grammar g, GrammarAST label, GrammarAST element, int labelOp) { this.label = label; this.element = element; // compute general case for label type if (element.GetFirstDescendantWithType(tokenTypeForTokens) != null) { if (labelOp == ANTLRParser.ASSIGN) type = LabelType.TOKEN_LABEL; else type = LabelType.TOKEN_LIST_LABEL; } else if (element.GetFirstDescendantWithType(ANTLRParser.RULE_REF) != null) { if (labelOp == ANTLRParser.ASSIGN) type = LabelType.RULE_LABEL; else type = LabelType.RULE_LIST_LABEL; } // now reset if lexer and string if (g.IsLexer()) { if (element.GetFirstDescendantWithType(ANTLRParser.STRING_LITERAL) != null) { if (labelOp == ANTLRParser.ASSIGN) type = LabelType.LEXER_STRING_LABEL; } } }
public virtual void SetOption(string key, GrammarAST node) { if (options == null) { options = new Dictionary <string, GrammarAST>(); } options[key] = node; }
public virtual GrammarAST DupTree() { GrammarAST t = this; ICharStream input = this.Token.InputStream; GrammarASTAdaptor adaptor = new GrammarASTAdaptor(input); return((GrammarAST)adaptor.DupTree(t)); }
protected SrcOp(OutputModelFactory factory, GrammarAST ast) : base(factory, ast) { if (ast != null) uniqueID = ast.Token.TokenIndex; enclosingBlock = factory.GetCurrentBlock(); enclosingRuleRunction = factory.GetCurrentRuleFunction(); }
public Loop(OutputModelFactory factory, GrammarAST blkOrEbnfRootAST, IList<CodeBlockForAlt> alts) : base(factory, blkOrEbnfRootAST, alts) { bool nongreedy = (blkOrEbnfRootAST is QuantifierAST) && !((QuantifierAST)blkOrEbnfRootAST).GetGreedy(); exitAlt = nongreedy ? 1 : alts.Count + 1; }
public virtual void ReduceBlocksToSets(GrammarAST root) { CommonTreeNodeStream nodes = new CommonTreeNodeStream(new GrammarASTAdaptor(), root); GrammarASTAdaptor adaptor = new GrammarASTAdaptor(); BlockSetTransformer transformer = new BlockSetTransformer(nodes, g); transformer.TreeAdaptor = adaptor; transformer.Downup(root); }
// @ModelElement public ThrowNoViableAlt error; public AltBlock(OutputModelFactory factory, GrammarAST blkOrEbnfRootAST, IList<CodeBlockForAlt> alts) : base(factory, blkOrEbnfRootAST, alts) { decision = ((BlockStartState)blkOrEbnfRootAST.atnState).decision; // interp.predict() throws exception // this.error = new ThrowNoViableAlt(factory, blkOrEbnfRootAST, null); }
public override void DiscoverRule(RuleAST rule, GrammarAST ID, IList<GrammarAST> modifiers, ActionAST arg, ActionAST returns, GrammarAST thrws, GrammarAST options, ActionAST locals, IList<GrammarAST> actions, GrammarAST block) { currentRule = g.GetRule(ID.Text); }
public TestSetInline(OutputModelFactory factory, GrammarAST ast, IntervalSet set, int wordSize) : base(factory, ast) { bitsetWordSize = wordSize; Bitset[] withZeroOffset = CreateBitsets(factory, set, wordSize, true); Bitset[] withoutZeroOffset = CreateBitsets(factory, set, wordSize, false); this.bitsets = withZeroOffset.Length <= withoutZeroOffset.Length ? withZeroOffset : withoutZeroOffset; this.varName = "_la"; }
public override IList<SrcOp> RuleRef(GrammarAST ID, GrammarAST label, GrammarAST args) { InvokeRule invokeOp = new InvokeRule(this, ID, label); // If no manual label and action refs as token/rule not label, we need to define implicit label if (controller.NeedsImplicitLabel(ID, invokeOp)) DefineImplicitLabel(ID, invokeOp); AddToLabelList listLabelOp = GetAddToListOpIfListLabelPresent(invokeOp, label); return List(invokeOp, listLabelOp); }
public MatchSet(OutputModelFactory factory, GrammarAST ast) : base(factory, ast) { SetTransition st = (SetTransition)ast.atnState.Transition(0); int wordSize = factory.GetGenerator().GetTarget().GetInlineTestSetWordSize(); expr = new TestSetInline(factory, null, st.set, wordSize); Decl.Decl d = new TokenTypeDecl(factory, expr.varName); factory.GetCurrentRuleFunction().AddLocalDecl(d); capture = new CaptureNextTokenType(factory, expr.varName); }
public ThrowRecognitionException(OutputModelFactory factory, GrammarAST ast, IntervalSet expecting) : base(factory, ast) { //this.decision = ((BlockStartState)ast.ATNState).decision; grammarLine = ast.Line; grammarLine = ast.CharPositionInLine; grammarFile = factory.GetGrammar().fileName; //this.expecting = factory.createExpectingBitSet(ast, decision, expecting, "error"); // factory.defineBitSet(this.expecting); }
public virtual string GetRuleName() { GrammarAST nameNode = (GrammarAST)GetChild(0); if (nameNode != null) { return(nameNode.Text); } return(null); }
public StarBlock(OutputModelFactory factory, GrammarAST blkOrEbnfRootAST, IList<CodeBlockForAlt> alts) : base(factory, blkOrEbnfRootAST, alts) { loopLabel = factory.GetTarget().GetLoopLabel(blkOrEbnfRootAST); StarLoopEntryState star = (StarLoopEntryState)blkOrEbnfRootAST.atnState; loopBackStateNumber = star.loopBackState.stateNumber; decision = star.decision; }
// public BitSetDecl expecting; public Sync(OutputModelFactory factory, GrammarAST blkOrEbnfRootAST, IntervalSet expecting, int decision, string position) : base(factory, blkOrEbnfRootAST) { this.decision = decision; // this.expecting = factory.createExpectingBitSet(ast, decision, expecting, position); // factory.defineBitSet(this.expecting); }
public LL1AltBlock(OutputModelFactory factory, GrammarAST blkAST, IList<CodeBlockForAlt> alts) : base(factory, blkAST, alts) { this.decision = ((DecisionState)blkAST.atnState).decision; /* Lookahead for each alt 1..n */ IntervalSet[] altLookSets = factory.GetGrammar().decisionLOOK[decision]; altLook = GetAltLookaheadAsStringLists(altLookSets); IntervalSet expecting = IntervalSet.Or(altLookSets); // combine alt sets this.error = GetThrowNoViableAlt(factory, blkAST, expecting); }
public virtual void GetNodesWithTypePreorderDFS_(IList <GrammarAST> nodes, IntervalSet types) { if (types.Contains(this.Type)) { nodes.Add(this); } // walk all children of root. for (int i = 0; i < ChildCount; i++) { GrammarAST child = (GrammarAST)GetChild(i); child.GetNodesWithTypePreorderDFS_(nodes, types); } }
public LL1StarBlockSingleAlt(OutputModelFactory factory, GrammarAST starRoot, IList<CodeBlockForAlt> alts) : base(factory, starRoot, alts) { StarLoopEntryState star = (StarLoopEntryState)starRoot.atnState; loopBackStateNumber = star.loopBackState.stateNumber; this.decision = star.decision; IntervalSet[] altLookSets = factory.GetGrammar().decisionLOOK[decision]; Debug.Assert(altLookSets.Length == 2); IntervalSet enterLook = altLookSets[0]; IntervalSet exitLook = altLookSets[1]; loopExpr = AddCodeForLoopLookaheadTempVar(enterLook); }
public PlusBlock(OutputModelFactory factory, GrammarAST plusRoot, IList<CodeBlockForAlt> alts) : base(factory, plusRoot, alts) { BlockAST blkAST = (BlockAST)plusRoot.GetChild(0); PlusBlockStartState blkStart = (PlusBlockStartState)blkAST.atnState; PlusLoopbackState loop = blkStart.loopBackState; stateNumber = blkStart.loopBackState.stateNumber; blockStartStateNumber = blkStart.stateNumber; loopBackStateNumber = loop.stateNumber; this.error = GetThrowNoViableAlt(factory, plusRoot, null); decision = loop.decision; }
public override void DiscoverRule(RuleAST rule, GrammarAST ID, IList<GrammarAST> modifiers, ActionAST arg, ActionAST returns, GrammarAST thrws, GrammarAST options, ActionAST locals, IList<GrammarAST> actions, GrammarAST block) { int numAlts = block.ChildCount; Rule r; if (LeftRecursiveRuleAnalyzer.HasImmediateRecursiveRuleRefs(rule, ID.Text)) { r = new LeftRecursiveRule(g, ID.Text, rule); } else { r = new Rule(g, ID.Text, rule, numAlts); } rules[r.name] = r; if (arg != null) { r.args = ScopeParser.ParseTypedArgList(arg, arg.Text, g); r.args.type = AttributeDict.DictType.ARG; r.args.ast = arg; arg.resolver = r.alt[currentOuterAltNumber]; } if (returns != null) { r.retvals = ScopeParser.ParseTypedArgList(returns, returns.Text, g); r.retvals.type = AttributeDict.DictType.RET; r.retvals.ast = returns; } if (locals != null) { r.locals = ScopeParser.ParseTypedArgList(locals, locals.Text, g); r.locals.type = AttributeDict.DictType.LOCAL; r.locals.ast = locals; } foreach (GrammarAST a in actions) { // a = ^(AT ID ACTION) ActionAST action = (ActionAST)a.GetChild(1); r.namedActions[a.GetChild(0).Text] = action; action.resolver = r; } }
public InvokeRule(ParserFactory factory, GrammarAST ast, GrammarAST labelAST) : base(factory, ast) { if (ast.atnState != null) { RuleTransition ruleTrans = (RuleTransition)ast.atnState.Transition(0); stateNumber = ast.atnState.stateNumber; } this.name = ast.Text; Rule r = factory.GetGrammar().GetRule(name); ctxName = factory.GetTarget().GetRuleFunctionContextStructName(r); // TODO: move to factory RuleFunction rf = factory.GetCurrentRuleFunction(); if (labelAST != null) { // for x=r, define <rule-context-type> x and list_x string label = labelAST.Text; if (labelAST.Parent.Type == ANTLRParser.PLUS_ASSIGN) { factory.DefineImplicitLabel(ast, this); string listLabel = factory.GetTarget().GetListLabel(label); RuleContextListDecl l = new RuleContextListDecl(factory, listLabel, ctxName); rf.AddContextDecl(ast.GetAltLabel(), l); } else { RuleContextDecl d = new RuleContextDecl(factory, label, ctxName); labels.Add(d); rf.AddContextDecl(ast.GetAltLabel(), d); } } ActionAST arg = (ActionAST)ast.GetFirstChildWithType(ANTLRParser.ARG_ACTION); if (arg != null) { argExprsChunks = ActionTranslator.TranslateAction(factory, rf, arg.Token, arg); } // If action refs rule as rulename not label, we need to define implicit label if (factory.GetCurrentOuterMostAlt().ruleRefsInActions.ContainsKey(ast.Text)) { string label = factory.GetTarget().GetImplicitRuleLabel(ast.Text); RuleContextDecl d = new RuleContextDecl(factory, label, ctxName); labels.Add(d); rf.AddContextDecl(ast.GetAltLabel(), d); } }
public LeftRecursiveRuleAnalyzer(GrammarAST ruleAST, AntlrTool tool, string ruleName, string language) : base(new CommonTreeNodeStream(new GrammarASTAdaptor(ruleAST.Token.InputStream), ruleAST)) { this.tool = tool; this.ruleName = ruleName; this.language = language; this.tokenStream = ruleAST.g.tokenStream; if (this.tokenStream == null) { throw new InvalidOperationException("grammar must have a token stream"); } LoadPrecRuleTemplates(); }
public LL1PlusBlockSingleAlt(OutputModelFactory factory, GrammarAST plusRoot, IList<CodeBlockForAlt> alts) : base(factory, plusRoot, alts) { BlockAST blkAST = (BlockAST)plusRoot.GetChild(0); PlusBlockStartState blkStart = (PlusBlockStartState)blkAST.atnState; stateNumber = blkStart.loopBackState.stateNumber; blockStartStateNumber = blkStart.stateNumber; PlusBlockStartState plus = (PlusBlockStartState)blkAST.atnState; this.decision = plus.loopBackState.decision; IntervalSet[] altLookSets = factory.GetGrammar().decisionLOOK[decision]; IntervalSet loopBackLook = altLookSets[0]; loopExpr = AddCodeForLoopLookaheadTempVar(loopBackLook); }
/** Find and replace * ID*[','] with ID (',' ID)* * ID+[','] with ID (',' ID)+ * (x {action} y)+[','] with x {action} y (',' x {action} y)+ * * Parameter must be a token. * todo: do we want? */ public virtual void ExpandParameterizedLoops(GrammarAST root) { TreeVisitor v = new TreeVisitor(new GrammarASTAdaptor()); Antlr.Runtime.Misc.Func<object, object> preAction = t => { if (((GrammarAST)t).Type == 3) { return ExpandParameterizedLoop((GrammarAST)t); } return t; }; Antlr.Runtime.Misc.Func<object, object> postAction = t => t; v.Visit(root, new TreeVisitorAction(preAction, postAction)); }
/** Utility visitor that sets grammar ptr in each node */ public static void SetGrammarPtr(Grammar g, GrammarAST tree) { if (tree == null) return; // ensure each node has pointer to surrounding grammar Antlr.Runtime.Misc.Func<object, object> preAction = t => { ((GrammarAST)t).g = g; return t; }; Antlr.Runtime.Misc.Func<object, object> postAction = t => t; TreeVisitor v = new TreeVisitor(new GrammarASTAdaptor()); v.Visit(tree, new TreeVisitorAction(preAction, postAction)); }
public virtual GrammarAST GetNodeWithTokenIndex(int index) { if (this.Token != null && this.Token.TokenIndex == index) { return(this); } // walk all children of root. for (int i = 0; i < ChildCount; i++) { GrammarAST child = (GrammarAST)GetChild(i); GrammarAST result = child.GetNodeWithTokenIndex(index); if (result != null) { return(result); } } return(null); }
public IList<SrcOp> followExpr; // might not work in template if size>1 public LL1OptionalBlockSingleAlt(OutputModelFactory factory, GrammarAST blkAST, IList<CodeBlockForAlt> alts) : base(factory, blkAST, alts) { this.decision = ((DecisionState)blkAST.atnState).decision; /* Lookahead for each alt 1..n */ // IntervalSet[] altLookSets = LinearApproximator.getLL1LookaheadSets(dfa); IntervalSet[] altLookSets = factory.GetGrammar().decisionLOOK[decision]; altLook = GetAltLookaheadAsStringLists(altLookSets); IntervalSet look = altLookSets[0]; IntervalSet followLook = altLookSets[1]; IntervalSet expecting = look.Or(followLook); this.error = GetThrowNoViableAlt(factory, blkAST, expecting); expr = AddCodeForLookaheadTempVar(look); followExpr = factory.GetLL1Test(followLook, blkAST); }
public virtual string ToTokenString() { ICharStream input = this.Token.InputStream; GrammarASTAdaptor adaptor = new GrammarASTAdaptor(input); CommonTreeNodeStream nodes = new CommonTreeNodeStream(adaptor, this); StringBuilder buf = new StringBuilder(); GrammarAST o = (GrammarAST)nodes.LT(1); int type = adaptor.GetType(o); while (type != TokenTypes.EndOfFile) { buf.Append(" "); buf.Append(o.Text); nodes.Consume(); o = (GrammarAST)nodes.LT(1); type = adaptor.GetType(o); } return(buf.ToString()); }
public virtual IList<SrcOp> StringRef(GrammarAST ID, GrammarAST label) { return TokenRef(ID, label, null); }
public virtual IList<SrcOp> TokenRef(GrammarAST ID, GrammarAST label, GrammarAST args) { return null; }
public virtual bool NeedsImplicitLabel(GrammarAST ID, LabeledOp op) { return false; }
public virtual IList<SrcOp> GetLL1Test(IntervalSet look, GrammarAST blkAST) { return null; }
public virtual Choice GetComplexEBNFBlock(GrammarAST ebnfRoot, IList<CodeBlockForAlt> alts) { return null; }
// BLOCKS public virtual Choice GetChoiceBlock(BlockAST blkAST, IList<CodeBlockForAlt> alts, GrammarAST label) { return null; }