Debian Codegraph @

codemirror-legacy-modes/mode/sieve.js

raw
1 function words(str) {
2 var obj = {}, words = str.split(" ");
3 for (var i = 0; i < words.length; ++i) obj[words[i]] = true;
4 return obj;
5 }
6
7 var keywords = words("if elsif else stop require");
8 var atoms = words("true false not");
9
10 function tokenBase(stream, state) {
11
12 var ch = stream.next();
13 if (ch == "/" && stream.eat("*")) {
14 state.tokenize = tokenCComment;
15 return tokenCComment(stream, state);
16 }
17
18 if (ch === '#') {
19 stream.skipToEnd();
20 return "comment";
21 }
22
23 if (ch == "\"") {
24 state.tokenize = tokenString(ch);
25 return state.tokenize(stream, state);
26 }
27
28 if (ch == "(") {
29 state._indent.push("(");
30 // add virtual angel wings so that editor behaves...
31 // ...more sane incase of broken brackets
32 state._indent.push("{");
33 return null;
34 }
35
36 if (ch === "{") {
37 state._indent.push("{");
38 return null;
39 }
40
41 if (ch == ")") {
42 state._indent.pop();
43 state._indent.pop();
44 }
45
46 if (ch === "}") {
47 state._indent.pop();
48 return null;
49 }
50
51 if (ch == ",")
52 return null;
53
54 if (ch == ";")
55 return null;
56
57
58 if (/[{}\(\),;]/.test(ch))
59 return null;
60
61 // 1*DIGIT "K" / "M" / "G"
62 if (/\d/.test(ch)) {
63 stream.eatWhile(/[\d]/);
64 stream.eat(/[KkMmGg]/);
65 return "number";
66 }
67
68 // ":" (ALPHA / "_") *(ALPHA / DIGIT / "_")
69 if (ch == ":") {
70 stream.eatWhile(/[a-zA-Z_]/);
71 stream.eatWhile(/[a-zA-Z0-9_]/);
72
73 return "operator";
74 }
75
76 stream.eatWhile(/\w/);
77 var cur = stream.current();
78
79 // "text:" *(SP / HTAB) (hash-comment / CRLF)
80 // *(multiline-literal / multiline-dotstart)
81 // "." CRLF
82 if ((cur == "text") && stream.eat(":"))
83 {
84 state.tokenize = tokenMultiLineString;
85 return "string";
86 }
87
88 if (keywords.propertyIsEnumerable(cur))
89 return "keyword";
90
91 if (atoms.propertyIsEnumerable(cur))
92 return "atom";
93
94 return null;
95 }
96
97 function tokenMultiLineString(stream, state)
98 {
99 state._multiLineString = true;
100 // the first line is special it may contain a comment
101 if (!stream.sol()) {
102 stream.eatSpace();
103
104 if (stream.peek() == "#") {
105 stream.skipToEnd();
106 return "comment";
107 }
108
109 stream.skipToEnd();
110 return "string";
111 }
112
113 if ((stream.next() == ".") && (stream.eol()))
114 {
115 state._multiLineString = false;
116 state.tokenize = tokenBase;
117 }
118
119 return "string";
120 }
121
122 function tokenCComment(stream, state) {
123 var maybeEnd = false, ch;
124 while ((ch = stream.next()) != null) {
125 if (maybeEnd && ch == "/") {
126 state.tokenize = tokenBase;
127 break;
128 }
129 maybeEnd = (ch == "*");
130 }
131 return "comment";
132 }
133
134 function tokenString(quote) {
135 return function(stream, state) {
136 var escaped = false, ch;
137 while ((ch = stream.next()) != null) {
138 if (ch == quote && !escaped)
139 break;
140 escaped = !escaped && ch == "\\";
141 }
142 if (!escaped) state.tokenize = tokenBase;
143 return "string";
144 };
145 }
146
147 export const sieve = {
148 name: "sieve",
149 startState: function(base) {
150 return {tokenize: tokenBase,
151 baseIndent: base || 0,
152 _indent: []};
153 },
154
155 token: function(stream, state) {
156 if (stream.eatSpace())
157 return null;
158
159 return (state.tokenize || tokenBase)(stream, state);
160 },
161
162 indent: function(state, _textAfter, cx) {
163 var length = state._indent.length;
164 if (_textAfter && (_textAfter[0] == "}"))
165 length--;
166
167 if (length <0)
168 length = 0;
169
170 return length * cx.unit;
171 },
172
173 languageData: {
174 indentOnInput: /^\s*\}$/
175 }
176 };