z80.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://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() {
  13. var 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;
  14. var keywords2 = /^(call|j[pr]|ret[in]?)\b/i;
  15. var keywords3 = /^b_?(call|jump)\b/i;
  16. var variables1 = /^(af?|bc?|c|de?|e|hl?|l|i[xy]?|r|sp)\b/i;
  17. var variables2 = /^(n?[zc]|p[oe]?|m)\b/i;
  18. var errors = /^([hl][xy]|i[xy][hl]|slia|sll)\b/i;
  19. var numbers = /^([\da-f]+h|[0-7]+o|[01]+b|\d+)\b/i;
  20. return {
  21. startState: function() {
  22. return {context: 0};
  23. },
  24. token: function(stream, state) {
  25. if (!stream.column())
  26. state.context = 0;
  27. if (stream.eatSpace())
  28. return null;
  29. var w;
  30. if (stream.eatWhile(/\w/)) {
  31. w = stream.current();
  32. if (stream.indentation()) {
  33. if (state.context == 1 && variables1.test(w))
  34. return 'variable-2';
  35. if (state.context == 2 && variables2.test(w))
  36. return 'variable-3';
  37. if (keywords1.test(w)) {
  38. state.context = 1;
  39. return 'keyword';
  40. } else if (keywords2.test(w)) {
  41. state.context = 2;
  42. return 'keyword';
  43. } else if (keywords3.test(w)) {
  44. state.context = 3;
  45. return 'keyword';
  46. }
  47. if (errors.test(w))
  48. return 'error';
  49. } else if (numbers.test(w)) {
  50. return 'number';
  51. } else {
  52. return null;
  53. }
  54. } else if (stream.eat(';')) {
  55. stream.skipToEnd();
  56. return 'comment';
  57. } else if (stream.eat('"')) {
  58. while (w = stream.next()) {
  59. if (w == '"')
  60. break;
  61. if (w == '\\')
  62. stream.next();
  63. }
  64. return 'string';
  65. } else if (stream.eat('\'')) {
  66. if (stream.match(/\\?.'/))
  67. return 'number';
  68. } else if (stream.eat('.') || stream.sol() && stream.eat('#')) {
  69. state.context = 4;
  70. if (stream.eatWhile(/\w/))
  71. return 'def';
  72. } else if (stream.eat('$')) {
  73. if (stream.eatWhile(/[\da-f]/i))
  74. return 'number';
  75. } else if (stream.eat('%')) {
  76. if (stream.eatWhile(/[01]/))
  77. return 'number';
  78. } else {
  79. stream.next();
  80. }
  81. return null;
  82. }
  83. };
  84. });
  85. CodeMirror.defineMIME("text/x-z80", "z80");
  86. });