private RulesetNode GetRuleset(string file, string content)
        {
            AntlrInputStream inputStream = new AntlrInputStream(content);

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

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

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

            DeltinScriptParser.RulesetContext ruleSetContext = parser.ruleset();

            Log.Write(LogLevel.Verbose, ruleSetContext.ToStringTree(parser));

            // Get the ruleset node.
            RulesetNode ruleset = null;

            if (!Diagnostics.ContainsErrors())
            {
                BuildAstVisitor bav = new BuildAstVisitor(file, Diagnostics);
                ruleset = (RulesetNode)bav.Visit(ruleSetContext);
            }

            AdditionalErrorChecking aec = new AdditionalErrorChecking(file, parser, Diagnostics);

            aec.Visit(ruleSetContext);

            return(ruleset);
        }
Пример #2
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();
        }
Пример #3
0
        public static Rule[] ParseText(string text)
        {
            AntlrInputStream inputStream = new AntlrInputStream(text);

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

            // Parse
            DeltinScriptParser parser = new DeltinScriptParser(commonTokenStream);

            parser.RemoveErrorListeners();
            parser.AddErrorListener(new ErrorListener());

            // Get context
            DeltinScriptParser.RulesetContext context = parser.ruleset();

            //PrintContext(context);
            Console.WriteLine(context.ToStringTree(parser));

            Visitor visitor = new Visitor();

            visitor.Visit(context);

            {
                // Get the internal global variable to use.
                if (!Enum.TryParse(context.useGlobalVar().PART().ToString(), out Variable useGlobalVar))
                {
                    throw new SyntaxErrorException("useGlobalVar must be a character.", context.useGlobalVar().start);
                }

                // Get the internal player variable to use.
                if (!Enum.TryParse(context.usePlayerVar().PART().ToString(), out Variable usePlayerVar))
                {
                    throw new SyntaxErrorException("usePlayerVar must be a character.", context.usePlayerVar().start);
                }

                Var.Setup(useGlobalVar, usePlayerVar);
            }

            // Get the defined variables.
            var vardefine = context.vardefine();

            for (int i = 0; i < vardefine.Length; i++)
            {
                // The new var is stored in Var.VarCollection
                new DefinedVar(vardefine[i]);
            }

            // Parse the rules.
            var rules         = context.ow_rule();
            var compiledRules = new List <Rule>();

            for (int i = 0; i < rules.Length; i++)
            {
                ParseRule parsing = new ParseRule(rules[i]);

                Log.Write($"Building rule: {parsing.Rule.Name}");
                parsing.Parse();
                Rule rule = parsing.Rule;
                Log.Write($"Finished rule: {parsing.Rule.Name}");

                compiledRules.Add(rule);
            }

            Log.Write("Build succeeded.");

            // List all variables
            Log.Write("Variable Guide:");
            foreach (DefinedVar var in DefinedVar.VarCollection)
            {
                Console.WriteLine($"{var.Name}: {(var.IsGlobal ? "global" : "player")} {var.Variable}{(var.IsInArray ? $"[{var.Index}]" : "")}");
            }

            return(compiledRules.ToArray());
        }
Пример #4
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
            });
        }