/Packages/pygments/all/pygments/lexers/esoteric.py

https://gitlab.com/vladvesa/sublimetext-php-plugins · Python · 114 lines · 78 code · 12 blank · 24 comment · 0 complexity · 3080e426f53a7286d52ebb0485b991a9 MD5 · raw file

  1. # -*- coding: utf-8 -*-
  2. """
  3. pygments.lexers.esoteric
  4. ~~~~~~~~~~~~~~~~~~~~~~~~
  5. Lexers for esoteric languages.
  6. :copyright: Copyright 2006-2015 by the Pygments team, see AUTHORS.
  7. :license: BSD, see LICENSE for details.
  8. """
  9. from pygments.lexer import RegexLexer, include
  10. from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
  11. Number, Punctuation, Error
  12. __all__ = ['BrainfuckLexer', 'BefungeLexer', 'RedcodeLexer']
  13. class BrainfuckLexer(RegexLexer):
  14. """
  15. Lexer for the esoteric `BrainFuck <http://www.muppetlabs.com/~breadbox/bf/>`_
  16. language.
  17. """
  18. name = 'Brainfuck'
  19. aliases = ['brainfuck', 'bf']
  20. filenames = ['*.bf', '*.b']
  21. mimetypes = ['application/x-brainfuck']
  22. tokens = {
  23. 'common': [
  24. # use different colors for different instruction types
  25. (r'[.,]+', Name.Tag),
  26. (r'[+-]+', Name.Builtin),
  27. (r'[<>]+', Name.Variable),
  28. (r'[^.,+\-<>\[\]]+', Comment),
  29. ],
  30. 'root': [
  31. (r'\[', Keyword, 'loop'),
  32. (r'\]', Error),
  33. include('common'),
  34. ],
  35. 'loop': [
  36. (r'\[', Keyword, '#push'),
  37. (r'\]', Keyword, '#pop'),
  38. include('common'),
  39. ]
  40. }
  41. class BefungeLexer(RegexLexer):
  42. """
  43. Lexer for the esoteric `Befunge <http://en.wikipedia.org/wiki/Befunge>`_
  44. language.
  45. .. versionadded:: 0.7
  46. """
  47. name = 'Befunge'
  48. aliases = ['befunge']
  49. filenames = ['*.befunge']
  50. mimetypes = ['application/x-befunge']
  51. tokens = {
  52. 'root': [
  53. (r'[0-9a-f]', Number),
  54. (r'[+*/%!`-]', Operator), # Traditional math
  55. (r'[<>^v?\[\]rxjk]', Name.Variable), # Move, imperatives
  56. (r'[:\\$.,n]', Name.Builtin), # Stack ops, imperatives
  57. (r'[|_mw]', Keyword),
  58. (r'[{}]', Name.Tag), # Befunge-98 stack ops
  59. (r'".*?"', String.Double), # Strings don't appear to allow escapes
  60. (r'\'.', String.Single), # Single character
  61. (r'[#;]', Comment), # Trampoline... depends on direction hit
  62. (r'[pg&~=@iotsy]', Keyword), # Misc
  63. (r'[()A-Z]', Comment), # Fingerprints
  64. (r'\s+', Text), # Whitespace doesn't matter
  65. ],
  66. }
  67. class RedcodeLexer(RegexLexer):
  68. """
  69. A simple Redcode lexer based on ICWS'94.
  70. Contributed by Adam Blinkinsop <blinks@acm.org>.
  71. .. versionadded:: 0.8
  72. """
  73. name = 'Redcode'
  74. aliases = ['redcode']
  75. filenames = ['*.cw']
  76. opcodes = ('DAT', 'MOV', 'ADD', 'SUB', 'MUL', 'DIV', 'MOD',
  77. 'JMP', 'JMZ', 'JMN', 'DJN', 'CMP', 'SLT', 'SPL',
  78. 'ORG', 'EQU', 'END')
  79. modifiers = ('A', 'B', 'AB', 'BA', 'F', 'X', 'I')
  80. tokens = {
  81. 'root': [
  82. # Whitespace:
  83. (r'\s+', Text),
  84. (r';.*$', Comment.Single),
  85. # Lexemes:
  86. # Identifiers
  87. (r'\b(%s)\b' % '|'.join(opcodes), Name.Function),
  88. (r'\b(%s)\b' % '|'.join(modifiers), Name.Decorator),
  89. (r'[A-Za-z_]\w+', Name),
  90. # Operators
  91. (r'[-+*/%]', Operator),
  92. (r'[#$@<>]', Operator), # mode
  93. (r'[.,]', Punctuation), # mode
  94. # Numbers
  95. (r'[-+]?\d+', Number.Integer),
  96. ],
  97. }