pascal.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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("pascal", 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("and array begin case const div do downto else end file for forward integer " +
  17. "boolean char function goto if in label mod nil not of or packed procedure " +
  18. "program record repeat set string then to type until var while with");
  19. var atoms = {"null": true};
  20. var isOperatorChar = /[+\-*&%=<>!?|\/]/;
  21. function tokenBase(stream, state) {
  22. var ch = stream.next();
  23. if (ch == "#" && state.startOfLine) {
  24. stream.skipToEnd();
  25. return "meta";
  26. }
  27. if (ch == '"' || ch == "'") {
  28. state.tokenize = tokenString(ch);
  29. return state.tokenize(stream, state);
  30. }
  31. if (ch == "(" && stream.eat("*")) {
  32. state.tokenize = tokenComment;
  33. return tokenComment(stream, state);
  34. }
  35. if (/[\[\]{}\(\),;\:\.]/.test(ch)) {
  36. return null;
  37. }
  38. if (/\d/.test(ch)) {
  39. stream.eatWhile(/[\w\.]/);
  40. return "number";
  41. }
  42. if (ch == "/") {
  43. if (stream.eat("/")) {
  44. stream.skipToEnd();
  45. return "comment";
  46. }
  47. }
  48. if (isOperatorChar.test(ch)) {
  49. stream.eatWhile(isOperatorChar);
  50. return "operator";
  51. }
  52. stream.eatWhile(/[\w\$_]/);
  53. var cur = stream.current();
  54. if (keywords.propertyIsEnumerable(cur)) return "keyword";
  55. if (atoms.propertyIsEnumerable(cur)) return "atom";
  56. return "variable";
  57. }
  58. function tokenString(quote) {
  59. return function(stream, state) {
  60. var escaped = false, next, end = false;
  61. while ((next = stream.next()) != null) {
  62. if (next == quote && !escaped) {end = true; break;}
  63. escaped = !escaped && next == "\\";
  64. }
  65. if (end || !escaped) state.tokenize = null;
  66. return "string";
  67. };
  68. }
  69. function tokenComment(stream, state) {
  70. var maybeEnd = false, ch;
  71. while (ch = stream.next()) {
  72. if (ch == ")" && maybeEnd) {
  73. state.tokenize = null;
  74. break;
  75. }
  76. maybeEnd = (ch == "*");
  77. }
  78. return "comment";
  79. }
  80. // Interface
  81. return {
  82. startState: function() {
  83. return {tokenize: null};
  84. },
  85. token: function(stream, state) {
  86. if (stream.eatSpace()) return null;
  87. var style = (state.tokenize || tokenBase)(stream, state);
  88. if (style == "comment" || style == "meta") return style;
  89. return style;
  90. },
  91. electricChars: "{}"
  92. };
  93. });
  94. CodeMirror.defineMIME("text/x-pascal", "pascal");
  95. });