Theme Inspinia
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.

sparql.js 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. "isblank", "isliteral", "a"]);
  20. var keywords = wordRegexp(["base", "prefix", "select", "distinct", "reduced", "construct", "describe",
  21. "ask", "from", "named", "where", "order", "limit", "offset", "filter", "optional",
  22. "graph", "by", "asc", "desc", "as", "having", "undef", "values", "group",
  23. "minus", "in", "not", "service", "silent", "using", "insert", "delete", "union",
  24. "data", "copy", "to", "move", "add", "create", "drop", "clear", "load"]);
  25. var operatorChars = /[*+\-<>=&|]/;
  26. function tokenBase(stream, state) {
  27. var ch = stream.next();
  28. curPunc = null;
  29. if (ch == "$" || ch == "?") {
  30. stream.match(/^[\w\d]*/);
  31. return "variable-2";
  32. }
  33. else if (ch == "<" && !stream.match(/^[\s\u00a0=]/, false)) {
  34. stream.match(/^[^\s\u00a0>]*>?/);
  35. return "atom";
  36. }
  37. else if (ch == "\"" || ch == "'") {
  38. state.tokenize = tokenLiteral(ch);
  39. return state.tokenize(stream, state);
  40. }
  41. else if (/[{}\(\),\.;\[\]]/.test(ch)) {
  42. curPunc = ch;
  43. return null;
  44. }
  45. else if (ch == "#") {
  46. stream.skipToEnd();
  47. return "comment";
  48. }
  49. else if (operatorChars.test(ch)) {
  50. stream.eatWhile(operatorChars);
  51. return null;
  52. }
  53. else if (ch == ":") {
  54. stream.eatWhile(/[\w\d\._\-]/);
  55. return "atom";
  56. }
  57. else {
  58. stream.eatWhile(/[_\w\d]/);
  59. if (stream.eat(":")) {
  60. stream.eatWhile(/[\w\d_\-]/);
  61. return "atom";
  62. }
  63. var word = stream.current();
  64. if (ops.test(word))
  65. return null;
  66. else if (keywords.test(word))
  67. return "keyword";
  68. else
  69. return "variable";
  70. }
  71. }
  72. function tokenLiteral(quote) {
  73. return function(stream, state) {
  74. var escaped = false, ch;
  75. while ((ch = stream.next()) != null) {
  76. if (ch == quote && !escaped) {
  77. state.tokenize = tokenBase;
  78. break;
  79. }
  80. escaped = !escaped && ch == "\\";
  81. }
  82. return "string";
  83. };
  84. }
  85. function pushContext(state, type, col) {
  86. state.context = {prev: state.context, indent: state.indent, col: col, type: type};
  87. }
  88. function popContext(state) {
  89. state.indent = state.context.indent;
  90. state.context = state.context.prev;
  91. }
  92. return {
  93. startState: function() {
  94. return {tokenize: tokenBase,
  95. context: null,
  96. indent: 0,
  97. col: 0};
  98. },
  99. token: function(stream, state) {
  100. if (stream.sol()) {
  101. if (state.context && state.context.align == null) state.context.align = false;
  102. state.indent = stream.indentation();
  103. }
  104. if (stream.eatSpace()) return null;
  105. var style = state.tokenize(stream, state);
  106. if (style != "comment" && state.context && state.context.align == null && state.context.type != "pattern") {
  107. state.context.align = true;
  108. }
  109. if (curPunc == "(") pushContext(state, ")", stream.column());
  110. else if (curPunc == "[") pushContext(state, "]", stream.column());
  111. else if (curPunc == "{") pushContext(state, "}", stream.column());
  112. else if (/[\]\}\)]/.test(curPunc)) {
  113. while (state.context && state.context.type == "pattern") popContext(state);
  114. if (state.context && curPunc == state.context.type) popContext(state);
  115. }
  116. else if (curPunc == "." && state.context && state.context.type == "pattern") popContext(state);
  117. else if (/atom|string|variable/.test(style) && state.context) {
  118. if (/[\}\]]/.test(state.context.type))
  119. pushContext(state, "pattern", stream.column());
  120. else if (state.context.type == "pattern" && !state.context.align) {
  121. state.context.align = true;
  122. state.context.col = stream.column();
  123. }
  124. }
  125. return style;
  126. },
  127. indent: function(state, textAfter) {
  128. var firstChar = textAfter && textAfter.charAt(0);
  129. var context = state.context;
  130. if (/[\]\}]/.test(firstChar))
  131. while (context && context.type == "pattern") context = context.prev;
  132. var closing = context && firstChar == context.type;
  133. if (!context)
  134. return 0;
  135. else if (context.type == "pattern")
  136. return context.col;
  137. else if (context.align)
  138. return context.col + (closing ? 0 : 1);
  139. else
  140. return context.indent + (closing ? 0 : indentUnit);
  141. }
  142. };
  143. });
  144. CodeMirror.defineMIME("application/x-sparql-query", "sparql");
  145. });