turtle.js 4.3 KB

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