solr.js 3.1 KB

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