sparql.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://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"]);
  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(/^[\w\d]*/);
  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 (operatorChars.test(ch)) {
  60. stream.eatWhile(operatorChars);
  61. return "operator";
  62. }
  63. else if (ch == ":") {
  64. stream.eatWhile(/[\w\d\._\-]/);
  65. return "atom";
  66. }
  67. else if (ch == "@") {
  68. stream.eatWhile(/[a-z\d\-]/i);
  69. return "meta";
  70. }
  71. else {
  72. stream.eatWhile(/[_\w\d]/);
  73. if (stream.eat(":")) {
  74. stream.eatWhile(/[\w\d_\-]/);
  75. return "atom";
  76. }
  77. var word = stream.current();
  78. if (ops.test(word))
  79. return "builtin";
  80. else if (keywords.test(word))
  81. return "keyword";
  82. else
  83. return "variable";
  84. }
  85. }
  86. function tokenLiteral(quote) {
  87. return function(stream, state) {
  88. var escaped = false, ch;
  89. while ((ch = stream.next()) != null) {
  90. if (ch == quote && !escaped) {
  91. state.tokenize = tokenBase;
  92. break;
  93. }
  94. escaped = !escaped && ch == "\\";
  95. }
  96. return "string";
  97. };
  98. }
  99. function pushContext(state, type, col) {
  100. state.context = {prev: state.context, indent: state.indent, col: col, type: type};
  101. }
  102. function popContext(state) {
  103. state.indent = state.context.indent;
  104. state.context = state.context.prev;
  105. }
  106. return {
  107. startState: function() {
  108. return {tokenize: tokenBase,
  109. context: null,
  110. indent: 0,
  111. col: 0};
  112. },
  113. token: function(stream, state) {
  114. if (stream.sol()) {
  115. if (state.context && state.context.align == null) state.context.align = false;
  116. state.indent = stream.indentation();
  117. }
  118. if (stream.eatSpace()) return null;
  119. var style = state.tokenize(stream, state);
  120. if (style != "comment" && state.context && state.context.align == null && state.context.type != "pattern") {
  121. state.context.align = true;
  122. }
  123. if (curPunc == "(") pushContext(state, ")", stream.column());
  124. else if (curPunc == "[") pushContext(state, "]", stream.column());
  125. else if (curPunc == "{") pushContext(state, "}", stream.column());
  126. else if (/[\]\}\)]/.test(curPunc)) {
  127. while (state.context && state.context.type == "pattern") popContext(state);
  128. if (state.context && curPunc == state.context.type) popContext(state);
  129. }
  130. else if (curPunc == "." && state.context && state.context.type == "pattern") popContext(state);
  131. else if (/atom|string|variable/.test(style) && state.context) {
  132. if (/[\}\]]/.test(state.context.type))
  133. pushContext(state, "pattern", stream.column());
  134. else if (state.context.type == "pattern" && !state.context.align) {
  135. state.context.align = true;
  136. state.context.col = stream.column();
  137. }
  138. }
  139. return style;
  140. },
  141. indent: function(state, textAfter) {
  142. var firstChar = textAfter && textAfter.charAt(0);
  143. var context = state.context;
  144. if (/[\]\}]/.test(firstChar))
  145. while (context && context.type == "pattern") context = context.prev;
  146. var closing = context && firstChar == context.type;
  147. if (!context)
  148. return 0;
  149. else if (context.type == "pattern")
  150. return context.col;
  151. else if (context.align)
  152. return context.col + (closing ? 0 : 1);
  153. else
  154. return context.indent + (closing ? 0 : indentUnit);
  155. }
  156. };
  157. });
  158. CodeMirror.defineMIME("application/sparql-query", "sparql");
  159. });