You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

z80.js 3.5KB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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('z80', function(_config, parserConfig) {
  13. var ez80 = parserConfig.ez80;
  14. var keywords1, keywords2;
  15. if (ez80) {
  16. keywords1 = /^(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;
  17. keywords2 = /^(((call|j[pr]|rst|ret[in]?)(\.([sl]?i)?[sl])?)|(rs|st)mix)\b/i;
  18. } else {
  19. 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;
  20. keywords2 = /^(call|j[pr]|ret[in]?|b_?(call|jump))\b/i;
  21. }
  22. var variables1 = /^(af?|bc?|c|de?|e|hl?|l|i[xy]?|r|sp)\b/i;
  23. var variables2 = /^(n?[zc]|p[oe]?|m)\b/i;
  24. var errors = /^([hl][xy]|i[xy][hl]|slia|sll)\b/i;
  25. var numbers = /^([\da-f]+h|[0-7]+o|[01]+b|\d+d?)\b/i;
  26. return {
  27. startState: function() {
  28. return {
  29. context: 0
  30. };
  31. },
  32. token: function(stream, state) {
  33. if (!stream.column())
  34. state.context = 0;
  35. if (stream.eatSpace())
  36. return null;
  37. var w;
  38. if (stream.eatWhile(/\w/)) {
  39. if (ez80 && stream.eat('.')) {
  40. stream.eatWhile(/\w/);
  41. }
  42. w = stream.current();
  43. if (stream.indentation()) {
  44. if ((state.context == 1 || state.context == 4) && variables1.test(w)) {
  45. state.context = 4;
  46. return 'var2';
  47. }
  48. if (state.context == 2 && variables2.test(w)) {
  49. state.context = 4;
  50. return 'var3';
  51. }
  52. if (keywords1.test(w)) {
  53. state.context = 1;
  54. return 'keyword';
  55. } else if (keywords2.test(w)) {
  56. state.context = 2;
  57. return 'keyword';
  58. } else if (state.context == 4 && numbers.test(w)) {
  59. return 'number';
  60. }
  61. if (errors.test(w))
  62. 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 == '"')
  74. break;
  75. if (w == '\\')
  76. stream.next();
  77. }
  78. return 'string';
  79. } else if (stream.eat('\'')) {
  80. if (stream.match(/\\?.'/))
  81. return 'number';
  82. } else if (stream.eat('.') || stream.sol() && stream.eat('#')) {
  83. state.context = 5;
  84. if (stream.eatWhile(/\w/))
  85. return 'def';
  86. } else if (stream.eat('$')) {
  87. if (stream.eatWhile(/[\da-f]/i))
  88. return 'number';
  89. } else if (stream.eat('%')) {
  90. if (stream.eatWhile(/[01]/))
  91. return 'number';
  92. } else {
  93. stream.next();
  94. }
  95. return null;
  96. }
  97. };
  98. });
  99. CodeMirror.defineMIME("text/x-z80", "z80");
  100. CodeMirror.defineMIME("text/x-ez80", { name: "z80", ez80: true });
  101. });