-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.ts
executable file
·252 lines (250 loc) · 6.13 KB
/
mod.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/**
* author: Rudy Alula
* project: exium
* description: global lexer for ogone
*
* we will use this lexer in different engine
* to make it isomorphic with Node and Deno
* this project shouldn't use the Deno namespace
*/
import type {
ContextReader,
CursorDescriber,
ExiumParseOptions,
Position,
} from "./src/types/main.d.ts";
import { Reason } from "./src/enums/error-reason.ts";
import { ContextTypes } from "./src/enums/context-types.ts";
import { ExiumContext } from "./src/classes/ExiumContext.ts";
import { ExiumDocument } from "./src/classes/ExiumDocument.ts";
import {
getUnexpected,
isEOF,
readCommentBlockCtx,
readCommentCtx,
readComponentCtx,
readExportComponentStatementsCtx,
readHTMLCommentCtx,
readImportAmbientCtx,
readImportStatementsCtx,
readLineBreakCtx,
readMultiSpacesCtx,
readNodeCtx,
readProtocolCtx,
readSpaceCtx,
readStringDoubleQuoteCtx,
readStringSingleQuoteCtx,
readStringTemplateQuoteCtx,
readStyleSheetCtx,
readTextnodeCtx,
topCTX,
} from "./src/functions/index.ts";
export { ContextTypes, ExiumContext, ExiumDocument, Reason };
export type { Position };
/**
* @README
* this class exists to improve the performances
* of the Ogone Compiler
* it should provide all the contexts of an Ogone Component
* and expose a way to parse error and unexpected tokens
*
* Ogone 0.29.0: a component can't reach 250 lines without performance issues
*
* so this lexer should go quick without any issues
*
* @usage
* ```typescript
* const exium = new Exium((
* reason: string,
* cursor: CursorDescriber,
* context: ExiumContext,
* ) => {
* // do things when there's an unexpected token
* });
*
* // to parse stylesheets (CSS and Typed-CSS)
* exium.readSync(text, { type: 'stylesheet' });
*
* // to test the lexic
* exium.readSync(text, { type: 'lexer' });
*
* // to parse ogone components
* exium.readSync(text, { type: "ogone" });
*
* // to parse protocols
* exium.readSync(text, { type: 'protocol' });
*
* // to parse custom languages
* exium.readSync(text, {
* type: 'custom',
* contexts: []
* });
* ```
*/
export class Exium {
public treePosition = 0;
public allowPseudoProperties = true;
public isInPseudoProperty = false;
/**
* you should save here all the retrieved context
* of the document
*/
public currentContexts: ExiumContext[] = [];
// to retrieve all remaining open tag
public openTags: ExiumContext[] = [];
/**
* this will shift the cursor into the document
*/
public cursor: CursorDescriber = {
x: 0,
};
public source = "";
public parseOptions: ExiumParseOptions | null = null;
constructor(
/**
* function used when Exium find an unexpected token
*/
public onError: (
reason: Reason,
cursor: CursorDescriber,
context: ExiumContext,
) => void,
) {}
private scopedTopLevel: Record<
ExiumParseOptions["type"],
ContextReader[]
> = {
/**
* use this scope to test the lexer
*/
lexer: [
readCommentCtx,
readCommentBlockCtx,
readLineBreakCtx,
readMultiSpacesCtx,
readSpaceCtx,
readStringTemplateQuoteCtx,
],
/**
* use this scope to parse ogone components
*/
ogone: [
readCommentCtx,
readCommentBlockCtx,
readLineBreakCtx,
readMultiSpacesCtx,
readSpaceCtx,
readStringSingleQuoteCtx,
readStringDoubleQuoteCtx,
readImportAmbientCtx,
readImportStatementsCtx,
readHTMLCommentCtx,
readNodeCtx,
readStyleSheetCtx,
readProtocolCtx,
readTextnodeCtx,
],
/**
* use this scope to parse bio components
*/
bio: [
readCommentCtx,
readCommentBlockCtx,
readLineBreakCtx,
readMultiSpacesCtx,
readSpaceCtx,
readStringSingleQuoteCtx,
readStringDoubleQuoteCtx,
readImportAmbientCtx,
readImportStatementsCtx,
readHTMLCommentCtx,
readExportComponentStatementsCtx,
readComponentCtx,
readTextnodeCtx,
],
script: [
readCommentCtx,
readCommentBlockCtx,
readLineBreakCtx,
readMultiSpacesCtx,
readSpaceCtx,
readImportAmbientCtx,
readImportStatementsCtx,
],
/**
* use this scope to parse stylesheets (CSS and Typed-CSS)
*/
stylesheet: [
readCommentCtx,
readCommentBlockCtx,
readLineBreakCtx,
readMultiSpacesCtx,
readSpaceCtx,
readStyleSheetCtx,
],
/**
* use this scope to parse ogone components protocols
*/
protocol: [
readLineBreakCtx,
readMultiSpacesCtx,
readSpaceCtx,
readProtocolCtx,
],
custom: [],
};
/**
* parse the text and retrieve all the contexts
*/
readSync(text: string, opts: ExiumParseOptions): ExiumContext[] {
try {
/**remove previous contexts */
this.currentContexts.length && this.currentContexts.splice(0);
/**
* save the options argument
*/
this.parseOptions = opts;
/**
* save the current parsed text
* to source
* used internally
*/
this.source = text;
/**
* retrieve the top level contexts
* if custom is used as the opts.type of the method
* push the opts.contexts or an empty array
*/
const toplevel = this.scopedTopLevel[opts.type];
if (opts.type === "custom") {
toplevel.push(...(opts.contexts || []));
}
while (!isEOF(this)) {
// we are at the top level
// start using context readers
const isValid = topCTX(this, toplevel);
if (!isValid) {
this.onError(
Reason.UnexpectedToken,
this.cursor,
getUnexpected(this),
);
break;
}
}
if (this.openTags.length) {
const lastNode = this.openTags[this.openTags.length - 1];
this.onError(Reason.HTMLTagNotClosed, this.cursor, lastNode);
}
return this.currentContexts;
} catch (err) {
throw err;
}
}
/**
* return position of the token in the source
*/
getPositionSync(context: ExiumContext): Position {
return context.getPosition(this.source);
}
}