z80.js 2.7 KB

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