pascal.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://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(
  19. "absolute and array asm begin case const constructor destructor div do " +
  20. "downto else end file for function goto if implementation in inherited " +
  21. "inline interface label mod nil not object of operator or packed procedure " +
  22. "program record reintroduce repeat self set shl shr string then to type " +
  23. "unit until uses var while with xor as class dispinterface except exports " +
  24. "finalization finally initialization inline is library on out packed " +
  25. "property raise resourcestring threadvar try absolute abstract alias " +
  26. "assembler bitpacked break cdecl continue cppdecl cvar default deprecated " +
  27. "dynamic enumerator experimental export external far far16 forward generic " +
  28. "helper implements index interrupt iocheck local message name near " +
  29. "nodefault noreturn nostackframe oldfpccall otherwise overload override " +
  30. "pascal platform private protected public published read register " +
  31. "reintroduce result safecall saveregisters softfloat specialize static " +
  32. "stdcall stored strict unaligned unimplemented varargs virtual write");
  33. var atoms = {"null": true};
  34. var isOperatorChar = /[+\-*&%=<>!?|\/]/;
  35. function tokenBase(stream, state) {
  36. var ch = stream.next();
  37. if (ch == "#" && state.startOfLine) {
  38. stream.skipToEnd();
  39. return "meta";
  40. }
  41. if (ch == '"' || ch == "'") {
  42. state.tokenize = tokenString(ch);
  43. return state.tokenize(stream, state);
  44. }
  45. if (ch == "(" && stream.eat("*")) {
  46. state.tokenize = tokenComment;
  47. return tokenComment(stream, state);
  48. }
  49. if (/[\[\]{}\(\),;\:\.]/.test(ch)) {
  50. return null;
  51. }
  52. if (/\d/.test(ch)) {
  53. stream.eatWhile(/[\w\.]/);
  54. return "number";
  55. }
  56. if (ch == "/") {
  57. if (stream.eat("/")) {
  58. stream.skipToEnd();
  59. return "comment";
  60. }
  61. }
  62. if (isOperatorChar.test(ch)) {
  63. stream.eatWhile(isOperatorChar);
  64. return "operator";
  65. }
  66. stream.eatWhile(/[\w\$_]/);
  67. var cur = stream.current();
  68. if (keywords.propertyIsEnumerable(cur)) return "keyword";
  69. if (atoms.propertyIsEnumerable(cur)) return "atom";
  70. return "variable";
  71. }
  72. function tokenString(quote) {
  73. return function(stream, state) {
  74. var escaped = false, next, end = false;
  75. while ((next = stream.next()) != null) {
  76. if (next == quote && !escaped) {end = true; break;}
  77. escaped = !escaped && next == "\\";
  78. }
  79. if (end || !escaped) state.tokenize = null;
  80. return "string";
  81. };
  82. }
  83. function tokenComment(stream, state) {
  84. var maybeEnd = false, ch;
  85. while (ch = stream.next()) {
  86. if (ch == ")" && maybeEnd) {
  87. state.tokenize = null;
  88. break;
  89. }
  90. maybeEnd = (ch == "*");
  91. }
  92. return "comment";
  93. }
  94. // Interface
  95. return {
  96. startState: function() {
  97. return {tokenize: null};
  98. },
  99. token: function(stream, state) {
  100. if (stream.eatSpace()) return null;
  101. var style = (state.tokenize || tokenBase)(stream, state);
  102. if (style == "comment" || style == "meta") return style;
  103. return style;
  104. },
  105. electricChars: "{}"
  106. };
  107. });
  108. CodeMirror.defineMIME("text/x-pascal", "pascal");
  109. });