pascal.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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("pascal", function() {
  13. function words(str) {
  14. var obj = {}, words = str.split(" ");
  15. for (var i = 0; i < words.length; ++i) obj[words[i]] = true;
  16. return obj;
  17. }
  18. var keywords = words("and array begin case const div do downto else end file for forward integer " +
  19. "boolean char function goto if in label mod nil not of or packed procedure " +
  20. "program record repeat set string then to type until var while with");
  21. var atoms = {"null": true};
  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 == '"' || 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 (atoms.propertyIsEnumerable(cur)) return "atom";
  58. return "variable";
  59. }
  60. function tokenString(quote) {
  61. return function(stream, state) {
  62. var escaped = false, next, end = false;
  63. while ((next = stream.next()) != null) {
  64. if (next == quote && !escaped) {end = true; break;}
  65. escaped = !escaped && next == "\\";
  66. }
  67. if (end || !escaped) state.tokenize = null;
  68. return "string";
  69. };
  70. }
  71. function tokenComment(stream, state) {
  72. var maybeEnd = false, ch;
  73. while (ch = stream.next()) {
  74. if (ch == ")" && maybeEnd) {
  75. state.tokenize = null;
  76. break;
  77. }
  78. maybeEnd = (ch == "*");
  79. }
  80. return "comment";
  81. }
  82. // Interface
  83. return {
  84. startState: function() {
  85. return {tokenize: null};
  86. },
  87. token: function(stream, state) {
  88. if (stream.eatSpace()) return null;
  89. var style = (state.tokenize || tokenBase)(stream, state);
  90. if (style == "comment" || style == "meta") return style;
  91. return style;
  92. },
  93. electricChars: "{}"
  94. };
  95. });
  96. CodeMirror.defineMIME("text/x-pascal", "pascal");
  97. });