Files
android-dev-ext/src/expression/parse.js
Dave Holoway 83eda790be version 1.2 (#93)
* initial working language server
* first hacky version of source parsing and type checking
* first iteration of method body parser
* add support for prefix/postfix inc expressions
* add basic support for parsing new expressions
* different attempt to parse using collapsable text ranges
* fix parsing of binary operstors following a bracket expression
* updated validation to use new JavaTypes module instead of MTIs
* add support for array-literal expressions
* fix || and && not being tokenized as operators allow float literals starting with dot
* add new method body parser to use direct linear parsing
* add super as an object literal
* fix interface constructors check constructor type modifiers
* fix assignment operator types
* Fix resolving of enclosed type identifiers
* add default constructor for class types with no explicit constructors
* add missing constructor validator
* add constructor parameters to list of resolvable types
* update SourceMethod to pass name in super constructor
* add Any* classes to reduce cascading errors
* update method call parameter checking use isTypeAssignable instead of getParameterCompatibleTypeSignatures
* tidy up isTypeAssignable allow class equivilents for primitives
* add more info when methods/ctrs cannot be matched
* allow interfaces to be cast to class instances
* use isTypeAssignable for checking branch test expressions
* allow AnyValue to be a constant value
* split shift operators from bitwise operators
* add support for literal numbers to be assignable to multiple primtive types
* clear diagnostics when document is closed
* update check for cast expression
* casting only applies to qualified term not a whole expression
* allow all primitive-number-type casts
* add support for synchronized statement
* update primitive type compatibility
* allow null to be cast to any non-primitve
* use better regex for string literals
* allow character literals to be assigned to number types
* add support for array qualifiers after a variable name
* make sure any long specifier is stripped from a bigint value
* improve invalid array expression message add AnyType array element to prevent cascading errors
* make default a modifer keyword for interface default method support
* initial support for wildcard type arguments
* fix parse issue with nested generic types
* allow generic types to be assigned to inherited types with compatible type arguments
* allow unicode characters, $ and _ in identifiers
* map primitive types to their boxed versions for class member
* support assert statement
* allow unicode char literals
* make type parser and body parser use same tokenizer
* reuse parsed tokens instead of tokenizing each method body
* re-add throws as a keyword
* treat default and synchronized as modifiers
* add SourceInitialiser support
* refactor to prepare for merging with type parsing
* add support for array qualifiers in type identifiers
* pass scoped type instead of method to typeIdent
* update ResolvableType to use same type resolving as method body parsing
* add support for post-name array qualifiers in fields and parameters
* post-name array qualifiers in method decls
* add type variables to SourceMethod
* initial attempt to support type variable arguments in methods
* specialise methods with type variables
* don't require default interface methods to be implemented
* make variable arity parameters an array type
* tidy array constructors and fix some warnings
* update isCallCompatible to handle variable arity calls
* improve assert statement support
* parse labels and break/continue targets
* refactor new term qualifiers
* add support for generic inferred-type arguments
* improve modifier checks for interface types
* improve reporting of unresolved type errors
* fix type checking of field and method declarations
* add missng strictfp modifier
* refactor in preparation for parsing local types
* replace Locals with scopeable MethodDeclarations to allow labels and types to be stored
* initial changes to support local type declarations
* update to use new set of SourceX classes
* refactor to allow expressions to have a type scope
* replace regex parsing with linear parsing
* generate source types before parsing
* fix support for resolving type variables in method declarations
* fix checking of array literal compatability
* report errors from unit parsing
* remove local modifier validation during parse add parameter modifier checking to validation
* allow trailing comma for array literals
* start separating validation from parsing
* add support for parsing enum values
* allow uppercase 0X in hex literals
* include enclosing types in identifier search
* add support for parsing parameterless lambdas
* ignore unresolved types in extends/implements
* implement specialisation of SourceType
* allow super as a member qualifier
* allow empty enums
* don't report missing constructors if superclass has none
* update typemap declarations to use CEIType instead of JavaType
* fix resolving of class type variables
* fix bad imports when resolving annotations
* allow null scope in findIdentifier
* add support for static member imports
* import types from same package
* remove this qualifier from isCastExpression
* add hex exponent support
* parse try-with-resources
* fix resolving imported enclosed types
* extract expression types into separate files
* extract statement types into separate files
* fix type warnings
* extract literals into separate files
* remove Value class, add NewExpression and separate out Any classes
* rename source types module
* remove some parse checks that should be in verify
* support token extraction in expressions
* implement resolveExpression
* add type cast checking
* check for valid type in class member expressions
* allow assigns for assignable type arguments
* improve reporting of unresolved identifiers
* add new array validation
* validate array literals
* validate array indexes
* improve validation of binary operators
* rename ResolvedType  to ResolvedValue
* improve checking of number literals
* support package name as a resolved value
* implement method body and ststement validation
* improve method call resolving
* add support for this() and super() constructor calls
* remove return type for source constructors
* add checks for unary operators
* ensure tokens are assigned for qualified expressions
* check castability using type assignments
* add implicit enum methods values() and valueOf()
* add basic type checking of lambda expressions
* fix return type check
* fix assert statement checks
* improve support for ternary operators in assignments and method invocations
* perform more detailed search of implemented methods
* initial test of context-dependant code completion
* support package, type and static field import completion
* support for member expressions
* use exact type signatures for locating types for completion items
* add support for field and method docs
* add support for docs in source types
* support member completion for array types improve comment formatting
* ensure Object is always last in the list of inherited types
* add owning method to statements create common keyword statement class
* improve code completion list add method parameters order list items by scope
* add source types to list hide this and super for non-methods
* fix bad member resolution at end of block fix missing method and type docs
* add support for editing multiple files
* allow multiple source files to be used in parsing
* load and parse files at startup
* add support for displaying method signatures
* add single trace function with timestamps
* implement shceduleReparse to reduce parsing load while typing
* remove parsed type list logging
* wait for reparsing before returning method signatures
* resolve new object contructors
* improve extraction of parameter docs
* update @types/vscode
* cache decoded android library in globalStoragePath
* load single android library cache from local folder
* android-29 library cache
* allow configurable app root setting
* set configurable trace logging and update section names
* description updates
* handle null token passed to ParseProblem
* refactoring
* Rename language client extension to Android
* ignore unnamed type declarations
* handle java file change notifications
* make sure we only try and parse java files
* add option to allow language server to be shutdown
* simplify handling of this and class member qualifiers
* relocate java-mti package into project
* get main node install to install langserver dependencies
* remove debugging pause
* rename body-parser3 to body-parser
* clean up import resolving code
* remove unused field from ResolvedImport
* remove validation modules that used old parser types
* remove old parser files
* remove redundant types and functions used by old parser
* move addproblem into TokenList
* remove unused ResolvedType class
* validate more statements
* add support for parsing and validating anonymous types
* hide some method modifiers which aren't useful to show
* code comments and minor improvements
* fix some type warnings
* improve support for completion of enum values
* add type name to parameter completion labels
* ignore synthetic members in completion list
* use a specialised map for handling case-insenstive file uris
* add basic build script
* reference java-mti package from GitHub
* revert @types/vscode
* update initial file loading to use URIs passed from the client changes to the appSourceRoot now require an extension restart
* add support for loading filtered androidx libraries for code completion
* update version of java-mti
* add mixpanel package
* add basic analytics
* fix dependency versions
* fix dependency versions
* set empty cache file markers
* add language server debug config
* add file to build script
* add unqualified type members when inside a method
* apply statics filter to enum values
* add basic debugger analytics
* include current time in startup event
* add terminate reason to debugger
* update changelog and readme
2020-07-03 01:54:32 +01:00

527 lines
15 KiB
JavaScript

/**
* Operator precedence levels.
* Lower number = higher precedence.
* Operators with equal precedence are evaluated left-to-right.
*/
const operator_precedences = {
'*': 1, '%': 1, '/': 1,
'+': 2, '-': 2,
'<<': 3, '>>': 3, '>>>': 3,
'<': 4, '>': 4, '<=': 4, '>=': 4, 'instanceof': 4,
'==': 5, '!=': 5,
'&': 6, '^': 7, '|': 8,
'&&': 9, '||': 10,
'?': 11,
'=': 12,
}
const lowest_precedence = 13;
class ExpressionText {
/**
* @param {string} text
*/
constructor(text) {
this.expr = text;
this.precedence_stack = [lowest_precedence];
}
get current_precedence() {
return this.precedence_stack[0];
}
}
class ParsedExpression {
}
class RootExpression extends ParsedExpression {
/**
* @param {string} root_term
* @param {string} root_term_type
* @param {QualifierExpression[]} qualified_terms
*/
constructor(root_term, root_term_type, qualified_terms) {
super();
this.root_term = root_term;
this.root_term_type = root_term_type;
this.qualified_terms = qualified_terms;
}
}
class TypeCastExpression extends ParsedExpression {
/**
*
* @param {ParsedExpression} cast_type
* @param {ParsedExpression} rhs
*/
constructor(cast_type, rhs) {
super();
this.cast_type = cast_type;
this.rhs = rhs;
}
}
class BinaryOpExpression extends ParsedExpression {
/**
* @param {ParsedExpression} lhs
* @param {string} operator
* @param {ParsedExpression} rhs
*/
constructor(lhs, operator, rhs) {
super();
this.lhs = lhs;
this.operator = operator;
this.rhs = rhs;
}
}
class UnaryOpExpression extends ParsedExpression {
/**
* @param {string} operator
* @param {ParsedExpression} rhs
*/
constructor(operator, rhs) {
super();
this.operator = operator;
this.rhs = rhs;
}
}
class IncOpExpression extends ParsedExpression {
/**
* @param {'e++'|'e--'|'++e'|'--e'} which
* @param {ParsedExpression} expression
*/
constructor(which, expression) {
super();
this.which = which;
this.expression = expression;
}
}
class TernaryExpression extends ParsedExpression {
/**
* @param {ParsedExpression} condition
*/
constructor(condition) {
super();
this.condition = condition;
/** @type {ParsedExpression} */
this.ternary_true = null;
/** @type {ParsedExpression} */
this.ternary_false = null;
}
}
class QualifierExpression extends ParsedExpression {
}
class ArrayIndexExpression extends QualifierExpression {
/**
* @param {ParsedExpression} index_expression
*/
constructor(index_expression) {
super();
this.indexExpression = index_expression;
}
}
class MethodCallExpression extends QualifierExpression {
/** @type {ParsedExpression[]} */
arguments = [];
}
class MemberExpression extends QualifierExpression {
/**
* @param {string} name
*/
constructor(name) {
super();
this.name = name;
}
}
class BracketedExpression extends ParsedExpression {
constructor(expression, qualified_terms) {
super();
this.expression = expression;
this.qualified_terms = qualified_terms;
}
}
class ArrayLiteralExpression extends ParsedExpression {
elements = [];
}
class ParsedNewExpression extends ParsedExpression {}
class NewObjectExpression extends ParsedNewExpression {
/**
* @param {RootExpression} ctr_call
* @param {QualifierExpression[]} post_ctr_qualifiers
* @param {string} method_body
*/
constructor(ctr_call, post_ctr_qualifiers, method_body) {
super();
this.ctr_call = ctr_call;
this.qualified_terms = post_ctr_qualifiers;
this.method_body = method_body;
}
}
class NewArrayExpression extends ParsedNewExpression {
/**
* @param {RootExpression} type
* @param {ArrayIndexExpression[]} arrdim_initers
* @param {QualifierExpression[]} post_ctr_qualifiers
*/
constructor(type, arrdim_initers, post_ctr_qualifiers) {
super();
this.type = type;
this.arrdim_initers = arrdim_initers;
this.post_ctr_qualifiers = post_ctr_qualifiers;
}
}
/**
* Remove characters from the expression followed by any leading whitespace/comments
* @param {ExpressionText} e
* @param {number|string} length_or_text
*/
function strip(e, length_or_text) {
if (typeof length_or_text === 'string') {
if (!e.expr.startsWith(length_or_text)) {
return false;
}
length_or_text = length_or_text.length;
}
e.expr = e.expr.slice(length_or_text).trimLeft();
for (;;) {
const comment = e.expr.match(/(^\/\/.+)|(^\/\*[\d\D]*?\*\/)/);
if (!comment) break;
e.expr = e.expr.slice(comment[0].length).trimLeft();
}
return true;
}
/**
* @param {ExpressionText} e
* @returns {(MemberExpression|ArrayIndexExpression|MethodCallExpression)[]}
*/
function parse_qualified_terms(e) {
const res = [];
while (/^[([.]/.test(e.expr)) {
if (strip(e, '.')) {
// member access
const name_match = e.expr.match(/^:?[a-zA-Z_$][a-zA-Z0-9_$]*/); // allow : at start for :super and :msg
if (!name_match) {
return null;
}
const member = new MemberExpression(name_match[0]);
strip(e, member.name.length)
res.push(member);
}
else if (strip(e, '(')) {
// method call
const call = new MethodCallExpression();
if (!strip(e, ')')) {
for (let arg; ;) {
if ((arg = parse_expression(e)) === null) {
return null;
}
call.arguments.push(arg);
if (strip(e, ',')) continue;
if (strip(e, ')')) break;
return null;
}
}
res.push(call);
}
else if (strip(e, '[')) {
// array index
const index_expr = parse_expression(e);
if (index_expr === null) {
return null;
}
if (!strip(e, ']')) {
return null;
}
res.push(new ArrayIndexExpression(index_expr));
}
}
return res;
}
/**
* @param {ExpressionText} e
*/
function parseBracketOrCastExpression(e) {
if (!strip(e, '(')) {
return null;
}
let res = parse_expression(e);
if (!res) {
return null;
}
if (!strip(e, ')')) {
return null;
}
// note - a bracketed expression followed by another bracketed expression is assumed to be a cast:
// double d = (double)(float)5; - is ok
// XYZ xyz = (new XYZ)(1,2,3); - nope
// - this will still need to be resolved for +/- e.g (int)+5 vs (some.field)+5
if (/^[\w"'(!~]|^\.\d/.test(e.expr) && !/^!=/.test(e.expr)) {
// typecast
const castexpr = parse_expression(e);
if (!castexpr) {
return null;
}
return new TypeCastExpression(res, castexpr);
}
const qt = parse_qualified_terms(e);
return new BracketedExpression(res, qt);
}
/**
*
* @param {ExpressionText} e
* @param {string} unop
*/
function parseUnaryExpression(e, unop) {
strip(e, unop.length);
let res = parse_expression_term(e);
if (!res) {
return null;
}
const op = unop.replace(/\s+/g, '');
for (let i = op.length - 1; i >= 0; --i) {
res = new UnaryOpExpression(op[i], res);
}
return res;
}
/**
* @param {ExpressionText} e
* @param {RootExpression} ctr
* @param {QualifierExpression[]} ctr_qualifiers
* @param {QualifierExpression[]} post_ctr_qualifiers
*/
function parseNewObjectExpression(e, ctr, ctr_qualifiers, post_ctr_qualifiers) {
const ctr_call = new RootExpression(ctr.root_term, ctr.root_term_type, ctr_qualifiers);
let method_body = null;
if (!post_ctr_qualifiers.length) {
// if there are no qualifiers following the constructor, look for an anonymous method body
if (e.expr.startsWith('{')) {
// don't parse it - just scan for the closing brace
const brace_re = /\/\*[\d\D]*?\*\/|\/\/.*|".*?"|".*|'.'?|(\{)|(\})/g;
let balance = 0, body_end = e.expr.length;
for (let m; m = brace_re.exec(e.expr); ) {
if (m[1]) balance++;
else if (m[2] && (--balance === 0)) {
body_end = m.index + 1;
break;
}
}
method_body = e.expr.slice(0, body_end);
strip(e, method_body.length);
}
}
return new NewObjectExpression(ctr_call, post_ctr_qualifiers, method_body);
}
/**
* @param {ExpressionText} e
* @param {RootExpression} ctr
* @param {Number} first_array_qualifier_idx
*/
function parseNewArrayExpression(e, ctr, first_array_qualifier_idx) {
let arrdim_initers = [];
let i = first_array_qualifier_idx;
for (; i < ctr.qualified_terms.length; i++) {
const term = ctr.qualified_terms[i];
if (term instanceof ArrayIndexExpression) {
arrdim_initers.push(term);
continue;
}
break;
}
const type = new RootExpression(ctr.root_term, ctr.root_term_type, ctr.qualified_terms.slice(0, first_array_qualifier_idx));
return new NewArrayExpression(type, arrdim_initers, ctr.qualified_terms.slice(i));
}
/**
* @param {ExpressionText} e
*/
function parseNewExpression(e) {
const ctr = parse_expression_term(e);
if (!(ctr instanceof RootExpression)) {
return null;
}
let new_expression = null;
ctr.qualified_terms.find((qt,idx) => {
if (qt instanceof MethodCallExpression) {
// new object contructor - split into constructor qualifiers and post-ctr-qualifiers
const qualified_terms = ctr.qualified_terms.slice();
const ctr_qualifiers = qualified_terms.splice(0, idx + 1);
new_expression = parseNewObjectExpression(e, ctr, ctr_qualifiers, qualified_terms);
return true;
}
if (qt instanceof ArrayIndexExpression) {
// new array constructor
// in java, multi-dimensional array constructors have priority over array accessors
// e.g new int[2][1] - is a 2D array,
// (new int[2])[1] - is the 1th element of a 1D array
new_expression = parseNewArrayExpression(e, ctr, idx);
return true;
}
});
if (!new_expression) {
// treat unqualified new expressions as object constructors with no parameters
// eg. new XYZ === new XYZ()
new_expression = parseNewObjectExpression(e, ctr, ctr.qualified_terms, []);
}
return new_expression;
}
/**
* @param {ExpressionText} e
*/
function parseArrayLiteral(e) {
const arr = new ArrayLiteralExpression();
if (!strip(e, '}')) {
for (let element; ;) {
if ((element = parse_expression(e)) === null) {
return null;
}
arr.elements.push(element);
if (strip(e, ',')) continue;
if (strip(e, '}')) break;
return null;
}
}
return arr;
}
/**
* @param {ExpressionText} e
*/
function parse_expression_term(e) {
if (e.expr[0] === '(') {
const subexpr = new ExpressionText(e.expr);
const bexpr = parseBracketOrCastExpression(subexpr);
e.expr = subexpr.expr;
return bexpr;
}
const unop = e.expr.match(/^(?:(!\s?)+|(~\s?)+|(?:([+-]\s?)+(?![\d.])))/);
if (unop) {
return parseUnaryExpression(e, unop[0]);
}
const root_term_types = ['boolean', 'boolean', 'null', 'ident', 'hexint', 'octint', 'decfloat', 'decint', 'char', 'echar', 'uchar', 'string'];
let root_term = e.expr.match(/^(?:(true(?![\w$]))|(false(?![\w$]))|(null(?![\w$]))|([a-zA-Z_$][a-zA-Z0-9_$]*)|([+-]?0x[0-9a-fA-F]+[lL]?)|([+-]?0[0-7]+[lL]?)|([+-]?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?[fFdD]?)|([+-]?\d+(?:[eE]\+?\d+)?[lL]?)|('[^\\']')|('\\[bfrntv0]')|('\\u[0-9a-fA-F]{4}')|("[^"]*"))/);
if (!root_term) {
return null;
}
strip(e, root_term[0].length);
const root_term_type = root_term_types[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12].find(x => root_term[x]) - 1];
const qualified_terms = parse_qualified_terms(e);
if (qualified_terms === null) {
return null;
}
return new RootExpression(root_term[0], root_term_type, qualified_terms);
}
/**
* @param {string} s
*/
function getBinaryOperator(s) {
const binary_op_match = s.match(/^([!=/%*^+-]=?|<<?=?|>>?[>=]?|&[&=]?|\|[|=]?|\+(?=[^+]|[+][\w\d.])|\-(?=[^-]|[-][\w\d.])|instanceof\b|\?)/);
return binary_op_match ? binary_op_match[0] : null;
}
/**
* @param {ExpressionText|string} e
* @returns {ParsedExpression}
*/
function parse_expression(e) {
if (typeof e === 'string') {
e = new ExpressionText(e);
}
const newop = e.expr.match(/^new\b/);
if (newop) {
strip(e, 3);
return parseNewExpression(e);
}
const arrayinit = e.expr.match(/^\{/);
if (arrayinit) {
strip(e, 1);
return parseArrayLiteral(e);
}
const prefix_incdec = e.expr.match(/^(?:(\+\+)|\-\-)(?=[a-zA-Z_])/);
if (prefix_incdec) {
strip(e, 2);
}
let res = parse_expression_term(e);
if (prefix_incdec) {
res = new IncOpExpression(e.expr[1] ? '++e' : '--e', res);
}
const postfix_incdec = e.expr.match(/^(?:(\+\+)|\-\-)(?![+-])/);
if (postfix_incdec) {
if (prefix_incdec) {
return null;
}
strip(e, 2);
res = new IncOpExpression(e.expr[1] ? 'e++' : 'e--', res);
}
for (; ;) {
const binary_operator = getBinaryOperator(e.expr);
if (!binary_operator) {
break;
}
const prec_diff = operator_precedences[binary_operator] - e.current_precedence;
if (prec_diff > 0) {
// bigger number -> lower precendence -> end of (sub)expression
break;
}
if (prec_diff === 0 && binary_operator !== '?') {
// equal precedence, ltr evaluation
break;
}
// higher or equal precendence
e.precedence_stack.unshift(e.current_precedence + prec_diff);
strip(e, binary_operator.length);
if (binary_operator === '?') {
res = new TernaryExpression(res);
res.ternary_true = parse_expression(e);
if (!strip(e, ':')) {
return null;
}
res.ternary_false = parse_expression(e);
} else {
res = new BinaryOpExpression(res, binary_operator, parse_expression(e));
}
e.precedence_stack.shift();
}
return res;
}
module.exports = {
ArrayIndexExpression,
BinaryOpExpression,
ExpressionText,
MemberExpression,
MethodCallExpression,
NewArrayExpression,
NewObjectExpression,
parse_expression,
ParsedExpression,
QualifierExpression,
RootExpression,
TypeCastExpression,
UnaryOpExpression,
}