public override Node VisitRuleset(DeltinScriptParser.RulesetContext context) { RuleNode[] rules = new RuleNode[context.ow_rule().Length]; for (int i = 0; i < rules.Length; i++) { rules[i] = (RuleNode)VisitOw_rule(context.ow_rule()[i]); } Variable useGlobalVar; Variable usePlayerVar; Enum.TryParse <Variable>(context.useGlobalVar()?.PART().GetText(), out useGlobalVar); Enum.TryParse <Variable>(context.usePlayerVar()?.PART().GetText(), out usePlayerVar); DefinedNode[] definedVars = new DefinedNode[context.vardefine().Length]; for (int i = 0; i < definedVars.Length; i++) { definedVars[i] = (DefinedNode)VisitVardefine(context.vardefine()[i]); } UserMethodNode[] userMethods = new UserMethodNode[context.user_method().Length]; for (int i = 0; i < userMethods.Length; i++) { userMethods[i] = (UserMethodNode)VisitUser_method(context.user_method()[i]); } Node node = new RulesetNode(rules, useGlobalVar, usePlayerVar, definedVars, userMethods, Range.GetRange(context)); CheckRange(node); return(node); }
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()); }
public RulesetNode(DeltinScriptParser.RulesetContext context, BuildAstVisitor visitor) : base(new Location(visitor.file, Range.GetRange(context))) { Imports = new ImportNode[context.import_file().Length]; for (int i = 0; i < Imports.Length; i++) { Imports[i] = new ImportNode(context.import_file(i), visitor); } ObjectImports = new ImportObjectNode[context.import_object().Length]; for (int i = 0; i < ObjectImports.Length; i++) { ObjectImports[i] = new ImportObjectNode(context.import_object(i), visitor); } Rules = new RuleNode[context.ow_rule().Length]; for (int i = 0; i < Rules.Length; i++) { Rules[i] = (RuleNode)visitor.VisitOw_rule(context.ow_rule()[i]); } if (context.useGlobalVar() != null) { if (Enum.TryParse(context.useGlobalVar().PART().GetText(), out Variable temp)) { UseGlobalVar = temp; } } if (context.usePlayerVar() != null) { if (Enum.TryParse(context.usePlayerVar().PART().GetText(), out Variable temp)) { UsePlayerVar = temp; } } if (context.useDimVar() != null) { if (Enum.TryParse(context.useDimVar().PART().GetText(), out Variable temp)) { UseBuilderVar = temp; } } DefinedVars = new RuleDefineNode[context.rule_define().Length]; for (int i = 0; i < DefinedVars.Length; i++) { DefinedVars[i] = (RuleDefineNode)visitor.VisitRule_define(context.rule_define(i)); } UserMethods = new UserMethodNode[context.user_method().Length]; for (int i = 0; i < UserMethods.Length; i++) { UserMethods[i] = (UserMethodNode)visitor.VisitUser_method(context.user_method(i)); } DefinedTypes = new TypeDefineNode[context.type_define().Length]; for (int i = 0; i < DefinedTypes.Length; i++) { DefinedTypes[i] = (TypeDefineNode)visitor.VisitType_define(context.type_define(i)); } }