| 1 | import {eat, finishToken, lookaheadTypeAndKeyword, match, nextTokenStart} from "../tokenizer/index";
|
|---|
| 2 |
|
|---|
| 3 | import {formatTokenType, TokenType as tt} from "../tokenizer/types";
|
|---|
| 4 | import {charCodes} from "../util/charcodes";
|
|---|
| 5 | import {input, state} from "./base";
|
|---|
| 6 |
|
|---|
| 7 | // ## Parser utilities
|
|---|
| 8 |
|
|---|
| 9 | // Tests whether parsed token is a contextual keyword.
|
|---|
| 10 | export function isContextual(contextualKeyword) {
|
|---|
| 11 | return state.contextualKeyword === contextualKeyword;
|
|---|
| 12 | }
|
|---|
| 13 |
|
|---|
| 14 | export function isLookaheadContextual(contextualKeyword) {
|
|---|
| 15 | const l = lookaheadTypeAndKeyword();
|
|---|
| 16 | return l.type === tt.name && l.contextualKeyword === contextualKeyword;
|
|---|
| 17 | }
|
|---|
| 18 |
|
|---|
| 19 | // Consumes contextual keyword if possible.
|
|---|
| 20 | export function eatContextual(contextualKeyword) {
|
|---|
| 21 | return state.contextualKeyword === contextualKeyword && eat(tt.name);
|
|---|
| 22 | }
|
|---|
| 23 |
|
|---|
| 24 | // Asserts that following token is given contextual keyword.
|
|---|
| 25 | export function expectContextual(contextualKeyword) {
|
|---|
| 26 | if (!eatContextual(contextualKeyword)) {
|
|---|
| 27 | unexpected();
|
|---|
| 28 | }
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| 31 | // Test whether a semicolon can be inserted at the current position.
|
|---|
| 32 | export function canInsertSemicolon() {
|
|---|
| 33 | return match(tt.eof) || match(tt.braceR) || hasPrecedingLineBreak();
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | export function hasPrecedingLineBreak() {
|
|---|
| 37 | const prevToken = state.tokens[state.tokens.length - 1];
|
|---|
| 38 | const lastTokEnd = prevToken ? prevToken.end : 0;
|
|---|
| 39 | for (let i = lastTokEnd; i < state.start; i++) {
|
|---|
| 40 | const code = input.charCodeAt(i);
|
|---|
| 41 | if (
|
|---|
| 42 | code === charCodes.lineFeed ||
|
|---|
| 43 | code === charCodes.carriageReturn ||
|
|---|
| 44 | code === 0x2028 ||
|
|---|
| 45 | code === 0x2029
|
|---|
| 46 | ) {
|
|---|
| 47 | return true;
|
|---|
| 48 | }
|
|---|
| 49 | }
|
|---|
| 50 | return false;
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | export function hasFollowingLineBreak() {
|
|---|
| 54 | const nextStart = nextTokenStart();
|
|---|
| 55 | for (let i = state.end; i < nextStart; i++) {
|
|---|
| 56 | const code = input.charCodeAt(i);
|
|---|
| 57 | if (
|
|---|
| 58 | code === charCodes.lineFeed ||
|
|---|
| 59 | code === charCodes.carriageReturn ||
|
|---|
| 60 | code === 0x2028 ||
|
|---|
| 61 | code === 0x2029
|
|---|
| 62 | ) {
|
|---|
| 63 | return true;
|
|---|
| 64 | }
|
|---|
| 65 | }
|
|---|
| 66 | return false;
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | export function isLineTerminator() {
|
|---|
| 70 | return eat(tt.semi) || canInsertSemicolon();
|
|---|
| 71 | }
|
|---|
| 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 | export function semicolon() {
|
|---|
| 76 | if (!isLineTerminator()) {
|
|---|
| 77 | unexpected('Unexpected token, expected ";"');
|
|---|
| 78 | }
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | // Expect a token of a given type. If found, consume it, otherwise,
|
|---|
| 82 | // raise an unexpected token error at given pos.
|
|---|
| 83 | export function expect(type) {
|
|---|
| 84 | const matched = eat(type);
|
|---|
| 85 | if (!matched) {
|
|---|
| 86 | unexpected(`Unexpected token, expected "${formatTokenType(type)}"`);
|
|---|
| 87 | }
|
|---|
| 88 | }
|
|---|
| 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 | export function unexpected(message = "Unexpected token", pos = state.start) {
|
|---|
| 95 | if (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 | state.error = err;
|
|---|
| 102 | state.pos = input.length;
|
|---|
| 103 | finishToken(tt.eof);
|
|---|
| 104 | }
|
|---|