apl.py 3.1 KB

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