-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokeniser.js
176 lines (169 loc) · 4.64 KB
/
tokeniser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Simple String Tokeniser
const EOF = -1;
const NUMBER = 0;
const STRING = 1;
const SYMBOL = 2;
const SPACE = 3;
class Lexicaliser {
constructor(options) {
this.options = options;
}
type(ch,pos) {
const { wordChars, whiteSpace } = this.options;
if (!ch) return EOF;
if (ch.match(/[0-9.]/)) return NUMBER;
if (pos === 0 && ch === '-') return NUMBER;
if (ch.match(wordChars)) return STRING;
if (ch.match(whiteSpace)) return SPACE;
return SYMBOL;
}
word(ch) {
return ch.match(this.options.wordChars);
}
}
class Tokeniser {
constructor({
quoteChars = '"\'',
slashStarComments = true,
slashSlashComments = true,
whiteSpace = /[\t \n]/,
wordChars = /[A-Za-z0-9$#_\-.]/
} = {}) {
this.options = { qc: quoteChars, sstc: slashStarComments, sslc: slashSlashComments };
this.lexicaliser = new Lexicaliser({ whiteSpace, wordChars });
this.lineno = 1;
}
getch() {
if (this.lastch === "\n") this.lineno ++;
this.lastch = this.get();
return this.lastch;
}
next() {
if (this.nextToken) {
const nextToken = this.nextToken;
this.nextToken = null;
return nextToken;
}
const { wordChars, qc, sstc, sslc } = this.options;
const l = this.lexicaliser;
let ch = this.nextch === undefined ? this.getch() : this.nextch;
let token = this.token = '';
let nextch = this.nextch = undefined;
let type = this.type = EOF;
let lws = '';
let q;
while (l.type(ch) === SPACE) { // consume white space
lws += ch;
ch = this.getch();
}
while (ch !== undefined) {
const chtype = l.type(ch, token.length);
if ((type === EOF||type === NUMBER) && chtype === NUMBER) {
token += ""+ch;
type = NUMBER;
} else if (chtype === STRING || l.word(ch)) {
token += ch;
type = STRING;
} else if (chtype === SPACE) {
nextch = ch;
break;
} else {
if (token.length && qc.indexOf(ch) === -1) {
// a symbol terminate an existing token (unless its a quote)
nextch = ch;
break;
}
nextch = this.getch();
if (qc.indexOf(ch)>=0) { // start of quoted string
q = ch;
type = STRING;
if (nextch === q) {
nextch = this.getch();
} else {
token += nextch;
let esc = 0;
while (ch = this.getch()) {
if (esc === 0 && ch === q) {
nextch = this.getch();
break;
}
if (esc) esc = 0;
else if (ch === '\\') esc = 1;
token += ch;
}
}
if (nextch && l.word(nextch)) {
ch = nextch;
nextch = undefined;
continue;
}
} else if (ch === '/') {
if (sslc && nextch === '/') {
// consume // comments
while (ch = this.getch()) {
if (ch === "\n") {
this.nextch = ch;
break;
}
}
if (ch) return this.next();
token = '';
type = EOF;
} else if (sstc && nextch === '*') {
// consume /* ... */ comments
while (ch = this.getch()) {
if (ch === "*") {
ch = this.getch();
if (ch === '/') {
break;
}
}
if (ch === "\n") this.lineno++;
}
if (ch) return this.next();
token = '';
type = EOF;
}
} else {
token = ch;
type = SYMBOL;
}
break;
}
ch = this.getch();
}
this.token = token;
this.type = type;
this.nextch = nextch;
this.quote = q || '';
if (type === NUMBER) {
token = parseFloat(token);
}
return { token, type, nextch, lineno: this.lineno, quote: this.quote, lws };
}
peek() {
if (this.nextToken) return this.nextToken;
const token = this.next();
this.nextToken = token;
return token;
}
}
class StringTokeniser extends Tokeniser {
constructor(options, string) {
super(options);
if (string) this.set(string);
}
set(string) {
this.string = string;
this.pos = 0;
}
get() {
return this.string[this.pos++];
}
}
StringTokeniser.EOF = Tokeniser.EOF = EOF;
StringTokeniser.NUMBER = Tokeniser.NUMBER = NUMBER;
StringTokeniser.STRING = Tokeniser.STRING = STRING;
StringTokeniser.SYMBOL = Tokeniser.SYMBOL = SYMBOL;
if (typeof module !== "undefined") module.exports = { Tokeniser, StringTokeniser };
/* vim: set ai ts=2 sw=2 expandtab smarttab softtabstop=2 : */