Skip to content

Commit 574af61

Browse files
committed
Fix and improve
1 parent 1ccad77 commit 574af61

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

lambdacalc.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ CodeMirror.defineMode("lambdacalc", function(_config, modeConfig) {
1414
const EMPTY = "text";
1515
const UNDEF = "error";
1616
const REDEF = "variable-3";
17+
const SUPPRESS = "text";
1718
const FAIL = "error";
1819

1920
const defName = /[a-zA-Z][a-zA-Z0-9_\-']*/
@@ -23,14 +24,14 @@ CodeMirror.defineMode("lambdacalc", function(_config, modeConfig) {
2324
const numconst = /\d+/
2425

2526
function expectDefOrTerm(stream, state) {
26-
return expectDef(stream, state)
27-
|| (state.debug ? null : expectTerm(stream, state));
27+
if (stream.match(/.*=/, false)) return expectDef(stream, state);
28+
else return expectTerm(stream, state);
2829
}
2930

3031
function expectDef(stream, state) {
3132
const name = (stream.match(defName)||[])[0];
3233
state.f = expectAssign;
33-
if (!name || !(/[=\s]/.test(stream.peek()) || stream.eol())) return null;
34+
if (!name || !(stream.match(/\s*=/, false))) return null;
3435
const res = [];
3536
if (state.defined.includes(name)) res.push(REDEF);
3637
state.defined.push(name);
@@ -81,7 +82,7 @@ CodeMirror.defineMode("lambdacalc", function(_config, modeConfig) {
8182
if (!res) return null;
8283
if (state.bound.some(v=>v.includes(res))) return BOUND;
8384
if (state.defined.includes(res)) return PREDEF;
84-
return state.debug ? UNDEF : "text";
85+
return UNDEF;
8586
}
8687

8788
function number(stream, state) {
@@ -103,12 +104,12 @@ CodeMirror.defineMode("lambdacalc", function(_config, modeConfig) {
103104

104105
function onFail(stream, state) {
105106
stream.match(/[^\s]*/);
106-
return FAIL
107+
return FAIL ;
107108
}
108109

109110
return {
110111
startState: function () { return {
111-
f: expectDefOrTerm,
112+
f: expectDef,
112113
depth: [],
113114
defined: [],
114115
bound: [[]],
@@ -136,13 +137,15 @@ CodeMirror.defineMode("lambdacalc", function(_config, modeConfig) {
136137
}
137138
if (stream.sol() && state.depth.length === 0) {
138139
state.bound = [[]];
139-
state.f = expectDefOrTerm;
140+
state.f = expectDef;
140141
}
141-
return state.f(stream, state) || onFail(stream, state);
142+
const res = state.f(stream, state)
143+
|| (state.debug ? null : expectDefOrTerm(stream, state))
144+
|| onFail(stream, state);
145+
return !state.debug && res == FAIL ? SUPPRESS : res ;
142146
},
143147

144148
indent: function(state, textAfter) {
145-
console.log(state.depth);
146149
if (!state.depth.length) return 0;
147150
return state.depth[state.depth.length-1] + 2;
148151
},

0 commit comments

Comments
 (0)