-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatchers.ts
162 lines (147 loc) · 3.9 KB
/
matchers.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
158
159
160
161
162
/**
* Functions that try to match next few source code characters to a given token
*/
import type { Tokens } from './tokens.js';
import type { MatcherResult } from './types.js';
export function defaultMatcher(
type: keyof Tokens,
matcher: string,
input: string
): MatcherResult<keyof Tokens> | undefined {
if (!input.startsWith(matcher)) return undefined;
const hasSpecialSymbols = matcher.search(/\W/u) !== -1;
// Don't match identifiers that begin with a reserved keyword
if (
!hasSpecialSymbols &&
input.search(new RegExp(`^${matcher}\\b`, 'u')) === -1
)
return undefined;
return {
type,
data: {},
tokenLength: matcher.length,
syntaxErrors: [],
};
}
export const endMatcher = (input: string): MatcherResult<'END'> | undefined =>
input.length === 0
? {
type: 'END',
data: {},
tokenLength: 0,
syntaxErrors: [],
}
: undefined;
const reId = /^[_a-z]\w*/iu;
export function idMatcher(input: string): MatcherResult<'ID'> | undefined {
const identifier = reId.exec(input)?.[0];
if (identifier === undefined) return undefined;
return {
type: 'ID',
data: {
literal: identifier,
},
tokenLength: identifier.length,
syntaxErrors: [],
};
}
const reInt = /^\d+/u;
export const INT_MAX = 2_147_483_647;
export function intLiteralMatcher(
input: string
): MatcherResult<'INTLITERAL'> | undefined {
const rawNumber = reInt.exec(input)?.[0];
if (rawNumber === undefined) return undefined;
const number = Number.parseInt(rawNumber);
return {
type: 'INTLITERAL',
data: {
literal: number > INT_MAX ? 0 : number,
},
tokenLength: rawNumber.length,
syntaxErrors:
number > INT_MAX
? [
{
type: 'SyntaxError',
message: 'Integer literal overflow',
start: 0,
end: rawNumber.length,
},
]
: [],
};
}
export function stringLiteralMatcher(
input: string
): MatcherResult<'STRINGLITERAL'> | undefined {
if (!input.startsWith('"')) return undefined;
const lineEnd = input.indexOf('\n');
const line = lineEnd === -1 ? input : input.slice(0, lineEnd);
let tokenLength = 1;
let isEscaped = false;
let isTerminated = false;
let hasInvalidEscape = false;
Array.from(line.slice(1)).every((character) => {
tokenLength += 1;
if (isEscaped) {
if (!['n', 't', '\\', '"'].includes(character)) hasInvalidEscape = true;
isEscaped = false;
} else if (character === '\\') isEscaped = !isEscaped;
else if (character === '"') {
isTerminated = true;
return false;
}
return true;
});
const hasErrors = !isTerminated || hasInvalidEscape;
return {
type: 'STRINGLITERAL',
data: hasErrors
? undefined
: {
literal: line.slice(0, tokenLength),
},
tokenLength,
syntaxErrors: hasErrors
? [
{
type: 'SyntaxError',
start: 0,
end: tokenLength,
message: hasInvalidEscape
? isTerminated
? 'String literal with bad escape sequence detected'
: 'Unterminated string literal with bad escape sequence detected'
: 'Unterminated string literal detected',
},
]
: [],
};
}
const reComment = /^\/\/.*/u;
export function commentMatcher(
input: string
): MatcherResult<'AND'> | undefined {
const match = reComment.exec(input);
if (match === null) return undefined;
return {
type: undefined,
data: undefined,
tokenLength: match[0].length,
syntaxErrors: [],
};
}
const reWhitespace = /^\s+/u;
export function whitespaceMatcher(
input: string
): MatcherResult<'AND'> | undefined {
const match = reWhitespace.exec(input);
if (match === null) return undefined;
return {
type: undefined,
data: undefined,
tokenLength: match[0].length,
syntaxErrors: [],
};
}