Наследование: AttributeResolver
 public LeftRecursiveRuleTransformer(GrammarRootAST ast, ICollection<Rule> rules, Grammar g)
 {
     this.ast = ast;
     this.rules = rules;
     this.g = g;
     this.tool = g.tool;
 }
Пример #2
0
        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;
                }
            }
        }
Пример #3
0
 public BuildDependencyGenerator(AntlrTool tool, Grammar g)
 {
     this.tool = tool;
     this.g = g;
     string language = g.GetOptionString("language");
     generator = new CodeGenerator(tool, g, language);
 }
 public LeftFactoringRuleTransformer([NotNull] GrammarRootAST ast, [NotNull] IDictionary<string, Rule> rules, [NotNull] Grammar g)
 {
     this._ast = ast;
     this._rules = rules;
     this._g = g;
     this._tool = g.tool;
 }
Пример #5
0
 public LeftRecursiveRule(Grammar g, string name, RuleAST ast)
     : base(g, name, ast, 1)
 {
     originalAST = ast;
     alt = new Alternative[numberOfAlts + 1]; // always just one
     for (int i = 1; i <= numberOfAlts; i++)
         alt[i] = new Alternative(this, i);
 }
 public ElementFrequenciesVisitor(Grammar grammar, ITreeNodeStream input)
     : base(input)
 {
     this.grammar = grammar;
     frequencies = new Stack<FrequencySet<string>>();
     frequencies.Push(new FrequencySet<string>());
     minFrequencies = new Stack<FrequencySet<string>>();
     minFrequencies.Push(SENTINEL);
 }
Пример #7
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;
 }
Пример #8
0
 public GrammarParserInterpreter(Grammar g, ATN atn, ITokenStream input)
     : base(g.fileName, g.GetVocabulary(),
           g.GetRuleNames(),
           atn, // must run ATN through serializer to set some state flags
           input)
 {
     this.g = g;
     decisionStatesThatSetOuterAltNumInContext = FindOuterMostDecisionStates();
     stateToAltsMap = new int[g.atn.states.Count][];
 }
Пример #9
0
 public GrammarParserInterpreter(Grammar g,
                                 string grammarFileName,
                                 IVocabulary vocabulary,
                                 ICollection<string> ruleNames,
                                 ATN atn,
                                 ITokenStream input)
     : base(grammarFileName, vocabulary, ruleNames, atn, input)
 {
     this.g = g;
 }
Пример #10
0
        protected DefaultOutputModelFactory([NotNull] CodeGenerator gen)
        {
            this.gen = gen;
            this.g = gen.g;

            if (gen.GetTarget() == null)
            {
                throw new NotSupportedException("Cannot build an output model without a target.");
            }
        }
Пример #11
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;
 }
Пример #12
0
        /** Get a meaningful name for a token type useful during code generation.
         *  Literals without associated names are converted to the string equivalent
         *  of their integer values. Used to generate x==ID and x==34 type comparisons
         *  etc...  Essentially we are looking for the most obvious way to refer
         *  to a token type in the generated code.
         */
        public virtual string GetTokenTypeAsTargetLabel(Grammar g, int ttype)
        {
            string name = g.GetTokenName(ttype);
            // If name is not valid, return the token type instead
            if (Grammar.INVALID_TOKEN_NAME.Equals(name))
            {
                return ttype.ToString();
            }

            return name;
        }
Пример #13
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;
 }
Пример #14
0
 // side-effect: updates Alternative with refs in actions
 public static void TrackTokenRuleRefsInActions(Grammar g)
 {
     foreach (Rule r in g.rules.Values)
     {
         for (int i = 1; i <= r.numberOfAlts; i++)
         {
             Alternative alt = r.alt[i];
             foreach (ActionAST a in alt.actions)
             {
                 ActionSniffer sniffer = new ActionSniffer(g, r, alt, a, a.Token);
                 sniffer.ExamineAction();
             }
         }
     }
 }
Пример #15
0
 /** 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));
 }
Пример #16
0
        public SymbolChecks(Grammar g, SymbolCollector collector)
        {
            this.g = g;
            this.collector = collector;
            this.errMgr = g.tool.errMgr;

            foreach (GrammarAST tokenId in collector.tokenIDRefs)
            {
                tokenIDs.Add(tokenId.Text);
            }

            //System.Console.WriteLine("rules="+collector.rules);
            //System.Console.WriteLine("rulerefs="+collector.rulerefs);
            //System.Console.WriteLine("tokenIDRefs="+collector.tokenIDRefs);
            //System.Console.WriteLine("terminals="+collector.terminals);
            //System.Console.WriteLine("strings="+collector.strings);
            //System.Console.WriteLine("tokensDef="+collector.tokensDefs);
            //System.Console.WriteLine("actions="+collector.actions);
            //System.Console.WriteLine("scopes="+collector.scopes);
        }
Пример #17
0
        public static IDictionary<Rule, ISet<Rule>> GetRuleDependencies(Grammar g, ICollection<Rule> rules)
        {
            IDictionary<Rule, ISet<Rule>> dependencies = new Dictionary<Rule, ISet<Rule>>();

            foreach (Rule r in rules)
            {
                IList<GrammarAST> tokenRefs = r.ast.GetNodesWithType(ANTLRParser.TOKEN_REF);
                foreach (GrammarAST tref in tokenRefs)
                {
                    ISet<Rule> calls;
                    if (!dependencies.TryGetValue(r, out calls) || calls == null)
                    {
                        calls = new HashSet<Rule>();
                        dependencies[r] = calls;
                    }
                    calls.Add(g.GetRule(tref.Text));
                }
            }

            return dependencies;
        }
Пример #18
0
        public static void CheckAllAttributeExpressions(Grammar g)
        {
            foreach (ActionAST act in g.namedActions.Values)
            {
                AttributeChecks checker = new AttributeChecks(g, null, null, act, act.Token);
                checker.ExamineAction();
            }

            foreach (Rule r in g.rules.Values)
            {
                foreach (ActionAST a in r.namedActions.Values)
                {
                    AttributeChecks checker = new AttributeChecks(g, r, null, a, a.Token);
                    checker.ExamineAction();
                }
                for (int i = 1; i <= r.numberOfAlts; i++)
                {
                    Alternative alt = r.alt[i];
                    foreach (ActionAST a in alt.actions)
                    {
                        AttributeChecks checker =
                            new AttributeChecks(g, r, alt, a, a.Token);
                        checker.ExamineAction();
                    }
                }
                foreach (GrammarAST e in r.exceptions)
                {
                    ActionAST a = (ActionAST)e.GetChild(1);
                    AttributeChecks checker = new AttributeChecks(g, r, null, a, a.Token);
                    checker.ExamineAction();
                }
                if (r.finallyAction != null)
                {
                    AttributeChecks checker =
                        new AttributeChecks(g, r, null, r.finallyAction, r.finallyAction.Token);
                    checker.ExamineAction();
                }
            }
        }
Пример #19
0
        public static string GetLabelName(Grammar g, GrammarAST t)
        {
            string labelName = t.Text;
            Rule referencedRule;
            if (g.rules.TryGetValue(labelName, out referencedRule) && referencedRule != null)
            {
                labelName = referencedRule.GetBaseContext();
            }

            return labelName;
        }
Пример #20
0
 public virtual string[] GetTokenTypesAsTargetLabels(Grammar g, int[] ttypes)
 {
     string[] labels = new string[ttypes.Length];
     for (int i = 0; i < ttypes.Length; i++)
     {
         labels[i] = GetTokenTypeAsTargetLabel(g, ttypes[i]);
     }
     return labels;
 }
Пример #21
0
 public TokenVocabParser(Grammar g) {
     this.g = g;
 }
Пример #22
0
 protected internal virtual void GenFile(Grammar g, Template outputFileST, string fileName)
 {
     GetCodeGenerator().Write(outputFileST, fileName);
 }
Пример #23
0
        internal virtual void AssignLexerTokenTypes(Grammar g, IList<GrammarAST> tokensDefs)
        {
            Grammar G = g.GetOutermostGrammar(); // put in root, even if imported
            foreach (GrammarAST def in tokensDefs)
            {
                // tokens { id (',' id)* } so must check IDs not TOKEN_REF
                if (Grammar.IsTokenName(def.Text))
                {
                    G.DefineTokenName(def.Text);
                }
            }

            /* Define token types for nonfragment rules which do not include a 'type(...)'
             * or 'more' lexer command.
             */
            foreach (Rule r in g.rules.Values)
            {
                if (!r.IsFragment() && !HasTypeOrMoreCommand(r))
                {
                    G.DefineTokenName(r.name);
                }
            }

            // FOR ALL X : 'xxx'; RULES, DEFINE 'xxx' AS TYPE X
            IList<System.Tuple<GrammarAST, GrammarAST>> litAliases = Grammar.GetStringLiteralAliasesFromLexerRules(g.ast);
            ISet<string> conflictingLiterals = new HashSet<string>();
            if (litAliases != null)
            {
                foreach (System.Tuple<GrammarAST, GrammarAST> pair in litAliases)
                {
                    GrammarAST nameAST = pair.Item1;
                    GrammarAST litAST = pair.Item2;
                    if (!G.stringLiteralToTypeMap.ContainsKey(litAST.Text))
                    {
                        G.DefineTokenAlias(nameAST.Text, litAST.Text);
                    }
                    else
                    {
                        // oops two literal defs in two rules (within or across modes).
                        conflictingLiterals.Add(litAST.Text);
                    }
                }

                foreach (string lit in conflictingLiterals)
                {
                    // Remove literal if repeated across rules so it's not
                    // found by parser grammar.
                    int value;
                    if (G.stringLiteralToTypeMap.TryGetValue(lit, out value))
                    {
                        G.stringLiteralToTypeMap.Remove(lit);
                        if (value > 0 && value < G.typeToStringLiteralList.Count && lit.Equals(G.typeToStringLiteralList[value]))
                        {
                            G.typeToStringLiteralList[value] = null;
                        }
                    }
                }
            }

        }
Пример #24
0
        public int actionIndex = -1; // if lexer; 0..n-1 for n actions in a rule

        public Rule(Grammar g, string name, RuleAST ast, int numberOfAlts)
        {
            this.g = g;
            this.name = name;
            this.ast = ast;
            this.numberOfAlts = numberOfAlts;
            alt = new Alternative[numberOfAlts + 1]; // 1..n
            for (int i = 1; i <= numberOfAlts; i++)
                alt[i] = new Alternative(this, i);
        }
Пример #25
0
 public BasicSemanticChecks(Grammar g, RuleCollector ruleCollector)
 {
     this.g = g;
     this.ruleCollector = ruleCollector;
     this.errMgr = g.tool.errMgr;
 }
Пример #26
0
 public SemanticPipeline(Grammar g)
 {
     this.g = g;
 }
Пример #27
0
        /**
         * Assign constant values to custom channels defined in a grammar.
         *
         * @param g The grammar.
         * @param channelDefs A collection of AST nodes defining individual channels
         * within a {@code channels{}} block in the grammar.
         */
        internal virtual void AssignChannelTypes(Grammar g, IList<GrammarAST> channelDefs)
        {
            Grammar outermost = g.GetOutermostGrammar();
            foreach (GrammarAST channel in channelDefs)
            {
                string channelName = channel.Text;

                // Channel names can't alias tokens or modes, because constant
                // values are also assigned to them and the ->channel(NAME) lexer
                // command does not distinguish between the various ways a constant
                // can be declared. This method does not verify that channels do not
                // alias rules, because rule names are not associated with constant
                // values in ANTLR grammar semantics.

                if (g.GetTokenType(channelName) != TokenConstants.InvalidType)
                {
                    g.tool.errMgr.GrammarError(ErrorType.CHANNEL_CONFLICTS_WITH_TOKEN, g.fileName, channel.Token, channelName);
                }

                if (LexerATNFactory.COMMON_CONSTANTS.ContainsKey(channelName))
                {
                    g.tool.errMgr.GrammarError(ErrorType.CHANNEL_CONFLICTS_WITH_COMMON_CONSTANTS, g.fileName, channel.Token, channelName);
                }

                if (outermost is LexerGrammar)
                {
                    LexerGrammar lexerGrammar = (LexerGrammar)outermost;
                    if (lexerGrammar.modes.ContainsKey(channelName))
                    {
                        g.tool.errMgr.GrammarError(ErrorType.CHANNEL_CONFLICTS_WITH_MODE, g.fileName, channel.Token, channelName);
                    }
                }

                outermost.DefineChannelName(channel.Text);
            }
        }
Пример #28
0
        internal virtual void AssignTokenTypes(Grammar g, IList<GrammarAST> tokensDefs,
                              IList<GrammarAST> tokenIDs, IList<GrammarAST> terminals)
        {
            //Grammar G = g.getOutermostGrammar(); // put in root, even if imported

            // create token types for tokens { A, B, C } ALIASES
            foreach (GrammarAST alias in tokensDefs)
            {
                if (g.GetTokenType(alias.Text) != TokenConstants.InvalidType)
                {
                    g.tool.errMgr.GrammarError(ErrorType.TOKEN_NAME_REASSIGNMENT, g.fileName, alias.Token, alias.Text);
                }

                g.DefineTokenName(alias.Text);
            }

            // DEFINE TOKEN TYPES FOR TOKEN REFS LIKE ID, INT
            foreach (GrammarAST idAST in tokenIDs)
            {
                if (g.GetTokenType(idAST.Text) == TokenConstants.InvalidType)
                {
                    g.tool.errMgr.GrammarError(ErrorType.IMPLICIT_TOKEN_DEFINITION, g.fileName, idAST.Token, idAST.Text);
                }

                g.DefineTokenName(idAST.Text);
            }

            // VERIFY TOKEN TYPES FOR STRING LITERAL REFS LIKE 'while', ';'
            foreach (GrammarAST termAST in terminals)
            {
                if (termAST.Type != ANTLRParser.STRING_LITERAL)
                {
                    continue;
                }

                if (g.GetTokenType(termAST.Text) == TokenConstants.InvalidType)
                {
                    g.tool.errMgr.GrammarError(ErrorType.IMPLICIT_STRING_DEFINITION, g.fileName, termAST.Token, termAST.Text);
                }
            }

            g.tool.Log("semantics", "tokens=" + g.tokenNameToTypeMap);
            g.tool.Log("semantics", "strings=" + g.stringLiteralToTypeMap);
        }
Пример #29
0
 public SymbolCollector(Grammar g)
 {
     this.g = g;
     this.errMgr = g.tool.errMgr;
 }
Пример #30
0
 public GrammarToken(Grammar g, IToken oldToken)
 {
     this.g = g;
     _token = new CommonToken(oldToken);
 }