sparql.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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("sparql", function(config) {
  13. var indentUnit = config.indentUnit;
  14. var curPunc;
  15. function wordRegexp(words) {
  16. return new RegExp("^(?:" + words.join("|") + ")$", "i");
  17. }
  18. var ops = wordRegexp(["str", "lang", "langmatches", "datatype", "bound", "sameterm", "isiri", "isuri",
  19. "iri", "uri", "bnode", "count", "sum", "min", "max", "avg", "sample",
  20. "group_concat", "rand", "abs", "ceil", "floor", "round", "concat", "substr", "strlen",
  21. "replace", "ucase", "lcase", "encode_for_uri", "contains", "strstarts", "strends",
  22. "strbefore", "strafter", "year", "month", "day", "hours", "minutes", "seconds",
  23. "timezone", "tz", "now", "uuid", "struuid", "md5", "sha1", "sha256", "sha384",
  24. "sha512", "coalesce", "if", "strlang", "strdt", "isnumeric", "regex", "exists",
  25. "isblank", "isliteral", "a", "bind"]);
  26. var keywords = wordRegexp(["base", "prefix", "select", "distinct", "reduced", "construct", "describe",
  27. "ask", "from", "named", "where", "order", "limit", "offset", "filter", "optional",
  28. "graph", "by", "asc", "desc", "as", "having", "undef", "values", "group",
  29. "minus", "in", "not", "service", "silent", "using", "insert", "delete", "union",
  30. "true", "false", "with",
  31. "data", "copy", "to", "move", "add", "create", "drop", "clear", "load"]);
  32. var operatorChars = /[*+\-<>=&|\^\/!\?]/;
  33. function tokenBase(stream, state) {
  34. var ch = stream.next();
  35. curPunc = null;
  36. if (ch == "$" || ch == "?") {
  37. if(ch == "?" && stream.match(/\s/, false)){
  38. return "operator";
  39. }
  40. stream.match(/^[A-Za-z0-9_\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD][A-Za-z0-9_\u00B7\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u037D\u037F-\u1FFF\u200C-\u200D\u203F-\u2040\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]*/);
  41. return "variable-2";
  42. }
  43. else if (ch == "<" && !stream.match(/^[\s\u00a0=]/, false)) {
  44. stream.match(/^[^\s\u00a0>]*>?/);
  45. return "atom";
  46. }
  47. else if (ch == "\"" || ch == "'") {
  48. state.tokenize = tokenLiteral(ch);
  49. return state.tokenize(stream, state);
  50. }
  51. else if (/[{}\(\),\.;\[\]]/.test(ch)) {
  52. curPunc = ch;
  53. return "bracket";
  54. }
  55. else if (ch == "#") {
  56. stream.skipToEnd();
  57. return "comment";
  58. }
  59. else if (ch === "^") {
  60. ch = stream.peek();
  61. if (ch === "^") stream.eat("^");
  62. else stream.eatWhile(operatorChars);
  63. return "operator";
  64. }
  65. else if (operatorChars.test(ch)) {
  66. stream.eatWhile(operatorChars);
  67. return "operator";
  68. }
  69. else if (ch == ":") {
  70. eatPnLocal(stream);
  71. return "atom";
  72. }
  73. else if (ch == "@") {
  74. stream.eatWhile(/[a-z\d\-]/i);
  75. return "meta";
  76. }
  77. else {
  78. stream.eatWhile(/[_\w\d]/);
  79. if (stream.eat(":")) {
  80. eatPnLocal(stream);
  81. return "atom";
  82. }
  83. var word = stream.current();
  84. if (ops.test(word))
  85. return "builtin";
  86. else if (keywords.test(word))
  87. return "keyword";
  88. else
  89. return "variable";
  90. }
  91. }
  92. function eatPnLocal(stream) {
  93. stream.match(/(\.(?=[\w_\-\\%])|[:\w_-]|\\[-\\_~.!$&'()*+,;=/?#@%]|%[a-f\d][a-f\d])+/i);
  94. }
  95. function tokenLiteral(quote) {
  96. return function(stream, state) {
  97. var escaped = false, ch;
  98. while ((ch = stream.next()) != null) {
  99. if (ch == quote && !escaped) {
  100. state.tokenize = tokenBase;
  101. break;
  102. }
  103. escaped = !escaped && ch == "\\";
  104. }
  105. return "string";
  106. };
  107. }
  108. function pushContext(state, type, col) {
  109. state.context = {prev: state.context, indent: state.indent, col: col, type: type};
  110. }
  111. function popContext(state) {
  112. state.indent = state.context.indent;
  113. state.context = state.context.prev;
  114. }
  115. return {
  116. startState: function() {
  117. return {tokenize: tokenBase,
  118. context: null,
  119. indent: 0,
  120. col: 0};
  121. },
  122. token: function(stream, state) {
  123. if (stream.sol()) {
  124. if (state.context && state.context.align == null) state.context.align = false;
  125. state.indent = stream.indentation();
  126. }
  127. if (stream.eatSpace()) return null;
  128. var style = state.tokenize(stream, state);
  129. if (style != "comment" && state.context && state.context.align == null && state.context.type != "pattern") {
  130. state.context.align = true;
  131. }
  132. if (curPunc == "(") pushContext(state, ")", stream.column());
  133. else if (curPunc == "[") pushContext(state, "]", stream.column());
  134. else if (curPunc == "{") pushContext(state, "}", stream.column());
  135. else if (/[\]\}\)]/.test(curPunc)) {
  136. while (state.context && state.context.type == "pattern") popContext(state);
  137. if (state.context && curPunc == state.context.type) {
  138. popContext(state);
  139. if (curPunc == "}" && state.context && state.context.type == "pattern")
  140. popContext(state);
  141. }
  142. }
  143. else if (curPunc == "." && state.context && state.context.type == "pattern") popContext(state);
  144. else if (/atom|string|variable/.test(style) && state.context) {
  145. if (/[\}\]]/.test(state.context.type))
  146. pushContext(state, "pattern", stream.column());
  147. else if (state.context.type == "pattern" && !state.context.align) {
  148. state.context.align = true;
  149. state.context.col = stream.column();
  150. }
  151. }
  152. return style;
  153. },
  154. indent: function(state, textAfter) {
  155. var firstChar = textAfter && textAfter.charAt(0);
  156. var context = state.context;
  157. if (/[\]\}]/.test(firstChar))
  158. while (context && context.type == "pattern") context = context.prev;
  159. var closing = context && firstChar == context.type;
  160. if (!context)
  161. return 0;
  162. else if (context.type == "pattern")
  163. return context.col;
  164. else if (context.align)
  165. return context.col + (closing ? 0 : 1);
  166. else
  167. return context.indent + (closing ? 0 : indentUnit);
  168. },
  169. lineComment: "#"
  170. };
  171. });
  172. CodeMirror.defineMIME("application/sparql-query", "sparql");
  173. });