sparql.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. CodeMirror.defineMode("sparql", function(config) {
  2. var indentUnit = config.indentUnit;
  3. var curPunc;
  4. function wordRegexp(words) {
  5. return new RegExp("^(?:" + words.join("|") + ")$", "i");
  6. }
  7. var ops = wordRegexp(["str", "lang", "langmatches", "datatype", "bound", "sameterm", "isiri", "isuri",
  8. "isblank", "isliteral", "union", "a"]);
  9. var keywords = wordRegexp(["base", "prefix", "select", "distinct", "reduced", "construct", "describe",
  10. "ask", "from", "named", "where", "order", "limit", "offset", "filter", "optional",
  11. "graph", "by", "asc", "desc"]);
  12. var operatorChars = /[*+\-<>=&|]/;
  13. function tokenBase(stream, state) {
  14. var ch = stream.next();
  15. curPunc = null;
  16. if (ch == "$" || ch == "?") {
  17. stream.match(/^[\w\d]*/);
  18. return "variable-2";
  19. }
  20. else if (ch == "<" && !stream.match(/^[\s\u00a0=]/, false)) {
  21. stream.match(/^[^\s\u00a0>]*>?/);
  22. return "atom";
  23. }
  24. else if (ch == "\"" || ch == "'") {
  25. state.tokenize = tokenLiteral(ch);
  26. return state.tokenize(stream, state);
  27. }
  28. else if (/[{}\(\),\.;\[\]]/.test(ch)) {
  29. curPunc = ch;
  30. return null;
  31. }
  32. else if (ch == "#") {
  33. stream.skipToEnd();
  34. return "comment";
  35. }
  36. else if (operatorChars.test(ch)) {
  37. stream.eatWhile(operatorChars);
  38. return null;
  39. }
  40. else if (ch == ":") {
  41. stream.eatWhile(/[\w\d\._\-]/);
  42. return "atom";
  43. }
  44. else {
  45. stream.eatWhile(/[_\w\d]/);
  46. if (stream.eat(":")) {
  47. stream.eatWhile(/[\w\d_\-]/);
  48. return "atom";
  49. }
  50. var word = stream.current(), type;
  51. if (ops.test(word))
  52. return null;
  53. else if (keywords.test(word))
  54. return "keyword";
  55. else
  56. return "variable";
  57. }
  58. }
  59. function tokenLiteral(quote) {
  60. return function(stream, state) {
  61. var escaped = false, ch;
  62. while ((ch = stream.next()) != null) {
  63. if (ch == quote && !escaped) {
  64. state.tokenize = tokenBase;
  65. break;
  66. }
  67. escaped = !escaped && ch == "\\";
  68. }
  69. return "string";
  70. };
  71. }
  72. function pushContext(state, type, col) {
  73. state.context = {prev: state.context, indent: state.indent, col: col, type: type};
  74. }
  75. function popContext(state) {
  76. state.indent = state.context.indent;
  77. state.context = state.context.prev;
  78. }
  79. return {
  80. startState: function(base) {
  81. return {tokenize: tokenBase,
  82. context: null,
  83. indent: 0,
  84. col: 0};
  85. },
  86. token: function(stream, state) {
  87. if (stream.sol()) {
  88. if (state.context && state.context.align == null) state.context.align = false;
  89. state.indent = stream.indentation();
  90. }
  91. if (stream.eatSpace()) return null;
  92. var style = state.tokenize(stream, state);
  93. if (style != "comment" && state.context && state.context.align == null && state.context.type != "pattern") {
  94. state.context.align = true;
  95. }
  96. if (curPunc == "(") pushContext(state, ")", stream.column());
  97. else if (curPunc == "[") pushContext(state, "]", stream.column());
  98. else if (curPunc == "{") pushContext(state, "}", stream.column());
  99. else if (/[\]\}\)]/.test(curPunc)) {
  100. while (state.context && state.context.type == "pattern") popContext(state);
  101. if (state.context && curPunc == state.context.type) popContext(state);
  102. }
  103. else if (curPunc == "." && state.context && state.context.type == "pattern") popContext(state);
  104. else if (/atom|string|variable/.test(style) && state.context) {
  105. if (/[\}\]]/.test(state.context.type))
  106. pushContext(state, "pattern", stream.column());
  107. else if (state.context.type == "pattern" && !state.context.align) {
  108. state.context.align = true;
  109. state.context.col = stream.column();
  110. }
  111. }
  112. return style;
  113. },
  114. indent: function(state, textAfter) {
  115. var firstChar = textAfter && textAfter.charAt(0);
  116. var context = state.context;
  117. if (/[\]\}]/.test(firstChar))
  118. while (context && context.type == "pattern") context = context.prev;
  119. var closing = context && firstChar == context.type;
  120. if (!context)
  121. return 0;
  122. else if (context.type == "pattern")
  123. return context.col;
  124. else if (context.align)
  125. return context.col + (closing ? 0 : 1);
  126. else
  127. return context.indent + (closing ? 0 : indentUnit);
  128. }
  129. };
  130. });
  131. CodeMirror.defineMIME("application/x-sparql-query", "sparql");