tal.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. """
  2. pygments.lexers.tal
  3. ~~~~~~~~~~~~~~~~~~~
  4. Lexer for Uxntal
  5. .. versionadded:: 2.12
  6. :copyright: Copyright 2006-2023 by the Pygments team, see AUTHORS.
  7. :license: BSD, see LICENSE for details.
  8. """
  9. from pygments.lexer import RegexLexer, words
  10. from pygments.token import Comment, Keyword, Name, String, Number, \
  11. Punctuation, Whitespace, Literal
  12. __all__ = ['TalLexer']
  13. class TalLexer(RegexLexer):
  14. """
  15. For `Uxntal <https://wiki.xxiivv.com/site/uxntal.html>`_ source code.
  16. .. versionadded:: 2.12
  17. """
  18. name = 'Tal'
  19. aliases = ['tal', 'uxntal']
  20. filenames = ['*.tal']
  21. mimetypes = ['text/x-uxntal']
  22. instructions = [
  23. 'BRK', 'LIT', 'INC', 'POP', 'DUP', 'NIP', 'SWP', 'OVR', 'ROT',
  24. 'EQU', 'NEQ', 'GTH', 'LTH', 'JMP', 'JCN', 'JSR', 'STH',
  25. 'LDZ', 'STZ', 'LDR', 'STR', 'LDA', 'STA', 'DEI', 'DEO',
  26. 'ADD', 'SUB', 'MUL', 'DIV', 'AND', 'ORA', 'EOR', 'SFT'
  27. ]
  28. tokens = {
  29. # the comment delimiters must not be adjacent to non-space characters.
  30. # this means ( foo ) is a valid comment but (foo) is not. this also
  31. # applies to nested comments.
  32. 'comment': [
  33. (r'(?<!\S)\((?!\S)', Comment.Multiline, '#push'), # nested comments
  34. (r'(?<!\S)\)(?!\S)', Comment.Multiline, '#pop'), # nested comments
  35. (r'[^()]+', Comment.Multiline), # comments
  36. (r'[()]+', Comment.Multiline), # comments
  37. ],
  38. 'root': [
  39. (r'\s+', Whitespace), # spaces
  40. (r'(?<!\S)\((?!\S)', Comment.Multiline, 'comment'), # comments
  41. (words(instructions, prefix=r'(?<!\S)', suffix=r'2?k?r?(?!\S)'),
  42. Keyword.Reserved), # instructions
  43. (r'[][{}](?!\S)', Punctuation), # delimiters
  44. (r'#([0-9a-f]{2}){1,2}(?!\S)', Number.Hex), # integer
  45. (r'"\S+', String), # raw string
  46. (r'([0-9a-f]{2}){1,2}(?!\S)', Literal), # raw integer
  47. (r'[|$][0-9a-f]{1,4}(?!\S)', Keyword.Declaration), # abs/rel pad
  48. (r'%\S+', Name.Decorator), # macro
  49. (r'@\S+', Name.Function), # label
  50. (r'&\S+', Name.Label), # sublabel
  51. (r'/\S+', Name.Tag), # spacer
  52. (r'\.\S+', Name.Variable.Magic), # literal zero page addr
  53. (r',\S+', Name.Variable.Instance), # literal rel addr
  54. (r';\S+', Name.Variable.Global), # literal abs addr
  55. (r'-\S+', Literal), # raw zero page addr
  56. (r'_\S+', Literal), # raw relative addr
  57. (r'=\S+', Literal), # raw absolute addr
  58. (r'!\S+', Name.Function), # immediate jump
  59. (r'\?\S+', Name.Function), # conditional immediate jump
  60. (r'~\S+', Keyword.Namespace), # include
  61. (r'\S+', Name.Function), # macro invocation, immediate subroutine
  62. ]
  63. }
  64. def analyse_text(text):
  65. return '|0100' in text[:500]