forked from aiscript-dev/aiscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.ts
157 lines (148 loc) · 5.41 KB
/
parser.ts
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
import * as assert from 'assert';
import { describe, test } from 'vitest';
import { Scanner } from '../src/parser/scanner';
import { TOKEN, TokenKind, TokenPosition } from '../src/parser/token';
import { CharStream } from '../src/parser/streams/char-stream';
describe('CharStream', () => {
test.concurrent('char', async () => {
const source = 'abc';
const stream = new CharStream(source);
assert.strictEqual('a', stream.char);
});
test.concurrent('next', async () => {
const source = 'abc';
const stream = new CharStream(source);
stream.next();
assert.strictEqual('b', stream.char);
});
describe('prev', () => {
test.concurrent('move', async () => {
const source = 'abc';
const stream = new CharStream(source);
stream.next();
assert.strictEqual('b', stream.char);
stream.prev();
assert.strictEqual('a', stream.char);
});
test.concurrent('境界外には移動しない', async () => {
const source = 'abc';
const stream = new CharStream(source);
stream.prev();
assert.strictEqual('a', stream.char);
});
});
test.concurrent('eof', async () => {
const source = 'abc';
const stream = new CharStream(source);
assert.strictEqual(false, stream.eof);
stream.next();
assert.strictEqual(false, stream.eof);
stream.next();
assert.strictEqual(false, stream.eof);
stream.next();
assert.strictEqual(true, stream.eof);
});
test.concurrent('EOFでcharを参照するとエラー', async () => {
const source = '';
const stream = new CharStream(source);
assert.strictEqual(true, stream.eof);
try {
stream.char;
} catch (e) {
return;
}
assert.fail();
});
test.concurrent('CRは読み飛ばされる', async () => {
const source = 'a\r\nb';
const stream = new CharStream(source);
assert.strictEqual('a', stream.char);
stream.next();
assert.strictEqual('\n', stream.char);
stream.next();
assert.strictEqual('b', stream.char);
stream.next();
assert.strictEqual(true, stream.eof);
});
});
describe('Scanner', () => {
function init(source: string) {
const stream = new Scanner(source);
return stream;
}
function next(stream: Scanner, kind: TokenKind, pos: TokenPosition, opts: { hasLeftSpacing?: boolean, value?: string }) {
assert.deepStrictEqual(stream.getToken(), TOKEN(kind, pos, opts));
stream.next();
}
test.concurrent('eof', async () => {
const source = '';
const stream = init(source);
next(stream, TokenKind.EOF, { line: 1, column: 1 }, { });
next(stream, TokenKind.EOF, { line: 1, column: 1 }, { });
});
test.concurrent('keyword', async () => {
const source = 'if';
const stream = init(source);
next(stream, TokenKind.IfKeyword, { line: 1, column: 1 }, { });
next(stream, TokenKind.EOF, { line: 1, column: 3 }, { });
});
test.concurrent('identifier', async () => {
const source = 'xyz';
const stream = init(source);
next(stream, TokenKind.Identifier, { line: 1, column: 1 }, { value: 'xyz' });
next(stream, TokenKind.EOF, { line: 1, column: 4 }, { });
});
test.concurrent('invalid token', async () => {
const source = '$';
try {
const stream = new Scanner(source);
} catch (e) {
return;
}
assert.fail();
});
test.concurrent('words', async () => {
const source = 'abc xyz';
const stream = init(source);
next(stream, TokenKind.Identifier, { line: 1, column: 1 }, { value: 'abc' });
next(stream, TokenKind.Identifier, { line: 1, column: 5 }, { hasLeftSpacing: true, value: 'xyz' });
next(stream, TokenKind.EOF, { line: 1, column: 8 }, { });
});
test.concurrent('stream', async () => {
const source = '@abc() { }';
const stream = init(source);
next(stream, TokenKind.At, { line: 1, column: 1 }, { });
next(stream, TokenKind.Identifier, { line: 1, column: 2 }, { value: 'abc' });
next(stream, TokenKind.OpenParen, { line: 1, column: 5 }, { });
next(stream, TokenKind.CloseParen, { line: 1, column: 6 }, { });
next(stream, TokenKind.OpenBrace, { line: 1, column: 8 }, { hasLeftSpacing: true });
next(stream, TokenKind.CloseBrace, { line: 1, column: 10 }, { hasLeftSpacing: true });
next(stream, TokenKind.EOF, { line: 1, column: 11 }, { });
});
test.concurrent('multi-lines', async () => {
const source = 'aaa\nbbb';
const stream = init(source);
next(stream, TokenKind.Identifier, { line: 1, column: 1 }, { value: 'aaa' });
next(stream, TokenKind.NewLine, { line: 1, column: 4 }, { });
next(stream, TokenKind.Identifier, { line: 2, column: 1 }, { value: 'bbb' });
next(stream, TokenKind.EOF, { line: 2, column: 4 }, { });
});
test.concurrent('lookahead', async () => {
const source = '@abc() { }';
const stream = init(source);
assert.deepStrictEqual(stream.lookahead(1), TOKEN(TokenKind.Identifier, { line: 1, column: 2 }, { value: 'abc' }));
next(stream, TokenKind.At, { line: 1, column: 1 }, { });
next(stream, TokenKind.Identifier, { line: 1, column: 2 }, { value: 'abc' });
next(stream, TokenKind.OpenParen, { line: 1, column: 5 }, { });
});
test.concurrent('empty lines', async () => {
const source = "match 1{\n// comment\n}";
const stream = init(source);
next(stream, TokenKind.MatchKeyword, { line: 1, column: 1 }, { });
next(stream, TokenKind.NumberLiteral, { line: 1, column: 7 }, { hasLeftSpacing: true, value: '1' });
next(stream, TokenKind.OpenBrace, { line: 1, column: 8 }, { });
next(stream, TokenKind.NewLine, { line: 1, column: 9 }, { });
next(stream, TokenKind.CloseBrace, { line: 3, column: 1 }, { });
next(stream, TokenKind.EOF, { line: 3, column: 2 }, { });
});
});