| [9af201e] | 1 | "use strict";Object.defineProperty(exports, "__esModule", {value: true});var _index = require('../tokenizer/index');
|
|---|
| 2 |
|
|---|
| 3 | var _types = require('../tokenizer/types');
|
|---|
| 4 | var _charcodes = require('../util/charcodes');
|
|---|
| 5 | var _base = require('./base');
|
|---|
| 6 |
|
|---|
| 7 | // ## Parser utilities
|
|---|
| 8 |
|
|---|
| 9 | // Tests whether parsed token is a contextual keyword.
|
|---|
| 10 | function isContextual(contextualKeyword) {
|
|---|
| 11 | return _base.state.contextualKeyword === contextualKeyword;
|
|---|
| 12 | } exports.isContextual = isContextual;
|
|---|
| 13 |
|
|---|
| 14 | function isLookaheadContextual(contextualKeyword) {
|
|---|
| 15 | const l = _index.lookaheadTypeAndKeyword.call(void 0, );
|
|---|
| 16 | return l.type === _types.TokenType.name && l.contextualKeyword === contextualKeyword;
|
|---|
| 17 | } exports.isLookaheadContextual = isLookaheadContextual;
|
|---|
| 18 |
|
|---|
| 19 | // Consumes contextual keyword if possible.
|
|---|
| 20 | function eatContextual(contextualKeyword) {
|
|---|
| 21 | return _base.state.contextualKeyword === contextualKeyword && _index.eat.call(void 0, _types.TokenType.name);
|
|---|
| 22 | } exports.eatContextual = eatContextual;
|
|---|
| 23 |
|
|---|
| 24 | // Asserts that following token is given contextual keyword.
|
|---|
| 25 | function expectContextual(contextualKeyword) {
|
|---|
| 26 | if (!eatContextual(contextualKeyword)) {
|
|---|
| 27 | unexpected();
|
|---|
| 28 | }
|
|---|
| 29 | } exports.expectContextual = expectContextual;
|
|---|
| 30 |
|
|---|
| 31 | // Test whether a semicolon can be inserted at the current position.
|
|---|
| 32 | function canInsertSemicolon() {
|
|---|
| 33 | return _index.match.call(void 0, _types.TokenType.eof) || _index.match.call(void 0, _types.TokenType.braceR) || hasPrecedingLineBreak();
|
|---|
| 34 | } exports.canInsertSemicolon = canInsertSemicolon;
|
|---|
| 35 |
|
|---|
| 36 | function hasPrecedingLineBreak() {
|
|---|
| 37 | const prevToken = _base.state.tokens[_base.state.tokens.length - 1];
|
|---|
| 38 | const lastTokEnd = prevToken ? prevToken.end : 0;
|
|---|
| 39 | for (let i = lastTokEnd; i < _base.state.start; i++) {
|
|---|
| 40 | const code = _base.input.charCodeAt(i);
|
|---|
| 41 | if (
|
|---|
| 42 | code === _charcodes.charCodes.lineFeed ||
|
|---|
| 43 | code === _charcodes.charCodes.carriageReturn ||
|
|---|
| 44 | code === 0x2028 ||
|
|---|
| 45 | code === 0x2029
|
|---|
| 46 | ) {
|
|---|
| 47 | return true;
|
|---|
| 48 | }
|
|---|
| 49 | }
|
|---|
| 50 | return false;
|
|---|
| 51 | } exports.hasPrecedingLineBreak = hasPrecedingLineBreak;
|
|---|
| 52 |
|
|---|
| 53 | function hasFollowingLineBreak() {
|
|---|
| 54 | const nextStart = _index.nextTokenStart.call(void 0, );
|
|---|
| 55 | for (let i = _base.state.end; i < nextStart; i++) {
|
|---|
| 56 | const code = _base.input.charCodeAt(i);
|
|---|
| 57 | if (
|
|---|
| 58 | code === _charcodes.charCodes.lineFeed ||
|
|---|
| 59 | code === _charcodes.charCodes.carriageReturn ||
|
|---|
| 60 | code === 0x2028 ||
|
|---|
| 61 | code === 0x2029
|
|---|
| 62 | ) {
|
|---|
| 63 | return true;
|
|---|
| 64 | }
|
|---|
| 65 | }
|
|---|
| 66 | return false;
|
|---|
| 67 | } exports.hasFollowingLineBreak = hasFollowingLineBreak;
|
|---|
| 68 |
|
|---|
| 69 | function isLineTerminator() {
|
|---|
| 70 | return _index.eat.call(void 0, _types.TokenType.semi) || canInsertSemicolon();
|
|---|
| 71 | } exports.isLineTerminator = isLineTerminator;
|
|---|
| 72 |
|
|---|
| 73 | // Consume a semicolon, or, failing that, see if we are allowed to
|
|---|
| 74 | // pretend that there is a semicolon at this position.
|
|---|
| 75 | function semicolon() {
|
|---|
| 76 | if (!isLineTerminator()) {
|
|---|
| 77 | unexpected('Unexpected token, expected ";"');
|
|---|
| 78 | }
|
|---|
| 79 | } exports.semicolon = semicolon;
|
|---|
| 80 |
|
|---|
| 81 | // Expect a token of a given type. If found, consume it, otherwise,
|
|---|
| 82 | // raise an unexpected token error at given pos.
|
|---|
| 83 | function expect(type) {
|
|---|
| 84 | const matched = _index.eat.call(void 0, type);
|
|---|
| 85 | if (!matched) {
|
|---|
| 86 | unexpected(`Unexpected token, expected "${_types.formatTokenType.call(void 0, type)}"`);
|
|---|
| 87 | }
|
|---|
| 88 | } exports.expect = expect;
|
|---|
| 89 |
|
|---|
| 90 | /**
|
|---|
| 91 | * Transition the parser to an error state. All code needs to be written to naturally unwind in this
|
|---|
| 92 | * state, which allows us to backtrack without exceptions and without error plumbing everywhere.
|
|---|
| 93 | */
|
|---|
| 94 | function unexpected(message = "Unexpected token", pos = _base.state.start) {
|
|---|
| 95 | if (_base.state.error) {
|
|---|
| 96 | return;
|
|---|
| 97 | }
|
|---|
| 98 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|---|
| 99 | const err = new SyntaxError(message);
|
|---|
| 100 | err.pos = pos;
|
|---|
| 101 | _base.state.error = err;
|
|---|
| 102 | _base.state.pos = _base.input.length;
|
|---|
| 103 | _index.finishToken.call(void 0, _types.TokenType.eof);
|
|---|
| 104 | } exports.unexpected = unexpected;
|
|---|