Skip to content

Commit 0747e87

Browse files
committed
fix
1 parent 9fca286 commit 0747e87

1 file changed

Lines changed: 82 additions & 57 deletions

File tree

csharp/Antlr4Helper.CSharpHelper/Program.cs

Lines changed: 82 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66

77
namespace Antlr4Helper.CSharpHelper
88
{
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,
1414
T__9 = 10, T__10 = 11, T__11 = 12, T__12 = 13, T__13 = 14, T__14 = 15, T__15 = 16, T__16 = 17,
1515
T__17 = 18, T__18 = 19, T__19 = 20, T__20 = 21, T__21 = 22, T__22 = 23, T__23 = 24,
1616
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)
5454
RULE_prog = 0, RULE_expr = 1, RULE_num = 2, RULE_unit = 3, RULE_arrayJson = 4,
5555
RULE_parameter2 = 5;
5656
";
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");
8686
csText = Regex.Replace(csText, @"public partial class", "internal partial class");
8787

8888

8989

9090
var list = dict.Keys.ToList().OrderByDescending(q => q.Length).ToList();
9191

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
101101

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
106106

107107

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);
112112

113113

114-
//mathParser.STRING
115-
//_la==T__16
116-
}
114+
//mathParser.STRING
115+
//_la==T__16
116+
}
117117

118118
csText = csText.Replace("//[System.Diagnostics.DebuggerNonUserCode] public ITerminalNode PARAMETER()", "[System.Diagnostics.DebuggerNonUserCode] public ITerminalNode PARAMETER()");
119119

120-
csText = "namespace ToolGood.Algorithm.math\r\n{"+csText+"\r\n}";
120+
csText = "namespace ToolGood.Algorithm.math\r\n{" + csText + "\r\n}";
121121
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+
}
124149

125150
}

0 commit comments

Comments
 (0)