|
| 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 | +} |
0 commit comments