Skip to content

Commit 6792348

Browse files
Glebov Borismarijnh
authored andcommitted
[fcl mode] Add
1 parent dbb2a5c commit 6792348

File tree

2 files changed

+281
-0
lines changed

2 files changed

+281
-0
lines changed

mode/fcl/fcl.js

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
// CodeMirror, copyright (c) by Marijn Haverbeke and others
2+
// Distributed under an MIT license: http://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+
14+
CodeMirror.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+
172+
CodeMirror.defineMIME("text/x-fcl", "fcl");
173+
});

mode/fcl/index.html

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<!doctype html>
2+
3+
<title>CodeMirror: FCL mode</title>
4+
<meta charset="utf-8"/>
5+
<link rel=stylesheet href="../../doc/docs.css">
6+
7+
<link rel="stylesheet" href="../../lib/codemirror.css">
8+
<link rel="stylesheet" href="../../theme/elegant.css">
9+
<script src="../../lib/codemirror.js"></script>
10+
<script src="../../addon/edit/matchbrackets.js"></script>
11+
<script src="fcl.js"></script>
12+
<style>.CodeMirror {border:1px solid #999; background:#ffc}</style>
13+
<div id=nav>
14+
<a href="http://codemirror.net"><h1>CodeMirror</h1><img id=logo src="../../doc/logo.png"></a>
15+
16+
<ul>
17+
<li><a href="../../index.html">Home</a>
18+
<li><a href="../../doc/manual.html">Manual</a>
19+
<li><a href="https://github.com/codemirror/codemirror">Code</a>
20+
</ul>
21+
<ul>
22+
<li><a href="../index.html">Language modes</a>
23+
<li><a class=active href="#">FCL</a>
24+
</ul>
25+
</div>
26+
27+
<article>
28+
<h2>FCL mode</h2>
29+
<form><textarea id="code" name="code">
30+
FUNCTION_BLOCK Fuzzy_FB
31+
VAR_INPUT
32+
TimeDay : REAL; (* RANGE(0 .. 23) *)
33+
ApplicateHost: REAL;
34+
TimeConfiguration: REAL;
35+
TimeRequirements: REAL;
36+
END_VAR
37+
38+
VAR_OUTPUT
39+
ProbabilityDistribution: REAL;
40+
ProbabilityAccess: REAL;
41+
END_VAR
42+
43+
FUZZIFY TimeDay
44+
TERM inside := (0, 0) (8, 1) (22,0);
45+
TERM outside := (0, 1) (8, 0) (22, 1);
46+
END_FUZZIFY
47+
48+
FUZZIFY ApplicateHost
49+
TERM few := (0, 1) (100, 0) (200, 0);
50+
TERM many := (0, 0) (100, 0) (200, 1);
51+
END_FUZZIFY
52+
53+
FUZZIFY TimeConfiguration
54+
TERM recently := (0, 1) (30, 1) (120, 0);
55+
TERM long := (0, 0) (30, 0) (120, 1);
56+
END_FUZZIFY
57+
58+
FUZZIFY TimeRequirements
59+
TERM recently := (0, 1) (30, 1) (365, 0);
60+
TERM long := (0, 0) (30, 0) (365, 1);
61+
END_FUZZIFY
62+
63+
DEFUZZIFY ProbabilityAccess
64+
TERM hight := 1;
65+
TERM medium := 0.5;
66+
TERM low := 0;
67+
ACCU: MAX;
68+
METHOD: COGS;
69+
DEFAULT := 0;
70+
END_DEFUZZIFY
71+
72+
DEFUZZIFY ProbabilityDistribution
73+
TERM hight := 1;
74+
TERM medium := 0.5;
75+
TERM low := 0;
76+
ACCU: MAX;
77+
METHOD: COGS;
78+
DEFAULT := 0;
79+
END_DEFUZZIFY
80+
81+
RULEBLOCK No1
82+
AND : MIN;
83+
RULE 1 : IF TimeDay IS outside AND ApplicateHost IS few THEN ProbabilityAccess IS hight;
84+
RULE 2 : IF ApplicateHost IS many THEN ProbabilityAccess IS hight;
85+
RULE 3 : IF TimeDay IS inside AND ApplicateHost IS few THEN ProbabilityAccess IS low;
86+
END_RULEBLOCK
87+
88+
RULEBLOCK No2
89+
AND : MIN;
90+
RULE 1 : IF ApplicateHost IS many THEN ProbabilityDistribution IS hight;
91+
END_RULEBLOCK
92+
93+
END_FUNCTION_BLOCK
94+
</textarea></form>
95+
96+
<script>
97+
var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
98+
theme: "elegant",
99+
matchBrackets: true,
100+
indentUnit: 8,
101+
tabSize: 8,
102+
indentWithTabs: true,
103+
mode: "text/x-fcl"
104+
});
105+
</script>
106+
107+
<p><strong>MIME type:</strong> <code>text/x-fcl</code></p>
108+
</article>

0 commit comments

Comments
 (0)