Skip to content

Commit 4448e75

Browse files
sapbenbenbenben.sap
andauthored
several features and bugfix (#34)
* fix bug for .class access and negative number * java parser support array type * adjust loc of VariableDeclaration * adjust loc of VariableDeclaration * support varargs --------- Co-authored-by: benben.sap <benben.sap@antgroup.com>
1 parent 2c0a698 commit 4448e75

File tree

1 file changed

+46
-5
lines changed

1 file changed

+46
-5
lines changed

parser-Java-Js/src/frontend/java/ASTBuilder.ts

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,22 @@ abstract class ParseTreeVisitor<Result> extends AbstractParseTreeVisitor<Result>
3636
end: { line: tree._stop?.line, column: tree._stop?._charPositionInLine + 1 },
3737
sourcefile: this.sourcefile
3838
};
39+
this.adJustNodeLoc(node)
3940
}
4041
return node;
4142
}
43+
44+
private adJustNodeLoc(node) {
45+
switch (node.type) {
46+
case 'VariableDeclaration':
47+
if (node?.loc?.end?.column && node?.id?.name && !node.init) {
48+
node.loc.end.column = node.loc.end.column + node.id.name.length - 1
49+
}
50+
break
51+
default:
52+
break
53+
}
54+
}
4255
}
4356

4457
export class ASTBuilder
@@ -1203,6 +1216,12 @@ export class ASTBuilder
12031216
// : variableModifier* typeType annotation* '...' variableDeclaratorId
12041217
public visitLastFormalParameter(ctx: JP.LastFormalParameterContext): UAST.Instruction {
12051218
const leftType = this.visit(ctx.typeType()) as UAST.Type;
1219+
if (ctx.children?.length === 3 && ctx.children[1].text === '...') {
1220+
if (!leftType._meta) {
1221+
leftType._meta = {}
1222+
}
1223+
leftType._meta.varargs = true
1224+
}
12061225
this.typesState.push(leftType);
12071226
const varId = this.visit(ctx.variableDeclaratorId()) as UAST.Identifier;
12081227
const type = this.typesState.pop();
@@ -1427,7 +1446,20 @@ export class ASTBuilder
14271446
return UAST.unaryExpression(toText(ctx._postfix) as '++' | '--', this.visit(ctx.expression()[0]) as UAST.Expression, true);
14281447
}
14291448
if (ctx._prefix) {
1430-
return UAST.unaryExpression(toText(ctx._prefix) as '--', this.visit(ctx.expression()[0]) as UAST.Expression, false);
1449+
const text = toText(ctx._prefix)
1450+
if (text === '-') {
1451+
const argExpr = this.visit(ctx.expression()[0]) as UAST.Expression;
1452+
if (argExpr.type === 'Literal') {
1453+
argExpr.value = String(-Number(argExpr.value))
1454+
return argExpr;
1455+
} else {
1456+
return UAST.unaryExpression(toText(ctx._prefix) as '--', argExpr, false);
1457+
}
1458+
} else if (text === '+') {
1459+
return this.visit(ctx.expression()[0]) as UAST.Expression
1460+
} else {
1461+
return UAST.unaryExpression(toText(ctx._prefix) as '--', this.visit(ctx.expression()[0]) as UAST.Expression, false);
1462+
}
14311463
}
14321464

14331465

@@ -1988,7 +2020,7 @@ export class ASTBuilder
19882020
const typeTypeCtx = ctx.typeTypeOrVoid();
19892021
if (typeTypeCtx) {
19902022
const typeType = this.visit(typeTypeCtx) as UAST.Type;
1991-
return UAST.memberAccess(convertToMemberAccess(('id' in typeType && typeType.id.name) || ('name' in typeType && typeType.name)), UAST.identifier('Class'), false);
2023+
return UAST.memberAccess(convertToMemberAccess(('id' in typeType && typeType.id.name) || ('name' in typeType && typeType.name)), UAST.identifier('class'), false);
19922024
}
19932025
throw new Error(`Primary type: ${toText(ctx)} not supported`);
19942026
}
@@ -2045,15 +2077,24 @@ export class ASTBuilder
20452077
// : annotation* (classOrInterfaceType | primitiveType) (annotation* '[' ']')*
20462078
// ;
20472079
public visitTypeType(ctx: JP.TypeTypeContext): UAST.Type {
2080+
let typeResult;
20482081
const primitiveType = ctx.primitiveType();
20492082
if (primitiveType) {
2050-
return this.visit(primitiveType) as UAST.Type;
2083+
typeResult = this.visit(primitiveType) as UAST.Type;
20512084
}
20522085
const classOrInterfaceType = ctx.classOrInterfaceType();
20532086
if (classOrInterfaceType) {
2054-
return this.visit(classOrInterfaceType) as UAST.Type;
2087+
typeResult = this.visit(classOrInterfaceType) as UAST.Type;
2088+
}
2089+
2090+
if (!typeResult) {
2091+
throw new Error('Unexpected TypeType');
2092+
}
2093+
2094+
if (ctx.children?.length === 3 && ctx.children[1].text === '[' && ctx.children[2].text === ']') {
2095+
typeResult = UAST.arrayType(typeResult.id, typeResult)
20552096
}
2056-
throw new Error('Unexpected TypeType');
2097+
return typeResult
20572098
}
20582099

20592100
//primitiveType

0 commit comments

Comments
 (0)