From 37dddc48d946963a670b29a9faa1be72ebe21be1 Mon Sep 17 00:00:00 2001 From: Dave Holoway Date: Sun, 7 Jun 2020 13:26:46 +0100 Subject: [PATCH] update check for cast expression --- langserver/java/body-parser3.js | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/langserver/java/body-parser3.js b/langserver/java/body-parser3.js index a76eda2..c035611 100644 --- a/langserver/java/body-parser3.js +++ b/langserver/java/body-parser3.js @@ -1378,6 +1378,34 @@ function isExpressionStart(token) { return /^(ident|primitive-type|[\w-]+-literal|(inc|plumin|unary)-operator|open-bracket|new-operator)$/.test(token.kind); } +/** + * @param {Token} token first token following the close bracket + * @param {ResolvedIdent} matches - the bracketed expression + */ +function isCastExpression(token, matches) { + // working out if this is supposed to be a cast expression is problematic. + // (a) + b -> cast or binary expression (depends on how a is resolved) + // if the bracketed expression cannot be resolved: + // (a) b -> assumed to be a cast + // (a) + b -> assumed to be an expression + // (a) 5 -> assumed to be a cast + // (a) + 5 -> assumed to be an expression + if (matches.types[0] && !(matches.types[0] instanceof AnyType)) { + // resolved type - this must be a cast + return true; + } + if (!matches.types[0]) { + // not a type - this must be an expression + return false; + } + // if we reach here, the type is AnyType - we assume a cast if the next + // value is the start of an expression, except for +/- + if (token.kind === 'plumin-operator') { + return false; + } + return this.isExpressionStart(token); +} + /** * @param {TokenList} tokens * @param {Local[]} locals @@ -1478,7 +1506,7 @@ function rootTerm(tokens, locals, method, imports, typemap) { matches = expression(tokens, locals, method, imports, typemap); const close_bracket = tokens.current; tokens.expectValue(')'); - if (isExpressionStart(tokens.current)) { + if (isCastExpression(tokens.current, matches)) { // typecast const type = matches.types[0]; if (!type) {