solr.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/LICENSE
  3. (function(mod) {
  4. if (typeof exports == "object" && typeof module == "object") // CommonJS
  5. mod(require("../../lib/codemirror"));
  6. else if (typeof define == "function" && define.amd) // AMD
  7. define(["../../lib/codemirror"], mod);
  8. else // Plain browser env
  9. mod(CodeMirror);
  10. })(function(CodeMirror) {
  11. "use strict";
  12. CodeMirror.defineMode("solr", function() {
  13. "use strict";
  14. var isStringChar = /[^\s\|\!\+\-\*\?\~\^\&\:\(\)\[\]\{\}\"\\]/;
  15. var isOperatorChar = /[\|\!\+\-\*\?\~\^\&]/;
  16. var isOperatorString = /^(OR|AND|NOT|TO)$/i;
  17. function isNumber(word) {
  18. return parseFloat(word).toString() === word;
  19. }
  20. function tokenString(quote) {
  21. return function(stream, state) {
  22. var escaped = false, next;
  23. while ((next = stream.next()) != null) {
  24. if (next == quote && !escaped) break;
  25. escaped = !escaped && next == "\\";
  26. }
  27. if (!escaped) state.tokenize = tokenBase;
  28. return "string";
  29. };
  30. }
  31. function tokenOperator(operator) {
  32. return function(stream, state) {
  33. var style = "operator";
  34. if (operator == "+")
  35. style += " positive";
  36. else if (operator == "-")
  37. style += " negative";
  38. else if (operator == "|")
  39. stream.eat(/\|/);
  40. else if (operator == "&")
  41. stream.eat(/\&/);
  42. else if (operator == "^")
  43. style += " boost";
  44. state.tokenize = tokenBase;
  45. return style;
  46. };
  47. }
  48. function tokenWord(ch) {
  49. return function(stream, state) {
  50. var word = ch;
  51. while ((ch = stream.peek()) && ch.match(isStringChar) != null) {
  52. word += stream.next();
  53. }
  54. state.tokenize = tokenBase;
  55. if (isOperatorString.test(word))
  56. return "operator";
  57. else if (isNumber(word))
  58. return "number";
  59. else if (stream.peek() == ":")
  60. return "field";
  61. else
  62. return "string";
  63. };
  64. }
  65. function tokenBase(stream, state) {
  66. var ch = stream.next();
  67. if (ch == '"')
  68. state.tokenize = tokenString(ch);
  69. else if (isOperatorChar.test(ch))
  70. state.tokenize = tokenOperator(ch);
  71. else if (isStringChar.test(ch))
  72. state.tokenize = tokenWord(ch);
  73. return (state.tokenize != tokenBase) ? state.tokenize(stream, state) : null;
  74. }
  75. return {
  76. startState: function() {
  77. return {
  78. tokenize: tokenBase
  79. };
  80. },
  81. token: function(stream, state) {
  82. if (stream.eatSpace()) return null;
  83. return state.tokenize(stream, state);
  84. }
  85. };
  86. });
  87. CodeMirror.defineMIME("text/x-solr", "solr");
  88. });