-
Notifications
You must be signed in to change notification settings - Fork 2
/
lambdacalc.js
159 lines (143 loc) · 4.07 KB
/
lambdacalc.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
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
export const defineMode = function(CodeMirror) {
CodeMirror.defineMode("lambdacalc", function(_config, modeConfig) {
// Modes for different types of symbols
const DEFNAME = "variable-2";
const EQUALS = "text";
const BRACKETS = "bracket";
const LAMBDA = "keyword";
const DOT = LAMBDA;
const PREDEF = "text";
const BOUND = "text";
const ARGS = "def";
const HOLE = "atom";
const NUMBER = "number";
const EMPTY = "text";
const UNDEF = "error";
const REDEF = "variable-3";
const SUPPRESS = "text";
const FAIL = "error";
const defName = /[a-zA-Z][-'\w]*\??/
const assign = /=/
const brack = /\(|\)/
const lamArg = /[_a-zA-Z][-'\w]*\??|\./
const numconst = /-?\d+/
function expectDefOrTerm(stream, state) {
if (stream.match(/.*=/, false)) return expectDef(stream, state);
else return expectTerm(stream, state);
}
function expectDef(stream, state) {
const name = (stream.match(defName)||[])[0];
state.f = expectAssign;
if (!name || !(stream.match(/\s*=/, false))) return null;
const res = [];
if (state.defined.includes(name)) res.push(REDEF);
state.defined.push(name);
res.push(DEFNAME);
return res.join(" ");
}
function expectAssign(stream, state) {
if (!stream.match(assign)) return null;
state.f = expectTerm;
return EQUALS;
}
function expectTerm(stream, state) {
return brackets(stream, state)
|| lambda(stream, state)
|| namedTerm(stream, state)
|| number(stream, state);
}
function brackets(stream, state) {
const v = stream.eat(brack);
if (!v) return null;
if (v == '(' && stream.peek() == ')') {
stream.next();
return EMPTY;
}
if (v == '(') {
state.depth.push(stream.column() + stream.indentation());
state.bound.push([]);
}
else {
if (!(state.depth.length && state.bound.length)) return FAIL;
state.depth.pop();
state.bound.pop();
}
state.f = expectTerm;
return BRACKETS;
}
function lambda(stream, state) {
if (!stream.eat("\\")) return null;
state.f = expectArg;
return LAMBDA;
}
function namedTerm(stream, state) {
const res = (stream.match(defName)||[])[0];
if (!res) return null;
if (state.bound.some(v=>v.includes(res))) return BOUND;
if (state.defined.includes(res)) return PREDEF;
return UNDEF;
}
function number(stream, state) {
const num = (stream.match(numconst)||[])[0];
return num && (/\s|\)/.test(stream.peek()) || stream.eol()) ? NUMBER : null;
}
function expectArg(stream, state) {
const arg = (stream.match(lamArg)||[])[0];
if (!arg) return null;
if (arg === '.') {
state.f = expectTerm;
return DOT;
}
if (arg[0] === '_') return HOLE;
state.bound[state.bound.length-1].push(arg);
return ARGS;
}
function onFail(stream, state) {
stream.match(/[^\s#]*/);
return FAIL ;
}
return {
startState: function () { return {
f: expectDef,
depth: [],
defined: [],
bound: [[]],
debug: false
}; },
copyState: function (s) { return {
f: s.f,
depth: [...s.depth],
defined: [...s.defined],
bound: s.bound.map(v=>[...v]),
debug: s.debug
}; },
token: function(stream, state) {
if (stream.eat(/\t/)) return FAIL;
if (/[ \n]/.test(stream.peek())) {
stream.eatWhile(/[ \n]/);
return;
}
if (stream.peek() === '#') {
if (stream.match(/^#debug/))
state.debug = !state.debug;
stream.skipToEnd();
return "comment"
}
if (stream.sol() && state.depth.length === 0) {
state.bound = [[]];
state.f = expectDef;
}
const res = state.f(stream, state)
|| (state.debug ? null : expectDefOrTerm(stream, state))
|| onFail(stream, state);
return !state.debug && res == FAIL ? SUPPRESS : res ;
},
indent: function(state, textAfter) {
if (!state.depth.length) return 0;
return state.depth[state.depth.length-1] + 2;
},
lineComment: "#",
};
});
CodeMirror.defineMIME("text/x-lambdacalc", "lambdacalc");
};