apl.py 3.3 KB

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