Skip to content

Commit

Permalink
fix(callExp): fix call expression in memeberexp
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Heavner committed Mar 10, 2020
1 parent effb244 commit a610507
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 58 deletions.
29 changes: 28 additions & 1 deletion diagram/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2067,7 +2067,7 @@
{
"type": "Rule",
"name": "PrimaryExpression",
"orgText": "() => {\n this.OR(this.cachePrimaryExpression ||\n (this.cachePrimaryExpression = [\n { ALT: () => this.SUBRULE(this.ArrayExpression) },\n { ALT: () => this.SUBRULE(this.ObjectExpression) },\n { ALT: () => this.SUBRULE(this.FunctionExpression) },\n { ALT: () => this.SUBRULE(this.SubExpression) },\n { ALT: () => this.SUBRULE(this.ParenthesisExpression) },\n { ALT: () => this.SUBRULE(this.Identifier) },\n { ALT: () => this.SUBRULE(this.ReservedWord) },\n { ALT: () => this.SUBRULE(this.Literal) }\n ]));\n }",
"orgText": "() => {\n this.OR(this.cachePrimaryExpression ||\n (this.cachePrimaryExpression = [\n { ALT: () => this.SUBRULE(this.ArrayExpression) },\n { ALT: () => this.SUBRULE(this.ObjectExpression) },\n { ALT: () => this.SUBRULE(this.FunctionExpression) },\n { ALT: () => this.SUBRULE(this.SubExpression) },\n { ALT: () => this.SUBRULE(this.ParenthesisExpression) },\n { ALT: () => this.SUBRULE(this.CallExpression) },\n { ALT: () => this.SUBRULE(this.Identifier) },\n { ALT: () => this.SUBRULE(this.ReservedWord) },\n { ALT: () => this.SUBRULE(this.Literal) }\n ]));\n }",
"definition": [
{
"type": "Alternation",
Expand Down Expand Up @@ -2123,6 +2123,16 @@
}
]
},
{
"type": "Flat",
"definition": [
{
"type": "NonTerminal",
"name": "CallExpression",
"idx": 0
}
]
},
{
"type": "Flat",
"definition": [
Expand Down Expand Up @@ -2157,6 +2167,23 @@
}
]
},
{
"type": "Rule",
"name": "CallExpression",
"orgText": "() => {\n this.SUBRULE(this.Identifier, { LABEL: 'id' });\n this.SUBRULE(this.Arguments, { LABEL: 'args' });\n }",
"definition": [
{
"type": "NonTerminal",
"name": "Identifier",
"idx": 0
},
{
"type": "NonTerminal",
"name": "Arguments",
"idx": 0
}
]
},
{
"type": "Rule",
"name": "ParenthesisExpression",
Expand Down
23 changes: 15 additions & 8 deletions src/ASTVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -715,17 +715,24 @@ export class ASTVisitor extends BaseVisitor {
*
* @memberOf ASTVisitor
*/
public CallExpression({ id, args, properties }: NodeContext): ASTNode {
properties = this.asArray(properties)
const tail = properties.length > 0 ? last(properties) : args
public CallMemberExpression({ id, args }: NodeContext): ASTNode {
return {
...this.Location(id, tail),
...this.Location(id, args),
args,
callee: id,
properties,
type: 'CallExpression',
}
}
public CallExpression(ctx: NodeContext): ASTNode {
return this.mapArguments(ctx, ({ id, args }) => {
return this.asNode({
...this.Location(id, args),
args,
callee: id,
type: 'CallExpression',
}, ctx)
})
}

/**
*
Expand All @@ -735,7 +742,7 @@ export class ASTVisitor extends BaseVisitor {
*
* @memberOf ASTVisitor
*/
public ObjectMemberExpression({ id, properties }: NodeContext): ASTNode {
public ObjectMemberExpression({ id, properties = [] }: NodeContext): ASTNode {
properties = this.asArray(properties)

return {
Expand All @@ -760,7 +767,7 @@ export class ASTVisitor extends BaseVisitor {
this.singleArgument(ctx) ||
this.mapArguments(ctx, ({ id, properties = [], args = '' }) => {
if (args) {
return this.asNode(this.CallExpression({ id, args, properties }), ctx)
return this.asNode(this.CallMemberExpression({ id, args }), ctx)
} else {
return this.asNode(this.ObjectMemberExpression({ id, properties }), ctx)
}
Expand All @@ -781,7 +788,7 @@ export class ASTVisitor extends BaseVisitor {
this.singleArgument(ctx) ||
this.mapArguments(ctx, ({ property, args }) => {
if (args) {
return this.CallExpression({ id: property, args, properties: [] })
return this.CallMemberExpression({ id: property, args })
} else {
return this.ObjectMemberExpression({ id: property, properties: [] })
}
Expand Down
104 changes: 55 additions & 49 deletions src/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,26 +118,26 @@ export class RokuBRSParser extends Parser {
protected Statement = this.RULE('Statement', () => {
this.OR2(
this.cacheStatement ||
(this.cacheStatement = [
{ ALT: () => this.SUBRULE(this.EmptyStatement, Empty) },
{ ALT: () => this.SUBRULE(this.ForStatement, Statement) },
{ ALT: () => this.SUBRULE(this.IfStatement, Statement) },
{ ALT: () => this.SUBRULE(this.ForEachStatement, Statement) },
{ ALT: () => this.SUBRULE(this.WhileStatement, Statement) },

{ ALT: () => this.SUBRULE(this.PrintStatement, Statement) },
{ ALT: () => this.SUBRULE(this.ReturnStatement, Statement) },
{ ALT: () => this.SUBRULE(this.StopStatement, Statement) },
{ ALT: () => this.SUBRULE(this.GoToStatement, Statement) },
{ ALT: () => this.SUBRULE(this.LabeledStatement, Statement) },

{ ALT: () => this.SUBRULE(this.ConditionalCompilationStatement, Statement) },
{ ALT: () => this.SUBRULE(this.DimStatement, Statement) },
{ ALT: () => this.SUBRULE(this.NextStatement, Statement) },
{ ALT: () => this.SUBRULE(this.ExitStatement, Statement) },

{ ALT: () => this.SUBRULE(this.ExpressionStatement, Statement) }
])
(this.cacheStatement = [
{ ALT: () => this.SUBRULE(this.EmptyStatement, Empty) },
{ ALT: () => this.SUBRULE(this.ForStatement, Statement) },
{ ALT: () => this.SUBRULE(this.IfStatement, Statement) },
{ ALT: () => this.SUBRULE(this.ForEachStatement, Statement) },
{ ALT: () => this.SUBRULE(this.WhileStatement, Statement) },

{ ALT: () => this.SUBRULE(this.PrintStatement, Statement) },
{ ALT: () => this.SUBRULE(this.ReturnStatement, Statement) },
{ ALT: () => this.SUBRULE(this.StopStatement, Statement) },
{ ALT: () => this.SUBRULE(this.GoToStatement, Statement) },
{ ALT: () => this.SUBRULE(this.LabeledStatement, Statement) },

{ ALT: () => this.SUBRULE(this.ConditionalCompilationStatement, Statement) },
{ ALT: () => this.SUBRULE(this.DimStatement, Statement) },
{ ALT: () => this.SUBRULE(this.NextStatement, Statement) },
{ ALT: () => this.SUBRULE(this.ExitStatement, Statement) },

{ ALT: () => this.SUBRULE(this.ExpressionStatement, Statement) }
])
)

this.OPTION(() => {
Expand Down Expand Up @@ -622,19 +622,25 @@ export class RokuBRSParser extends Parser {
protected PrimaryExpression = this.RULE('PrimaryExpression', () => {
this.OR(
this.cachePrimaryExpression ||
(this.cachePrimaryExpression = [
{ ALT: () => this.SUBRULE(this.ArrayExpression) },
{ ALT: () => this.SUBRULE(this.ObjectExpression) },
{ ALT: () => this.SUBRULE(this.FunctionExpression) },
{ ALT: () => this.SUBRULE(this.SubExpression) },
{ ALT: () => this.SUBRULE(this.ParenthesisExpression) },
{ ALT: () => this.SUBRULE(this.Identifier) },
{ ALT: () => this.SUBRULE(this.ReservedWord) },
{ ALT: () => this.SUBRULE(this.Literal) }
])
(this.cachePrimaryExpression = [
{ ALT: () => this.SUBRULE(this.ArrayExpression) },
{ ALT: () => this.SUBRULE(this.ObjectExpression) },
{ ALT: () => this.SUBRULE(this.FunctionExpression) },
{ ALT: () => this.SUBRULE(this.SubExpression) },
{ ALT: () => this.SUBRULE(this.ParenthesisExpression) },
{ ALT: () => this.SUBRULE(this.CallExpression) },
{ ALT: () => this.SUBRULE(this.Identifier) },
{ ALT: () => this.SUBRULE(this.ReservedWord) },
{ ALT: () => this.SUBRULE(this.Literal) }
])
)
})

protected CallExpression = this.RULE('CallExpression', () => {
this.SUBRULE(this.Identifier, { LABEL: 'id' })
this.SUBRULE(this.Arguments, { LABEL: 'args' })
})

protected ParenthesisExpression = this.RULE('ParenthesisExpression', () => {
this.CONSUME(OPEN_PAREN)
this.SUBRULE(this.ExpressionStatement, { LABEL: 'innerExpression' })
Expand Down Expand Up @@ -663,31 +669,31 @@ export class RokuBRSParser extends Parser {
protected ReservedWord = this.RULE('ReservedWord', () => {
this.OR(
this.cacheReservedWord ||
(this.cacheReservedWord = [
{ ALT: () => this.CONSUME(END) },
{ ALT: () => this.CONSUME(IN) },
{ ALT: () => this.CONSUME(MOD) },
{ ALT: () => this.CONSUME(OBJECT) },
{ ALT: () => this.CONSUME(STOP) },
{ ALT: () => this.CONSUME(NEXT) },
{ ALT: () => this.CONSUME(BOOLEAN) },
{ ALT: () => this.CONSUME(INTEGER) },
{ ALT: () => this.CONSUME(LONGINTEGER) },
{ ALT: () => this.CONSUME(STRING) }
//
])
(this.cacheReservedWord = [
{ ALT: () => this.CONSUME(END) },
{ ALT: () => this.CONSUME(IN) },
{ ALT: () => this.CONSUME(MOD) },
{ ALT: () => this.CONSUME(OBJECT) },
{ ALT: () => this.CONSUME(STOP) },
{ ALT: () => this.CONSUME(NEXT) },
{ ALT: () => this.CONSUME(BOOLEAN) },
{ ALT: () => this.CONSUME(INTEGER) },
{ ALT: () => this.CONSUME(LONGINTEGER) },
{ ALT: () => this.CONSUME(STRING) }
//
])
)
})

protected ConditionalCompilationStatement = this.RULE('ConditionalCompilationStatement', () => {
this.OR(
this.cacheConditionalCompilationStatement ||
(this.cacheConditionalCompilationStatement = [
{ ALT: () => this.SUBRULE(this.ConditionalConst) },
{ ALT: () => this.SUBRULE(this.ConditionalError) },
{ ALT: () => this.SUBRULE(this.ConditionalIfStatement) }
//
])
(this.cacheConditionalCompilationStatement = [
{ ALT: () => this.SUBRULE(this.ConditionalConst) },
{ ALT: () => this.SUBRULE(this.ConditionalError) },
{ ALT: () => this.SUBRULE(this.ConditionalIfStatement) }
//
])
)
})

Expand Down

0 comments on commit a610507

Please sign in to comment.