forked from considerate/simple-interpreter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtokenizer.js
More file actions
73 lines (66 loc) · 1.4 KB
/
tokenizer.js
File metadata and controls
73 lines (66 loc) · 1.4 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
module.exports = tokenize;
var syntax = {
number: /^(-?\d+)/,
identifier: /^([a-zA-Zåäö]+[a-zA-Zåäö_$\d]*)/,
operator: /^([:=+*\.])/,
fullstop: /^(\.)/,
space: /^(\s+)/,
};
function capitalize(string)
{
return string.charAt(0).toUpperCase() + string.slice(1);
}
var peek = {};
var get = {};
var syntaxes = ['operator','number','identifier', 'fullstop', 'space'];
syntaxes.forEach(function(word) {
var caps = capitalize(word);
peek[word] = function (input, index) {
var text = input.substring(index);
var match = syntax[word].exec(text);
if(match) {
return {
text: match[1],
length: match[0].length
};
}
return null;
};
get[word] = function(input, index) {
var text = input.substring(index);
var match = syntax[word].exec(text);
var src = match[1];
return {
token: {
src: src,
type: word
},
length: match[0].length
};
}
}, this);
function tokenize(input) {
var index = 0;
var tokens = [];
function hasInput(input, index) {
if(input.length === index) {
return false;
}
return true;
}
while(hasInput(input, index)) {
var didHaveToken = false;
syntaxes.forEach(function (tokentype) {
if(peek[tokentype](input,index)) {
var info = get[tokentype](input,index);
index += info.length;
tokens.push(info.token);
didHaveToken = true;
}
});
if(!didHaveToken) {
throw new Error('Syntax error.');
}
}
return tokens;
};