2
0

pascal.js 2.5 KB

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