solr.js 2.5 KB

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