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