-
-
Notifications
You must be signed in to change notification settings - Fork 185
Expand file tree
/
Copy pathfind-lua-requires.ts
More file actions
168 lines (139 loc) · 4.66 KB
/
find-lua-requires.ts
File metadata and controls
168 lines (139 loc) · 4.66 KB
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
export interface LuaRequire {
from: number;
to: number;
requirePath: string;
}
export function findLuaRequires(lua: string): LuaRequire[] {
return findRequire(lua, 0);
}
function findRequire(lua: string, offset: number): LuaRequire[] {
const result = [];
while (offset < lua.length) {
const c = lua[offset];
if (
c === "r" &&
(offset === 0 ||
isWhitespace(lua[offset - 1]) ||
lua[offset - 1] === "]" ||
lua[offset - 1] === "(" ||
lua[offset - 1] === "[")
) {
const m = matchRequire(lua, offset);
if (m.matched) {
offset = m.match.to + 1;
result.push(m.match);
} else {
offset = m.end;
}
} else if (c === '"' || c === "'") {
offset = readString(lua, offset, c).offset; // Skip string and surrounding quotes
} else if (c === "-" && offset + 1 < lua.length && lua[offset + 1] === "-") {
offset = skipComment(lua, offset);
} else {
offset++;
}
}
return result;
}
type MatchResult<T> = { matched: true; match: T } | { matched: false; end: number };
function matchRequire(lua: string, offset: number): MatchResult<LuaRequire> {
const start = offset;
for (const c of "require") {
if (offset > lua.length) {
return { matched: false, end: offset };
}
if (lua[offset] !== c) {
return { matched: false, end: offset };
}
offset++;
}
offset = skipWhitespace(lua, offset);
let hasParentheses = false;
if (offset > lua.length) {
return { matched: false, end: offset };
} else {
if (lua[offset] === "(") {
hasParentheses = true;
offset++;
offset = skipWhitespace(lua, offset);
} else if (lua[offset] === '"' || lua[offset] === "'") {
// require without parentheses
} else {
// otherwise fail match
return { matched: false, end: offset };
}
}
if (offset > lua.length || (lua[offset] !== '"' && lua[offset] !== "'")) {
return { matched: false, end: offset };
}
const { value: requireString, offset: offsetAfterString } = readString(lua, offset, lua[offset]);
offset = offsetAfterString; // Skip string and surrounding quotes
if (hasParentheses) {
offset = skipWhitespace(lua, offset);
if (offset > lua.length || lua[offset] !== ")") {
return { matched: false, end: offset };
}
offset++;
}
return { matched: true, match: { from: start, to: offset - 1, requirePath: requireString } };
}
function readString(lua: string, offset: number, delimiter: string): { value: string; offset: number } {
expect(lua, offset, delimiter);
offset++;
let start = offset;
let result = "";
let escaped = false;
while (offset < lua.length && (lua[offset] !== delimiter || escaped)) {
if (lua[offset] === "\\" && !escaped) {
escaped = true;
} else {
if (lua[offset] === delimiter) {
result += lua.slice(start, offset - 1);
start = offset;
}
escaped = false;
}
offset++;
}
if (offset < lua.length) {
expect(lua, offset, delimiter);
}
result += lua.slice(start, offset);
return { value: result, offset: offset + 1 };
}
function skipWhitespace(lua: string, offset: number): number {
while (offset < lua.length && isWhitespace(lua[offset])) {
offset++;
}
return offset;
}
function isWhitespace(c: string): boolean {
return c === " " || c === "\t" || c === "\r" || c === "\n";
}
function skipComment(lua: string, offset: number): number {
expect(lua, offset, "-");
expect(lua, offset + 1, "-");
offset += 2;
if (offset + 1 < lua.length && lua[offset] === "[" && lua[offset + 1] === "[") {
return skipMultiLineComment(lua, offset);
} else {
return skipSingleLineComment(lua, offset);
}
}
function skipMultiLineComment(lua: string, offset: number): number {
while (offset < lua.length && !(lua[offset] === "]" && lua[offset - 1] === "]")) {
offset++;
}
return offset + 1;
}
function skipSingleLineComment(lua: string, offset: number): number {
while (offset < lua.length && lua[offset] !== "\n") {
offset++;
}
return offset + 1;
}
function expect(lua: string, offset: number, char: string) {
if (lua[offset] !== char) {
throw new Error(`Expected ${char} at position ${offset} but found ${lua[offset]}`);
}
}