UNPKG

4.01 kBJavaScriptView Raw
1"use strict";
2/*!
3 * Copyright 2016 The ANTLR Project. All rights reserved.
4 * Licensed under the BSD-3-Clause license. See LICENSE file in the project root for license information.
5 */
6var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
7 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
8 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
9 else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
10 return c > 3 && r && Object.defineProperty(target, key, r), r;
11};
12Object.defineProperty(exports, "__esModule", { value: true });
13exports.BailErrorStrategy = void 0;
14// ConvertTo-TS run at 2016-10-04T11:26:49.2855056-07:00
15const DefaultErrorStrategy_1 = require("./DefaultErrorStrategy");
16const InputMismatchException_1 = require("./InputMismatchException");
17const Decorators_1 = require("./Decorators");
18const ParseCancellationException_1 = require("./misc/ParseCancellationException");
19/**
20 * This implementation of {@link ANTLRErrorStrategy} responds to syntax errors
21 * by immediately canceling the parse operation with a
22 * {@link ParseCancellationException}. The implementation ensures that the
23 * {@link ParserRuleContext#exception} field is set for all parse tree nodes
24 * that were not completed prior to encountering the error.
25 *
26 * This error strategy is useful in the following scenarios.
27 *
28 * * **Two-stage parsing:** This error strategy allows the first
29 * stage of two-stage parsing to immediately terminate if an error is
30 * encountered, and immediately fall back to the second stage. In addition to
31 * avoiding wasted work by attempting to recover from errors here, the empty
32 * implementation of {@link BailErrorStrategy#sync} improves the performance of
33 * the first stage.
34 * * **Silent validation:** When syntax errors are not being
35 * reported or logged, and the parse result is simply ignored if errors occur,
36 * the {@link BailErrorStrategy} avoids wasting work on recovering from errors
37 * when the result will be ignored either way.
38 *
39 * ```
40 * myparser.errorHandler = new BailErrorStrategy();
41 * ```
42 *
43 * @see Parser.errorHandler
44 */
45class BailErrorStrategy extends DefaultErrorStrategy_1.DefaultErrorStrategy {
46 /** Instead of recovering from exception `e`, re-throw it wrapped
47 * in a {@link ParseCancellationException} so it is not caught by the
48 * rule function catches. Use {@link Exception#getCause()} to get the
49 * original {@link RecognitionException}.
50 */
51 recover(recognizer, e) {
52 for (let context = recognizer.context; context; context = context.parent) {
53 context.exception = e;
54 }
55 throw new ParseCancellationException_1.ParseCancellationException(e);
56 }
57 /** Make sure we don't attempt to recover inline; if the parser
58 * successfully recovers, it won't throw an exception.
59 */
60 recoverInline(recognizer) {
61 let e = new InputMismatchException_1.InputMismatchException(recognizer);
62 for (let context = recognizer.context; context; context = context.parent) {
63 context.exception = e;
64 }
65 throw new ParseCancellationException_1.ParseCancellationException(e);
66 }
67 /** Make sure we don't attempt to recover from problems in subrules. */
68 sync(recognizer) {
69 // intentionally empty
70 }
71}
72__decorate([
73 Decorators_1.Override
74], BailErrorStrategy.prototype, "recover", null);
75__decorate([
76 Decorators_1.Override
77], BailErrorStrategy.prototype, "recoverInline", null);
78__decorate([
79 Decorators_1.Override
80], BailErrorStrategy.prototype, "sync", null);
81exports.BailErrorStrategy = BailErrorStrategy;
82//# sourceMappingURL=BailErrorStrategy.js.map
\No newline at end of file