sparql.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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", "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(/^[\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) {
  129. popContext(state);
  130. if (curPunc == "}" && state.context && state.context.type == "pattern")
  131. popContext(state);
  132. }
  133. }
  134. else if (curPunc == "." && state.context && state.context.type == "pattern") popContext(state);
  135. else if (/atom|string|variable/.test(style) && state.context) {
  136. if (/[\}\]]/.test(state.context.type))
  137. pushContext(state, "pattern", stream.column());
  138. else if (state.context.type == "pattern" && !state.context.align) {
  139. state.context.align = true;
  140. state.context.col = stream.column();
  141. }
  142. }
  143. return style;
  144. },
  145. indent: function(state, textAfter) {
  146. var firstChar = textAfter && textAfter.charAt(0);
  147. var context = state.context;
  148. if (/[\]\}]/.test(firstChar))
  149. while (context && context.type == "pattern") context = context.prev;
  150. var closing = context && firstChar == context.type;
  151. if (!context)
  152. return 0;
  153. else if (context.type == "pattern")
  154. return context.col;
  155. else if (context.align)
  156. return context.col + (closing ? 0 : 1);
  157. else
  158. return context.indent + (closing ? 0 : indentUnit);
  159. },
  160. lineComment: "#"
  161. };
  162. });
  163. CodeMirror.defineMIME("application/sparql-query", "sparql");
  164. });