turtle.js 4.6 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("turtle", 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([]);
  17. var keywords = wordRegexp(["@prefix", "@base", "a"]);
  18. var operatorChars = /[*+\-<>=&|]/;
  19. function tokenBase(stream, state) {
  20. var ch = stream.next();
  21. curPunc = null;
  22. if (ch == "<" && !stream.match(/^[\s\u00a0=]/, false)) {
  23. stream.match(/^[^\s\u00a0>]*>?/);
  24. return "atom";
  25. }
  26. else if (ch == "\"" || ch == "'") {
  27. state.tokenize = tokenLiteral(ch);
  28. return state.tokenize(stream, state);
  29. }
  30. else if (/[{}\(\),\.;\[\]]/.test(ch)) {
  31. curPunc = ch;
  32. return null;
  33. }
  34. else if (ch == "#") {
  35. stream.skipToEnd();
  36. return "comment";
  37. }
  38. else if (operatorChars.test(ch)) {
  39. stream.eatWhile(operatorChars);
  40. return null;
  41. }
  42. else if (ch == ":") {
  43. return "operator";
  44. } else {
  45. stream.eatWhile(/[_\w\d]/);
  46. if(stream.peek() == ":") {
  47. return "variable-3";
  48. } else {
  49. var word = stream.current();
  50. if(keywords.test(word)) {
  51. return "meta";
  52. }
  53. if(ch >= "A" && ch <= "Z") {
  54. return "comment";
  55. } else {
  56. return "keyword";
  57. }
  58. }
  59. var word = stream.current();
  60. if (ops.test(word))
  61. return null;
  62. else if (keywords.test(word))
  63. return "meta";
  64. else
  65. return "variable";
  66. }
  67. }
  68. function tokenLiteral(quote) {
  69. return function(stream, state) {
  70. var escaped = false, ch;
  71. while ((ch = stream.next()) != null) {
  72. if (ch == quote && !escaped) {
  73. state.tokenize = tokenBase;
  74. break;
  75. }
  76. escaped = !escaped && ch == "\\";
  77. }
  78. return "string";
  79. };
  80. }
  81. function pushContext(state, type, col) {
  82. state.context = {prev: state.context, indent: state.indent, col: col, type: type};
  83. }
  84. function popContext(state) {
  85. state.indent = state.context.indent;
  86. state.context = state.context.prev;
  87. }
  88. return {
  89. startState: function() {
  90. return {tokenize: tokenBase,
  91. context: null,
  92. indent: 0,
  93. col: 0};
  94. },
  95. token: function(stream, state) {
  96. if (stream.sol()) {
  97. if (state.context && state.context.align == null) state.context.align = false;
  98. state.indent = stream.indentation();
  99. }
  100. if (stream.eatSpace()) return null;
  101. var style = state.tokenize(stream, state);
  102. if (style != "comment" && state.context && state.context.align == null && state.context.type != "pattern") {
  103. state.context.align = true;
  104. }
  105. if (curPunc == "(") pushContext(state, ")", stream.column());
  106. else if (curPunc == "[") pushContext(state, "]", stream.column());
  107. else if (curPunc == "{") pushContext(state, "}", stream.column());
  108. else if (/[\]\}\)]/.test(curPunc)) {
  109. while (state.context && state.context.type == "pattern") popContext(state);
  110. if (state.context && curPunc == state.context.type) popContext(state);
  111. }
  112. else if (curPunc == "." && state.context && state.context.type == "pattern") popContext(state);
  113. else if (/atom|string|variable/.test(style) && state.context) {
  114. if (/[\}\]]/.test(state.context.type))
  115. pushContext(state, "pattern", stream.column());
  116. else if (state.context.type == "pattern" && !state.context.align) {
  117. state.context.align = true;
  118. state.context.col = stream.column();
  119. }
  120. }
  121. return style;
  122. },
  123. indent: function(state, textAfter) {
  124. var firstChar = textAfter && textAfter.charAt(0);
  125. var context = state.context;
  126. if (/[\]\}]/.test(firstChar))
  127. while (context && context.type == "pattern") context = context.prev;
  128. var closing = context && firstChar == context.type;
  129. if (!context)
  130. return 0;
  131. else if (context.type == "pattern")
  132. return context.col;
  133. else if (context.align)
  134. return context.col + (closing ? 0 : 1);
  135. else
  136. return context.indent + (closing ? 0 : indentUnit);
  137. }
  138. };
  139. });
  140. CodeMirror.defineMIME("text/turtle", "turtle");
  141. });