|
6 | 6 |
|
7 | 7 | namespace Antlr4Helper.CSharpHelper |
8 | 8 | { |
9 | | - class Program |
10 | | - { |
11 | | - static void Main(string[] args) |
12 | | - { |
13 | | - var str = @"T__0 = 1, T__1 = 2, T__2 = 3, T__3 = 4, T__4 = 5, T__5 = 6, T__6 = 7, T__7 = 8, T__8 = 9, |
| 9 | + class Program |
| 10 | + { |
| 11 | + static void Main(string[] args) |
| 12 | + { |
| 13 | + var str = @"T__0 = 1, T__1 = 2, T__2 = 3, T__3 = 4, T__4 = 5, T__5 = 6, T__6 = 7, T__7 = 8, T__8 = 9, |
14 | 14 | T__9 = 10, T__10 = 11, T__11 = 12, T__12 = 13, T__13 = 14, T__14 = 15, T__15 = 16, T__16 = 17, |
15 | 15 | T__17 = 18, T__18 = 19, T__19 = 20, T__20 = 21, T__21 = 22, T__22 = 23, T__23 = 24, |
16 | 16 | T__24 = 25, T__25 = 26, T__26 = 27, T__27 = 28, SUB = 29, NUM = 30, STRING = 31, NULL = 32, |
@@ -54,72 +54,97 @@ static void Main(string[] args) |
54 | 54 | RULE_prog = 0, RULE_expr = 1, RULE_num = 2, RULE_unit = 3, RULE_arrayJson = 4, |
55 | 55 | RULE_parameter2 = 5; |
56 | 56 | "; |
57 | | - var array = str.Split(",\r\n\t;".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); |
58 | | - Dictionary<string, string> dict = new Dictionary<string, string>(); |
59 | | - dict["Eof"] = "-1"; |
60 | | - foreach (var item in array) { |
61 | | - var sp = item.Split('='); |
62 | | - dict[sp[0].Trim()] = sp[1].Trim(); |
63 | | - } |
64 | | - |
65 | | - var filePath = Path.GetFullPath(@"..\..\..\..\..\g4\bin\mathParser.cs"); |
66 | | - var csText = File.ReadAllText(filePath); |
67 | | - |
68 | | - |
69 | | - |
70 | | - csText = csText.Replace("[System.CLSCompliant(false)]", ""); |
71 | | - csText = csText.Replace("[System.CodeDom.Compiler.GeneratedCode(\"ANTLR\", \"4.13.2\")]", ""); |
72 | | - csText = csText.Replace("[RuleVersion(0)]", ""); |
73 | | - csText = csText.Replace("[NotNull]", ""); |
74 | | - csText = csText.Replace("[System.Diagnostics.DebuggerNonUserCode] public ITerminalNode", "//[System.Diagnostics.DebuggerNonUserCode] public ITerminalNode"); |
75 | | - csText = csText.Replace("else return visitor.VisitChildren(this);", ""); |
76 | | - csText = csText.Replace("if (typedVisitor != null) ", ""); |
77 | | - |
78 | | - csText = Regex.Replace(csText, "if \\(!\\(Precpred.*\\r\\n\\t*", ""); |
79 | | - csText = Regex.Replace(csText, "State = \\d+;\\r\\n[ \\t]+State =", "State ="); |
80 | | - csText = Regex.Replace(csText, "return GetRuleContext<ExprContext>\\(i\\);\\r\\n", "return GetRuleContext<ExprContext>(i);"); |
81 | | - csText = Regex.Replace(csText, @"\[System.Diagnostics.DebuggerNonUserCode\] public ExprContext expr\(int i\) \{\r\n", "// [System.Diagnostics.DebuggerNonUserCode] public ExprContext expr(int i) {"); |
82 | | - |
83 | | - csText = Regex.Replace(csText, @"\bState = (\d+);\r\n[ \t]*Match\(", "Match("); |
84 | | - csText = Regex.Replace(csText, @"\bState = (\d+);\r\n[ \t]*ErrorHandler", "ErrorHandler"); |
85 | | - csText = Regex.Replace(csText, @"public partial class mathParser", "partial class mathParser"); |
| 57 | + var array = str.Split(",\r\n\t;".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); |
| 58 | + Dictionary<string, string> dict = new Dictionary<string, string>(); |
| 59 | + dict["Eof"] = "-1"; |
| 60 | + foreach(var item in array) { |
| 61 | + var sp = item.Split('='); |
| 62 | + dict[sp[0].Trim()] = sp[1].Trim(); |
| 63 | + } |
| 64 | + |
| 65 | + var filePath = Path.GetFullPath(@"..\..\..\..\..\g4\bin\mathParser.cs"); |
| 66 | + var csText = File.ReadAllText(filePath); |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | + csText = csText.Replace("[System.CLSCompliant(false)]", ""); |
| 71 | + csText = csText.Replace("[System.CodeDom.Compiler.GeneratedCode(\"ANTLR\", \"4.13.2\")]", ""); |
| 72 | + csText = csText.Replace("[RuleVersion(0)]", ""); |
| 73 | + csText = csText.Replace("[NotNull]", ""); |
| 74 | + csText = csText.Replace("[System.Diagnostics.DebuggerNonUserCode] public ITerminalNode", "//[System.Diagnostics.DebuggerNonUserCode] public ITerminalNode"); |
| 75 | + csText = csText.Replace("else return visitor.VisitChildren(this);", ""); |
| 76 | + csText = csText.Replace("if (typedVisitor != null) ", ""); |
| 77 | + |
| 78 | + csText = Regex.Replace(csText, "if \\(!\\(Precpred.*\\r\\n\\t*", ""); |
| 79 | + csText = Regex.Replace(csText, "State = \\d+;\\r\\n[ \\t]+State =", "State ="); |
| 80 | + csText = Regex.Replace(csText, "return GetRuleContext<ExprContext>\\(i\\);\\r\\n", "return GetRuleContext<ExprContext>(i);"); |
| 81 | + csText = Regex.Replace(csText, @"\[System.Diagnostics.DebuggerNonUserCode\] public ExprContext expr\(int i\) \{\r\n", "// [System.Diagnostics.DebuggerNonUserCode] public ExprContext expr(int i) {"); |
| 82 | + |
| 83 | + csText = Regex.Replace(csText, @"\bState = (\d+);\r\n[ \t]*Match\(", "Match("); |
| 84 | + csText = Regex.Replace(csText, @"\bState = (\d+);\r\n[ \t]*ErrorHandler", "ErrorHandler"); |
| 85 | + csText = Regex.Replace(csText, @"public partial class mathParser", "partial class mathParser"); |
86 | 86 | csText = Regex.Replace(csText, @"public partial class", "internal partial class"); |
87 | 87 |
|
88 | 88 |
|
89 | 89 |
|
90 | 90 | var list = dict.Keys.ToList().OrderByDescending(q => q.Length).ToList(); |
91 | 91 |
|
92 | | - foreach (var item in list) { |
93 | | - var value = dict[item]; |
94 | | - csText = Regex.Replace(csText, $"case {item}:", $"case {value}:"); |
95 | | - csText = Regex.Replace(csText, @$"Match\({item}\);", $"Match({value});"); |
96 | | - csText = Regex.Replace(csText, @$"\(1L << {item}\)", $"(1L << {value})"); |
97 | | - csText = Regex.Replace(csText, @$"\(1L << \({item} ", $"(1L << ({value} "); //(1L << (MULTINOMIAL |
98 | | - csText = csText.Replace(@$"_la=={item}", $"_la=={value}"); //(1L << (MULTINOMIAL |
99 | | - csText = csText.Replace(@$"_la == {item}", $"_la == {value}"); //(1L << (MULTINOMIAL |
100 | | - csText = csText.Replace(@$"mathParser.{item}", $"{value}"); //(1L << (MULTINOMIAL |
| 92 | + foreach(var item in list) { |
| 93 | + var value = dict[item]; |
| 94 | + csText = Regex.Replace(csText, $"case {item}:", $"case {value}:"); |
| 95 | + csText = Regex.Replace(csText, @$"Match\({item}\);", $"Match({value});"); |
| 96 | + csText = Regex.Replace(csText, @$"\(1L << {item}\)", $"(1L << {value})"); |
| 97 | + csText = Regex.Replace(csText, @$"\(1L << \({item} ", $"(1L << ({value} "); //(1L << (MULTINOMIAL |
| 98 | + csText = csText.Replace(@$"_la=={item}", $"_la=={value}"); //(1L << (MULTINOMIAL |
| 99 | + csText = csText.Replace(@$"_la == {item}", $"_la == {value}"); //(1L << (MULTINOMIAL |
| 100 | + csText = csText.Replace(@$"mathParser.{item}", $"{value}"); //(1L << (MULTINOMIAL |
101 | 101 |
|
102 | | - csText = csText.Replace($"PushNewRecursionContext(_localctx, _startState, {item})", $"PushNewRecursionContext(_localctx, _startState, {value})"); //(1L << (MULTINOMIAL |
103 | | - csText = csText.Replace($"public override int RuleIndex {{ get {{ return {item}; }} }}", $"public override int RuleIndex {{ get {{ return {value}; }} }}"); //(1L << (MULTINOMIAL |
104 | | - csText = Regex.Replace(csText, @$"EnterRule\(_localctx, (\d+), {item}\);", $"EnterRule(_localctx, $1, {value});"); //(1L << (MULTINOMIAL |
105 | | - csText = Regex.Replace(csText, @$"EnterRecursionRule\(_localctx, (\d+), {item}, _p\);", $"EnterRecursionRule(_localctx, $1, {value}, _p);"); //(1L << (MULTINOMIAL |
| 102 | + csText = csText.Replace($"PushNewRecursionContext(_localctx, _startState, {item})", $"PushNewRecursionContext(_localctx, _startState, {value})"); //(1L << (MULTINOMIAL |
| 103 | + csText = csText.Replace($"public override int RuleIndex {{ get {{ return {item}; }} }}", $"public override int RuleIndex {{ get {{ return {value}; }} }}"); //(1L << (MULTINOMIAL |
| 104 | + csText = Regex.Replace(csText, @$"EnterRule\(_localctx, (\d+), {item}\);", $"EnterRule(_localctx, $1, {value});"); //(1L << (MULTINOMIAL |
| 105 | + csText = Regex.Replace(csText, @$"EnterRecursionRule\(_localctx, (\d+), {item}, _p\);", $"EnterRecursionRule(_localctx, $1, {value}, _p);"); //(1L << (MULTINOMIAL |
106 | 106 |
|
107 | 107 |
|
108 | | - //EnterRule(_localctx, 4, RULE_parameter); |
109 | | - //public override int RuleIndex { get { return RULE_expr; } } |
110 | | - //EnterRecursionRule(_localctx, 2, RULE_expr, _p); |
111 | | - //PushNewRecursionContext(_localctx, _startState, RULE_expr); |
| 108 | + //EnterRule(_localctx, 4, RULE_parameter); |
| 109 | + //public override int RuleIndex { get { return RULE_expr; } } |
| 110 | + //EnterRecursionRule(_localctx, 2, RULE_expr, _p); |
| 111 | + //PushNewRecursionContext(_localctx, _startState, RULE_expr); |
112 | 112 |
|
113 | 113 |
|
114 | | - //mathParser.STRING |
115 | | - //_la==T__16 |
116 | | - } |
| 114 | + //mathParser.STRING |
| 115 | + //_la==T__16 |
| 116 | + } |
117 | 117 |
|
118 | 118 | csText = csText.Replace("//[System.Diagnostics.DebuggerNonUserCode] public ITerminalNode PARAMETER()", "[System.Diagnostics.DebuggerNonUserCode] public ITerminalNode PARAMETER()"); |
119 | 119 |
|
120 | | - csText = "namespace ToolGood.Algorithm.math\r\n{"+csText+"\r\n}"; |
| 120 | + csText = "namespace ToolGood.Algorithm.math\r\n{" + csText + "\r\n}"; |
121 | 121 | File.WriteAllText("mathParser.cs", csText); |
122 | | - } |
123 | | - } |
| 122 | + |
| 123 | + |
| 124 | + filePath = Path.GetFullPath(@"..\..\..\..\..\g4\bin\mathLexer.cs"); |
| 125 | + csText = File.ReadAllText(filePath); |
| 126 | + |
| 127 | + csText = csText.Replace("[System.CLSCompliant(false)]", ""); |
| 128 | + csText = csText.Replace("[System.CodeDom.Compiler.GeneratedCode(\"ANTLR\", \"4.13.2\")]", ""); |
| 129 | + csText = csText.Replace("public partial class mathLexer", "partial class mathLexer"); |
| 130 | + |
| 131 | + csText = "namespace ToolGood.Algorithm.math\r\n{" + csText + "\r\n}"; |
| 132 | + File.WriteAllText("mathLexer.cs", csText); |
| 133 | + |
| 134 | + |
| 135 | + |
| 136 | + filePath = Path.GetFullPath(@"..\..\..\..\..\g4\bin\mathVisitor.cs"); |
| 137 | + csText = File.ReadAllText(filePath); |
| 138 | + |
| 139 | + csText = csText.Replace("[System.CLSCompliant(false)]", ""); |
| 140 | + csText = csText.Replace("[System.CodeDom.Compiler.GeneratedCode(\"ANTLR\", \"4.13.2\")]", ""); |
| 141 | + csText = csText.Replace("public interface ImathVisitor<Result>", "interface ImathVisitor<Result>"); |
| 142 | + csText = csText.Replace("[NotNull] ", ""); |
| 143 | + |
| 144 | + csText = "namespace ToolGood.Algorithm.math\r\n{" + csText + "\r\n}"; |
| 145 | + File.WriteAllText("mathVisitor.cs", csText); |
| 146 | + |
| 147 | + } |
| 148 | + } |
124 | 149 |
|
125 | 150 | } |
0 commit comments