pascal.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 (ch == "{") {
  50. state.tokenize = tokenCommentBraces;
  51. return tokenCommentBraces(stream, state);
  52. }
  53. if (/[\[\]\(\),;\:\.]/.test(ch)) {
  54. return null;
  55. }
  56. if (/\d/.test(ch)) {
  57. stream.eatWhile(/[\w\.]/);
  58. return "number";
  59. }
  60. if (ch == "/") {
  61. if (stream.eat("/")) {
  62. stream.skipToEnd();
  63. return "comment";
  64. }
  65. }
  66. if (isOperatorChar.test(ch)) {
  67. stream.eatWhile(isOperatorChar);
  68. return "operator";
  69. }
  70. stream.eatWhile(/[\w\$_]/);
  71. var cur = stream.current();
  72. if (keywords.propertyIsEnumerable(cur)) return "keyword";
  73. if (atoms.propertyIsEnumerable(cur)) return "atom";
  74. return "variable";
  75. }
  76. function tokenString(quote) {
  77. return function(stream, state) {
  78. var escaped = false, next, end = false;
  79. while ((next = stream.next()) != null) {
  80. if (next == quote && !escaped) {end = true; break;}
  81. escaped = !escaped && next == "\\";
  82. }
  83. if (end || !escaped) state.tokenize = null;
  84. return "string";
  85. };
  86. }
  87. function tokenComment(stream, state) {
  88. var maybeEnd = false, ch;
  89. while (ch = stream.next()) {
  90. if (ch == ")" && maybeEnd) {
  91. state.tokenize = null;
  92. break;
  93. }
  94. maybeEnd = (ch == "*");
  95. }
  96. return "comment";
  97. }
  98. function tokenCommentBraces(stream, state) {
  99. var ch;
  100. while (ch = stream.next()) {
  101. if (ch == "}") {
  102. state.tokenize = null;
  103. break;
  104. }
  105. }
  106. return "comment";
  107. }
  108. // Interface
  109. return {
  110. startState: function() {
  111. return {tokenize: null};
  112. },
  113. token: function(stream, state) {
  114. if (stream.eatSpace()) return null;
  115. var style = (state.tokenize || tokenBase)(stream, state);
  116. if (style == "comment" || style == "meta") return style;
  117. return style;
  118. },
  119. electricChars: "{}"
  120. };
  121. });
  122. CodeMirror.defineMIME("text/x-pascal", "pascal");
  123. });