From 5ce60ea123a6073079d69e78256ff657f8908158 Mon Sep 17 00:00:00 2001 From: "lwc.cx" Date: Tue, 21 Mar 2023 17:45:13 +0800 Subject: [PATCH] feat: support for more syntax error fixes --- dist/json_parser.js | 75 ++++++++++++++++++++------------------------- lib/backslash.js | 61 ++++++++++++++++++++++++++++++++++++ package.json | 6 ++-- src/index.jison | 15 +++------ test/main.test.js | 48 +++++++++++++++++++++++++++++ 5 files changed, 150 insertions(+), 55 deletions(-) create mode 100644 lib/backslash.js diff --git a/dist/json_parser.js b/dist/json_parser.js index 8690c87..7b45632 100644 --- a/dist/json_parser.js +++ b/dist/json_parser.js @@ -1,4 +1,4 @@ -/* parser generated by jison 0.4.17 */ +/* parser generated by jison 0.4.18 */ /* Returns a Parser object of the following structure: @@ -73,7 +73,7 @@ */ var json_parser = (function(){ var o=function(k,v,o,l){for(o=o||{},l=k.length;l--;o[k[l]]=v);return o},$V0=[1,14],$V1=[1,15],$V2=[1,11],$V3=[1,10],$V4=[1,12],$V5=[1,13],$V6=[1,16],$V7=[1,17],$V8=[4,16,20,24,25,27],$V9=[1,23],$Va=[4,20,25],$Vb=[24,27]; -var parser = {trace: function trace() { }, +var parser = {trace: function trace () { }, yy: {}, symbols_: {"error":2,"JSONString":3,"STRING":4,"JSONNumber":5,"NUMBER":6,"JSONUndefinedLiteral":7,"UNDEFINED":8,"JSONNullLiteral":9,"NULL":10,"JSONBooleanLiteral":11,"TRUE":12,"FALSE":13,"JSONText":14,"JSONValue":15,"EOF":16,"JSONObject":17,"JSONArray":18,"{":19,"}":20,"JSONMemberList":21,"JSONMember":22,":":23,",":24,"JSONKEY":25,"[":26,"]":27,"JSONElementList":28,"$accept":0,"$end":1}, terminals_: {2:"error",4:"STRING",6:"NUMBER",8:"UNDEFINED",10:"NULL",12:"TRUE",13:"FALSE",16:"EOF",19:"{",20:"}",23:":",24:",",25:"JSONKEY",26:"[",27:"]"}, @@ -85,13 +85,7 @@ var $0 = $$.length - 1; switch (yystate) { case 1: // replace escaped characters with actual character - this.$ = yytext.replace(/\\(\\|")/g, "$"+"1") - .replace(/\\n/g,'\n') - .replace(/\\r/g,'\r') - .replace(/\\t/g,'\t') - .replace(/\\v/g,'\v') - .replace(/\\f/g,'\f') - .replace(/\\b/g,'\b'); + this.$ = backslash(yytext); break; case 2: @@ -143,17 +137,13 @@ break; }, table: [{3:6,4:$V0,5:7,6:$V1,7:4,8:$V2,9:3,10:$V3,11:5,12:$V4,13:$V5,14:1,15:2,17:8,18:9,19:$V6,26:$V7},{1:[3]},{16:[1,18]},o($V8,[2,8]),o($V8,[2,9]),o($V8,[2,10]),o($V8,[2,11]),o($V8,[2,12]),o($V8,[2,13]),o($V8,[2,14]),o($V8,[2,4]),o($V8,[2,3]),o($V8,[2,5]),o($V8,[2,6]),o([4,16,20,23,24,25,27],[2,1]),o($V8,[2,2]),{3:22,4:$V0,20:[1,19],21:20,22:21,25:$V9},{3:6,4:$V0,5:7,6:$V1,7:4,8:$V2,9:3,10:$V3,11:5,12:$V4,13:$V5,15:26,17:8,18:9,19:$V6,26:$V7,27:[1,24],28:25},{1:[2,7]},o($V8,[2,15]),{3:22,4:$V0,20:[1,27],22:28,25:$V9},o($Va,[2,21]),{23:[1,29]},{23:[1,30]},o($V8,[2,23]),{24:[1,32],27:[1,31]},o($Vb,[2,25]),o($V8,[2,16]),o($Va,[2,22]),{3:6,4:$V0,5:7,6:$V1,7:4,8:$V2,9:3,10:$V3,11:5,12:$V4,13:$V5,15:33,17:8,18:9,19:$V6,26:$V7},{3:6,4:$V0,5:7,6:$V1,7:4,8:$V2,9:3,10:$V3,11:5,12:$V4,13:$V5,15:34,17:8,18:9,19:$V6,26:$V7},o($V8,[2,24]),{3:6,4:$V0,5:7,6:$V1,7:4,8:$V2,9:3,10:$V3,11:5,12:$V4,13:$V5,15:35,17:8,18:9,19:$V6,26:$V7},o($Va,[2,19],{24:[1,36]}),o($Va,[2,20],{24:[1,37]}),o($Vb,[2,26]),o($Va,[2,17]),o($Va,[2,18])], defaultActions: {18:[2,7]}, -parseError: function parseError(str, hash) { +parseError: function parseError (str, hash) { if (hash.recoverable) { this.trace(str); } else { - function _parseError (msg, hash) { - this.message = msg; - this.hash = hash; - } - _parseError.prototype = Error; - - throw new _parseError(str, hash); + var error = new Error(str); + error.hash = hash; + throw error; } }, parse: function parse(input) { @@ -293,7 +283,8 @@ parse: function parse(input) { } return true; }}; -/* generated by jison-lex 0.3.4 */ + +const backslash = require('../lib/backslash');/* generated by jison-lex 0.3.4 */ var lexer = (function(){ var lexer = ({ @@ -435,7 +426,7 @@ showPosition:function () { }, // test the lexed token: return FALSE when not a match, otherwise return token -test_match:function (match, indexed_rule) { +test_match:function(match, indexed_rule) { var token, lines, backup; @@ -565,7 +556,7 @@ next:function () { }, // return next match that has a token -lex:function lex() { +lex:function lex () { var r = this.next(); if (r) { return r; @@ -575,12 +566,12 @@ lex:function lex() { }, // activates a new lexer condition state (pushes the new lexer condition state onto the condition stack) -begin:function begin(condition) { +begin:function begin (condition) { this.conditionStack.push(condition); }, // pop the previously active lexer condition state off the condition stack -popState:function popState() { +popState:function popState () { var n = this.conditionStack.length - 1; if (n > 0) { return this.conditionStack.pop(); @@ -590,7 +581,7 @@ popState:function popState() { }, // produce the lexer rule set which is active for the currently active lexer condition state -_currentRules:function _currentRules() { +_currentRules:function _currentRules () { if (this.conditionStack.length && this.conditionStack[this.conditionStack.length - 1]) { return this.conditions[this.conditionStack[this.conditionStack.length - 1]].rules; } else { @@ -599,7 +590,7 @@ _currentRules:function _currentRules() { }, // return the currently active lexer condition state; when an index argument is provided it produces the N-th previous condition state, if available -topState:function topState(n) { +topState:function topState (n) { n = this.conditionStack.length - 1 - Math.abs(n || 0); if (n >= 0) { return this.conditionStack[n]; @@ -609,7 +600,7 @@ topState:function topState(n) { }, // alias for begin(condition) -pushState:function pushState(condition) { +pushState:function pushState (condition) { this.begin(condition); }, @@ -659,7 +650,7 @@ case 17:return 'INVALID' break; } }, -rules: [/^(?:[\s↵]+)/,/^(?:[\/\/].*\n)/,/^(?:(-?([0-9]|[1-9][0-9]+))(\.[0-9]+)?([eE][-+]?[0-9]+)?\b)/,/^(?:"(?:\\[\\"bfnrt\/]|\\u[a-fA-F0-9]{4}|[^\\\0-\x09\x0a-\x1f"])*")/,/^(?:'(?:\\[\\"bfnrt\/]|\\u[a-fA-F0-9]{4}|[^\\\0-\x09\x0a-\x1f'])*')/,/^(?:\{)/,/^(?:\})/,/^(?:\[)/,/^(?:\])/,/^(?:,)/,/^(?::)/,/^(?:true\b)/,/^(?:false\b)/,/^(?:null\b)/,/^(?:undefined\b)/,/^(?:[a-zA-Z0-9_\u4e00-\u9fa5]+)/,/^(?:$)/,/^(?:.)/], +rules: [/^(?:[\s↵]+)/,/^(?:[\/\/].*\n)/,/^(?:(-?([0-9]|[1-9][0-9]+))(\.[0-9]+)?([eE][-+]?[0-9]+)?\b)/,/^(?:"(?:\\[\\"]|[^\0-\x09\x0a-\x1f"])*")/,/^(?:'(?:\\[\\']|[^\0-\x09\x0a-\x1f'])*')/,/^(?:\{)/,/^(?:\})/,/^(?:\[)/,/^(?:\])/,/^(?:,)/,/^(?::)/,/^(?:true\b)/,/^(?:false\b)/,/^(?:null\b)/,/^(?:undefined\b)/,/^(?:[a-zA-Z0-9_\u4e00-\u9fa5]+)/,/^(?:$)/,/^(?:.)/], conditions: {"INITIAL":{"rules":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17],"inclusive":true}} }); return lexer; @@ -671,19 +662,21 @@ function Parser () { Parser.prototype = parser;parser.Parser = Parser; return new Parser; })(); -var root = this -// AMD / RequireJS -if (typeof define !== 'undefined' && define.amd) { - define([], function () { - return json_parser; - }); -} -// Node.js -else if (typeof module !== 'undefined' && module.exports) { - module.exports = json_parser; -} -// included directly via