Example #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);
        }
Example #2
0
        public static IList <ActionChunk> TranslateActionChunk(OutputModelFactory factory,
                                                               RuleFunction rf,
                                                               string action,
                                                               ActionAST node)
        {
            IToken           tokenWithinAction = node.Token;
            ActionTranslator translator        = new ActionTranslator(factory, node);

            translator.rf = rf;
            factory.GetGrammar().tool.Log("action-translator", "translate " + action);
            string altLabel = node.GetAltLabel();

            if (rf != null)
            {
                translator.nodeContext = rf.ruleCtx;
                if (altLabel != null)
                {
                    AltLabelStructDecl decl;
                    rf.altLabelCtxs.TryGetValue(altLabel, out decl);
                    translator.nodeContext = decl;
                }
            }
            ANTLRStringStream @in = new ANTLRStringStream(action);

            @in.Line = tokenWithinAction.Line;
            @in.CharPositionInLine = tokenWithinAction.CharPositionInLine;
            ActionSplitter trigger = new ActionSplitter(@in, translator);

            // forces eval, triggers listener methods
            trigger.GetActionTokens();
            return(translator.chunks);
        }
Example #3
0
        public override void RuleCatch(GrammarAST arg, ActionAST action)
        {
            GrammarAST catchme = (GrammarAST)action.Parent;

            currentRule.exceptions.Add(catchme);
            action.resolver = currentRule;
        }
Example #4
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));
        }
Example #5
0
 public virtual bool ResolvesToAttributeDict(string x, ActionAST node)
 {
     if (ResolvesToToken(x, node))
     {
         return(true);
     }
     return(false);
 }
Example #6
0
 public ExceptionClause(OutputModelFactory factory,
                        ActionAST catchArg,
                        ActionAST catchAction)
     : base(factory, catchArg)
 {
     this.catchArg    = new Action(factory, catchArg);
     this.catchAction = new Action(factory, catchAction);
 }
Example #7
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;
     }
 }
Example #8
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));
        }
Example #9
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));
        }
Example #10
0
 public virtual void DefineActionInAlt(int currentAlt, ActionAST actionAST)
 {
     actions.Add(actionAST);
     alt[currentAlt].actions.Add(actionAST);
     if (g.IsLexer())
     {
         DefineLexerAction(actionAST);
     }
 }
 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);
     }
 }
Example #12
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;
 }
Example #13
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);
 }
Example #14
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);
     }
 }
        public virtual IList <SrcOp> Sempred(ActionAST ast)
        {
            IList <SrcOp> ops = @delegate.Sempred(ast);

            foreach (CodeGeneratorExtension ext in extensions)
            {
                ops = ext.Sempred(ops);
            }
            return(ops);
        }
Example #16
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);
        }
 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)
 {
     // TODO: chk that all or no alts have "# label"
     CheckInvalidRuleDef(ID.Token);
 }
Example #18
0
        public virtual IDictionary <string, Action> BuildNamedActions(Grammar g)
        {
            IDictionary <string, Action> namedActions = new Dictionary <string, Action>();

            foreach (string name in g.namedActions.Keys)
            {
                ActionAST ast = g.namedActions[name];
                namedActions[name] = new Action(factory, ast);
            }
            return(namedActions);
        }
Example #19
0
        public virtual Handle Action([NotNull] ActionAST action)
        {
            //System.out.println("action: "+action);
            ATNState left = NewState(action);

            ATNState         right = NewState(action);
            ActionTransition a     = new ActionTransition(right, currentRule.index);

            left.AddTransition(a);
            action.atnState = left;
            return(new Handle(left, right));
        }
Example #20
0
        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);
            }
        }
Example #21
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);
        }
Example #22
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;
            }
        }
        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));
        }
Example #24
0
        public static AttributeDict Parse([Nullable] ActionAST action, string s, char separator, Grammar g)
        {
            AttributeDict dict = new AttributeDict();
            IList <System.Tuple <string, int> > decls = SplitDecls(s, separator);

            foreach (System.Tuple <string, int> decl in decls)
            {
                if (decl.Item1.Trim().Length > 0)
                {
                    Attribute a = ParseAttributeDef(action, decl, g);
                    dict.Add(a);
                }
            }
            return(dict);
        }
Example #25
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);
        }
Example #26
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));
        }
Example #27
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);
        }
Example #28
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));
            }
        }
Example #29
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);
        }
Example #30
0
 public override void RuleRef(GrammarAST @ref, ActionAST arg)
 {
     RuleAST ruleAST;
     ruleToAST.TryGetValue(@ref.Text, out ruleAST);
     string fileName = @ref.Token.InputStream.SourceName;
     if (char.IsUpper(currentRuleName[0]) &&
         char.IsLower(@ref.Text[0]))
     {
         badref = true;
         tool.errMgr.GrammarError(ErrorType.PARSER_RULE_REF_IN_LEXER_RULE,
                             fileName, @ref.Token, @ref.Text, currentRuleName);
     }
     else if (ruleAST == null)
     {
         badref = true;
         tool.errMgr.GrammarError(ErrorType.UNDEFINED_RULE_REF,
                             fileName, @ref.Token, @ref.Text);
     }
 }