Inheritance: GrammarASTWithOptions, RuleElementAST
Exemplo n.º 1
0
        /** $x.y, x can be surrounding rule, token/rule/label ref. y is visible
         *  attr in that dictionary.  Can't see args on rule refs.
         */
        public virtual Attribute ResolveToAttribute(string x, string y, ActionAST node)
        {
            if (tokenRefs.ContainsKey(x) && tokenRefs[x] != null)
            {
                // token ref in this alt?
                return rule.GetPredefinedScope(LabelType.TOKEN_LABEL).Get(y);
            }

            if (ruleRefs.ContainsKey(x) && ruleRefs[x] != null)
            {
                // rule ref in this alt?
                // look up rule, ask it to resolve y (must be retval or predefined)
                return rule.g.GetRule(x).ResolveRetvalOrProperty(y);
            }

            LabelElementPair anyLabelDef = GetAnyLabelDef(x);
            if (anyLabelDef != null && anyLabelDef.type == LabelType.RULE_LABEL)
            {
                return rule.g.GetRule(anyLabelDef.element.Text).ResolveRetvalOrProperty(y);
            }
            else if (anyLabelDef != null)
            {
                AttributeDict scope = rule.GetPredefinedScope(anyLabelDef.type);
                if (scope == null)
                {
                    return null;
                }

                return scope.Get(y);
            }
            return null;
        }
Exemplo n.º 2
0
 public ExceptionClause(OutputModelFactory factory,
                        ActionAST catchArg,
                        ActionAST catchAction)
     : base(factory, catchArg)
 {
     this.catchArg = new Action(factory, catchArg);
     this.catchAction = new Action(factory, catchAction);
 }
Exemplo n.º 3
0
 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);
 }
Exemplo n.º 4
0
 public ActionSniffer(Grammar g, Rule r, Alternative alt, ActionAST node, IToken actionToken)
 {
     this.g = g;
     this.r = r;
     this.alt = alt;
     this.node = node;
     this.actionToken = actionToken;
     this.errMgr = g.tool.errMgr;
 }
Exemplo n.º 5
0
 public virtual bool ResolvesToAttributeDict(string x, ActionAST node)
 {
     if (ResolvesToToken(x, node))
         return true;
     if (ruleRefs.ContainsKey(x) && ruleRefs[x] != null)
         return true; // rule ref in this alt?
     LabelElementPair anyLabelDef = GetAnyLabelDef(x);
     if (anyLabelDef != null && anyLabelDef.type == LabelType.RULE_LABEL)
         return true;
     return false;
 }
Exemplo n.º 6
0
        public virtual bool ResolvesToToken(string x, ActionAST node)
        {
            if (tokenRefs.ContainsKey(x) && tokenRefs[x] != null)
                return true;

            LabelElementPair anyLabelDef = GetAnyLabelDef(x);
            if (anyLabelDef != null && anyLabelDef.type == LabelType.TOKEN_LABEL)
                return true;

            return false;
        }
Exemplo n.º 7
0
 public static bool ActionIsContextDependent(ActionAST actionAST)
 {
     ANTLRStringStream @in = new ANTLRStringStream(actionAST.Token.Text);
     @in.Line = actionAST.Token.Line;
     @in.CharPositionInLine = actionAST.Token.CharPositionInLine;
     var listener = new ContextDependentListener();
     ActionSplitter splitter = new ActionSplitter(@in, listener);
     // forces eval, triggers listener methods
     splitter.GetActionTokens();
     return listener.dependent;
 }
Exemplo n.º 8
0
 public Action(OutputModelFactory factory, ActionAST ast)
     : base(factory, ast)
 {
     RuleFunction rf = factory.GetCurrentRuleFunction();
     if (ast != null)
     {
         chunks = ActionTranslator.TranslateAction(factory, rf, ast.Token, ast);
     }
     else
     {
         chunks = new List<ActionChunk>();
     }
     //System.out.println("actions="+chunks);
 }
Exemplo n.º 9
0
        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;
            }
        }
Exemplo n.º 10
0
 public Action(OutputModelFactory factory, StructDecl ctx, string action)
     : base(factory, null)
 {
     ActionAST ast = new ActionAST(new CommonToken(ANTLRParser.ACTION, action));
     RuleFunction rf = factory.GetCurrentRuleFunction();
     if (rf != null)
     { // we can translate
         ast.resolver = rf.rule;
         chunks = ActionTranslator.TranslateActionChunk(factory, rf, action, ast);
     }
     else
     {
         chunks = new List<ActionChunk>();
         chunks.Add(new ActionText(ctx, action));
     }
 }
Exemplo n.º 11
0
        public static IList<ActionChunk> TranslateAction(OutputModelFactory factory,
                                                        RuleFunction rf,
                                                        IToken tokenWithinAction,
                                                        ActionAST node)
        {
            string action = tokenWithinAction.Text;
            if (action != null && action.Length > 0 && action[0] == '{')
            {
                int firstCurly = action.IndexOf('{');
                int lastCurly = action.LastIndexOf('}');
                if (firstCurly >= 0 && lastCurly >= 0)
                {
                    action = action.Substring(firstCurly + 1, lastCurly - firstCurly - 1); // trim {...}
                }
            }

            return TranslateActionChunk(factory, rf, action, node);
        }
Exemplo n.º 12
0
 public override void RuleRef(GrammarAST @ref, ActionAST arg)
 {
     CheckInvalidRuleRef(@ref.Token);
 }
Exemplo n.º 13
0
 public virtual bool ResolvesToAttributeDict(string x, ActionAST node)
 {
     if (ResolvesToToken(x, node))
         return true;
     return false;
 }
Exemplo n.º 14
0
 public virtual bool ResolvesToToken(string x, ActionAST node)
 {
     LabelElementPair anyLabelDef = GetAnyLabelDef(x);
     if (anyLabelDef != null && anyLabelDef.type == LabelType.TOKEN_LABEL)
         return true;
     return false;
 }
Exemplo n.º 15
0
        // ACTIONS

        public virtual IList<SrcOp> Action(ActionAST ast)
        {
            return null;
        }
Exemplo n.º 16
0
 public override void RuleRef(GrammarAST @ref, ActionAST arg)
 {
     //		if ( inContext("DOT ...") ) qualifiedRulerefs.add((GrammarAST)ref.getParent());
     rulerefs.Add(@ref);
     if (currentRule != null)
     {
         currentRule.alt[currentOuterAltNumber].ruleRefs.Map(@ref.Text, @ref);
     }
 }
Exemplo n.º 17
0
 public override void RuleCatch(GrammarAST arg, ActionAST action)
 {
     GrammarAST catchme = (GrammarAST)action.Parent;
     currentRule.exceptions.Add(catchme);
     action.resolver = currentRule;
 }
Exemplo n.º 18
0
 public override Handle Action(ActionAST action)
 {
     int ruleIndex = currentRule.index;
     int actionIndex = g.lexerActions[action];
     LexerCustomAction lexerAction = new LexerCustomAction(ruleIndex, actionIndex);
     return Action(action, lexerAction);
 }
Exemplo n.º 19
0
        public IList <IToken> chunks; // useful for ANTLR IDE developers

        public ActionAST(ActionAST node)
            : base(node)
        {
            this.resolver = node.resolver;
            this.chunks   = node.chunks;
        }
Exemplo n.º 20
0
        public override void RuleRef(GrammarAST @ref, ActionAST arg)
        {
            if (@ref is GrammarASTWithOptions)
            {
                GrammarASTWithOptions grammarASTWithOptions = (GrammarASTWithOptions)@ref;
                if (bool.Parse(grammarASTWithOptions.GetOptionString(LeftFactoringRuleTransformer.SUPPRESS_ACCESSOR) ?? "false"))
                {
                    return;
                }
            }

            frequencies.Peek().Add(RuleFunction.GetLabelName(grammar, @ref));
            minFrequencies.Peek().Add(RuleFunction.GetLabelName(grammar, @ref));
        }
Exemplo n.º 21
0
 public override void ActionInAlt(ActionAST action)
 {
     if (inFragmentRule)
     {
         string fileName = action.Token.InputStream.SourceName;
         string ruleName = currentRuleName;
         g.tool.errMgr.GrammarError(ErrorType.FRAGMENT_ACTION_IGNORED, fileName, action.Token, ruleName);
     }
 }
Exemplo n.º 22
0
        public override Handle Action(string action)
        {
            if (string.IsNullOrWhiteSpace(action))
            {
                ATNState left = NewState(null);
                ATNState right = NewState(null);
                Epsilon(left, right);
                return new Handle(left, right);
            }

            // define action AST for this rule as if we had found in grammar
            ActionAST ast = new ActionAST(new CommonToken(ANTLRParser.ACTION, action));
            currentRule.DefineActionInAlt(currentOuterAlt, ast);
            return Action(ast);
        }
Exemplo n.º 23
0
 public virtual void DefineActionInAlt(int currentAlt, ActionAST actionAST)
 {
     actions.Add(actionAST);
     alt[currentAlt].actions.Add(actionAST);
     if (g.IsLexer())
     {
         DefineLexerAction(actionAST);
     }
 }
Exemplo n.º 24
0
 public override void FinallyAction(ActionAST action)
 {
     currentRule.finallyAction = action;
     action.resolver = currentRule;
 }
Exemplo n.º 25
0
 /** Lexer actions are numbered across rules 0..n-1 */
 public virtual void DefineLexerAction(ActionAST actionAST)
 {
     actionIndex = g.lexerActions.Count;
     if (!g.lexerActions.ContainsKey(actionAST))
     {
         g.lexerActions[actionAST] = actionIndex;
     }
 }
Exemplo n.º 26
0
 public override void GlobalNamedAction(GrammarAST scope, GrammarAST ID, ActionAST action)
 {
     namedActions.Add((GrammarAST)ID.Parent);
     action.resolver = g;
 }
Exemplo n.º 27
0
 /**  $x		Attribute: rule arguments, return values, predefined rule prop.
  */
 public virtual Attribute ResolveToAttribute(string x, ActionAST node)
 {
     if (args != null)
     {
         Attribute a = args.Get(x);
         if (a != null)
             return a;
     }
     if (retvals != null)
     {
         Attribute a = retvals.Get(x);
         if (a != null)
             return a;
     }
     if (locals != null)
     {
         Attribute a = locals.Get(x);
         if (a != null)
             return a;
     }
     AttributeDict properties = GetPredefinedScope(LabelType.RULE_LABEL);
     return properties.Get(x);
 }
Exemplo n.º 28
0
 public override void ActionInAlt(ActionAST action)
 {
     currentRule.DefineActionInAlt(currentOuterAltNumber, action);
     action.resolver = currentRule.alt[currentOuterAltNumber];
 }
Exemplo n.º 29
0
        /** $x.y	Attribute: x is surrounding rule, label ref (in any alts) */
        public virtual Attribute ResolveToAttribute(string x, string y, ActionAST node)
        {
            LabelElementPair anyLabelDef = GetAnyLabelDef(x);
            if (anyLabelDef != null)
            {
                if (anyLabelDef.type == LabelType.RULE_LABEL)
                {
                    return g.GetRule(anyLabelDef.element.Text).ResolveRetvalOrProperty(y);
                }
                else
                {
                    AttributeDict scope = GetPredefinedScope(anyLabelDef.type);
                    if (scope == null)
                    {
                        return null;
                    }

                    return scope.Get(y);
                }
            }
            return null;

        }
Exemplo n.º 30
0
 public virtual IList<SrcOp> Sempred(ActionAST ast)
 {
     return null;
 }
Exemplo n.º 31
0
 public virtual bool ResolvesToListLabel(string x, ActionAST node)
 {
     LabelElementPair anyLabelDef = GetAnyLabelDef(x);
     return anyLabelDef != null &&
            (anyLabelDef.type == LabelType.RULE_LIST_LABEL ||
             anyLabelDef.type == LabelType.TOKEN_LIST_LABEL);
 }