Skip to content

Commit

Permalink
Fixes side effects when backtracking in a CSTParser.
Browse files Browse the repository at this point in the history
Fixes #789
  • Loading branch information
bd82 committed Sep 12, 2019
1 parent 10de5d2 commit 1dc658e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
4 changes: 4 additions & 0 deletions packages/chevrotain/docs/changes/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

- [Improve duplicate DSL methods suffix error message](https://github.com/SAP/chevrotain/issues/1020)

#### Bug Fixes

- [Unexpected Side Effects When backtracking in a CstParser](https://github.com/SAP/chevrotain/issues/789)

## 6.3.1 (9-10-2019)

#### Bug Fixes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1001,7 +1001,7 @@ export class RecognizerEngine {
}

isBackTracking(this: MixedInParser): boolean {
return !isEmpty(this.isBackTrackingStack)
return this.isBackTrackingStack.length !== 0
}

getCurrRuleFullName(this: MixedInParser): string {
Expand Down
19 changes: 13 additions & 6 deletions packages/chevrotain/src/parse/parser/traits/tree_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,12 +265,19 @@ export class TreeBuilder {
ruleCstResult: CstNode,
ruleName: string
): void {
const node = this.CST_STACK[this.CST_STACK.length - 1]

addNoneTerminalToCst(node, ruleName, ruleCstResult)

// This is only used when **both** error recovery and CST Output are enabled.
this.setNodeLocationFromNode(node.location, ruleCstResult.location)
// Avoid side effects due to back tracking
// TODO: This costs a 2-3% in performance, A flag on IParserConfig
// could be used to get rid of this conditional, but not sure its worth the effort
// and API complexity.
if (this.isBackTracking() !== true) {
const preCstNode = this.CST_STACK[this.CST_STACK.length - 1]
addNoneTerminalToCst(preCstNode, ruleName, ruleCstResult)
// This is only used when **both** error recovery and CST Output are enabled.
this.setNodeLocationFromNode(
preCstNode.location,
ruleCstResult.location
)
}
}

getBaseCstVisitorConstructor(
Expand Down

0 comments on commit 1dc658e

Please sign in to comment.