UNPKG

4.7 kBJavaScriptView Raw
1// CodeMirror, copyright (c) by Marijn Haverbeke and others
2// Distributed under an MIT license: https://codemirror.net/LICENSE
3
4(function(mod) {
5 if (typeof exports == "object" && typeof module == "object") // CommonJS
6 mod(require("../../lib/codemirror"));
7 else if (typeof define == "function" && define.amd) // AMD
8 define(["../../lib/codemirror"], mod);
9 else // Plain browser env
10 mod(CodeMirror);
11})(function(CodeMirror) {
12"use strict";
13
14CodeMirror.defineMode("fcl", function(config) {
15 var indentUnit = config.indentUnit;
16
17 var keywords = {
18 "term": true,
19 "method": true, "accu": true,
20 "rule": true, "then": true, "is": true, "and": true, "or": true,
21 "if": true, "default": true
22 };
23
24 var start_blocks = {
25 "var_input": true,
26 "var_output": true,
27 "fuzzify": true,
28 "defuzzify": true,
29 "function_block": true,
30 "ruleblock": true
31 };
32
33 var end_blocks = {
34 "end_ruleblock": true,
35 "end_defuzzify": true,
36 "end_function_block": true,
37 "end_fuzzify": true,
38 "end_var": true
39 };
40
41 var atoms = {
42 "true": true, "false": true, "nan": true,
43 "real": true, "min": true, "max": true, "cog": true, "cogs": true
44 };
45
46 var isOperatorChar = /[+\-*&^%:=<>!|\/]/;
47
48 function tokenBase(stream, state) {
49 var ch = stream.next();
50
51 if (/[\d\.]/.test(ch)) {
52 if (ch == ".") {
53 stream.match(/^[0-9]+([eE][\-+]?[0-9]+)?/);
54 } else if (ch == "0") {
55 stream.match(/^[xX][0-9a-fA-F]+/) || stream.match(/^0[0-7]+/);
56 } else {
57 stream.match(/^[0-9]*\.?[0-9]*([eE][\-+]?[0-9]+)?/);
58 }
59 return "number";
60 }
61
62 if (ch == "/" || ch == "(") {
63 if (stream.eat("*")) {
64 state.tokenize = tokenComment;
65 return tokenComment(stream, state);
66 }
67 if (stream.eat("/")) {
68 stream.skipToEnd();
69 return "comment";
70 }
71 }
72 if (isOperatorChar.test(ch)) {
73 stream.eatWhile(isOperatorChar);
74 return "operator";
75 }
76 stream.eatWhile(/[\w\$_\xa1-\uffff]/);
77
78 var cur = stream.current().toLowerCase();
79 if (keywords.propertyIsEnumerable(cur) ||
80 start_blocks.propertyIsEnumerable(cur) ||
81 end_blocks.propertyIsEnumerable(cur)) {
82 return "keyword";
83 }
84 if (atoms.propertyIsEnumerable(cur)) return "atom";
85 return "variable";
86 }
87
88
89 function tokenComment(stream, state) {
90 var maybeEnd = false, ch;
91 while (ch = stream.next()) {
92 if ((ch == "/" || ch == ")") && maybeEnd) {
93 state.tokenize = tokenBase;
94 break;
95 }
96 maybeEnd = (ch == "*");
97 }
98 return "comment";
99 }
100
101 function Context(indented, column, type, align, prev) {
102 this.indented = indented;
103 this.column = column;
104 this.type = type;
105 this.align = align;
106 this.prev = prev;
107 }
108
109 function pushContext(state, col, type) {
110 return state.context = new Context(state.indented, col, type, null, state.context);
111 }
112
113 function popContext(state) {
114 if (!state.context.prev) return;
115 var t = state.context.type;
116 if (t == "end_block")
117 state.indented = state.context.indented;
118 return state.context = state.context.prev;
119 }
120
121 // Interface
122
123 return {
124 startState: function(basecolumn) {
125 return {
126 tokenize: null,
127 context: new Context((basecolumn || 0) - indentUnit, 0, "top", false),
128 indented: 0,
129 startOfLine: true
130 };
131 },
132
133 token: function(stream, state) {
134 var ctx = state.context;
135 if (stream.sol()) {
136 if (ctx.align == null) ctx.align = false;
137 state.indented = stream.indentation();
138 state.startOfLine = true;
139 }
140 if (stream.eatSpace()) return null;
141
142 var style = (state.tokenize || tokenBase)(stream, state);
143 if (style == "comment") return style;
144 if (ctx.align == null) ctx.align = true;
145
146 var cur = stream.current().toLowerCase();
147
148 if (start_blocks.propertyIsEnumerable(cur)) pushContext(state, stream.column(), "end_block");
149 else if (end_blocks.propertyIsEnumerable(cur)) popContext(state);
150
151 state.startOfLine = false;
152 return style;
153 },
154
155 indent: function(state, textAfter) {
156 if (state.tokenize != tokenBase && state.tokenize != null) return 0;
157 var ctx = state.context;
158
159 var closing = end_blocks.propertyIsEnumerable(textAfter);
160 if (ctx.align) return ctx.column + (closing ? 0 : 1);
161 else return ctx.indented + (closing ? 0 : indentUnit);
162 },
163
164 electricChars: "ryk",
165 fold: "brace",
166 blockCommentStart: "(*",
167 blockCommentEnd: "*)",
168 lineComment: "//"
169 };
170});
171
172CodeMirror.defineMIME("text/x-fcl", "fcl");
173});