1
0

z80.js 2.4 KB

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