1
0

editorMode.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. (function(mod) {
  2. if (typeof exports == "object" && typeof module == "object") // CommonJS
  3. mod(require("codemirror/lib/codemirror"));
  4. else if (typeof define == "function" && define.amd) // AMD
  5. define(["codemirror/lib/codemirror"], mod);
  6. else // Plain browser env
  7. mod(CodeMirror);
  8. })(function(CodeMirror) {
  9. "use strict";
  10. CodeMirror.defineMode("ivprog", function() {
  11. function words(str) {
  12. var obj = {}, words = str.split(" ");
  13. for (var i = 0; i < words.length; ++i) obj[words[i]] = true;
  14. return obj;
  15. }
  16. var keywords = words(
  17. "programa E OU nao senao se " +
  18. "enquanto faca pare para retorne funcao const " +
  19. "contrario caso escolha");
  20. var atoms = words("verdadeiro falso");
  21. var types = words("inteiro logico cadeia real vazio")
  22. var isOperatorChar = /[+\-*%=<>!\/]/;
  23. function tokenBase(stream, state) {
  24. var ch = stream.next();
  25. if (ch == "#" && state.startOfLine) {
  26. stream.skipToEnd();
  27. return "meta";
  28. }
  29. if (ch == '"') {
  30. state.tokenize = tokenString(ch);
  31. return state.tokenize(stream, state);
  32. }
  33. if (ch == "/" && stream.eat("*")) {
  34. state.tokenize = tokenComment;
  35. return tokenComment(stream, state);
  36. }
  37. if (/[\[\]{}\(\),;\:\.]/.test(ch)) {
  38. return null;
  39. }
  40. if (/\d/.test(ch)) {
  41. stream.eatWhile(/[\w\.]/);
  42. return "number";
  43. }
  44. if (ch == "/") {
  45. if (stream.eat("/")) {
  46. stream.skipToEnd();
  47. return "comment";
  48. }
  49. }
  50. if (isOperatorChar.test(ch)) {
  51. stream.eatWhile(isOperatorChar);
  52. return "operator";
  53. }
  54. stream.eatWhile(/[\w\$_]/);
  55. var cur = stream.current();
  56. if (keywords.propertyIsEnumerable(cur)) return "keyword";
  57. if (types.propertyIsEnumerable(cur)) return "type";
  58. if (atoms.propertyIsEnumerable(cur)) return "atom";
  59. return "variable";
  60. }
  61. function tokenString(quote) {
  62. return function(stream, state) {
  63. var escaped = false, next, end = false;
  64. while ((next = stream.next()) != null) {
  65. if (next == quote && !escaped) {end = true; break;}
  66. escaped = !escaped && next == "\\";
  67. }
  68. if (end || !escaped) state.tokenize = null;
  69. return "string";
  70. };
  71. }
  72. function tokenComment(stream, state) {
  73. var maybeEnd = false, ch;
  74. while (ch = stream.next()) {
  75. if (ch == "/" && maybeEnd) {
  76. state.tokenize = null;
  77. break;
  78. }
  79. maybeEnd = (ch == "*");
  80. }
  81. return "comment";
  82. }
  83. // Interface
  84. return {
  85. startState: function() {
  86. return {tokenize: null};
  87. },
  88. token: function(stream, state) {
  89. if (stream.eatSpace()) return null;
  90. var style = (state.tokenize || tokenBase)(stream, state);
  91. if (style == "comment" || style == "meta") return style;
  92. return style;
  93. },
  94. electricChars: "{}",
  95. blockCommentStart: "/*",
  96. blockCommentEnd: "*/",
  97. blockCommentContinue: " * ",
  98. lineComment: "//",
  99. };
  100. });
  101. CodeMirror.defineMIME("text/x-ivprog", "ivprog");
  102. });