arrow.py 3.4 KB

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