/// <summary> /// Creates a CPL parser for the given code, using the given error tracker. /// </summary> /// <param name="p_strCode">The code be parsed.</param> /// <param name="p_ertErrorTracker">The error tracker to use to log /// parsing errors.</param> /// <returns>A CPL parser for the given code.</returns> public AntlrParserBase CreateParser(string p_strCode, ErrorTracker p_ertErrorTracker) { AntlrLexerBase lexLexer = CreateLexer(p_strCode, p_ertErrorTracker); CommonTokenStream ctsTokens = new CommonTokenStream(lexLexer); CPLParser prsParser = new CPLParser(ctsTokens); prsParser.ErrorTracker = p_ertErrorTracker; return prsParser; }
/// <summary> /// Creates a CPL parser for the given code, using the given error tracker. /// </summary> /// <param name="p_strCode">The code be parsed.</param> /// <param name="p_ertErrorTracker">The error tracker to use to log /// parsing errors.</param> /// <returns>A CPL parser for the given code.</returns> public AntlrParserBase CreateParser(string p_strCode, ErrorTracker p_ertErrorTracker) { AntlrLexerBase lexLexer = CreateLexer(p_strCode, p_ertErrorTracker); CommonTokenStream ctsTokens = new CommonTokenStream(lexLexer); SkyrimCplParser prsParser = new SkyrimCplParser(ctsTokens, ""); prsParser.SetErrorTracker(p_ertErrorTracker); return prsParser; }
/// <summary> /// Parses the given CPL into an AST. /// </summary> /// <param name="p_strCplCode">The CPL to convert.</param> /// <returns>The AST built from the given CPL.</returns> private ITree GenerateAst(string p_strCplCode) { ErrorTracker ertErrors = new ErrorTracker(); AntlrParserBase cpbParser = ParserFactory.CreateParser(p_strCplCode, ertErrors); ITree astCPL = cpbParser.Parse(); if (ertErrors.HasErrors) throw new ArgumentException("Invalid CPL:" + Environment.NewLine + ertErrors.ToString(), "p_strCplCode"); return astCPL; }
/// <summary> /// Validates the CPL. /// </summary> /// <returns><c>true</c> if the CPL is valid; <c>false</c> otherwise.</returns> public bool ValidateCPL() { bool booIsValid = true; if (Code.Length > 0) { ErrorTracker et = new ErrorTracker(); CPLParserFactory.CreateParser(Code, et).Parse(); List<LanguageError> lstErrors = new List<LanguageError>(et.ParserErrors); lstErrors.AddRange(et.LexerErrors); Errors = lstErrors; booIsValid = (lstErrors.Count == 0); } else Errors = new List<LanguageError>(); CodeValidated(this, new EventArgs()); return booIsValid; }
/// <summary> /// Creates a CPL lexer for the given code, using the given error tracker. /// </summary> /// <param name="p_strCode">The code be lexed.</param> /// <param name="p_ertErrorTracker">The error tracker to use to log /// lexing errors.</param> /// <returns>A CPL lexer for the given code.</returns> public AntlrLexerBase CreateLexer(string p_strCode, ErrorTracker p_ertErrorTracker) { CPLLexer lexLexer = new CPLLexer(new ANTLRStringStream(p_strCode)); lexLexer.ErrorTracker = p_ertErrorTracker; return lexLexer; }
/// <summary> /// Creates a CPL lexer for the given code, using the given error tracker. /// </summary> /// <param name="p_strCode">The code be lexed.</param> /// <param name="p_ertErrorTracker">The error tracker to use to log /// lexing errors.</param> /// <returns>A CPL lexer for the given code.</returns> public AntlrLexerBase CreateLexer(string p_strCode, ErrorTracker p_ertErrorTracker) { SkyrimCplLexer lexLexer = new SkyrimCplLexer(new ANTLRStringStream(p_strCode)); lexLexer.SetErrorTracker(p_ertErrorTracker); return lexLexer; }
/// <summary> /// Parses the given Mod Script into an AST. /// </summary> /// <param name="p_strModScriptCode">The Mod Script to compile.</param> /// <param name="p_booCompileTest">Whether the prograsm is just checking if the script compiles.</param> /// <returns>The AST built from the given Mod Script.</returns> private ITree GenerateAst(string p_strModScriptCode, bool p_booCompileTest) { ErrorTracker ertErrors = new ErrorTracker(); string strCode = p_strModScriptCode; //unescape characters Regex rgxStrings = new Regex("[^\\\\](\"(\"|(.*?[^\\\\]\")))", RegexOptions.Multiline); MatchCollection colStrings = rgxStrings.Matches(strCode); Dictionary<string, string> dicProtectedStrings = new Dictionary<string, string>(); for (Int32 i = colStrings.Count - 1; i >= 0; i--) { string strShieldText = "<SHIELD" + i + ">"; strCode = strCode.Replace(colStrings[i].Groups[1].Value, strShieldText); dicProtectedStrings[strShieldText] = colStrings[i].Value; } strCode = strCode.Replace(@"\""", @"""").Replace(@"\\", @"\"); foreach (string strKey in dicProtectedStrings.Keys) strCode = strCode.Replace(strKey, dicProtectedStrings[strKey]); //clean code colStrings = rgxStrings.Matches(strCode); dicProtectedStrings.Clear(); for (Int32 i = colStrings.Count - 1; i >= 0; i--) { string strShieldText = "<SHIELD" + i + ">"; strCode = strCode.Replace(colStrings[i].Value, strShieldText); dicProtectedStrings[strShieldText] = colStrings[i].Value; } //strip comments Regex rgxComments = new Regex(";.*$", RegexOptions.Multiline); strCode = rgxComments.Replace(strCode, ""); foreach (string strKey in dicProtectedStrings.Keys) strCode = strCode.Replace(strKey, dicProtectedStrings[strKey]); AntlrParserBase cpbParser = CreateParser(strCode, ertErrors); ITree astModSCript = cpbParser.Parse(); if ((ertErrors.HasErrors) && !p_booCompileTest) { m_sicContext.FunctionProxy.ExtendedMessageBox("Invalid Mod Script", "Error", ertErrors.ToHtml(), MessageBoxButtons.OK, MessageBoxIcon.Error); return null; } return astModSCript; }