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 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());
        }