Skip to content

Commit

Permalink
feat: detect more bracket handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
Yesterday17 committed Apr 9, 2020
1 parent ddb9364 commit 9393c0a
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 24 deletions.
8 changes: 6 additions & 2 deletions server/parser/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,12 @@ export interface ASTNodeGlobalDeclare extends ASTNodeDeclare {
type: 'global' | 'static';
}

export interface ASTNodeFunction extends ASTNode, ASTBody {
export interface ASTNodeFunction extends ASTNode {
type: 'function';
fName: string;
fPara: any[];
fType: any;
fBody: ASTNode;
}

export interface ASTNodeZenClass extends ASTNode {
Expand Down Expand Up @@ -163,7 +164,10 @@ export interface ASTNodePostfixExpression extends ASTNode {
primary: ASTNode;
}

type ASTNodePrimaryExpression = ASTNodeLiteral | ASTNodeBracketHandler; // TODO
type ASTNodePrimaryExpression =
| ASTNodeLiteral
| ASTNodeBracketHandler
| ASTNodeAssignExpression; // TODO

export interface ASTNodeBracketHandler extends ASTNode {
type: 'BracketHandler';
Expand Down
2 changes: 1 addition & 1 deletion server/parser/zsBasicInterpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class ZenScriptBasicInterpreter extends ZSParser.getBaseCstVisitorConstructor()
fName: ctx.FunctionName[0].image,
fPara: ctx.ParameterList ? this.visit(ctx.ParameterList) : [],
fType: ctx.TypeDeclare ? this.visit(ctx.TypeDeclare) : 'any',
body: this.visit(ctx.StatementBody),
fBody: this.visit(ctx.StatementBody),
errors: [],
};
}
Expand Down
87 changes: 76 additions & 11 deletions server/parser/zsInterpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ function sortBody(node: ASTBody): ASTNode {
*/
function pushBody(node: ASTBody, value: ASTNode): ASTNode {
node.body.push(value);
if (value.errors) {
node.errors.push(...value.errors);
}
return node;
}

Expand Down Expand Up @@ -215,15 +218,21 @@ class ZenScriptInterpreter extends ZSParser.getBaseCstVisitorConstructor() {
}

protected FunctionDeclaration(ctx: NodeContext): ASTNodeFunction {
return {
const node: ASTNodeFunction = {
type: 'function',
start: (ctx.FunctionName[0] as IToken).startOffset,
fName: ctx.FunctionName[0].image,
fPara: ctx.ParameterList ? this.visit(ctx.ParameterList) : [],
fType: ctx.TypeDeclare ? this.visit(ctx.TypeDeclare) : 'any',
body: this.visit(ctx.StatementBody),
fPara: ctx.ParameterList ? this.visit(ctx.ParameterList[0]) : [],
fType: ctx.TypeDeclare ? this.visit(ctx.TypeDeclare[0]) : 'any',
fBody: this.visit(ctx.StatementBody[0]),
errors: [],
};

if (node.fBody.errors) {
node.errors.push(...node.fBody.errors);
}

return node;
}

protected ZenClassDeclaration(ctx: NodeContext): ASTNode {
Expand All @@ -235,11 +244,20 @@ class ZenScriptInterpreter extends ZSParser.getBaseCstVisitorConstructor() {
}

protected BlockStatement(ctx: NodeContext): ASTNode {
return {
type: 'block-statement',
const node: ASTNode = {
type: 'BlockStatement',
start: 0,
errors: [],
};
ctx.Statement.forEach((s: any) => {
// TODO
const n: ASTNode = this.visit(s);
if (n.errors) {
node.errors.push(...n.errors);
}
});

return node;
}

/**
Expand All @@ -248,11 +266,21 @@ class ZenScriptInterpreter extends ZSParser.getBaseCstVisitorConstructor() {
*/

protected StatementBody(ctx: NodeContext): ASTNode {
return {
type: 'statement-body',
const node: ASTNode = {
type: 'StatementBody',
start: 0,
errors: [],
};

// TODO
if (ctx.BlockStatement) {
const n: ASTNode = this.visit(ctx.BlockStatement[0]);
if (n.errors) {
node.errors.push(...n.errors);
}
}

return node;
}

/**
Expand Down Expand Up @@ -772,7 +800,42 @@ class ZenScriptInterpreter extends ZSParser.getBaseCstVisitorConstructor() {
node.errors.push(...node.primary.errors);
}

// TODO: Postfix
if (ctx.property) {
//
} else if (ctx.to) {
// TODO
const to: ASTNodeAssignExpression = this.visit(ctx.to[0]);
if (to.errors) {
node.errors.push(...to.errors);
}
} else if (ctx.dotdot) {
// TODO
const dotdot: ASTNodeAssignExpression = this.visit(ctx.dotdot[0]);
if (dotdot.errors) {
node.errors.push(...dotdot.errors);
}
} else if (ctx.index) {
//
if (ctx.indexAssign) {
// TODO
const assign: ASTNodeAssignExpression = this.visit(ctx.indexAssign[0]);
if (assign.errors) {
node.errors.push(...assign.errors);
}
}
} else if (ctx.brExpressions) {
// TODO
ctx.brExpressions.forEach((e: any) => {
const exp: ASTNodeAssignExpression = this.visit(e);
if (exp.errors) {
node.errors.push(...exp.errors);
}
});
} else if (ctx.type) {
//
} else if (ctx.instanceof) {
//
}

return node;
}
Expand All @@ -789,7 +852,9 @@ class ZenScriptInterpreter extends ZSParser.getBaseCstVisitorConstructor() {
value: '',
};
} else if (ctx.BracketHandler) {
return this.visit(ctx.BracketHandler[0]);
return this.visit(ctx.BracketHandler[0]) as ASTNodeBracketHandler;
} else if (ctx.AssignExpression) {
return this.visit(ctx.AssignExpression[0]) as ASTNodeAssignExpression;
} else {
// TODO
return {
Expand Down Expand Up @@ -825,7 +890,7 @@ class ZenScriptInterpreter extends ZSParser.getBaseCstVisitorConstructor() {
type: 'BracketHandler',
items: ctx.$BracketHandlerItemGroup.map((item: any) => this.visit(item)),
start: (ctx.LT[0] as IToken).startOffset,
end: (ctx.GT[0] as IToken).endOffset,
end: (ctx.GT[0] as IToken).endOffset + 1,
errors: [],
};

Expand Down
22 changes: 12 additions & 10 deletions server/parser/zsParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,9 +511,9 @@ export class ZenScriptParser extends Parser {
ALT: () => {
this.CONSUME(DOT);
this.OR2([
{ ALT: () => this.CONSUME(IDENTIFIER) },
{ ALT: () => this.CONSUME(VERSION) },
{ ALT: () => this.CONSUME(STRING) },
{ ALT: () => this.CONSUME(IDENTIFIER, { LABEL: 'property' }) },
{ ALT: () => this.CONSUME(VERSION, { LABEL: 'property' }) },
{ ALT: () => this.CONSUME(STRING, { LABEL: 'property' }) },
]);
},
},
Expand All @@ -522,23 +522,23 @@ export class ZenScriptParser extends Parser {
this.LA(1).tokenType === IDENTIFIER && this.LA(1).image === 'to',
ALT: () => {
this.CONSUME2(IDENTIFIER);
this.SUBRULE(this.AssignExpression);
this.SUBRULE(this.AssignExpression, { LABEL: 'to' });
},
},
{
ALT: () => {
this.CONSUME(DOT2);
this.SUBRULE2(this.AssignExpression);
this.SUBRULE2(this.AssignExpression, { LABEL: 'dotdot' });
},
},
{
ALT: () => {
this.CONSUME(SQBR_OPEN);
this.SUBRULE3(this.AssignExpression, { LABEL: 'INDEX' });
this.SUBRULE3(this.AssignExpression, { LABEL: 'index' });
this.CONSUME(SQBR_CLOSE);
this.OPTION(() => {
this.CONSUME(ASSIGN);
this.SUBRULE4(this.AssignExpression);
this.SUBRULE4(this.AssignExpression, { LABEL: 'indexAssign' });
});
},
},
Expand All @@ -548,21 +548,23 @@ export class ZenScriptParser extends Parser {
this.MANY_SEP({
SEP: COMMA,
DEF: () => {
this.SUBRULE5(this.AssignExpression);
this.SUBRULE5(this.AssignExpression, {
LABEL: 'brExpressions',
});
},
});
this.CONSUME(BR_CLOSE);
},
},
{
ALT: () => {
this.SUBRULE(this.TypeDeclare);
this.SUBRULE(this.TypeDeclare, { LABEL: 'type' });
},
},
{
ALT: () => {
this.CONSUME(INSTANCEOF);
this.SUBRULE(this.TypeAnnotation);
this.SUBRULE(this.TypeAnnotation, { LABEL: 'instanceof' });
},
},
]);
Expand Down

0 comments on commit 9393c0a

Please sign in to comment.