You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

184 lines
6.7 KiB

2 months ago
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/5/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", "into"]);
  32. var operatorChars = /[*+\-<>=&|\^\/!\?]/;
  33. var PN_CHARS = "[A-Za-z_\\-0-9]";
  34. var PREFIX_START = new RegExp("[A-Za-z]");
  35. var PREFIX_REMAINDER = new RegExp("((" + PN_CHARS + "|\\.)*(" + PN_CHARS + "))?:");
  36. function tokenBase(stream, state) {
  37. var ch = stream.next();
  38. curPunc = null;
  39. if (ch == "$" || ch == "?") {
  40. if(ch == "?" && stream.match(/\s/, false)){
  41. return "operator";
  42. }
  43. 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]*/);
  44. return "variable-2";
  45. }
  46. else if (ch == "<" && !stream.match(/^[\s\u00a0=]/, false)) {
  47. stream.match(/^[^\s\u00a0>]*>?/);
  48. return "atom";
  49. }
  50. else if (ch == "\"" || ch == "'") {
  51. state.tokenize = tokenLiteral(ch);
  52. return state.tokenize(stream, state);
  53. }
  54. else if (/[{}\(\),\.;\[\]]/.test(ch)) {
  55. curPunc = ch;
  56. return "bracket";
  57. }
  58. else if (ch == "#") {
  59. stream.skipToEnd();
  60. return "comment";
  61. }
  62. else if (operatorChars.test(ch)) {
  63. return "operator";
  64. }
  65. else if (ch == ":") {
  66. eatPnLocal(stream);
  67. return "atom";
  68. }
  69. else if (ch == "@") {
  70. stream.eatWhile(/[a-z\d\-]/i);
  71. return "meta";
  72. }
  73. else if (PREFIX_START.test(ch) && stream.match(PREFIX_REMAINDER)) {
  74. eatPnLocal(stream);
  75. return "atom";
  76. }
  77. stream.eatWhile(/[_\w\d]/);
  78. var word = stream.current();
  79. if (ops.test(word))
  80. return "builtin";
  81. else if (keywords.test(word))
  82. return "keyword";
  83. else
  84. return "variable";
  85. }
  86. function eatPnLocal(stream) {
  87. stream.match(/(\.(?=[\w_\-\\%])|[:\w_-]|\\[-\\_~.!$&'()*+,;=/?#@%]|%[a-f\d][a-f\d])+/i);
  88. }
  89. function tokenLiteral(quote) {
  90. return function(stream, state) {
  91. var escaped = false, ch;
  92. while ((ch = stream.next()) != null) {
  93. if (ch == quote && !escaped) {
  94. state.tokenize = tokenBase;
  95. break;
  96. }
  97. escaped = !escaped && ch == "\\";
  98. }
  99. return "string";
  100. };
  101. }
  102. function pushContext(state, type, col) {
  103. state.context = {prev: state.context, indent: state.indent, col: col, type: type};
  104. }
  105. function popContext(state) {
  106. state.indent = state.context.indent;
  107. state.context = state.context.prev;
  108. }
  109. return {
  110. startState: function() {
  111. return {tokenize: tokenBase,
  112. context: null,
  113. indent: 0,
  114. col: 0};
  115. },
  116. token: function(stream, state) {
  117. if (stream.sol()) {
  118. if (state.context && state.context.align == null) state.context.align = false;
  119. state.indent = stream.indentation();
  120. }
  121. if (stream.eatSpace()) return null;
  122. var style = state.tokenize(stream, state);
  123. if (style != "comment" && state.context && state.context.align == null && state.context.type != "pattern") {
  124. state.context.align = true;
  125. }
  126. if (curPunc == "(") pushContext(state, ")", stream.column());
  127. else if (curPunc == "[") pushContext(state, "]", stream.column());
  128. else if (curPunc == "{") pushContext(state, "}", stream.column());
  129. else if (/[\]\}\)]/.test(curPunc)) {
  130. while (state.context && state.context.type == "pattern") popContext(state);
  131. if (state.context && curPunc == state.context.type) {
  132. popContext(state);
  133. if (curPunc == "}" && state.context && state.context.type == "pattern")
  134. popContext(state);
  135. }
  136. }
  137. else if (curPunc == "." && state.context && state.context.type == "pattern") popContext(state);
  138. else if (/atom|string|variable/.test(style) && state.context) {
  139. if (/[\}\]]/.test(state.context.type))
  140. pushContext(state, "pattern", stream.column());
  141. else if (state.context.type == "pattern" && !state.context.align) {
  142. state.context.align = true;
  143. state.context.col = stream.column();
  144. }
  145. }
  146. return style;
  147. },
  148. indent: function(state, textAfter) {
  149. var firstChar = textAfter && textAfter.charAt(0);
  150. var context = state.context;
  151. if (/[\]\}]/.test(firstChar))
  152. while (context && context.type == "pattern") context = context.prev;
  153. var closing = context && firstChar == context.type;
  154. if (!context)
  155. return 0;
  156. else if (context.type == "pattern")
  157. return context.col;
  158. else if (context.align)
  159. return context.col + (closing ? 0 : 1);
  160. else
  161. return context.indent + (closing ? 0 : indentUnit);
  162. },
  163. lineComment: "#"
  164. };
  165. });
  166. CodeMirror.defineMIME("application/sparql-query", "sparql");
  167. });