sparql.js 5.0 KB

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