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.

turtle.js 4.7KB

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("turtle", 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([]);
  19. var keywords = wordRegexp(["@prefix", "@base", "a"]);
  20. var operatorChars = /[*+\-<>=&|]/;
  21. function tokenBase(stream, state) {
  22. var ch = stream.next();
  23. curPunc = null;
  24. if (ch == "<" && !stream.match(/^[\s\u00a0=]/, false)) {
  25. stream.match(/^[^\s\u00a0>]*>?/);
  26. return "atom";
  27. }
  28. else if (ch == "\"" || ch == "'") {
  29. state.tokenize = tokenLiteral(ch);
  30. return state.tokenize(stream, state);
  31. }
  32. else if (/[{}\(\),\.;\[\]]/.test(ch)) {
  33. curPunc = ch;
  34. return null;
  35. }
  36. else if (ch == "#") {
  37. stream.skipToEnd();
  38. return "comment";
  39. }
  40. else if (operatorChars.test(ch)) {
  41. stream.eatWhile(operatorChars);
  42. return null;
  43. }
  44. else if (ch == ":") {
  45. return "operator";
  46. } else {
  47. stream.eatWhile(/[_\w\d]/);
  48. if(stream.peek() == ":") {
  49. return "variable-3";
  50. } else {
  51. var word = stream.current();
  52. if(keywords.test(word)) {
  53. return "meta";
  54. }
  55. if(ch >= "A" && ch <= "Z") {
  56. return "comment";
  57. } else {
  58. return "keyword";
  59. }
  60. }
  61. var word = stream.current();
  62. if (ops.test(word))
  63. return null;
  64. else if (keywords.test(word))
  65. return "meta";
  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("text/turtle", "turtle");
  143. });