/// <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;
}