public HashSet <string> GetOverriddenRulesWithDifferentFIRST() { // walk every rule in this grammar and compare FIRST set with // those in imported grammars. HashSet <string> rules = new HashSet <string>(); for (Iterator it = getRules().iterator(); it.hasNext();) { Rule r = (Rule)it.next(); //[email protected](r.name+" FIRST="+r.FIRST); for (int i = 0; i < delegates.size(); i++) { Grammar g = delegates.get(i); Rule importedRule = g.getRule(r.name); if (importedRule != null) // exists in imported grammar // [email protected](r.name+" exists in imported grammar: FIRST="+importedRule.FIRST); { if (!r.FIRST.equals(importedRule.FIRST)) { rules.add(r.name); } } } } return(rules); }
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); }
/** Is there a non-syn-pred predicate visible from s that is not in * the rule enclosing s? This accounts for most predicate situations * and lets ANTLR do a simple LL(1)+pred computation. * * TODO: what about gated vs regular preds? */ public bool DetectConfoundingPredicates(NFAState s) { _lookBusy.Clear(); Rule r = s.enclosingRule; return(DetectConfoundingPredicatesCore(s, r, false) == DETECT_PRED_FOUND); }
public RuleClosureTransition( Rule rule, NFAState ruleStart, NFAState followState ) : base(Label.EPSILON, ruleStart) { this.rule = rule; this.followState = followState; }
public RuleClosureTransition(Rule rule, NFAState ruleStart, NFAState followState) : base(Label.EPSILON, ruleStart) { this._rule = rule; this._followState = followState; }
public LookaheadSet Look(Rule r) { if (r.FIRST == null) { r.FIRST = FIRST(r.startState); } return(r.FIRST); }
public ActionTranslator(CodeGenerator generator, string ruleName, GrammarAST actionAST) : this(new ANTLRStringStream(actionAST.Token.Text)) { this.generator = generator; this.grammar = generator.Grammar; this.enclosingRule = grammar.GetLocallyDefinedRule(ruleName); this.actionToken = actionAST.Token; this.outerAltNum = actionAST.outerAltNum; }
/** For \$rulelabel.name, return the Attribute found for name. It * will be a predefined property or a return value. */ public Attribute GetRuleLabelAttribute(string ruleName, string attrName) { Rule r = grammar.GetRule(ruleName); AttributeScope scope = r.GetLocalAttributeScope(attrName); if (scope != null && !scope.IsParameterScope) { return(scope.GetAttribute(attrName)); } return(null); }
public ActionTranslator(CodeGenerator generator, string ruleName, IToken actionToken, int outerAltNum) : this(new ANTLRStringStream(actionToken.Text)) { this.generator = generator; grammar = generator.Grammar; this.enclosingRule = grammar.GetRule(ruleName); this.actionToken = actionToken; this.outerAltNum = outerAltNum; }
protected StringTemplate GetTokenElementST(string name, string elementName, GrammarAST elementAST, GrammarAST ast_suffix, string label) { bool tryUnchecked = false; if (name == "matchSet" && !string.IsNullOrEmpty(elementAST.enclosingRuleName) && Rule.GetRuleType(elementAST.enclosingRuleName) == RuleType.Lexer) { if ((elementAST.Parent.Type == ANTLRLexer.ALT && elementAST.Parent.Parent.Parent.Type == RULE && elementAST.Parent.Parent.ChildCount == 2) || (elementAST.Parent.Type == ANTLRLexer.NOT && elementAST.Parent.Parent.Parent.Parent.Type == RULE && elementAST.Parent.Parent.Parent.ChildCount == 2)) { // single alt at the start of the rule needs to be checked } else { tryUnchecked = true; } } string suffix = GetSTSuffix(elementAST, ast_suffix, label); // 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)) { label = generator.CreateUniqueLabel(elementName); CommonToken labelTok = new CommonToken(ANTLRParser.ID, label); grammar.DefineTokenRefLabel(currentRuleName, labelTok, elementAST); } StringTemplate elementST = null; if (tryUnchecked && templates.IsDefined(name + "Unchecked" + suffix)) { elementST = templates.GetInstanceOf(name + "Unchecked" + suffix); } if (elementST == null) { elementST = templates.GetInstanceOf(name + suffix); } if (label != null) { elementST.SetAttribute("label", label); } return(elementST); }
public LookaheadSet Follow(Rule r) { //[email protected]("> FOLLOW("+r.name+") in rule "+r.startState.enclosingRule); LookaheadSet f = _followCache.get(r); if (f != null) { return(f); } f = FirstCore(r.stopState, true); _followCache[r] = f; //[email protected]("< FOLLOW("+r+") in rule "+r.startState.enclosingRule+"="+f.toString(this.grammar)); return(f); }
private AttributeScope ResolveDynamicScope(string scopeName) { if (grammar.GetGlobalScope(scopeName) != null) { return(grammar.GetGlobalScope(scopeName)); } Rule scopeRule = grammar.GetRule(scopeName); if (scopeRule != null) { return(scopeRule.RuleScope); } return(null); // not a valid dynamic scope }
private void HandleRuleScopeAttribute(string x, string y) { Grammar.LabelElementPair pair = enclosingRule.GetRuleLabel(x); string refdRuleName = pair != null ? pair.referencedRuleName : x; string label = x; if (pair == null) { // $ruleref.attr gotta get old label or compute new one CheckElementRefUniqueness(x, false); label = enclosingRule.GetElementLabel(x, outerAltNum, generator); if (label == null) { ErrorManager.GrammarError(ErrorManager.MSG_FORWARD_ELEMENT_REF, grammar, actionToken, "$" + x + "." + y); label = x; } } StringTemplate st; Rule refdRule = grammar.GetRule(refdRuleName); AttributeScope scope = refdRule.GetLocalAttributeScope(y); if (scope.IsPredefinedRuleScope) { st = Template("ruleLabelPropertyRef_" + y); grammar.ReferenceRuleLabelPredefinedAttribute(refdRuleName); st.SetAttribute("scope", label); st.SetAttribute("attr", y); } else if (scope.IsPredefinedLexerRuleScope) { st = Template("lexerRuleLabelPropertyRef_" + y); grammar.ReferenceRuleLabelPredefinedAttribute(refdRuleName); st.SetAttribute("scope", label); st.SetAttribute("attr", y); } else if (scope.IsParameterScope) { // TODO: error! } else { st = Template("ruleLabelRef"); st.SetAttribute("referencedRule", refdRule); st.SetAttribute("scope", label); st.SetAttribute("attr", scope.GetAttribute(y)); } }
public virtual void ComputeRuleFIRSTSets() { if (NumberOfDecisions == 0) { createNFAs(); } for (Iterator it = getRules().iterator(); it.hasNext();) { Rule r = (Rule)it.next(); if (r.isSynPred) { continue; } LookaheadSet s = FIRST(r); [email protected]("FIRST(" + r.name + ")=" + s); } }
/** Rewrite alt to have a synpred as first element; * (xxx)=>xxx * but only if they didn't specify one manually. */ protected virtual void PrefixWithSynPred(GrammarAST alt) { // if they want backtracking and it's not a lexer rule in combined grammar string autoBacktrack = (string)Grammar.GetBlockOption(currentBlockAST, "backtrack"); if (autoBacktrack == null) { autoBacktrack = (string)Grammar.GetOption("backtrack"); } if (autoBacktrack != null && autoBacktrack.Equals("true") && !(GrammarType == GrammarType.Combined && Rule.GetRuleType(currentRuleName) == RuleType.Lexer) && alt.GetChild(0).Type != SYN_SEMPRED) { // duplicate alt and make a synpred block around that dup'd alt GrammarAST synpredBlockAST = CreateBlockFromDupAlt(alt); // Create a BACKTRACK_SEMPRED node as if user had typed this in // Effectively we replace (xxx)=>xxx with {synpredxxx}? xxx GrammarAST synpredAST = CreateSynSemPredFromBlock(synpredBlockAST, BACKTRACK_SEMPRED); // insert BACKTRACK_SEMPRED as first element of alt //synpredAST.getLastSibling().setNextSibling( alt.getFirstChild() ); //synpredAST.addChild( alt.getFirstChild() ); //alt.setFirstChild( synpredAST ); GrammarAST[] children = alt.GetChildrenAsArray(); adaptor.SetChild(alt, 0, synpredAST); for (int i = 0; i < children.Length; i++) { if (i < children.Length - 1) { adaptor.SetChild(alt, i + 1, children[i]); } else { adaptor.AddChild(alt, children[i]); } } } }
public bool IsListLabel(string label) { bool hasListLabel = false; if (label != null) { Rule r = grammar.GetRule(currentRuleName); //String stName = null; if (r != null) { Grammar.LabelElementPair pair = r.GetLabel(label); if (pair != null && (pair.type == LabelType.TokenList || pair.type == LabelType.RuleList || pair.type == LabelType.WildcardTreeList)) { hasListLabel = true; } } } return(hasListLabel); }
/** Return a non-empty template name suffix if the token is to be * tracked, added to a tree, or both. */ protected string GetSTSuffix(GrammarAST elementAST, GrammarAST ast_suffix, string label) { if (grammar.type == GrammarType.Lexer) { return(""); } // handle list label stuff; make element use "Track" string operatorPart = ""; string rewritePart = ""; string listLabelPart = ""; Rule ruleDescr = grammar.GetRule(currentRuleName); if (ast_suffix != null && !ruleDescr.isSynPred) { if (ast_suffix.Type == ANTLRParser.ROOT) { operatorPart = "RuleRoot"; } else if (ast_suffix.Type == ANTLRParser.BANG) { operatorPart = "Bang"; } } if (currentAltHasASTRewrite && elementAST.Type != WILDCARD) { rewritePart = "Track"; } if (IsListLabel(label)) { listLabelPart = "AndListLabel"; } string STsuffix = operatorPart + rewritePart + listLabelPart; //[email protected]("suffix = "+STsuffix); return(STsuffix); }
/** Use for translating rule @init{...} actions that have no scope */ protected internal virtual void TranslateActionAttributeReferencesForSingleScope( Rule r, IDictionary<string,object> scopeActions ) { string ruleName = null; if ( r != null ) { ruleName = r.Name; } ICollection<string> actionNameSet = scopeActions.Keys.ToArray(); foreach ( string name in actionNameSet ) { GrammarAST actionAST = (GrammarAST)scopeActions.get( name ); IList chunks = TranslateAction( ruleName, actionAST ); scopeActions[name] = chunks; // replace with translation } }
public ActionTranslator( CodeGenerator generator, string ruleName, IToken actionToken, int outerAltNum ) : this(new ANTLRStringStream( actionToken.Text )) { this.generator = generator; grammar = generator.grammar; this.enclosingRule = grammar.GetRule( ruleName ); this.actionToken = actionToken; this.outerAltNum = outerAltNum; }
public ActionTranslator( CodeGenerator generator, string ruleName, GrammarAST actionAST ) : this(new ANTLRStringStream( actionAST.Token.Text )) { this.generator = generator; this.grammar = generator.grammar; this.enclosingRule = grammar.GetLocallyDefinedRule( ruleName ); this.actionToken = actionAST.Token; this.outerAltNum = actionAST.outerAltNum; }
protected virtual int DetectConfoundingPredicatesCore( NFAState s, Rule enclosingRule, bool chaseFollowTransitions ) { //[email protected]("_detectNonAutobacktrackPredicates("+s+")"); if ( !chaseFollowTransitions && s.IsAcceptState ) { if ( _grammar.type == GrammarType.Lexer ) { // FOLLOW makes no sense (at the moment!) for lexical rules. // assume all char can follow return DETECT_PRED_NOT_FOUND; } return DETECT_PRED_EOR; } if ( _lookBusy.Contains( s ) ) { // return a copy of an empty set; we may modify set inline return DETECT_PRED_NOT_FOUND; } _lookBusy.Add( s ); Transition transition0 = s.transition[0]; if ( transition0 == null ) { return DETECT_PRED_NOT_FOUND; } if ( !( transition0.label.IsSemanticPredicate || transition0.label.IsEpsilon ) ) { return DETECT_PRED_NOT_FOUND; } if ( transition0.label.IsSemanticPredicate ) { //[email protected]("pred "+transition0.label); SemanticContext ctx = transition0.label.SemanticContext; SemanticContext.Predicate p = (SemanticContext.Predicate)ctx; if ( p.predicateAST.Type != ANTLRParser.BACKTRACK_SEMPRED ) { return DETECT_PRED_FOUND; } } /* if ( transition0.label.isSemanticPredicate() ) { [email protected]("pred "+transition0.label); SemanticContext ctx = transition0.label.getSemanticContext(); SemanticContext.Predicate p = (SemanticContext.Predicate)ctx; // if a non-syn-pred found not in enclosingRule, say we found one if ( p.predicateAST.getType() != ANTLRParser.BACKTRACK_SEMPRED && !p.predicateAST.enclosingRuleName.equals(enclosingRule.name) ) { [email protected]("found pred "+p+" not in "+enclosingRule.name); return DETECT_PRED_FOUND; } } */ int result = DetectConfoundingPredicatesCore( (NFAState)transition0.target, enclosingRule, chaseFollowTransitions ); if ( result == DETECT_PRED_FOUND ) { return DETECT_PRED_FOUND; } if ( result == DETECT_PRED_EOR ) { if ( transition0 is RuleClosureTransition ) { // we called a rule that found the end of the rule. // That means the rule is nullable and we need to // keep looking at what follows the rule ref. E.g., // a : b A ; where b is nullable means that LOOK(a) // should include A. RuleClosureTransition ruleInvocationTrans = (RuleClosureTransition)transition0; NFAState following = (NFAState)ruleInvocationTrans.followState; int afterRuleResult = DetectConfoundingPredicatesCore( following, enclosingRule, chaseFollowTransitions ); if ( afterRuleResult == DETECT_PRED_FOUND ) { return DETECT_PRED_FOUND; } } } Transition transition1 = s.transition[1]; if ( transition1 != null ) { int t1Result = DetectConfoundingPredicatesCore( (NFAState)transition1.target, enclosingRule, chaseFollowTransitions ); if ( t1Result == DETECT_PRED_FOUND ) { return DETECT_PRED_FOUND; } } return DETECT_PRED_NOT_FOUND; }
public virtual void IssueInvalidAttributeError( string x, string y, Rule enclosingRule, IToken actionToken, int outerAltNum ) { //[email protected]("error $"+x+"."+y); if ( enclosingRule == null ) { // action not in a rule ErrorManager.GrammarError( ErrorManager.MSG_ATTRIBUTE_REF_NOT_IN_RULE, grammar, actionToken, x, y ); return; } // action is in a rule Grammar.LabelElementPair label = enclosingRule.GetRuleLabel( x ); if ( label != null || enclosingRule.GetRuleRefsInAlt( x, outerAltNum ) != null ) { // $rulelabel.attr or $ruleref.attr; must be unknown attr string refdRuleName = x; if ( label != null ) { refdRuleName = enclosingRule.GetRuleLabel( x ).referencedRuleName; } Rule refdRule = grammar.GetRule( refdRuleName ); AttributeScope scope = refdRule.GetAttributeScope( y ); if ( scope == null ) { ErrorManager.GrammarError( ErrorManager.MSG_UNKNOWN_RULE_ATTRIBUTE, grammar, actionToken, refdRuleName, y ); } else if ( scope.isParameterScope ) { ErrorManager.GrammarError( ErrorManager.MSG_INVALID_RULE_PARAMETER_REF, grammar, actionToken, refdRuleName, y ); } else if ( scope.isDynamicRuleScope ) { ErrorManager.GrammarError( ErrorManager.MSG_INVALID_RULE_SCOPE_ATTRIBUTE_REF, grammar, actionToken, refdRuleName, y ); } } }
private void HandleRuleInit(GrammarAST start, out string initAction, out GrammarAST block2, out Antlr3.Analysis.DFA dfa, out Rule ruleDescr, out string description) { initAction = null; // get the dfa for the BLOCK block2 = (GrammarAST)start.GetFirstChildWithType(BLOCK); dfa = block2.LookaheadDFA; // init blockNestingLevel so it's block level RULE_BLOCK_NESTING_LEVEL // for alts of rule blockNestingLevel = RULE_BLOCK_NESTING_LEVEL - 1; ruleDescr = grammar.GetRule(start.GetChild(0).Text); currentRuleName = start.GetChild(0).Text; description = string.Empty; }
public virtual void IssueInvalidScopeError( string x, string y, Rule enclosingRule, IToken actionToken, int outerAltNum ) { //[email protected]("error $"+x+"::"+y); Rule r = grammar.GetRule( x ); AttributeScope scope = grammar.GetGlobalScope( x ); if ( scope == null ) { if ( r != null ) { scope = r.ruleScope; // if not global, might be rule scope } } if ( scope == null ) { ErrorManager.GrammarError( ErrorManager.MSG_UNKNOWN_DYNAMIC_SCOPE, grammar, actionToken, x ); } else if ( scope.GetAttribute( y ) == null ) { ErrorManager.GrammarError( ErrorManager.MSG_UNKNOWN_DYNAMIC_SCOPE_ATTRIBUTE, grammar, actionToken, x, y ); } }
public LookaheadSet Follow( Rule r ) { //[email protected]("> FOLLOW("+r.name+") in rule "+r.startState.enclosingRule); LookaheadSet f = _followCache.get( r ); if ( f != null ) { return f; } f = FirstCore( r.stopState, true ); _followCache[r] = f; //[email protected]("< FOLLOW("+r+") in rule "+r.startState.enclosingRule+"="+f.toString(this.grammar)); return f; }
private void HandleRuleAfterBlock(out Template code, GrammarAST start, GrammarAST block2, Rule ruleDescr, string description, Template blockCode) { description = grammar.GrammarTreeToString((GrammarAST)start.GetFirstChildWithType(BLOCK), false); description = generator.Target.GetTargetStringLiteralFromString(description); blockCode.SetAttribute("description", description); // do not generate lexer rules in combined grammar string stName = null; if (ruleDescr.IsSynPred) { stName = "synpredRule"; } else if (grammar.type == GrammarType.Lexer) { if (currentRuleName.Equals(Grammar.ArtificialTokensRuleName)) stName = "tokensRule"; else stName = "lexerRule"; } else { if (!(grammar.type == GrammarType.Combined && Rule.GetRuleType(currentRuleName) == RuleType.Lexer)) stName = "rule"; } code = templates.GetInstanceOf(stName); if (code.Name.Equals("/rule")) code.SetAttribute("emptyRule", grammar.IsEmptyRule(block2)); code.SetAttribute("ruleDescriptor", ruleDescr); string memo = (string)grammar.GetBlockOption(start, "memoize"); if (memo == null) memo = (string)grammar.GetOption("memoize"); if (memo != null && memo.Equals("true") && (stName.Equals("rule") || stName.Equals("lexerRule"))) code.SetAttribute("memoize", memo != null && memo.Equals("true")); }
public virtual void IssueInvalidAttributeError( string x, Rule enclosingRule, IToken actionToken, int outerAltNum ) { //[email protected]("error $"+x); if ( enclosingRule == null ) { // action not in a rule ErrorManager.GrammarError( ErrorManager.MSG_ATTRIBUTE_REF_NOT_IN_RULE, grammar, actionToken, x ); return; } // action is in a rule Grammar.LabelElementPair label = enclosingRule.GetRuleLabel( x ); AttributeScope scope = enclosingRule.GetAttributeScope( x ); if ( label != null || enclosingRule.GetRuleRefsInAlt( x, outerAltNum ) != null || enclosingRule.Name.Equals( x ) ) { ErrorManager.GrammarError( ErrorManager.MSG_ISOLATED_RULE_SCOPE, grammar, actionToken, x ); } else if ( scope != null && scope.isDynamicRuleScope ) { ErrorManager.GrammarError( ErrorManager.MSG_ISOLATED_RULE_ATTRIBUTE, grammar, actionToken, x ); } else { ErrorManager.GrammarError( ErrorManager.MSG_UNKNOWN_SIMPLE_ATTRIBUTE, grammar, actionToken, x ); } }
protected virtual int DetectConfoundingPredicatesCore(NFAState s, Rule enclosingRule, bool chaseFollowTransitions) { //[email protected]("_detectNonAutobacktrackPredicates("+s+")"); if (!chaseFollowTransitions && s.IsAcceptState) { if (_grammar.type == GrammarType.Lexer) { // FOLLOW makes no sense (at the moment!) for lexical rules. // assume all char can follow return(DETECT_PRED_NOT_FOUND); } return(DETECT_PRED_EOR); } if (_lookBusy.Contains(s)) { // return a copy of an empty set; we may modify set inline return(DETECT_PRED_NOT_FOUND); } _lookBusy.Add(s); Transition transition0 = s.transition[0]; if (transition0 == null) { return(DETECT_PRED_NOT_FOUND); } if (!(transition0.label.IsSemanticPredicate || transition0.label.IsEpsilon)) { return(DETECT_PRED_NOT_FOUND); } if (transition0.label.IsSemanticPredicate) { //[email protected]("pred "+transition0.label); SemanticContext ctx = transition0.label.SemanticContext; SemanticContext.Predicate p = (SemanticContext.Predicate)ctx; if (p.predicateAST.Type != ANTLRParser.BACKTRACK_SEMPRED) { return(DETECT_PRED_FOUND); } } /* * if ( transition0.label.isSemanticPredicate() ) { * [email protected]("pred "+transition0.label); * SemanticContext ctx = transition0.label.getSemanticContext(); * SemanticContext.Predicate p = (SemanticContext.Predicate)ctx; * // if a non-syn-pred found not in enclosingRule, say we found one * if ( p.predicateAST.getType() != ANTLRParser.BACKTRACK_SEMPRED && * !p.predicateAST.enclosingRuleName.equals(enclosingRule.name) ) * { * [email protected]("found pred "+p+" not in "+enclosingRule.name); * return DETECT_PRED_FOUND; * } * } */ int result = DetectConfoundingPredicatesCore((NFAState)transition0.target, enclosingRule, chaseFollowTransitions); if (result == DETECT_PRED_FOUND) { return(DETECT_PRED_FOUND); } if (result == DETECT_PRED_EOR) { if (transition0 is RuleClosureTransition) { // we called a rule that found the end of the rule. // That means the rule is nullable and we need to // keep looking at what follows the rule ref. E.g., // a : b A ; where b is nullable means that LOOK(a) // should include A. RuleClosureTransition ruleInvocationTrans = (RuleClosureTransition)transition0; NFAState following = (NFAState)ruleInvocationTrans.followState; int afterRuleResult = DetectConfoundingPredicatesCore(following, enclosingRule, chaseFollowTransitions); if (afterRuleResult == DETECT_PRED_FOUND) { return(DETECT_PRED_FOUND); } } } Transition transition1 = s.transition[1]; if (transition1 != null) { int t1Result = DetectConfoundingPredicatesCore((NFAState)transition1.target, enclosingRule, chaseFollowTransitions); if (t1Result == DETECT_PRED_FOUND) { return(DETECT_PRED_FOUND); } } return(DETECT_PRED_NOT_FOUND); }
public LookaheadSet Look(Rule r) { if ( r.FIRST==null ) { r.FIRST = FIRST(r.startState); } return r.FIRST; }