Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade chevrotain #268

Merged
2 changes: 1 addition & 1 deletion packages/java-parser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"repository": "https://github.com/jhipster/prettier-java/tree/master/packages/java-parser",
"license": "Apache-2.0",
"dependencies": {
"chevrotain": "4.7.0",
"chevrotain": "6.5.0",
"lodash": "4.17.15"
},
"scripts": {
Expand Down
34 changes: 18 additions & 16 deletions packages/java-parser/src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,23 +172,25 @@ class JavaParser extends Parser {
}

BACKTRACK_LOOKAHEAD(production, errValue = false) {
this.isBackTrackingStack.push(1);
// 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);
} catch (e) {
if (isRecognitionException(e)) {
return errValue;
return this.ACTION(() => {
this.isBackTrackingStack.push(1);
// 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);
} catch (e) {
if (isRecognitionException(e)) {
return errValue;
}
throw e;
} finally {
this.outputCst = true;
this.reloadRecogState(orgState);
this.isBackTrackingStack.pop();
}
throw e;
} finally {
this.outputCst = true;
this.reloadRecogState(orgState);
this.isBackTrackingStack.pop();
}
});
}

setIgnoredComments(comments) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ function defineRules($, t) {
const isLocalVariableDeclaration = this.BACKTRACK_LOOKAHEAD(
$.isLocalVariableDeclaration
);

$.OR([
{
GATE: () => isLocalVariableDeclaration,
Expand Down
27 changes: 11 additions & 16 deletions packages/java-parser/src/productions/classes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

const { isRecognitionException, tokenMatcher } = require("chevrotain");
const { tokenMatcher } = require("chevrotain");

function defineRules($, t) {
// https://docs.oracle.com/javase/specs/jls/se11/html/jls-8.html#jls-ClassDeclaration
Expand Down Expand Up @@ -662,24 +662,19 @@ function defineRules($, t) {
return false;
}

try {
// The {classModifier} is a super grammar of the "interfaceModifier"
// So we must parse all the "{classModifier}" before we can distinguish
// between the alternatives.
$.MANY({
GATE: () =>
(tokenMatcher($.LA(1).tokenType, t.At) &&
tokenMatcher($.LA(2).tokenType, t.Interface)) === false,
DEF: () => {
let continueLoop = true;
while (continueLoop) {
if (
(tokenMatcher($.LA(1).tokenType, t.At) &&
tokenMatcher($.LA(2).tokenType, t.Interface)) === false
) {
try {
$.SUBRULE($.classModifier);
} catch (e) {
continueLoop = false;
}
});
} catch (e) {
if (isRecognitionException(e)) {
// TODO: add original syntax error?
throw "Cannot Identify if the <TypeDeclaration> is a <ClassDeclaration> or an <InterfaceDeclaration>";
} else {
throw e;
continueLoop = false;
}
clementdessoude marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down
31 changes: 15 additions & 16 deletions packages/java-parser/src/productions/expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -604,21 +604,19 @@ function defineRules($, t) {
return true;
});

let firstForUnaryExpressionNotPlusMinus = undefined;
$.RULE("isReferenceTypeCastExpression", () => {
if (firstForUnaryExpressionNotPlusMinus === undefined) {
const firstUnaryExpressionNotPlusMinus = this.computeContentAssist(
"unaryExpressionNotPlusMinus",
[]
);
const nextTokTypes = firstUnaryExpressionNotPlusMinus.map(
x => x.nextTokenType
);
// uniq
firstForUnaryExpressionNotPlusMinus = nextTokTypes.filter(
(v, i, a) => a.indexOf(v) === i
);
}
const firstUnaryExpressionNotPlusMinus = this.computeContentAssist(
clementdessoude marked this conversation as resolved.
Show resolved Hide resolved
"unaryExpressionNotPlusMinus",
[]
);
const nextTokTypes = firstUnaryExpressionNotPlusMinus.map(
x => x.nextTokenType
);
// uniq
const firstForUnaryExpressionNotPlusMinus = nextTokTypes.filter(
(v, i, a) => a.indexOf(v) === i
);

$.CONSUME(t.LBrace);
$.SUBRULE($.referenceType);
$.MANY(() => {
Expand Down Expand Up @@ -653,10 +651,11 @@ function defineRules($, t) {
}

// in the middle of a "classReferenceType"
$.OPTION2(() => {
try {
clementdessoude marked this conversation as resolved.
Show resolved Hide resolved
$.CONSUME(t.Dot);
$.SUBRULE($.classOrInterfaceType);
});
// eslint-disable-next-line no-empty
} catch (e) {}

const firstTokTypeAfterRefType = this.LA(1).tokenType;
return tokenMatcher(firstTokTypeAfterRefType, t.ColonColon);
Expand Down
3 changes: 3 additions & 0 deletions packages/java-parser/src/productions/interfaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ function defineRules($, t) {
const detectedType = this.BACKTRACK_LOOKAHEAD(
$.identifyInterfaceBodyDeclarationType
);

$.OR([
{
GATE: () => detectedType === InterfaceBodyTypes.constantDeclaration,
Expand Down Expand Up @@ -170,6 +171,7 @@ function defineRules($, t) {
const detectedType = this.BACKTRACK_LOOKAHEAD(
$.identifyAnnotationBodyDeclarationType
);

$.OR([
{
GATE: () =>
Expand Down Expand Up @@ -274,6 +276,7 @@ function defineRules($, t) {
const isSimpleElementValueAnnotation = this.BACKTRACK_LOOKAHEAD(
$.isSimpleElementValueAnnotation
);

$.OR([
// Spec Deviation: "conditionalExpression" replaced with "expression"
// Because we cannot differentiate between the two using fixed lookahead.
Expand Down
3 changes: 3 additions & 0 deletions packages/java-parser/src/productions/packages-and-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ function defineRules($, t) {
$.RULE("compilationUnit", () => {
// custom optimized backtracking lookahead logic
const isModule = $.BACKTRACK_LOOKAHEAD($.isModuleCompilationUnit);

$.OR([
{
GATE: () => isModule === false,
Expand Down Expand Up @@ -98,6 +99,7 @@ function defineRules($, t) {
$.RULE("typeDeclaration", () => {
// TODO: consider extracting the prefix modifiers here to avoid backtracking
const isClassDeclaration = this.BACKTRACK_LOOKAHEAD($.isClassDeclaration);

$.OR([
{
GATE: () => isClassDeclaration,
Expand Down Expand Up @@ -243,6 +245,7 @@ function defineRules($, t) {
throw e;
}
}

const nextTokenType = this.LA(1).tokenType;
return (
tokenMatcher(nextTokenType, t.Open) ||
Expand Down
2 changes: 1 addition & 1 deletion packages/prettier-plugin-java/src/printers/expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ class ExpressionsPrettierVisitor {
for (let i = 0; i < subgroup.length; i++) {
const token = subgroup[i];
const shiftOperator = isShiftOperator(subgroup, i);
if (token.tokenType.tokenName === "Instanceof") {
if (token.tokenType.name === "Instanceof") {
currentSegment.push(
rejectAndJoin(" ", [ctx.Instanceof[0], referenceType.shift()])
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function getImageWithComments(token) {
if (element.startLine !== token.startLine) {
arr.push(concat(formatComment(element)));
arr.push(hardLineWithoutBreakParent);
} else if (element.tokenType.tokenName === "LineComment") {
} else if (element.tokenType.name === "LineComment") {
// Do not add extra space in case of empty statement
const separator = token.image === "" ? "" : " ";
arr.push(
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1517,10 +1517,10 @@ check-error@^1.0.2:
resolved "https://registry.yarnpkg.com/check-error/-/check-error-1.0.2.tgz#574d312edd88bb5dd8912e9286dd6c0aed4aac82"
integrity sha1-V00xLt2Iu13YkS6Sht1sCu1KrII=

chevrotain@4.7.0:
version "4.7.0"
resolved "https://registry.yarnpkg.com/chevrotain/-/chevrotain-4.7.0.tgz#1a1acab4275cee8b1e208e6a10b1622c5b92b02e"
integrity sha512-FmXTi1hMN/6TT9NlrYP1kHHW6P7qpVzQD2vGrbsfI7wbFXXg2ANpIaN3pHPnVx/ZPHOnConE0TmyP6rguyzkZQ==
chevrotain@6.5.0:
version "6.5.0"
resolved "https://registry.yarnpkg.com/chevrotain/-/chevrotain-6.5.0.tgz#dcbef415516b0af80fd423cc0d96b28d3f11374e"
integrity sha512-BwqQ/AgmKJ8jcMEjaSnfMybnKMgGTrtDKowfTP3pX4jwVy0kNjRsT/AP6h+wC3+3NC+X8X15VWBnTCQlX+wQFg==
dependencies:
regexp-to-ast "0.4.0"

Expand Down