z80.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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('z80', function (_config, parserConfig) {
  15. var ez80 = parserConfig.ez80
  16. var keywords1, keywords2
  17. if (ez80) {
  18. keywords1 =
  19. /^(exx?|(ld|cp)([di]r?)?|[lp]ea|pop|push|ad[cd]|cpl|daa|dec|inc|neg|sbc|sub|and|bit|[cs]cf|x?or|res|set|r[lr]c?a?|r[lr]d|s[lr]a|srl|djnz|nop|[de]i|halt|im|in([di]mr?|ir?|irx|2r?)|ot(dmr?|[id]rx|imr?)|out(0?|[di]r?|[di]2r?)|tst(io)?|slp)(\.([sl]?i)?[sl])?\b/i
  20. keywords2 = /^(((call|j[pr]|rst|ret[in]?)(\.([sl]?i)?[sl])?)|(rs|st)mix)\b/i
  21. } else {
  22. keywords1 = /^(exx?|(ld|cp|in)([di]r?)?|pop|push|ad[cd]|cpl|daa|dec|inc|neg|sbc|sub|and|bit|[cs]cf|x?or|res|set|r[lr]c?a?|r[lr]d|s[lr]a|srl|djnz|nop|rst|[de]i|halt|im|ot[di]r|out[di]?)\b/i
  23. keywords2 = /^(call|j[pr]|ret[in]?|b_?(call|jump))\b/i
  24. }
  25. var variables1 = /^(af?|bc?|c|de?|e|hl?|l|i[xy]?|r|sp)\b/i
  26. var variables2 = /^(n?[zc]|p[oe]?|m)\b/i
  27. var errors = /^([hl][xy]|i[xy][hl]|slia|sll)\b/i
  28. var numbers = /^([\da-f]+h|[0-7]+o|[01]+b|\d+d?)\b/i
  29. return {
  30. startState: function () {
  31. return {
  32. context: 0,
  33. }
  34. },
  35. token: function (stream, state) {
  36. if (!stream.column()) state.context = 0
  37. if (stream.eatSpace()) return null
  38. var w
  39. if (stream.eatWhile(/\w/)) {
  40. if (ez80 && stream.eat('.')) {
  41. stream.eatWhile(/\w/)
  42. }
  43. w = stream.current()
  44. if (stream.indentation()) {
  45. if ((state.context == 1 || state.context == 4) && variables1.test(w)) {
  46. state.context = 4
  47. return 'var2'
  48. }
  49. if (state.context == 2 && variables2.test(w)) {
  50. state.context = 4
  51. return 'var3'
  52. }
  53. if (keywords1.test(w)) {
  54. state.context = 1
  55. return 'keyword'
  56. } else if (keywords2.test(w)) {
  57. state.context = 2
  58. return 'keyword'
  59. } else if (state.context == 4 && numbers.test(w)) {
  60. return 'number'
  61. }
  62. if (errors.test(w)) return 'error'
  63. } else if (stream.match(numbers)) {
  64. return 'number'
  65. } else {
  66. return null
  67. }
  68. } else if (stream.eat(';')) {
  69. stream.skipToEnd()
  70. return 'comment'
  71. } else if (stream.eat('"')) {
  72. while ((w = stream.next())) {
  73. if (w == '"') break
  74. if (w == '\\') stream.next()
  75. }
  76. return 'string'
  77. } else if (stream.eat("'")) {
  78. if (stream.match(/\\?.'/)) return 'number'
  79. } else if (stream.eat('.') || (stream.sol() && stream.eat('#'))) {
  80. state.context = 5
  81. if (stream.eatWhile(/\w/)) return 'def'
  82. } else if (stream.eat('$')) {
  83. if (stream.eatWhile(/[\da-f]/i)) return 'number'
  84. } else if (stream.eat('%')) {
  85. if (stream.eatWhile(/[01]/)) return 'number'
  86. } else {
  87. stream.next()
  88. }
  89. return null
  90. },
  91. }
  92. })
  93. CodeMirror.defineMIME('text/x-z80', 'z80')
  94. CodeMirror.defineMIME('text/x-ez80', { name: 'z80', ez80: true })
  95. })