Skip to content

Commit

Permalink
Fix bug in which general js errors during in rule recovery
Browse files Browse the repository at this point in the history
could have been 'swallowed'.
  • Loading branch information
Shahar Soel committed Jan 29, 2016
1 parent 1a3240c commit 929ae12
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion examples/typescript_ecma5/src/ecmascript5_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ module ecma5 {
}

protected canTokenTypeBeInsertedInRecovery(tokClass:Function) {
var tokInstance = new (<any>tokClass)()
var tokInstance = new (<any>tokClass)("")
// Literals and Identifiers tokens carry additional information.
// Thus inserting them automatically can cause other errors "down the line"
// for example, inserting a variable Identifier may cause duplicate identifiers,
Expand Down
12 changes: 2 additions & 10 deletions src/parse/parser_public.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1573,20 +1573,12 @@ export class Parser {
try {
return this.tryInRuleRecovery(tokClass, follows)
} catch (eFromInRuleRecovery) {
/* istanbul ignore next */ // TODO: try removing this istanbul ignore with tsc 1.5.
// it is only needed for the else branch but in tsc 1.4.1 comments
// between if and else seem to get swallowed and disappear.
if (eFromConsumption instanceof InRuleRecoveryException) {
if (eFromInRuleRecovery.name === functionName(InRuleRecoveryException)) {
// failed in RuleRecovery.
// throw the original error in order to trigger reSync error recovery
throw eFromConsumption
}
// this is not part of the contract, just a workaround javascript weak error handling
// for a test to reach this code one would have to extend the BaseErrorRecoveryParser
// and override some of the recovery code to be faulty (example: throw undefined is not a function error)
// this is not a useful use case that needs to be tested...
/* istanbul ignore next */
else {
// some other error Type (built in JS error) this needs to be rethrown, we don't want to swallow it
throw eFromInRuleRecovery
}
}
Expand Down
18 changes: 18 additions & 0 deletions test/parse/recognizer_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -544,4 +544,22 @@ describe("The BaseRecognizer", () => {
}).to.throw()
})

it("will not swallow none Recognizer errors when attempting 'in rule error recovery'", () => {

let InRuleParser = class InRuleParser extends Parser {

constructor(input:Token[] = []) {
super(input, ALL_TOKENS)
Parser.performSelfAnalysis(this)
}

public someRule = this.RULE("someRule", () => {
this.CONSUME1(DotTok)
})
}
let parser:any = new InRuleParser([new IntToken("1")])
parser.tryInRuleRecovery = () => { throw Error("oops")}
expect(() => parser.someRule()).to.throw("oops");
})

})

0 comments on commit 929ae12

Please sign in to comment.