diff --git a/examples/typescript_ecma5/src/ecmascript5_parser.ts b/examples/typescript_ecma5/src/ecmascript5_parser.ts index 8d4447fbd..d382bf23c 100644 --- a/examples/typescript_ecma5/src/ecmascript5_parser.ts +++ b/examples/typescript_ecma5/src/ecmascript5_parser.ts @@ -63,7 +63,7 @@ module ecma5 { } protected canTokenTypeBeInsertedInRecovery(tokClass:Function) { - var tokInstance = new (tokClass)() + var tokInstance = new (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, diff --git a/src/parse/parser_public.ts b/src/parse/parser_public.ts index ea74a6696..c5a7af867 100644 --- a/src/parse/parser_public.ts +++ b/src/parse/parser_public.ts @@ -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 } } diff --git a/test/parse/recognizer_spec.ts b/test/parse/recognizer_spec.ts index bc46586f8..e0e11a595 100644 --- a/test/parse/recognizer_spec.ts +++ b/test/parse/recognizer_spec.ts @@ -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"); + }) + })