Skip to content

Commit d08579e

Browse files
author
linzhijun
committed
fix
1 parent 7a1cee6 commit d08579e

3 files changed

Lines changed: 139 additions & 1 deletion

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net10.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
using System.Text.RegularExpressions;
2+
3+
namespace Antlr4Helper.JavaScriptHelper2
4+
{
5+
class Program
6+
{
7+
static void Main(string[] args)
8+
{
9+
var filePath = Path.GetFullPath(@"..\..\..\..\..\g4\antlr4\mathjsParser.js");
10+
var text = File.ReadAllText(filePath);
11+
text = text.Replace("import antlr4 from 'antlr4';", "import antlr4 from '../antlr4/index.web.js';");
12+
text = text.Replace("import mathjsVisitor from './mathjsVisitor.js';", "");
13+
text = text.Replace("mathjsParser.EOF = antlr4.Token.EOF;", "");
14+
text = Regex.Replace(text, @"mathjsParser\..*?Context = .*?Context;", "");
15+
text = Regex.Replace(text, @"this\.state = \d+;[\r\n\t ]*this.match", "this.match");
16+
17+
// 替换所有的 mathjsParser.xxx = n 为字典,然后再替换回去
18+
var ms = Regex.Matches(text, @"(mathjsParser\..*) = (\d+);");
19+
Dictionary<string, string> dict = new Dictionary<string, string>();
20+
dict["mathjsParser.EOF"] = "-1";
21+
foreach(Match m in ms) {
22+
dict[m.Groups[1].Value] = m.Groups[2].Value;
23+
}
24+
text = Regex.Replace(text, @"(mathjsParser\..*) = (\d+);", "");
25+
var keys = dict.Keys.OrderByDescending(q => q.Length).ToList();
26+
foreach(var item in keys) {
27+
text = Regex.Replace(text, @$"\b{item}\b", dict[item]);
28+
}
29+
30+
text = Regex.Replace(text, @"([A-Z][0-9A-Z]*\(\) \{)[\r\n\t ]*(return this\.getToken\()", "$1$2");
31+
text = Regex.Replace(text, @"([A-Z][0-9A-Z]*\(\) \{return this\.getToken.*?;)[\r\n\t ]*};", "$1};");
32+
text = Regex.Replace(text, @"([A-Z][0-9A-Z]*\(\) \{return this\.getToken.*?};)", "//$1");
33+
34+
35+
text = text.Replace("if ( visitor instanceof mathjsVisitor ) {", "");
36+
text = Regex.Replace(text, @"\} else \{[\r\n\t ]*return visitor\.visitChildren\(this\);[\r\n\t ]*\}", "");
37+
38+
39+
text = text.Replace("static grammarFileName = \"math.g4\";", "static grammarFileName = \"\";");
40+
text = Regex.Replace(text, @"static literalNames = \[[\s\S]+?\];", "static literalNames =[];");
41+
text = Regex.Replace(text, @"static symbolicNames = \[[\s\S]+?\];", "static symbolicNames =[];");
42+
text = Regex.Replace(text, @"static ruleNames = \[[\s\S]+?\];", "static ruleNames =[];");
43+
44+
text = Regex.Replace(text, @"[\r\n]+[\t ]*throw new antlr4\.error\.FailedPredicateException.*?[\r\n]+", "");
45+
text = Regex.Replace(text, @"[\r\n]+[\t ]*if \(!\( this\.precpred\(this\._ctx, \d+\)\)\) \{[\s\t ]+\}", "");
46+
text = Regex.Replace(text, @"this\.state = \d+;[\r\n\t ]*this\.match", "this.match");
47+
text = Regex.Replace(text, @"this\.state = \d+;[\r\n\t ]*this\._errHandler\.sync\(this\);", "this._errHandler.sync(this);");
48+
text = Regex.Replace(text, @"this\.state = \d+;[\r\n\t ]*_la", "_la");
49+
text = Regex.Replace(text, @"this\.state = \d+;[\r\n\t ]*localctx", "localctx");
50+
text = Regex.Replace(text, @"this\.state = \d+;[\r\n\t ]*localctx", "localctx");
51+
52+
53+
54+
55+
// expr 一般情况下返回 ExprContext 的数组 或 单个 ExprContext
56+
text = Regex.Replace(text, @"expr = function\(i\) \{[\s\S]+?\};", "expr = function(i) {return this.getTypedRuleContexts(ExprContext);};");
57+
text = Regex.Replace(text, @"arrayJson = function\(i\) \{[\s\S]+?\};", "arrayJson = function(i) {return this.getTypedRuleContexts(ArrayJsonContext);};");
58+
59+
text = Regex.Replace(text, @"if\(parent===undefined\) \{[\r\n\t ]*parent = null;[\r\n\t ]*\}", "");
60+
text = Regex.Replace(text, @"if\(invokingState===undefined \|\| invokingState===null\) \{[\r\n\t ]*invokingState = -1;[\r\n\t ]*}", "");
61+
62+
text = Regex.Replace(text, @"let _prevctx = localctx;", "");
63+
text = Regex.Replace(text, @"_prevctx = localctx;", "");
64+
65+
66+
text = Regex.Replace(text, @"this\.match\((\d+)\);[\r\n\t ]*this\.match\((\d+)\);[\r\n\t ]*this\.match\((\d+)\);[\r\n\t ]*this\.match\((\d+)\);[\r\n\t ]*this\.state = (\d+);[\r\n\t ]*this\.expr\((\d+)\);"
67+
, "this.F($1,$2,$3,$4,$5,$6);");
68+
text = Regex.Replace(text, @"this\.match\((\d+)\);[\r\n\t ]*this\.match\((\d+)\);[\r\n\t ]*this\.match\((\d+)\);[\r\n\t ]*this\.state = (\d+);[\r\n\t ]*this\.expr\((\d+)\);"
69+
, "this.G($1,$2,$3,$4,$5);");
70+
text = Regex.Replace(text, @"this\.match\((\d+)\);[\r\n\t ]*this\.match\((\d+)\);[\r\n\t ]*this\.state = (\d+);[\r\n\t ]*this\.expr\((\d+)\);"
71+
, "this.I($1,$2,$3,$4);");
72+
text = Regex.Replace(text, @"this\.match\((\d+)\);[\r\n\t ]*this\.state = (\d+);[\r\n\t ]*this\.expr\((\d+)\);"
73+
, "this.J($1,$2,$3);");
74+
text = Regex.Replace(text, @"this\.match\((\d+)\);[\r\n\t ]*this\.match\((\d+)\);[\r\n\t ]*this\.match\((\d+)\);[\r\n\t ]*this\.match\((\d+)\);", "this.D($1,$2,$3,$4);");
75+
text = Regex.Replace(text, @"this\.match\((\d+)\);[\r\n\t ]*this\.match\((\d+)\);[\r\n\t ]*this\.match\((\d+)\);", "this.C($1,$2,$3);");
76+
text = Regex.Replace(text, @"this\.match\((\d+)\);[\r\n\t ]*this\.match\((\d+)\);", "this.B($1,$2);");
77+
text = Regex.Replace(text, @"this\.match\((\d+)\);", "this.A($1);");
78+
text = Regex.Replace(text, @"this\.state = (\d+);[\r\n\t ]*this\.expr\((\d+)\);", "this.E($1,$2);");
79+
text = Regex.Replace(text, @"this\._errHandler\.sync\(this\);[\r\n\t ]*_la = this\._input\.LA\(1\);", "_la = this.Z();");
80+
//text = Regex.Replace(text, @"this\._input\.LA\(1\);", "this.Y();");
81+
text = Regex.Replace(text, @"this\._errHandler\.sync\(this\);", "this.X();");
82+
text = text.Replace("this.pushNewRecursionContext(localctx, _startState, 1);", "this.W(localctx, _startState, 1);");
83+
text = Regex.Replace(text, @"this\._errHandler\.reportMatch\(this\);[\r\n\t ]*this\.consume\(\);", "this.Q();");
84+
text = text.Replace("this._input.LA(1);", "this.Y();");
85+
text = text.Replace("this._input.LT(1);", "this.R();");
86+
87+
88+
text = Regex.Replace(text, @"sempred\(localctx, ruleIndex, predIndex\) \{[\s\S]+?\};", @" sempred(localctx, ruleIndex, predIndex){return true;};
89+
A(a){this.match(a);}
90+
B(a,b){this.match(a);this.match(b);}
91+
C(a,b,c){this.match(a);this.match(b);this.match(c);}
92+
D(a,b,c,d){this.match(a);this.match(b);this.match(c);this.match(d);}
93+
E(a,b){this.state=a;this.expr(b);}
94+
I(a,b,e,f){this.match(a);this.match(b);this.state=e;this.expr(f);}
95+
J(a,e,f){this.match(a);this.state=e;this.expr(f);}
96+
97+
98+
R(){return this._input.LT(1);}
99+
Q(){this._errHandler.reportMatch(this);this.consume();}
100+
W(a,b,c){this.pushNewRecursionContext(a, b, c);}
101+
X(){this._errHandler.sync(this);}
102+
Y(){return this._input.LA(1);}
103+
Z(){this._errHandler.sync(this);return this._input.LA(1);}
104+
");
105+
106+
107+
// new DAY_funContext(this, new ExprContext(this, _parentctx, _parentState))
108+
// 清理多余的空行和注释
109+
text = Regex.Replace(text, @"[\t ]*//.*([\r\n])", "$1");
110+
text = Regex.Replace(text, @"[\t ]*([\r\n])([\r\n])+", "$1");
111+
text = Regex.Replace(text, @"([\r\n])([\r\n])+", "$1");
112+
113+
114+
115+
File.WriteAllText("mathjsParser.js", text);
116+
117+
118+
119+
120+
}
121+
}
122+
}

tools/Antlr4Helper.sln

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 18
4-
VisualStudioVersion = 18.2.11415.280 d18.0
4+
VisualStudioVersion = 18.2.11415.280
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Antlr4Helper.JavaScriptHelper", "Antlr4Helper.JavaScriptHelper\Antlr4Helper.JavaScriptHelper.csproj", "{F1B70AE9-51ED-4D4E-8E3B-F7C85F0CA316}"
77
EndProject
88
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Antlr4Helper.JavaHelper", "Antlr4Helper.JavaHelper\Antlr4Helper.JavaHelper.csproj", "{C495CEF6-C191-4BBB-B95F-072F6248E86B}"
99
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Antlr4Helper.JavaScriptHelper2", "Antlr4Helper.JavaScriptHelper2\Antlr4Helper.JavaScriptHelper2.csproj", "{3F136506-2D40-4ABB-9E25-467132652B9E}"
11+
EndProject
1012
Global
1113
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1214
Debug|Any CPU = Debug|Any CPU
@@ -21,6 +23,10 @@ Global
2123
{C495CEF6-C191-4BBB-B95F-072F6248E86B}.Debug|Any CPU.Build.0 = Debug|Any CPU
2224
{C495CEF6-C191-4BBB-B95F-072F6248E86B}.Release|Any CPU.ActiveCfg = Release|Any CPU
2325
{C495CEF6-C191-4BBB-B95F-072F6248E86B}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{3F136506-2D40-4ABB-9E25-467132652B9E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{3F136506-2D40-4ABB-9E25-467132652B9E}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{3F136506-2D40-4ABB-9E25-467132652B9E}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{3F136506-2D40-4ABB-9E25-467132652B9E}.Release|Any CPU.Build.0 = Release|Any CPU
2430
EndGlobalSection
2531
GlobalSection(SolutionProperties) = preSolution
2632
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)