Debian Codegraph @

codemirror-legacy-modes/mode/fcl.js

raw
1 var keywords = {
2 "term": true,
3 "method": true, "accu": true,
4 "rule": true, "then": true, "is": true, "and": true, "or": true,
5 "if": true, "default": true
6 };
7
8 var start_blocks = {
9 "var_input": true,
10 "var_output": true,
11 "fuzzify": true,
12 "defuzzify": true,
13 "function_block": true,
14 "ruleblock": true
15 };
16
17 var end_blocks = {
18 "end_ruleblock": true,
19 "end_defuzzify": true,
20 "end_function_block": true,
21 "end_fuzzify": true,
22 "end_var": true
23 };
24
25 var atoms = {
26 "true": true, "false": true, "nan": true,
27 "real": true, "min": true, "max": true, "cog": true, "cogs": true
28 };
29
30 var isOperatorChar = /[+\-*&^%:=<>!|\/]/;
31
32 function tokenBase(stream, state) {
33 var ch = stream.next();
34
35 if (/[\d\.]/.test(ch)) {
36 if (ch == ".") {
37 stream.match(/^[0-9]+([eE][\-+]?[0-9]+)?/);
38 } else if (ch == "0") {
39 stream.match(/^[xX][0-9a-fA-F]+/) || stream.match(/^0[0-7]+/);
40 } else {
41 stream.match(/^[0-9]*\.?[0-9]*([eE][\-+]?[0-9]+)?/);
42 }
43 return "number";
44 }
45
46 if (ch == "/" || ch == "(") {
47 if (stream.eat("*")) {
48 state.tokenize = tokenComment;
49 return tokenComment(stream, state);
50 }
51 if (stream.eat("/")) {
52 stream.skipToEnd();
53 return "comment";
54 }
55 }
56 if (isOperatorChar.test(ch)) {
57 stream.eatWhile(isOperatorChar);
58 return "operator";
59 }
60 stream.eatWhile(/[\w\$_\xa1-\uffff]/);
61
62 var cur = stream.current().toLowerCase();
63 if (keywords.propertyIsEnumerable(cur) ||
64 start_blocks.propertyIsEnumerable(cur) ||
65 end_blocks.propertyIsEnumerable(cur)) {
66 return "keyword";
67 }
68 if (atoms.propertyIsEnumerable(cur)) return "atom";
69 return "variable";
70 }
71
72
73 function tokenComment(stream, state) {
74 var maybeEnd = false, ch;
75 while (ch = stream.next()) {
76 if ((ch == "/" || ch == ")") && maybeEnd) {
77 state.tokenize = tokenBase;
78 break;
79 }
80 maybeEnd = (ch == "*");
81 }
82 return "comment";
83 }
84
85 function Context(indented, column, type, align, prev) {
86 this.indented = indented;
87 this.column = column;
88 this.type = type;
89 this.align = align;
90 this.prev = prev;
91 }
92
93 function pushContext(state, col, type) {
94 return state.context = new Context(state.indented, col, type, null, state.context);
95 }
96
97 function popContext(state) {
98 if (!state.context.prev) return;
99 var t = state.context.type;
100 if (t == "end_block")
101 state.indented = state.context.indented;
102 return state.context = state.context.prev;
103 }
104
105 // Interface
106
107 export const fcl = {
108 name: "fcl",
109 startState: function(indentUnit) {
110 return {
111 tokenize: null,
112 context: new Context(-indentUnit, 0, "top", false),
113 indented: 0,
114 startOfLine: true
115 };
116 },
117
118 token: function(stream, state) {
119 var ctx = state.context;
120 if (stream.sol()) {
121 if (ctx.align == null) ctx.align = false;
122 state.indented = stream.indentation();
123 state.startOfLine = true;
124 }
125 if (stream.eatSpace()) return null;
126
127 var style = (state.tokenize || tokenBase)(stream, state);
128 if (style == "comment") return style;
129 if (ctx.align == null) ctx.align = true;
130
131 var cur = stream.current().toLowerCase();
132
133 if (start_blocks.propertyIsEnumerable(cur)) pushContext(state, stream.column(), "end_block");
134 else if (end_blocks.propertyIsEnumerable(cur)) popContext(state);
135
136 state.startOfLine = false;
137 return style;
138 },
139
140 indent: function(state, textAfter, cx) {
141 if (state.tokenize != tokenBase && state.tokenize != null) return 0;
142 var ctx = state.context;
143
144 var closing = end_blocks.propertyIsEnumerable(textAfter);
145 if (ctx.align) return ctx.column + (closing ? 0 : 1);
146 else return ctx.indented + (closing ? 0 : cx.unit);
147 },
148
149 languageData: {
150 commentTokens: {line: "//", block: {open: "(*", close: "*)"}}
151 }
152 };
153