arrow.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. """
  2. pygments.lexers.arrow
  3. ~~~~~~~~~~~~~~~~~~~~~
  4. Lexer for Arrow.
  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, bygroups, default, include
  9. from pygments.token import Text, Operator, Keyword, Punctuation, Name, \
  10. String, Number, Whitespace
  11. __all__ = ['ArrowLexer']
  12. TYPES = r'\b(int|bool|char)((?:\[\])*)(?=\s+)'
  13. IDENT = r'([a-zA-Z_][a-zA-Z0-9_]*)'
  14. DECL = TYPES + r'(\s+)' + IDENT
  15. class ArrowLexer(RegexLexer):
  16. """
  17. Lexer for Arrow
  18. """
  19. name = 'Arrow'
  20. url = 'https://pypi.org/project/py-arrow-lang/'
  21. aliases = ['arrow']
  22. filenames = ['*.arw']
  23. version_added = '2.7'
  24. tokens = {
  25. 'root': [
  26. (r'\s+', Whitespace),
  27. (r'^[|\s]+', Punctuation),
  28. include('blocks'),
  29. include('statements'),
  30. include('expressions'),
  31. ],
  32. 'blocks': [
  33. (r'(function)(\n+)(/-->)(\s*)' +
  34. DECL + # 4 groups
  35. r'(\()', bygroups(
  36. Keyword.Reserved, Whitespace, Punctuation,
  37. Whitespace, Keyword.Type, Punctuation, Whitespace,
  38. Name.Function, Punctuation
  39. ), 'fparams'),
  40. (r'/-->$|\\-->$|/--<|\\--<|\^', Punctuation),
  41. ],
  42. 'statements': [
  43. (DECL, bygroups(Keyword.Type, Punctuation, Text, Name.Variable)),
  44. (r'\[', Punctuation, 'index'),
  45. (r'=', Operator),
  46. (r'require|main', Keyword.Reserved),
  47. (r'print', Keyword.Reserved, 'print'),
  48. ],
  49. 'expressions': [
  50. (r'\s+', Whitespace),
  51. (r'[0-9]+', Number.Integer),
  52. (r'true|false', Keyword.Constant),
  53. (r"'", String.Char, 'char'),
  54. (r'"', String.Double, 'string'),
  55. (r'\{', Punctuation, 'array'),
  56. (r'==|!=|<|>|\+|-|\*|/|%', Operator),
  57. (r'and|or|not|length', Operator.Word),
  58. (r'(input)(\s+)(int|char\[\])', bygroups(
  59. Keyword.Reserved, Whitespace, Keyword.Type
  60. )),
  61. (IDENT + r'(\()', bygroups(
  62. Name.Function, Punctuation
  63. ), 'fargs'),
  64. (IDENT, Name.Variable),
  65. (r'\[', Punctuation, 'index'),
  66. (r'\(', Punctuation, 'expressions'),
  67. (r'\)', Punctuation, '#pop'),
  68. ],
  69. 'print': [
  70. include('expressions'),
  71. (r',', Punctuation),
  72. default('#pop'),
  73. ],
  74. 'fparams': [
  75. (DECL, bygroups(Keyword.Type, Punctuation, Whitespace, Name.Variable)),
  76. (r',', Punctuation),
  77. (r'\)', Punctuation, '#pop'),
  78. ],
  79. 'escape': [
  80. (r'\\(["\\/abfnrtv]|[0-9]{1,3}|x[0-9a-fA-F]{2}|u[0-9a-fA-F]{4})',
  81. String.Escape),
  82. ],
  83. 'char': [
  84. (r"'", String.Char, '#pop'),
  85. include('escape'),
  86. (r"[^'\\]", String.Char),
  87. ],
  88. 'string': [
  89. (r'"', String.Double, '#pop'),
  90. include('escape'),
  91. (r'[^"\\]+', String.Double),
  92. ],
  93. 'array': [
  94. include('expressions'),
  95. (r'\}', Punctuation, '#pop'),
  96. (r',', Punctuation),
  97. ],
  98. 'fargs': [
  99. include('expressions'),
  100. (r'\)', Punctuation, '#pop'),
  101. (r',', Punctuation),
  102. ],
  103. 'index': [
  104. include('expressions'),
  105. (r'\]', Punctuation, '#pop'),
  106. ],
  107. }