Debian Codegraph @

codemirror-legacy-modes/mode/solr.js

raw
1 var isStringChar = /[^\s\|\!\+\-\*\?\~\^\&\:\(\)\[\]\{\}\"\\]/;
2 var isOperatorChar = /[\|\!\+\-\*\?\~\^\&]/;
3 var isOperatorString = /^(OR|AND|NOT|TO)$/;
4
5 function isNumber(word) {
6 return parseFloat(word).toString() === word;
7 }
8
9 function tokenString(quote) {
10 return function(stream, state) {
11 var escaped = false, next;
12 while ((next = stream.next()) != null) {
13 if (next == quote && !escaped) break;
14 escaped = !escaped && next == "\\";
15 }
16
17 if (!escaped) state.tokenize = tokenBase;
18 return "string";
19 };
20 }
21
22 function tokenOperator(operator) {
23 return function(stream, state) {
24 if (operator == "|")
25 stream.eat(/\|/);
26 else if (operator == "&")
27 stream.eat(/\&/);
28
29 state.tokenize = tokenBase;
30 return "operator";
31 };
32 }
33
34 function tokenWord(ch) {
35 return function(stream, state) {
36 var word = ch;
37 while ((ch = stream.peek()) && ch.match(isStringChar) != null) {
38 word += stream.next();
39 }
40
41 state.tokenize = tokenBase;
42 if (isOperatorString.test(word))
43 return "operator";
44 else if (isNumber(word))
45 return "number";
46 else if (stream.peek() == ":")
47 return "propertyName";
48 else
49 return "string";
50 };
51 }
52
53 function tokenBase(stream, state) {
54 var ch = stream.next();
55 if (ch == '"')
56 state.tokenize = tokenString(ch);
57 else if (isOperatorChar.test(ch))
58 state.tokenize = tokenOperator(ch);
59 else if (isStringChar.test(ch))
60 state.tokenize = tokenWord(ch);
61
62 return (state.tokenize != tokenBase) ? state.tokenize(stream, state) : null;
63 }
64
65 export const solr = {
66 name: "solr",
67
68 startState: function() {
69 return {
70 tokenize: tokenBase
71 };
72 },
73
74 token: function(stream, state) {
75 if (stream.eatSpace()) return null;
76 return state.tokenize(stream, state);
77 }
78 };