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); }
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); }
private void GetObjects(RulesetNode rulesetNode, string file, TranslateRule globalTranslate, TranslateRule playerTranslate) { string absolute = new Uri(file).AbsolutePath; // Get the defined types foreach (var definedType in rulesetNode.DefinedTypes) { try { if (DefinedTypes.Any(type => type.Name == definedType.Name)) { throw SyntaxErrorException.NameAlreadyDefined(definedType.Location); } DefinedTypes.Add(DefinedType.GetDefinedType(definedType)); } catch (SyntaxErrorException ex) { Diagnostics.Error(ex); } } // Get the user methods. for (int i = 0; i < rulesetNode.UserMethods.Length; i++) { try { UserMethods.Add(UserMethod.CreateUserMethod(Root, rulesetNode.UserMethods[i])); } catch (SyntaxErrorException ex) { Diagnostics.Error(ex); } } // Get the rules RuleNodes.AddRange(rulesetNode.Rules); List <string> importedFiles = new List <string>(); foreach (ImportObjectNode importObject in rulesetNode.ObjectImports) { try { Importer importer = new Importer(Diagnostics, importedFiles, importObject.File, file, importObject.Location); if (!importer.AlreadyImported) { importedFiles.Add(importer.ResultingPath); switch (importer.FileType) { case ".obj": importer.FileData.Update(); string content = importer.FileData.Content; Model newModel = Model.ImportObj(content); new ModelVar(importObject.Name, Root, importObject, newModel); break; case ".pathmap": PathMap pathMap = PathMap.ImportFromXML(importer.ResultingPath); new PathMapVar(this, importObject.Name, Root, importObject, pathMap); break; } } } catch (SyntaxErrorException ex) { Diagnostics.Error(ex); } } // Get the variables foreach (var definedVar in rulesetNode.DefinedVars) { try { IndexedVar var; if (!definedVar.Extended) { if (definedVar.OverrideID == -1) { var = IndexedVar.AssignVar(VarCollection, Root, definedVar.VariableName, definedVar.IsGlobal, definedVar); } else { var = IndexedVar.AssignVar( VarCollection, Root, definedVar.VariableName, definedVar.IsGlobal, new WorkshopVariable(definedVar.IsGlobal, definedVar.OverrideID, VarCollection.WorkshopNameFromCodeName(definedVar.IsGlobal, definedVar.VariableName)), definedVar ); } } else { var = IndexedVar.AssignVarExt(VarCollection, Root, definedVar.VariableName, definedVar.IsGlobal, definedVar); } if (definedVar.Type != null) { var.Type = GetDefinedType(definedVar.Type, definedVar.Location); } } catch (SyntaxErrorException ex) { Diagnostics.Error(ex); } } }
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 }); }
private void GetObjects(string document, string file, TranslateRule globalTranslate, TranslateRule playerTranslate, bool isRoot) { // If this file was already loaded, don't load it again. if (Imported.Contains(file)) { return; } Imported.Add(file); Diagnostics.AddFile(file); // Get the ruleset. RulesetNode ruleset = GetRuleset(file, document); Rulesets.Add(file, ruleset); if (ruleset != null && !Diagnostics.ContainsErrors()) { if (isRoot) { VarCollection = new VarCollection(ruleset.UseGlobalVar, ruleset.UsePlayerVar, ruleset.UseBuilderVar); Root = new ScopeGroup(VarCollection); } // Get the defined types foreach (var definedType in ruleset.DefinedTypes) { try { if (DefinedTypes.Any(type => type.Name == definedType.Name)) { throw SyntaxErrorException.NameAlreadyDefined(definedType.Location); } DefinedTypes.Add(DefinedType.GetDefinedType(definedType)); } catch (SyntaxErrorException ex) { Diagnostics.Error(ex); } } // Get the user methods. for (int i = 0; i < ruleset.UserMethods.Length; i++) { try { UserMethods.Add(new UserMethod(Root, ruleset.UserMethods[i])); } catch (SyntaxErrorException ex) { Diagnostics.Error(ex); } } // Get the rules RuleNodes.AddRange(ruleset.Rules); List <string> importedFiles = new List <string>(); foreach (ImportObjectNode importObject in ruleset.ObjectImports) { try { Importer importer = new Importer(Diagnostics, importedFiles, importObject.File, file, importObject.Location); if (!importer.AlreadyImported) { importedFiles.Add(importer.ResultingPath); string content = importer.GetFile(); switch (importer.FileType) { case ".obj": Model newModel = Model.ImportObj(content); new ModelVar(importObject.Name, Root, importObject, newModel); break; } } } catch (SyntaxErrorException ex) { Diagnostics.Error(ex); } } // Check the imported files. foreach (ImportNode importNode in ruleset.Imports) { try { Importer importer = new Importer(Diagnostics, importedFiles, importNode.File, file, importNode.Location); if (!importer.AlreadyImported) { string content = File.ReadAllText(importer.ResultingPath); GetObjects(content, importer.ResultingPath, globalTranslate, playerTranslate, false); importedFiles.Add(importer.ResultingPath); } } catch (SyntaxErrorException ex) { Diagnostics.Error(ex); } } // Get the variables foreach (var definedVar in ruleset.DefinedVars) { try { IndexedVar var; if (definedVar.UseVar == null) { var = VarCollection.AssignVar(Root, definedVar.VariableName, definedVar.IsGlobal, definedVar); } else { var = VarCollection.AssignVar( Root, definedVar.VariableName, definedVar.IsGlobal, definedVar.UseVar.Variable, definedVar.UseVar.Index, definedVar ); } if (definedVar.Type != null) { var.Type = GetDefinedType(definedVar.Type, definedVar.Location); } } catch (SyntaxErrorException ex) { Diagnostics.Error(ex); } } } }