scripts/CodeMirror/mode/solr/solr.js (view 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("solr", function() {
15 "use strict";
16
17 var isStringChar = /[^\s\|\!\+\-\*\?\~\^\&\:\(\)\[\]\{\}\"\\]/;
18 var isOperatorChar = /[\|\!\+\-\*\?\~\^\&]/;
19 var isOperatorString = /^(OR|AND|NOT|TO)$/i;
20
21 function isNumber(word) {
22 return parseFloat(word).toString() === word;
23 }
24
25 function tokenString(quote) {
26 return function(stream, state) {
27 var escaped = false, next;
28 while ((next = stream.next()) != null) {
29 if (next == quote && !escaped) break;
30 escaped = !escaped && next == "\\";
31 }
32
33 if (!escaped) state.tokenize = tokenBase;
34 return "string";
35 };
36 }
37
38 function tokenOperator(operator) {
39 return function(stream, state) {
40 var style = "operator";
41 if (operator == "+")
42 style += " positive";
43 else if (operator == "-")
44 style += " negative";
45 else if (operator == "|")
46 stream.eat(/\|/);
47 else if (operator == "&")
48 stream.eat(/\&/);
49 else if (operator == "^")
50 style += " boost";
51
52 state.tokenize = tokenBase;
53 return style;
54 };
55 }
56
57 function tokenWord(ch) {
58 return function(stream, state) {
59 var word = ch;
60 while ((ch = stream.peek()) && ch.match(isStringChar) != null) {
61 word += stream.next();
62 }
63
64 state.tokenize = tokenBase;
65 if (isOperatorString.test(word))
66 return "operator";
67 else if (isNumber(word))
68 return "number";
69 else if (stream.peek() == ":")
70 return "field";
71 else
72 return "string";
73 };
74 }
75
76 function tokenBase(stream, state) {
77 var ch = stream.next();
78 if (ch == '"')
79 state.tokenize = tokenString(ch);
80 else if (isOperatorChar.test(ch))
81 state.tokenize = tokenOperator(ch);
82 else if (isStringChar.test(ch))
83 state.tokenize = tokenWord(ch);
84
85 return (state.tokenize != tokenBase) ? state.tokenize(stream, state) : null;
86 }
87
88 return {
89 startState: function() {
90 return {
91 tokenize: tokenBase
92 };
93 },
94
95 token: function(stream, state) {
96 if (stream.eatSpace()) return null;
97 return state.tokenize(stream, state);
98 }
99 };
100});
101
102CodeMirror.defineMIME("text/x-solr", "solr");
103
104});