Repository navigation
Expand file tree
/
Copy pathtokeniser.ts
More file actions
424 lines (423 loc) · 19.2 KB
/
Copy pathtokeniser.ts
File metadata and controls
424 lines (423 loc) · 19.2 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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
const words_regex = /\/\*[\s\S]*?\*\/|\/\/.*|[rf]?(?:r(#+)"[\s\S]*?"\1|"(?:\\u....|\\x..|\\.|[^"\n])*?")|[@$#]\*|(?:\?&|&\?|\|\?|\?!)|[|:]>|<[|:]|>:|::?|\\|(?:!<|!>)|=>|->|[!=]==|[><!=]=?|>{1,3}|<{1,2}|%%%|([+\-*%&|^~])\2?|#(?:\.\.|[#@?/\\])|\${1,2}|[¬\\]|\s+|[\(\[\{]|[\)\]\}]|\b(?:(?:\d|[1-9][_\d]*)(?:\.[_\d]+)?|0[box][_\dA-Fa-f]+(?:\.[_\dA-Fa-f]+)?)\b|!!!|\.\.\.|\.\.=?|\.|\b\w+\b|\S/g;//TODO: add back '#.'
//note: float numbers are handed during syntax parting to allow for '3.<' aswell as '3.2'
//TODO:handle format strings: need to combine words together when a format string is encountered
//currently cannot embed format strings in other format strings
export function tokeniser_module_method(Public_Object){
const {
todo,
unimplemented,
loga,
logData,
match,
pass,
assert,
assume,
forBailOld,
forBailGenerator,
matchFlags,
printTree,
EnumSymbols,
} = Public_Object;
class Language{//base class
//external interface
constructor({compile,syntaxTree}={}){
this.#compile = compile ?? this.compile;
this.syntaxTree = syntaxTree ?? this.syntaxTree;
}
compile(text,throwError,fileName){
fileName ??= "";
throwError ??= e => {throw e}
this.currentContext = {text,throwError,fileName};
return this.#compile(this.currentContext);
}
//SyntaxTreeData
syntaxTree_regex = /\s+|[\w_]+|[()\[\]{}]|\S/g;//:Regex ; main regex for passing raw file into words
syntaxTree_getData = function(word){
return {type:"symbol",subtype:"symbol"};
}
//----
#compile(){}//:using(currentContext)->compiled object
currentContext;
getSyntaxTree(text,throwError,fileName){
return new SyntaxTree(this.syntaxTree);
}
//internal interface
}
class Errors{//for error messages that need to mark multiple words
constructor(data={}){Object.assign(this,data)}
static new(type,message,comments:[WordSymbol,String][]){
return new Errors({
type,
message,
errors:comments.map(([wordSymbol,message])=>({wordSymbol,message})),
})
}
type:String;
message:String;
errors:{wordSymbol:WordSymbol,message:String}[];//words to be underlines
getErrorString(extraIndentation = 0):String{
let lines = new Map();
for(let {wordSymbol,message} of this.errors){
lines.getOrInsert(wordSymbol.errorData.line,[]).push({wordSymbol,message});
}
todo()
}
intoError(error=e=>Error(e)):Error{
return error(this.getErrorString());
}
throwError(error=e=>Error(e)){
throw this.intoError(error);
}
}
//Syntax tree:
class WordSymbol extends String{
constructor(data={}){
super(data.word);
if(data instanceof WordSymbol){
data = {...data};
for(let i=0;i<data.length;i++)delete data[i];
}
Object.assign(this,data);
this.errorData = Object.assign(new this.constructor.ErrorData(),data.errorData);
}
clone(name?:String){
return new WordSymbol({
word:name??this.word,
afix:this.afix,
type:this.type,
subtype:this.subtype,
patternType:this.patternType,
indent:this.indent,
errorData:this.#errorData,
});
}
//
word;//:string
afix;//:Int & (!!left_arg * 2) + !!right_arg
type;//:Symbol
subtype;//:Symbol
patternType;//:((Object & Class())|string)? ; used to contain pattern data ; UNUSED
indent;//:Number ; counts from 0 ; used for parsing multiline-strings
isAfterWhiteSpace;//:bool
//when type == (number|string)
//value//:number|string ; is the evaluated version of 'word'
//valueType
//used with type == "bracket" && subtype == "open" || for many patterns in the syntax tree like 'a + b'
//contence;//:Tree(WordSymbol?)? & (when type == "parameterPattern": [WordSymbol&"bracket"]) | when 'a' from 'a=' assignmentPatturn.arguments.contence: (WordSymbol & type=="keyword")[]
//endBracket;//:WordSymbol & close bracket ; used when type == bracket open
//error data
#errorData:ErrorData;//is private so it does not show up when debugging compiler
get errorData(){return this.#errorData}
set errorData(value){this.#errorData = value}
//errorData;
throwError(...args){this.errorData.throwError(...args)}
static ErrorData = class ErrorData{
file;//:SourceFile
line;//:Number ; counts from 1
column;//:Number ; counts from 1
indent;//:Number ; counts from 0
word;
static throwError;//is message=>throw Error(message)
getErrorMsg(errorType,errorMessage,stack=undefined){
return " ERROR:\n"
+ this.display_location() + "\n"
// " ".repeat(lineLen)+" |\n"
+ this.display_markWordInLine(" " + errorType + " error") + "\n"
+ "error" + ": " + errorMessage + "\n"
;
}
throw(msg,errorFunc){
this.constructor.throwError(errorFunc(msg));
}
throwError(errorType,errorMessage,errorFunc,stack=undefined){
this.constructor.throwError(errorFunc(this.getErrorMsg(errorType,errorMessage,stack)));
}
display_location(){
return this.file.name+":"+this.line+":"+this.column;
}
display_markWordInLine(lineRaw){
let line = this.file.lines[this.line-1].substr(this.indent);
return line+"\n"+line.substr(0,(this.column-1) - this.indent).replaceAll(/./g," ")+"^".repeat(this.word.length) + lineRaw;
}
}
toString(){
return this.word;
}
}
class SyntaxTree extends Array{
//types and subtypes for the 2nd phase of building syntax tree
static type = EnumSymbols(
"whiteSpace",
"comment",
"value",//bool|number|string|special
"label",
"bracket",// '(' ')'
"operator",
"sepparator",//';'
"constant",
);
static subtype = EnumSymbols(
// whitespace
"whiteSpace",
"comment",
// bracket
"open",
"closed",
// value
"string",
"formatString",
"number",
"bool",
"object",
"null",// 'null', ';' in ';;'
//UNUSED: "undefined",//'undefined' == '{}'
// label
"operator",//operators e.g. '>' '=' in '.>' '.=' ; allows for 'a.>foo' --> 'b.>,foo'
// operator
"comparitor",
"pipeline",// '|>' '<|' ':>' '<:'
"interval",// 'a..b' , 'a..=b'
"ternary",// 'a ?& b |? c', 'b &? a |? c' for 'if a=>b else c'
"declaration",// ':' ; used for ':=' syntaxes
"assignment",// '='
"typeAnnotation",
"return",// '?' '?!'
"statement",//'if' 'while' etc... ; statements with 'statement exp => exp'
"autoParameter",// '#' e.g. '#', '#@', '#?' etc...
);
static subtype2 = EnumSymbols(//misc operators
"regex",// 'r"..."'
"dot",// '.' '#.'
"operatorAllowsDoubleExp",//statement that allow for `statement exp exp`
// bracket
"struct",// `(`
"array",// `[`
"block",// `(`
);
static AfixType = {//e.g. '!a' is prefix --> '0b01'
nofix:0b00,//'a'
postfix:0b10,//'a++'
prefix:0b01,//'++a'
infix:0b11,//'a+b'
operatorWithBothArgs:0b11,//default value
operatorWithLeftArg:0b10,//'a++'
operatorWithRightArg:0b01,//'++a'
};
constructor(text,throwError,fileName = "",regexs = {},addExtraWordData){
if(typeof text == "number"){super(text);return;}//for .forEach calls
let {allRegex, types} = regexs;
throwError ??= (msg = "", errorFunction = a => Error(a)) => {throw errorFunction(msg)};
allRegex ??= words_regex;
if(0)types ??= [
{match:"",name:""},
{match:/\s+/,name:"whiteSpace"},
{match:/^\/[/*]/,name:"comment"},
{match:/^[()\[\]{}]$/,name:"bracket"},
{match:/^(?:[+\-*^&~|]{1,2}|[/!%]|\w+|\S|={1,2})$/,name:"label"},
];
addExtraWordData ??= (wordSymbol,wordString,wordSymbols)=>wordSymbol;
//classes
//WordSymbol
//----
const file = new SourceFile(fileName);
const words = ((text,regex,file)=>{
"use strict";
let words = [];
let column = 1, line = 1, indent = 0;
let isIndenting = true;
let isAfterWhiteSpace = true;//is symbol separated by white space e.g. for '+ =' vs '+='
for(let v of text.matchAll(regex)){
let word = v[0];
let type;//:string
let wordSymbol = addExtraWordData(
new WordSymbol({
word,
errorData:{column,line,file,word,indent,match:v},
//
...((v)=>{if(!v.type)throw Error("property {type} is found but it is referencing undefined value in the SyntaxTree.type enum, for '"+word+"'");return v})(
word.match(/^\s/) ? {type:SyntaxTree.type.whiteSpace,subtype:SyntaxTree.subtype.whiteSpace}:
word.match(/^\/[/*]/) ? {type:SyntaxTree.type.whiteSpace,subtype:SyntaxTree.subtype.comment} :
word.match(/^\($/) ? {type:SyntaxTree.type.bracket,subtype:SyntaxTree.subtype.open,subtype2:SyntaxTree.subtype2.struct} :
word.match(/^\[$/) ? {type:SyntaxTree.type.bracket,subtype:SyntaxTree.subtype.open,subtype2:SyntaxTree.subtype2.array} :
word.match(/^\{$/) ? {type:SyntaxTree.type.bracket,subtype:SyntaxTree.subtype.open,subtype2:SyntaxTree.subtype2.block} :
word.match(/^[)\]}]$/) ? {type:SyntaxTree.type.bracket,subtype:SyntaxTree.subtype.closed} :
word.match(/^r(?:"|r#+")/) ? {type:SyntaxTree.type.value,subtype:SyntaxTree.subtype.string,subtype2:SyntaxTree.subtype2.regex,afix:SyntaxTree.AfixType.nofix}:
word.match(/^f(?:"|r#+")/) ? {type:SyntaxTree.type.value,subtype:SyntaxTree.subtype.formatString,afix:SyntaxTree.AfixType.nofix}:
word.match(/^"|^r#+"/) ? {type:SyntaxTree.type.value,subtype:SyntaxTree.subtype.string,afix:SyntaxTree.AfixType.nofix}:
word.match(/^(?:0[xo]?|[0-9])/) ? {type:SyntaxTree.type.value,subtype:SyntaxTree.subtype.number,afix:SyntaxTree.AfixType.nofix} :
word.match(/^(?:NaN|Infinity)$/) ? {type:SyntaxTree.type.value,subtype:SyntaxTree.subtype.number,afix:SyntaxTree.AfixType.nofix} :
word.match(/^(?:true|false)$/) ? {type:SyntaxTree.type.value,subtype:SyntaxTree.subtype.bool,afix:SyntaxTree.AfixType.nofix} :
word.match(/^null$/) ? {type:SyntaxTree.type.value,subtype:SyntaxTree.subtype.null,afix:SyntaxTree.AfixType.nofix} :
//word.match(/^undefined$/) ? {type:SyntaxTree.type.value,subtype:SyntaxTree.subtype.undefined,afix:SyntaxTree.AfixType.nofix} :
word.match(/^(?:%%%|([+\-*%&|^~])\1?|>{1,3}|<{1,2}|[!\/<>])$/) ? {type:SyntaxTree.type.operator} ://numerical operators
word.match(/^([!<>]=?|[!=]?==)$/) ? {type:SyntaxTree.type.operator} :
word.match(/^(?:\?&|[&|]\?)$/) ? {type:SyntaxTree.type.operator,subtype:SyntaxTree.subtype.ternary} ://ternary operators
word.match(/^(?:=>|->)$/) ? {type:SyntaxTree.type.operator} :
word.match(/=$/) ? {type:SyntaxTree.type.operator,subtype:SyntaxTree.subtype.assignment} ://e.g. '=' '+='
word.match(/^:$/) ? {type:SyntaxTree.type.operator,subtype:SyntaxTree.subtype.declaration} :
word.match(/^::$/) ? {type:SyntaxTree.type.operator,subtype:SyntaxTree.subtype.typeAnnotation} ://type operator
word.match(/^(?:[|:]>)$/) ? {type:SyntaxTree.type.operator,subtype:SyntaxTree.subtype.pipeline,isReversed:false} ://'|>' or ':>'
word.match(/^(?:<[|:])$/) ? {type:SyntaxTree.type.operator,subtype:SyntaxTree.subtype.pipeline,isReversed:true} ://'<|' or '<:'
word.match(/^,$/) ? {type:SyntaxTree.type.operator} ://','
word.match(/^¬$/) ? {type:SyntaxTree.type.operator} ://'¬'
word.match(/^\?!?$/) ? {type:SyntaxTree.type.operator,subtype:SyntaxTree.subtype.return} ://
word.match(/^£$/) ? {type:SyntaxTree.type.operator}://void operator
word.match(/^\.$/) ? {type:SyntaxTree.type.operator,subtype2:SyntaxTree.subtype2.dot} ://dot operator
word.match(/^#\.$/) ? {type:SyntaxTree.type.operator,subtype:SyntaxTree.subtype.autoParameter,subtype2:SyntaxTree.subtype2.dot} ://dot operator
word.match(/^\.\.=?$/) ? {type:SyntaxTree.type.operator,subtype:SyntaxTree.subtype.interval} ://interval '1..3'
word.match(/^(?:ref)$/) ? {type:SyntaxTree.type.operator} :
word.match(/^@$/) ? {type:SyntaxTree.type.operator} :
word.match(/^(?:\\)$/) ? {type:SyntaxTree.type.operator} :
word.match(/^(?:\$\$)$/) ? {type:SyntaxTree.type.operator} :
word.match(/^(?:\.\.\*)$/) ? {type:SyntaxTree.type.operator} :
word.match(/^`$/) ? {type:SyntaxTree.type.operator}:
word.match(/^#(?:\.\.|[#@!?/\\])?$/) ? {type:SyntaxTree.type.operator,subtype:SyntaxTree.subtype.autoParameter,afix:SyntaxTree.AfixType.nofix} ://'#' or '##' or '#@' in: '#name' '##'
word.match(/^\$$/) ? {type:SyntaxTree.type.operator} ://'$type' '$key'
word.match(/^[$@*]\*$/) ? {type:SyntaxTree.type.operator,afix:SyntaxTree.AfixType.prefix}://'@*' in '@* = (a=1,b=2,c=3)'
word.match(/^\.\.\.$/) ? {type:SyntaxTree.type.operator} :
word.match(/^(?:if|while|match|for)$/) ? {type:SyntaxTree.type.operator,subtype:SyntaxTree.subtype.statement,subtype2:SyntaxTree.subtype2.operatorAllowsDoubleExp} :
word.match(/^(?:break|continue|return|catch|assert|as|is)$/) ? {type:SyntaxTree.type.operator} :
word.match(/^(?:mod)$/) ? {type:SyntaxTree.type.operator} :
word.match(/^in$/) ? {type:SyntaxTree.type.operator} :
word.match(/^(?:else|do)$/) ? {type:SyntaxTree.type.operator} :
word.match(/^\w+$/) ? {type:SyntaxTree.type.label,afix:SyntaxTree.AfixType.nofix} :
word.match(/^[;]$/) ? {type:SyntaxTree.type.sepparator} :
word.match(/^"$/) ? {type:SyntaxTree.type.symbol} ://extra '"'s are caught and are handled later on
//word.match(/^\S+$/) ? "symbol":
(()=>{throw Error(`compiler error: unhandled symbol: '${word}' on line ${line}. Either add a case using 'type:SyntaxTree.type.symbol' for this or add a proper error for this case`)})()
),
indent,
isAfterWhiteSpace,//'+ ='
//parent:undefined,//assigned later, after the expression AST is constructed
}),
word,
words
);
isAfterWhiteSpace = [SyntaxTree.type.whiteSpace,SyntaxTree.type.comment].includes(wordSymbol.type);
if(word.match("\n")){
column = word.match(/(?<=\n)[^\n]*$/)[0].length+1;
line+=[...word.matchAll("\n")].length;
indent = word.match(/(?<=\n)[\t ]*(?=\N*$)/)?.[0]?.length??0;//note '\t \t' => 3 indents
isIndenting = !!word.match(/[\t ]*$/);
}else {
if(isIndenting){
indent+=word.match(/^[\t ]/);
if(word.match(/\S/))isIndenting = false;
}
column+=word.length;
}
words.push(wordSymbol);
};
file.words = words;
return words;
})(text,allRegex,file);//:WordSymbol[]
const NumberType = EnumSymbols("int","uint","float");
interface NumberLiteral {
type:NumberType,
value:Number|Number[],//where Number:i32|f32
}
const getNumber = wordSymbol => {//:NumberType
if("Inf" == wordSymbol.word)
return {valueType:NumberType,value:Infinity};
if("NaN" == wordSymbol.word)
return {valueType:NumberType,value:NaN};
const javascriptIntSize = 32;
assert((1 << javascriptIntSize) == 1);
let numberString = wordSymbol.replaceAll("_","");
let numberMatches = numberString.match(/(^.*?)(?:([IUF])([8|16|32|64|128|size])?)?$/)??[];
assert(numberMatches.length >= 2,"invalid number '" + wordSymbol + "'");
let valueString = numberMatches[1];
let type:""|"I"|"U"|"F" = numberMatches[2] ?? "";
let size:null|Number = numberMatches[3] ? +numberMatches[2] : null;
if(type[0] == "I" && numberString.includes("."))wordSymbol.throwError("syntax", "integers cannot have a decimal point", e=>Error(e));
if(type[0] == "U" && numberString.includes("."))wordSymbol.throwError("syntax", "unsigned integers cannot have a decimal point", e=>Error(e));
let value;
if(size == null || size <= javascriptIntSize)value = +valueString;
else {todo("remove this `else`branch. this untyped language does not have(or need) number types")
let [_,base,numberString] = valueString.match(/(0[box])?(.*)/);
let numbers = [];
for(let i = 0; i < numberString.length; i += javascriptIntSize){
numbers.push(+(base+numberString.substr(i,javascriptIntSize)));
}
value = numbers;
}
return {value,valueType:type};
}
const getString = wordSymbol => {//assume: string is valid
let isExtraLiteralString = !!wordSymbol.word.match(/^r?r#/);
let isRegex = wordSymbol.subtype2 == SyntaxTree.subtype2.regex;
let string = wordSymbol.word.match(/^r?(?:r#+)?"([\s\S]*)"#*/)[1]
.replace(/^\n/,"")
.replace(/\n\t*$/,"")
.replaceAll(/(\n|^)(\t+)/g,(_,m1,m2)=>m1+m2.substr(wordSymbol.indent+1))
.replaceAll("\n","\\n")
.replaceAll("\t","\\t")
;
if(isExtraLiteralString||isRegex){
string = string.replaceAll(/\\(?![nt])/g,"\\\\");
}
string = "\"" + string + "\"";
try{
string = JSON.parse(string);
}catch(err){
wordSymbol.throwError("syntax",`invalid ${isRegex?"regex":"string"} got error:"${err}"`,a=>Error(a))
}
return string;
}
const syntaxTree = ((words)=>{//()->syntaxTree:WordSymbol
let treePartList = [[]];//:(WordSymbol[] & WordSymbol().contence & Tree<WordSymbols>)[]
let bracketLevel = 0;
file.lines = text.split("\n");
let syntaxTree = words.forEach(wordSymbol=>{
let lastTree:WordSymbol[] = treePartList[treePartList.length-2];
if(wordSymbol=="\"")wordSymbol.throwError("syntax", "missing closing quote in string",a=>Error(a));
if(wordSymbol.type == SyntaxTree.type.comment || wordSymbol.type == SyntaxTree.type.whiteSpace)return;
if(wordSymbol.type == SyntaxTree.type.bracket){
if(wordSymbol.subtype == SyntaxTree.subtype.open){
treePartList[treePartList.length-1].push(wordSymbol);
treePartList.push([]);
}
else if(wordSymbol.subtype == SyntaxTree.subtype.closed){
if(!lastTree)
wordSymbol.throwError("syntax",
"extra closing bracket",
a=>Error(a))
;
let openBracket = lastTree[lastTree.length-1];//:WordSymbol ; corresponding open bracket
if({"{": "}", "[": "]", "(": ")"}[openBracket] != wordSymbol.word){
wordSymbol.throwError("syntax",
"unmatching brackets '" + openBracket.word + "' '" + wordSymbol.word + "'"
+"\nopened at: "+openBracket.errorData.display_location()+"\n"
//+openBracket.errorData.display_markWordInLine(" bracket opened", "")+"\n"
,
a=>Error(a));
}
if(treePartList.length == 1)wordSymbol.throwError("syntax", "too many closing brackets",a=>Error(a));
lastTree[lastTree.length-1].contence = treePartList.pop();
lastTree[lastTree.length-1].endBracket = wordSymbol;
}
else throw Error("compiler error: impossible case '"+wordSymbol+"'");
}
else treePartList[treePartList.length-1].push(wordSymbol);
if(wordSymbol.subtype == SyntaxTree.subtype.string || wordSymbol.subtype == SyntaxTree.subtype.formatString)
wordSymbol.value = getString(wordSymbol);
if(wordSymbol.subtype == SyntaxTree.subtype.number)Object.assign(wordSymbol,getNumber(wordSymbol));
});
let tree;//temporty variable
if(treePartList.length > 1)(tree=treePartList[0])[tree.length-1].throwError("syntax", "unclosed bracket",a=>Error(a));
return treePartList[0];
})(words);
super(...syntaxTree);
}
}
class SourceFile{//used for error data
constructor(name){
this.name = name;
}
name;//:string
words;//:WordSymbol[]
lines;//:string[]
};
//----
return {SyntaxTree, WordSymbol};
}