bqn.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. """
  2. pygments.lexers.bqn
  3. ~~~~~~~~~~~~~~~~~~~
  4. Lexer for BQN.
  5. :copyright: Copyright 2006-2025 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__ = ['BQNLexer']
  12. class BQNLexer(RegexLexer):
  13. """
  14. A simple BQN lexer.
  15. """
  16. name = 'BQN'
  17. url = 'https://mlochbaum.github.io/BQN/index.html'
  18. aliases = ['bqn']
  19. filenames = ['*.bqn']
  20. mimetypes = []
  21. version_added = '2.16'
  22. # An inter_word_char. Necessary because \w matches all alphanumeric
  23. # Unicode characters, including ones (e.g., 𝕊) that BQN treats special.
  24. _iwc = r'((?=[^𝕎𝕏𝔽𝔾𝕊𝕨𝕩𝕗𝕘𝕤𝕣])\w)'
  25. tokens = {
  26. 'root': [
  27. # Whitespace
  28. # ==========
  29. (r'\s+', Whitespace),
  30. #
  31. # Comment
  32. # =======
  33. # '#' is a comment that continues to the end of the line
  34. (r'#.*$', Comment.Single),
  35. #
  36. # Strings
  37. # =======
  38. (r'\'((\'\')|[^\'])*\'', String.Single),
  39. (r'"(("")|[^"])*"', String.Double),
  40. #
  41. # Null Character
  42. # ==============
  43. # Literal representation of the null character
  44. (r'@', String.Symbol),
  45. #
  46. # Punctuation
  47. # ===========
  48. # This token type is used for diamond, commas
  49. # and array and list brackets and strand syntax
  50. (r'[\.⋄,\[\]⟨⟩‿]', Punctuation),
  51. #
  52. # Expression Grouping
  53. # ===================
  54. # Since this token type is important in BQN, it is not included in
  55. # the punctuation token type but rather in the following one
  56. (r'[\(\)]', String.Regex),
  57. #
  58. # Numbers
  59. # =======
  60. # Includes the numeric literals and the Nothing character
  61. (r'¯?[0-9](([0-9]|_)*\.?([0-9]|_)+|([0-9]|_)*)([Ee][¯]?([0-9]|_)+)?|¯|∞|π|·', Number),
  62. #
  63. # Variables
  64. # =========
  65. (r'[a-z]' + _iwc + r'*', Name.Variable),
  66. #
  67. # 2-Modifiers
  68. # ===========
  69. # Needs to come before the 1-modifiers due to _𝕣 and _𝕣_
  70. (r'[∘○⊸⟜⌾⊘◶⎉⚇⍟⎊]', Name.Property),
  71. (r'_(𝕣|[a-zA-Z0-9]+)_', Name.Property),
  72. #
  73. # 1-Modifiers
  74. # ===========
  75. (r'[˙˜˘¨⌜⁼´˝`𝕣]', Name.Attribute),
  76. (r'_(𝕣|[a-zA-Z0-9]+)', Name.Attribute),
  77. #
  78. # Functions
  79. # =========
  80. # The monadic or dyadic function primitives and function
  81. # operands and arguments, along with function self-reference
  82. (r'[+\-×÷\⋆√⌊⌈∧∨¬|≤<>≥=≠≡≢⊣⊢⥊∾≍⋈↑↓↕«»⌽⍉/⍋⍒⊏⊑⊐⊒∊⍷⊔!𝕎𝕏𝔽𝔾𝕊]',
  83. Operator),
  84. (r'[A-Z]' + _iwc + r'*|•' + _iwc + r'+', Operator),
  85. #
  86. # Constant
  87. # ========
  88. (r'˙', Name.Constant),
  89. #
  90. # Define/Export/Change
  91. # ====================
  92. (r'[←↩⇐]', Keyword.Declaration),
  93. #
  94. # Blocks
  95. # ======
  96. (r'[{}]', Keyword.Type),
  97. #
  98. # Extra characters
  99. # ================
  100. (r'[;:?𝕨𝕩𝕗𝕘𝕤]', Name.Entity),
  101. #
  102. ],
  103. }