-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.js
60 lines (55 loc) · 1.22 KB
/
lexer.js
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
const moo = require('moo')
const fs = require('fs/promises')
const os = require('os')
let newLine = {}
if(os.platform() === 'win32'){
newLine = { match: /[\r\n]+/, lineBreaks: true }
}
else{
newLine = { match: /\n/, lineBreaks: true }
}
let lexer = moo.compile({
WS: /[ \t]+/,
comment: /\/\/.*?$/,
number: {match:/0|[1-9][0-9]*/, value: n => Number(n)},
string: /"(?:\\["\\]|[^\n"\\])*"/,
lparen: '(',
rparen: ')',
lbrace: '{',
rbrace: '}',
lbrack: '[',
rbrack: ']',
identifier: /[a-zA-Z][a-zA-Z_0-9]*/,
dequal: '===',
NL: newLine,
true: {match: 'true', value: n => true},
false: {match: 'false', value: n => false},
comma: ',',
kind: "mut",
function: 'fn',
if: 'if',
else: 'else',
elif: 'elif',
great: '>',
less: '<',
and: 'and',
or: 'or',
equal: '==',
assign: '=',
ge: '>=',
le: '<=',
return: 'return'
})
lexer.reset(`name = () => {
print("Hello")
}`)
function tokens(){
while (true) {
const token = lexer.next()
if(!token){
break;
}
console.log(token);
}
}
module.exports = lexer;