UNPKG

2.28 kBJavaScriptView Raw
1/* @flow */
2
3"use strict";
4
5var _interopRequireDefault = require("babel-runtime/helpers/interop-require-default")["default"];
6
7var _tokenizerTypes = require("../tokenizer/types");
8
9var _index = require("./index");
10
11var _index2 = _interopRequireDefault(_index);
12
13var _utilWhitespace = require("../util/whitespace");
14
15var pp = _index2["default"].prototype;
16
17// ## Parser utilities
18
19// TODO
20
21pp.addExtra = function (node, key, val) {
22 if (!node) return;
23
24 var extra = node.extra = node.extra || {};
25 extra[key] = val;
26};
27
28// TODO
29
30pp.isRelational = function (op) {
31 return this.match(_tokenizerTypes.types.relational) && this.state.value === op;
32};
33
34// TODO
35
36pp.expectRelational = function (op) {
37 if (this.isRelational(op)) {
38 this.next();
39 } else {
40 this.unexpected();
41 }
42};
43
44// Tests whether parsed token is a contextual keyword.
45
46pp.isContextual = function (name) {
47 return this.match(_tokenizerTypes.types.name) && this.state.value === name;
48};
49
50// Consumes contextual keyword if possible.
51
52pp.eatContextual = function (name) {
53 return this.state.value === name && this.eat(_tokenizerTypes.types.name);
54};
55
56// Asserts that following token is given contextual keyword.
57
58pp.expectContextual = function (name) {
59 if (!this.eatContextual(name)) this.unexpected();
60};
61
62// Test whether a semicolon can be inserted at the current position.
63
64pp.canInsertSemicolon = function () {
65 return this.match(_tokenizerTypes.types.eof) || this.match(_tokenizerTypes.types.braceR) || _utilWhitespace.lineBreak.test(this.input.slice(this.state.lastTokEnd, this.state.start));
66};
67
68// TODO
69
70pp.isLineTerminator = function () {
71 return this.eat(_tokenizerTypes.types.semi) || this.canInsertSemicolon();
72};
73
74// Consume a semicolon, or, failing that, see if we are allowed to
75// pretend that there is a semicolon at this position.
76
77pp.semicolon = function () {
78 if (!this.eat(_tokenizerTypes.types.semi) && !this.canInsertSemicolon()) this.unexpected();
79};
80
81// Expect a token of a given type. If found, consume it, otherwise,
82// raise an unexpected token error.
83
84pp.expect = function (type) {
85 return this.eat(type) || this.unexpected();
86};
87
88// Raise an unexpected token error.
89
90pp.unexpected = function (pos) {
91 this.raise(pos != null ? pos : this.state.start, "Unexpected token");
92};
\No newline at end of file