pascal.js 5.2 KB

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