apl.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. """
  2. pygments.lexers.apl
  3. ~~~~~~~~~~~~~~~~~~~
  4. Lexers for APL.
  5. :copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. from pygments.lexer import RegexLexer
  9. from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
  10. Number, Punctuation
  11. __all__ = ['APLLexer']
  12. class APLLexer(RegexLexer):
  13. """
  14. A simple `APL <https://en.m.wikipedia.org/wiki/APL_(programming_language)>`_ lexer.
  15. .. versionadded:: 2.0
  16. """
  17. name = 'APL'
  18. aliases = ['apl']
  19. filenames = [
  20. '*.apl', '*.aplf', '*.aplo', '*.apln',
  21. '*.aplc', '*.apli', '*.dyalog',
  22. ]
  23. tokens = {
  24. 'root': [
  25. # Whitespace
  26. # ==========
  27. (r'\s+', Text),
  28. #
  29. # Comment
  30. # =======
  31. # '⍝' is traditional; '#' is supported by GNU APL and NGN (but not Dyalog)
  32. (r'[⍝#].*$', Comment.Single),
  33. #
  34. # Strings
  35. # =======
  36. (r'\'((\'\')|[^\'])*\'', String.Single),
  37. (r'"(("")|[^"])*"', String.Double), # supported by NGN APL
  38. #
  39. # Punctuation
  40. # ===========
  41. # This token type is used for diamond and parenthesis
  42. # but not for bracket and ; (see below)
  43. (r'[⋄◇()]', Punctuation),
  44. #
  45. # Array indexing
  46. # ==============
  47. # Since this token type is very important in APL, it is not included in
  48. # the punctuation token type but rather in the following one
  49. (r'[\[\];]', String.Regex),
  50. #
  51. # Distinguished names
  52. # ===================
  53. # following IBM APL2 standard
  54. (r'⎕[A-Za-zΔ∆⍙][A-Za-zΔ∆⍙_¯0-9]*', Name.Function),
  55. #
  56. # Labels
  57. # ======
  58. # following IBM APL2 standard
  59. # (r'[A-Za-zΔ∆⍙][A-Za-zΔ∆⍙_¯0-9]*:', Name.Label),
  60. #
  61. # Variables
  62. # =========
  63. # following IBM APL2 standard (with a leading _ ok for GNU APL and Dyalog)
  64. (r'[A-Za-zΔ∆⍙_][A-Za-zΔ∆⍙_¯0-9]*', Name.Variable),
  65. #
  66. # Numbers
  67. # =======
  68. (r'¯?(0[Xx][0-9A-Fa-f]+|[0-9]*\.?[0-9]+([Ee][+¯]?[0-9]+)?|¯|∞)'
  69. r'([Jj]¯?(0[Xx][0-9A-Fa-f]+|[0-9]*\.?[0-9]+([Ee][+¯]?[0-9]+)?|¯|∞))?',
  70. Number),
  71. #
  72. # Operators
  73. # ==========
  74. (r'[\.\\\/⌿⍀¨⍣⍨⍠⍤∘⌸&⌶@⌺⍥⍛⍢]', Name.Attribute), # closest token type
  75. (r'[+\-×÷⌈⌊∣|⍳?*⍟○!⌹<≤=>≥≠≡≢∊⍷∪∩~∨∧⍱⍲⍴,⍪⌽⊖⍉↑↓⊂⊃⌷⍋⍒⊤⊥⍕⍎⊣⊢⍁⍂≈⌸⍯↗⊆⊇⍸√⌾…⍮]',
  76. Operator),
  77. #
  78. # Constant
  79. # ========
  80. (r'⍬', Name.Constant),
  81. #
  82. # Quad symbol
  83. # ===========
  84. (r'[⎕⍞]', Name.Variable.Global),
  85. #
  86. # Arrows left/right
  87. # =================
  88. (r'[←→]', Keyword.Declaration),
  89. #
  90. # D-Fn
  91. # ====
  92. (r'[⍺⍵⍶⍹∇:]', Name.Builtin.Pseudo),
  93. (r'[{}]', Keyword.Type),
  94. ],
  95. }