コード例 #1
0
        public void Update(string content)
        {
            AntlrInputStream inputStream = new AntlrInputStream(content);

            // Lexer
            DeltinScriptLexer lexer             = new DeltinScriptLexer(inputStream);
            CommonTokenStream commonTokenStream = new CommonTokenStream(lexer);

            commonTokenStream.Fill();
            Tokens = commonTokenStream.GetTokens().ToArray();
            commonTokenStream.Reset();

            // Parse
            DeltinScriptParser parser = new DeltinScriptParser(commonTokenStream);
            var errorListener         = new ErrorListener();

            parser.RemoveErrorListeners();
            parser.AddErrorListener(errorListener);

            StructuralDiagnostics = errorListener.Diagnostics;
            Context = parser.ruleset();
        }
コード例 #2
0
        public static ParserData GetParser(string document, Pos documentPos)
        {
            AntlrInputStream inputStream = new AntlrInputStream(document);

            // Lexer
            DeltinScriptLexer lexer             = new DeltinScriptLexer(inputStream);
            CommonTokenStream commonTokenStream = new CommonTokenStream(lexer);

            // Parse
            DeltinScriptParser parser = new DeltinScriptParser(commonTokenStream);
            var errorListener         = new ErrorListener();

            parser.RemoveErrorListeners();
            parser.AddErrorListener(errorListener);

            DeltinScriptParser.RulesetContext ruleSetContext = parser.ruleset();

            List <Diagnostic> diagnostics = new List <Diagnostic>();

            diagnostics.AddRange(errorListener.Errors);

            // Get the ruleset node.
            BuildAstVisitor bav         = null;
            RulesetNode     ruleSetNode = null;

            if (diagnostics.Count == 0)
            {
                bav         = new BuildAstVisitor(documentPos, diagnostics);
                ruleSetNode = (RulesetNode)bav.Visit(ruleSetContext);
            }

            VarCollection     varCollection = null;
            ScopeGroup        root          = null;
            List <UserMethod> userMethods   = null;

            Rule[] rules   = null;
            bool   success = false;

            AdditionalErrorChecking aec = new AdditionalErrorChecking(parser, diagnostics);

            aec.Visit(ruleSetContext);

            bool parse = diagnostics.Count == 0;

            if (parse)
            {
                varCollection = new VarCollection();
                root          = new ScopeGroup();
                userMethods   = new List <UserMethod>();

                foreach (var definedVar in ruleSetNode.DefinedVars)
                {
                    varCollection.AssignDefinedVar(root, definedVar.IsGlobal, definedVar.VariableName, definedVar.Range);
                }

                // Get the user methods.
                for (int i = 0; i < ruleSetNode.UserMethods.Length; i++)
                {
                    userMethods.Add(new UserMethod(ruleSetNode.UserMethods[i]));
                }

                // Parse the rules.
                rules = new Rule[ruleSetNode.Rules.Length];

                for (int i = 0; i < rules.Length; i++)
                {
                    try
                    {
                        var result = Translate.GetRule(ruleSetNode.Rules[i], root, varCollection, userMethods.ToArray());
                        rules[i] = result.Rule;
                        diagnostics.AddRange(result.Diagnostics);
                    }
                    catch (SyntaxErrorException ex)
                    {
                        diagnostics.Add(new Diagnostic(ex.Message, ex.Range)
                        {
                            severity = Diagnostic.Error
                        });
                    }
                }

                success = true;
            }

            return(new ParserData()
            {
                Parser = parser,
                RulesetContext = ruleSetContext,
                RuleSetNode = ruleSetNode,
                Bav = bav,
                Diagnostics = diagnostics,
                Rules = rules,
                UserMethods = userMethods?.ToArray(),
                Root = root,
                Success = success,
                VarCollection = varCollection
            });
        }