From 3591437e808694c158d96cc083fe73d6e14106b0 Mon Sep 17 00:00:00 2001 From: Jordan Kiesel Date: Sun, 5 Nov 2023 17:58:10 -0700 Subject: [PATCH] chore: upgrade to Chevrotain 11 closes #423 --- packages/java-parser/package.json | 2 +- packages/java-parser/scripts/gen-diagrams.js | 2 +- packages/java-parser/src/parser.js | 38 ++++++++------ packages/prettier-plugin-java/package.json | 2 +- yarn.lock | 52 ++++++++++++++++++-- 5 files changed, 75 insertions(+), 21 deletions(-) diff --git a/packages/java-parser/package.json b/packages/java-parser/package.json index 93ccaae8..4b323148 100644 --- a/packages/java-parser/package.json +++ b/packages/java-parser/package.json @@ -8,7 +8,7 @@ "license": "Apache-2.0", "types": "./api.d.ts", "dependencies": { - "chevrotain": "6.5.0", + "chevrotain": "11.0.3", "lodash": "4.17.21" }, "scripts": { diff --git a/packages/java-parser/scripts/gen-diagrams.js b/packages/java-parser/scripts/gen-diagrams.js index 77f809f2..e257289e 100644 --- a/packages/java-parser/scripts/gen-diagrams.js +++ b/packages/java-parser/scripts/gen-diagrams.js @@ -10,7 +10,7 @@ */ import path from "path"; import fs from "fs"; -import chevrotain from "chevrotain"; +import * as chevrotain from "chevrotain"; import url from "url"; import JavaParser from "../src/parser.js"; diff --git a/packages/java-parser/src/parser.js b/packages/java-parser/src/parser.js index 88335fd7..04a1a729 100644 --- a/packages/java-parser/src/parser.js +++ b/packages/java-parser/src/parser.js @@ -1,4 +1,4 @@ -import { Parser, isRecognitionException } from "chevrotain"; +import { CstParser, isRecognitionException } from "chevrotain"; import { allTokens, tokens as t } from "./tokens.js"; import * as lexicalStructure from "./productions/lexical-structure.js"; import * as typesValuesVariables from "./productions/types-values-and-variables.js"; @@ -35,7 +35,7 @@ import { shouldNotFormat } from "./comments.js"; * TODO: document guide lines for using back tracking * */ -export default class JavaParser extends Parser { +export default class JavaParser extends CstParser { constructor() { super(allTokens, { maxLookahead: 1, @@ -76,16 +76,16 @@ export default class JavaParser extends Parser { } cstPostNonTerminal(ruleCstResult, ruleName) { + if (this.isBackTracking()) { + return; + } super.cstPostNonTerminal(ruleCstResult, ruleName); - if (this.isBackTracking() === false) { - this.mostEnclosiveCstNodeByStartOffset[ - ruleCstResult.location.startOffset - ] = ruleCstResult; - this.mostEnclosiveCstNodeByEndOffset[ruleCstResult.location.endOffset] = - ruleCstResult; + this.mostEnclosiveCstNodeByStartOffset[ruleCstResult.location.startOffset] = + ruleCstResult; + this.mostEnclosiveCstNodeByEndOffset[ruleCstResult.location.endOffset] = + ruleCstResult; - shouldNotFormat(ruleCstResult, this.onOffCommentPairs); - } + shouldNotFormat(ruleCstResult, this.onOffCommentPairs); } BACKTRACK_LOOKAHEAD(production, errValue = false) { @@ -94,16 +94,26 @@ export default class JavaParser extends Parser { // TODO: "saveRecogState" does not handle the occurrence stack const orgState = this.saveRecogState(); try { - // hack to enable outputting none CST values from grammar rules. - this.outputCst = false; - return production.call(this); + // hack to enable outputting non-CST values from grammar rules. + const { ruleName, originalGrammarAction } = production; + try { + this.ruleInvocationStateUpdate( + this.fullRuleNameToShort[ruleName], + ruleName, + this.subruleIdx + ); + return originalGrammarAction.call(this); + } catch (e) { + return this.invokeRuleCatch(e, true, () => undefined); + } finally { + this.ruleFinallyStateUpdate(); + } } catch (e) { if (isRecognitionException(e)) { return errValue; } throw e; } finally { - this.outputCst = true; this.reloadRecogState(orgState); this.isBackTrackingStack.pop(); } diff --git a/packages/prettier-plugin-java/package.json b/packages/prettier-plugin-java/package.json index ee1b7e92..02e38bf1 100644 --- a/packages/prettier-plugin-java/package.json +++ b/packages/prettier-plugin-java/package.json @@ -26,7 +26,7 @@ "build:watch": "tsc --inlineSourceMap -w" }, "devDependencies": { - "@chevrotain/types": "9.0.2", + "@chevrotain/types": "11.0.3", "@types/chai": "4.3.4", "@types/fs-extra": "9.0.13", "@types/jest": "29.2.3", diff --git a/yarn.lock b/yarn.lock index db1a5d88..7177688a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -194,10 +194,37 @@ "@babel/helper-validator-identifier" "^7.22.20" to-fast-properties "^2.0.0" -"@chevrotain/types@9.0.2": - version "9.0.2" - resolved "https://registry.yarnpkg.com/@chevrotain/types/-/types-9.0.2.tgz#477bb3b973b91ff47377399d1e9f4410be6c0141" - integrity sha512-lo1dQPX7DQffJb26eaYLEy4/jUTFmsGKa43mDvMNAHwItEgUQHUkTZR0iAkHG0aJv8ejM/KqYpRVSNetrOK8qw== +"@chevrotain/cst-dts-gen@11.0.3": + version "11.0.3" + resolved "https://registry.yarnpkg.com/@chevrotain/cst-dts-gen/-/cst-dts-gen-11.0.3.tgz#5e0863cc57dc45e204ccfee6303225d15d9d4783" + integrity sha512-BvIKpRLeS/8UbfxXxgC33xOumsacaeCKAjAeLyOn7Pcp95HiRbrpl14S+9vaZLolnbssPIUuiUd8IvgkRyt6NQ== + dependencies: + "@chevrotain/gast" "11.0.3" + "@chevrotain/types" "11.0.3" + lodash-es "4.17.21" + +"@chevrotain/gast@11.0.3": + version "11.0.3" + resolved "https://registry.yarnpkg.com/@chevrotain/gast/-/gast-11.0.3.tgz#e84d8880323fe8cbe792ef69ce3ffd43a936e818" + integrity sha512-+qNfcoNk70PyS/uxmj3li5NiECO+2YKZZQMbmjTqRI3Qchu8Hig/Q9vgkHpI3alNjr7M+a2St5pw5w5F6NL5/Q== + dependencies: + "@chevrotain/types" "11.0.3" + lodash-es "4.17.21" + +"@chevrotain/regexp-to-ast@11.0.3": + version "11.0.3" + resolved "https://registry.yarnpkg.com/@chevrotain/regexp-to-ast/-/regexp-to-ast-11.0.3.tgz#11429a81c74a8e6a829271ce02fc66166d56dcdb" + integrity sha512-1fMHaBZxLFvWI067AVbGJav1eRY7N8DDvYCTwGBiE/ytKBgP8azTdgyrKyWZ9Mfh09eHWb5PgTSO8wi7U824RA== + +"@chevrotain/types@11.0.3": + version "11.0.3" + resolved "https://registry.yarnpkg.com/@chevrotain/types/-/types-11.0.3.tgz#f8a03914f7b937f594f56eb89312b3b8f1c91848" + integrity sha512-gsiM3G8b58kZC2HaWR50gu6Y1440cHiJ+i3JUvcp/35JchYejb2+5MVeJK0iKThYpAa/P2PYFV4hoi44HD+aHQ== + +"@chevrotain/utils@11.0.3": + version "11.0.3" + resolved "https://registry.yarnpkg.com/@chevrotain/utils/-/utils-11.0.3.tgz#e39999307b102cff3645ec4f5b3665f5297a2224" + integrity sha512-YslZMgtJUyuMbZ+aKvfF3x1f5liK4mWNxghFRv7jqRR9C3R3fAOGTTKvxXDa2Y1s9zSbcpuO0cAxDYsc9SrXoQ== "@cspotcode/source-map-support@^0.8.0": version "0.8.1" @@ -2017,6 +2044,18 @@ check-error@^1.0.3: dependencies: get-func-name "^2.0.2" +chevrotain@11.0.3: + version "11.0.3" + resolved "https://registry.yarnpkg.com/chevrotain/-/chevrotain-11.0.3.tgz#88ffc1fb4b5739c715807eaeedbbf200e202fc1b" + integrity sha512-ci2iJH6LeIkvP9eJW6gpueU8cnZhv85ELY8w8WiFtNjMHA5ad6pQLaJo9mEly/9qUyCpvqX8/POVUTf18/HFdw== + dependencies: + "@chevrotain/cst-dts-gen" "11.0.3" + "@chevrotain/gast" "11.0.3" + "@chevrotain/regexp-to-ast" "11.0.3" + "@chevrotain/types" "11.0.3" + "@chevrotain/utils" "11.0.3" + lodash-es "4.17.21" + chevrotain@6.5.0: version "6.5.0" resolved "https://registry.yarnpkg.com/chevrotain/-/chevrotain-6.5.0.tgz#dcbef415516b0af80fd423cc0d96b28d3f11374e" @@ -4023,6 +4062,11 @@ locate-path@^6.0.0: dependencies: p-locate "^5.0.0" +lodash-es@4.17.21: + version "4.17.21" + resolved "https://registry.yarnpkg.com/lodash-es/-/lodash-es-4.17.21.tgz#43e626c46e6591b7750beb2b50117390c609e3ee" + integrity sha512-mKnC+QJ9pWVzv+C4/U3rRsHapFfHvQFoFB92e52xeyGMcX6/OlIl78je1u8vePzYZSkkogMPJ2yjxxsb89cxyw== + lodash.flattendeep@^4.4.0: version "4.4.0" resolved "https://registry.yarnpkg.com/lodash.flattendeep/-/lodash.flattendeep-4.4.0.tgz#fb030917f86a3134e5bc9bec0d69e0013ddfedb2"