ParserMediator.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text.RegularExpressions;
  5. using NTERA.Core;
  6. using NTERA.EmuEra.Game.EraEmu.GameProc;
  7. using NTERA.EmuEra.Game.EraEmu.Sub;
  8. namespace NTERA.EmuEra.Game.EraEmu.GameData
  9. {
  10. //1756 新設。ParserやLexicalAnalyzerなどが知りたい情報をまとめる
  11. //本当は引数として渡すべきなのかもしれないが全てのParserの引数を書きなおすのが面倒なのでstatic
  12. internal static class ParserMediator
  13. {
  14. /// <summary>
  15. /// emuera.config等で発生した警告
  16. /// Initializeより前に発生する
  17. /// </summary>
  18. /// <param name="str"></param>
  19. /// <param name="?"></param>
  20. public static void ConfigWarn(string str, ScriptPosition pos, int level, string stack)
  21. {
  22. if (level < Config.Config.DisplayWarningLevel && !Program.AnalysisMode)
  23. return;
  24. warningList.Add(new ParserWarning(str, pos, level, stack));
  25. }
  26. static IConsole console;
  27. public static void Initialize(IConsole console)
  28. {
  29. ParserMediator.console = console;
  30. }
  31. #region Rename
  32. public static Dictionary<string, string> RenameDic { get; private set; }
  33. //1756 Process.Load.csより移動
  34. public static void LoadEraExRenameFile(string filepath)
  35. {
  36. if (RenameDic != null)
  37. RenameDic.Clear();
  38. //とにかく辞書を作る。辞書がnullのときは UseRenameFileがNOの時のみ
  39. RenameDic = new Dictionary<string, string>();
  40. EraStreamReader eReader = new EraStreamReader(false);
  41. if ((!File.Exists(filepath)) || (!eReader.Open(filepath)))
  42. {
  43. return;
  44. }
  45. string line = null;
  46. ScriptPosition pos = null;
  47. Regex reg = new Regex(@"\\,", RegexOptions.Compiled);
  48. try
  49. {
  50. while ((line = eReader.ReadLine()) != null)
  51. {
  52. if (line.Length == 0)
  53. continue;
  54. if (line.StartsWith(";"))
  55. continue;
  56. string[] baseTokens = reg.Split(line);
  57. if (!baseTokens[baseTokens.Length - 1].Contains(","))
  58. continue;
  59. string[] last = baseTokens[baseTokens.Length - 1].Split(',');
  60. baseTokens[baseTokens.Length - 1] = last[0];
  61. string[] tokens = new string[2];
  62. tokens[0] = string.Join(",", baseTokens);
  63. tokens[1] = last[1];
  64. pos = new ScriptPosition(eReader.Filename, eReader.LineNo, line);
  65. //右がERB中の表記、左が変換先になる。
  66. string value = tokens[0].Trim();
  67. string key = string.Format("[[{0}]]", tokens[1].Trim());
  68. RenameDic[key] = value;
  69. pos = null;
  70. }
  71. }
  72. catch (Exception e)
  73. {
  74. if (pos != null)
  75. throw new CodeEE(e.Message, pos);
  76. else
  77. throw new CodeEE(e.Message);
  78. }
  79. finally
  80. {
  81. eReader.Close();
  82. }
  83. }
  84. #endregion
  85. public static void Warn(string str, ScriptPosition pos, int level)
  86. {
  87. Warn(str, pos, level, null);
  88. }
  89. public static void Warn(string str, ScriptPosition pos, int level, string stack)
  90. {
  91. if (level < Config.Config.DisplayWarningLevel && !Program.AnalysisMode)
  92. return;
  93. if (console != null && !console.RunERBFromMemory)
  94. warningList.Add(new ParserWarning(str, pos, level, stack));
  95. }
  96. /// <summary>
  97. /// Parser中での警告出力
  98. /// </summary>
  99. /// <param name="str"></param>
  100. /// <param name="line"></param>
  101. /// <param name="level">警告レベル.0:軽微なミス.1:無視できる行.2:行が実行されなければ無害.3:致命的</param>
  102. public static void Warn(string str, LogicalLine line, int level, bool isError, bool isBackComp)
  103. {
  104. Warn(str, line, level, isError, isBackComp, null);
  105. }
  106. public static void Warn(string str, LogicalLine line, int level, bool isError, bool isBackComp, string stack)
  107. {
  108. if (isError)
  109. {
  110. line.IsError = true;
  111. line.ErrMes = str;
  112. }
  113. if (level < Config.Config.DisplayWarningLevel && !Program.AnalysisMode)
  114. return;
  115. if (isBackComp && !Config.Config.WarnBackCompatibility)
  116. return;
  117. if (console != null && !console.RunERBFromMemory)
  118. warningList.Add(new ParserWarning(str, line.Position, level, stack));
  119. // console.PrintWarning(str, line.Position, level);
  120. }
  121. private static List<ParserWarning> warningList = new List<ParserWarning>();
  122. public static bool HasWarning => warningList.Count > 0;
  123. public static void ClearWarningList()
  124. {
  125. warningList.Clear();
  126. }
  127. public static void FlushWarningList()
  128. {
  129. for (int i = 0; i < warningList.Count; i++)
  130. {
  131. ParserWarning warning = warningList[i];
  132. console.PrintWarning(warning.WarningMes + warning.WarningPos, warning.WarningLevel);
  133. if (warning.StackTrace != null)
  134. {
  135. string[] stacks = warning.StackTrace.Split('\n');
  136. for (int j = 0; j < stacks.Length; j++)
  137. {
  138. console.PrintSystemLine(stacks[j]);
  139. }
  140. }
  141. }
  142. warningList.Clear();
  143. }
  144. private class ParserWarning
  145. {
  146. public ParserWarning(string mes, ScriptPosition pos, int level, string stackTrace)
  147. {
  148. WarningMes = mes;
  149. WarningPos = pos;
  150. WarningLevel = level;
  151. StackTrace = stackTrace;
  152. }
  153. public string WarningMes;
  154. public ScriptPosition WarningPos;
  155. public int WarningLevel;
  156. public string StackTrace;
  157. }
  158. }
  159. }