From 9de4cf4da298d44bd25bcafd07009871e679d286 Mon Sep 17 00:00:00 2001 From: XR-stb <2322882096@qq.com> Date: Tue, 14 Oct 2025 17:06:54 +0800 Subject: [PATCH 1/2] fix: skip extra semicolons --- src/parse.js | 120 +++++++++++++++++++++++++++++---------------------- 1 file changed, 68 insertions(+), 52 deletions(-) diff --git a/src/parse.js b/src/parse.js index 9c3cc2753..1fe6bc8ce 100644 --- a/src/parse.js +++ b/src/parse.js @@ -169,21 +169,21 @@ function parse(source, root, options) { } while (skip(",", true)); var dummy = {options: undefined}; dummy.setOption = function(name, value) { - if (this.options === undefined) this.options = {}; - this.options[name] = value; + if (this.options === undefined) this.options = {}; + this.options[name] = value; }; ifBlock( dummy, function parseRange_block(token) { - /* istanbul ignore else */ - if (token === "option") { - parseOption(dummy, token); // skip - skip(";"); - } else - throw illegal(token); + /* istanbul ignore else */ + if (token === "option") { + parseOption(dummy, token); // skip + skip(";"); + } else + throw illegal(token); }, function parseRange_line() { - parseInlineOptions(dummy); // skip + parseInlineOptions(dummy); // skip }); } @@ -267,7 +267,7 @@ function parse(source, root, options) { break; case "public": next(); - // eslint-disable-next-line no-fallthrough + // eslint-disable-next-line no-fallthrough default: whichImports = imports || (imports = []); break; @@ -332,7 +332,7 @@ function parse(source, root, options) { var trailingLine = tn.line; if (obj) { if(typeof obj.comment !== "string") { - obj.comment = cmnt(); // try block-type comment + obj.comment = cmnt(); // try block-type comment } obj.filename = parse.filename; } @@ -398,6 +398,10 @@ function parse(source, root, options) { readRanges(type.reserved || (type.reserved = []), true); break; + case ";": + // Skip extra semicolons + break; + default: /* istanbul ignore if */ if (edition === "proto2" || !typeRefRe.test(token)) { @@ -527,13 +531,17 @@ function parse(source, root, options) { readRanges(type.reserved || (type.reserved = []), true); break; + case ";": + // Skip extra semicolons + break; + /* istanbul ignore next */ default: throw illegal(token); // there are no groups with proto3 semantics } }); parent.add(type) - .add(field); + .add(field); } function parseMapField(parent) { @@ -586,6 +594,8 @@ function parse(source, root, options) { if (token === "option") { parseOption(oneof, token); skip(";"); + } else if (token === ";") { + // Skip extra semicolons } else { push(token); parseField(oneof, "optional"); @@ -603,19 +613,23 @@ function parse(source, root, options) { var enm = new Enum(token); ifBlock(enm, function parseEnum_block(token) { switch(token) { - case "option": - parseOption(enm, token); - skip(";"); - break; + case "option": + parseOption(enm, token); + skip(";"); + break; - case "reserved": - readRanges(enm.reserved || (enm.reserved = []), true); + case "reserved": + readRanges(enm.reserved || (enm.reserved = []), true); if(enm.reserved === undefined) enm.reserved = []; - break; + break; - default: - parseEnumValue(enm, token); - } + case ";": + // Skip extra semicolons + break; + + default: + parseEnumValue(enm, token); + } }); parent.add(enm); if (parent === ptr) { @@ -659,38 +673,38 @@ function parse(source, root, options) { } function parseOption(parent, token) { - var option; - var propName; - var isOption = true; - if (token === "option") { - token = next(); - } + var option; + var propName; + var isOption = true; + if (token === "option") { + token = next(); + } - while (token !== "=") { - if (token === "(") { - var parensValue = next(); - skip(")"); - token = "(" + parensValue + ")"; - } - if (isOption) { - isOption = false; - if (token.includes(".") && !token.includes("(")) { - var tokens = token.split("."); - option = tokens[0] + "."; - token = tokens[1]; - continue; - } - option = token; - } else { - propName = propName ? propName += token : token; + while (token !== "=") { + if (token === "(") { + var parensValue = next(); + skip(")"); + token = "(" + parensValue + ")"; + } + if (isOption) { + isOption = false; + if (token.includes(".") && !token.includes("(")) { + var tokens = token.split("."); + option = tokens[0] + "."; + token = tokens[1]; + continue; } - token = next(); + option = token; + } else { + propName = propName ? propName += token : token; } - var name = propName ? option.concat(propName) : option; - var optionValue = parseOptionValue(parent, name); - propName = propName && propName[0] === "." ? propName.slice(1) : propName; - option = option && option[option.length - 1] === "." ? option.slice(0, -1) : option; - setParsedOption(parent, option, optionValue, propName); + token = next(); + } + var name = propName ? option.concat(propName) : option; + var optionValue = parseOptionValue(parent, name); + propName = propName && propName[0] === "." ? propName.slice(1) : propName; + option = option && option[option.length - 1] === "." ? option.slice(0, -1) : option; + setParsedOption(parent, option, optionValue, propName); } function parseOptionValue(parent, name) { @@ -704,7 +718,7 @@ function parse(source, root, options) { throw illegal(token, "name"); } if (token === null) { - throw illegal(token, "end of input"); + throw illegal(token, "end of input"); } var value; @@ -795,6 +809,8 @@ function parse(source, root, options) { /* istanbul ignore else */ if (token === "rpc") parseMethod(service, token); + else if (token === ";") + ; // Skip extra semicolons else throw illegal(token); }); From b135b7dfd1193372e4d6fd65fddd959ede66b3e7 Mon Sep 17 00:00:00 2001 From: XR-stb <2322882096@qq.com> Date: Tue, 14 Oct 2025 17:55:23 +0800 Subject: [PATCH 2/2] feat: recover format --- src/parse.js | 110 +++++++++++++++++++++++++-------------------------- 1 file changed, 55 insertions(+), 55 deletions(-) diff --git a/src/parse.js b/src/parse.js index 1fe6bc8ce..e82f1b559 100644 --- a/src/parse.js +++ b/src/parse.js @@ -169,21 +169,21 @@ function parse(source, root, options) { } while (skip(",", true)); var dummy = {options: undefined}; dummy.setOption = function(name, value) { - if (this.options === undefined) this.options = {}; - this.options[name] = value; + if (this.options === undefined) this.options = {}; + this.options[name] = value; }; ifBlock( dummy, function parseRange_block(token) { - /* istanbul ignore else */ - if (token === "option") { - parseOption(dummy, token); // skip - skip(";"); - } else - throw illegal(token); + /* istanbul ignore else */ + if (token === "option") { + parseOption(dummy, token); // skip + skip(";"); + } else + throw illegal(token); }, function parseRange_line() { - parseInlineOptions(dummy); // skip + parseInlineOptions(dummy); // skip }); } @@ -267,7 +267,7 @@ function parse(source, root, options) { break; case "public": next(); - // eslint-disable-next-line no-fallthrough + // eslint-disable-next-line no-fallthrough default: whichImports = imports || (imports = []); break; @@ -332,7 +332,7 @@ function parse(source, root, options) { var trailingLine = tn.line; if (obj) { if(typeof obj.comment !== "string") { - obj.comment = cmnt(); // try block-type comment + obj.comment = cmnt(); // try block-type comment } obj.filename = parse.filename; } @@ -541,7 +541,7 @@ function parse(source, root, options) { } }); parent.add(type) - .add(field); + .add(field); } function parseMapField(parent) { @@ -613,23 +613,23 @@ function parse(source, root, options) { var enm = new Enum(token); ifBlock(enm, function parseEnum_block(token) { switch(token) { - case "option": - parseOption(enm, token); - skip(";"); - break; + case "option": + parseOption(enm, token); + skip(";"); + break; - case "reserved": - readRanges(enm.reserved || (enm.reserved = []), true); + case "reserved": + readRanges(enm.reserved || (enm.reserved = []), true); if(enm.reserved === undefined) enm.reserved = []; - break; + break; - case ";": - // Skip extra semicolons - break; + case ";": + // Skip extra semicolons + break; - default: - parseEnumValue(enm, token); - } + default: + parseEnumValue(enm, token); + } }); parent.add(enm); if (parent === ptr) { @@ -673,38 +673,38 @@ function parse(source, root, options) { } function parseOption(parent, token) { - var option; - var propName; - var isOption = true; - if (token === "option") { - token = next(); - } - - while (token !== "=") { - if (token === "(") { - var parensValue = next(); - skip(")"); - token = "(" + parensValue + ")"; + var option; + var propName; + var isOption = true; + if (token === "option") { + token = next(); } - if (isOption) { - isOption = false; - if (token.includes(".") && !token.includes("(")) { - var tokens = token.split("."); - option = tokens[0] + "."; - token = tokens[1]; - continue; + + while (token !== "=") { + if (token === "(") { + var parensValue = next(); + skip(")"); + token = "(" + parensValue + ")"; } - option = token; - } else { - propName = propName ? propName += token : token; + if (isOption) { + isOption = false; + if (token.includes(".") && !token.includes("(")) { + var tokens = token.split("."); + option = tokens[0] + "."; + token = tokens[1]; + continue; + } + option = token; + } else { + propName = propName ? propName += token : token; + } + token = next(); } - token = next(); - } - var name = propName ? option.concat(propName) : option; - var optionValue = parseOptionValue(parent, name); - propName = propName && propName[0] === "." ? propName.slice(1) : propName; - option = option && option[option.length - 1] === "." ? option.slice(0, -1) : option; - setParsedOption(parent, option, optionValue, propName); + var name = propName ? option.concat(propName) : option; + var optionValue = parseOptionValue(parent, name); + propName = propName && propName[0] === "." ? propName.slice(1) : propName; + option = option && option[option.length - 1] === "." ? option.slice(0, -1) : option; + setParsedOption(parent, option, optionValue, propName); } function parseOptionValue(parent, name) { @@ -718,7 +718,7 @@ function parse(source, root, options) { throw illegal(token, "name"); } if (token === null) { - throw illegal(token, "end of input"); + throw illegal(token, "end of input"); } var value;