-
Notifications
You must be signed in to change notification settings - Fork 0
/
nos.js
128 lines (122 loc) · 3.54 KB
/
nos.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
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
/*
** This file is licensed in BSD 2 Clause.
*/
class Token{
constructor(typeName, offset, length, raw, value) {
return {typeName, offset, length, raw, value};
}
}
function saferEval(literal) {
return new Function(`return ${literal};`)();
}
function lex(string) {
// This doesn't consider UTF-32, which may be fixed soon
let tokens = [];
let raw = "";
let STATES = {
NORMAL: "NORMAL",
IN_STRING: "IN_STRING",
ESCAPE: "ESCAPE"
};
let index = 0;
let quotationMark = undefined;
let state = STATES.NORMAL;
for(let i = 0; i < string.length; ++i) {
let ch = string[i];
switch(state) {
case STATES.NORMAL:
switch(ch) {
case "\"":
case "'":
case "`":
if(raw !== "") {
tokens.push(new Token("directLiteral", index, raw.length, raw, raw));
}
raw = ch;
index = i;
quotationMark = ch;
state = STATES.IN_STRING;
break;
case " ":
if(raw !== "") {
tokens.push(new Token("directLiteral", index, raw.length, raw, raw));
}
raw = "";
index = i + 1;
break;
case "|":
if(raw !== "") {
tokens.push(new Token("directLiteral", index, raw.length, raw, raw));
}
raw = "|";
index = i;
tokens.push(new Token("operator", index, raw.length, raw, raw));
raw = "";
index = i + 1;
break;
default:
raw += ch;
break;
}
break;
case STATES.IN_STRING:
switch(ch) {
case "\\":
raw += ch;
state = STATES.ESCAPE;
break;
case quotationMark:
raw += ch;
if(quotationMark === "`") {
throw new SyntaxError("Template literal is still not support.");
}
tokens.push(new Token("stringLiteral", index, raw.length, raw, saferEval(raw)));
raw = "";
index = i + 1;
quotationMark = undefined;
state = STATES.NORMAL;
break;
default:
raw += ch;
break;
}
break;
case STATES.ESCAPE:
switch(ch) {
default:
raw += ch;
state = STATES.IN_STRING;
break;
}
break;
default:
throw new Error("This never happen!");
break;
}
}
if(state !== STATES.NORMAL) {
throw new SyntaxError(`Unclosed string literal ${raw}.`);
}
if(raw !== "") {
tokens.push(new Token("directLiteral", index, raw.length, raw, raw));
}
return tokens;
}
exports.lex = lex;
function runNOS(runtime, nos, input) {
let tokens = lex(nos);
return tokens.reduce((argss, token) => {
if(token.typeName === "operator" && token.value === "|") {
argss.push([]);
} else {
argss[argss.length - 1].push(token.value);
}
}, [[]])
.reduce((input, args) => {
if(args.length === 0) {
throw new SyntaxError("Unexpected token |");
}
runtime.execv(args[0], input, args);
}, input)
}
exports.runNOS = runNOS;