savi.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. """
  2. pygments.lexers.savi
  3. ~~~~~~~~~~~~~~~~~~~~
  4. Lexer for Savi.
  5. :copyright: Copyright 2006-2023 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. from pygments.lexer import RegexLexer, bygroups, include
  9. from pygments.token import Whitespace, Keyword, Name, String, Number, \
  10. Operator, Punctuation, Comment, Generic, Error
  11. __all__ = ['SaviLexer']
  12. # The canonical version of this file can be found in the following repository,
  13. # where it is kept in sync with any language changes, as well as the other
  14. # pygments-like lexers that are maintained for use with other tools:
  15. # - https://github.com/savi-lang/savi/blob/main/tooling/pygments/lexers/savi.py
  16. #
  17. # If you're changing this file in the pygments repository, please ensure that
  18. # any changes you make are also propagated to the official Savi repository,
  19. # in order to avoid accidental clobbering of your changes later when an update
  20. # from the Savi repository flows forward into the pygments repository.
  21. #
  22. # If you're changing this file in the Savi repository, please ensure that
  23. # any changes you make are also reflected in the other pygments-like lexers
  24. # (rouge, vscode, etc) so that all of the lexers can be kept cleanly in sync.
  25. class SaviLexer(RegexLexer):
  26. """
  27. For Savi source code.
  28. .. versionadded: 2.10
  29. """
  30. name = 'Savi'
  31. url = 'https://github.com/savi-lang/savi'
  32. aliases = ['savi']
  33. filenames = ['*.savi']
  34. tokens = {
  35. "root": [
  36. # Line Comment
  37. (r'//.*?$', Comment.Single),
  38. # Doc Comment
  39. (r'::.*?$', Comment.Single),
  40. # Capability Operator
  41. (r'(\')(\w+)(?=[^\'])', bygroups(Operator, Name)),
  42. # Double-Quote String
  43. (r'\w?"', String.Double, "string.double"),
  44. # Single-Char String
  45. (r"'", String.Char, "string.char"),
  46. # Type Name
  47. (r'(_?[A-Z]\w*)', Name.Class),
  48. # Nested Type Name
  49. (r'(\.)(\s*)(_?[A-Z]\w*)', bygroups(Punctuation, Whitespace, Name.Class)),
  50. # Declare
  51. (r'^([ \t]*)(:\w+)',
  52. bygroups(Whitespace, Name.Tag),
  53. "decl"),
  54. # Error-Raising Calls/Names
  55. (r'((\w+|\+|\-|\*)\!)', Generic.Deleted),
  56. # Numeric Values
  57. (r'\b\d([\d_]*(\.[\d_]+)?)\b', Number),
  58. # Hex Numeric Values
  59. (r'\b0x([0-9a-fA-F_]+)\b', Number.Hex),
  60. # Binary Numeric Values
  61. (r'\b0b([01_]+)\b', Number.Bin),
  62. # Function Call (with braces)
  63. (r'\w+(?=\()', Name.Function),
  64. # Function Call (with receiver)
  65. (r'(\.)(\s*)(\w+)', bygroups(Punctuation, Whitespace, Name.Function)),
  66. # Function Call (with self receiver)
  67. (r'(@)(\w+)', bygroups(Punctuation, Name.Function)),
  68. # Parenthesis
  69. (r'\(', Punctuation, "root"),
  70. (r'\)', Punctuation, "#pop"),
  71. # Brace
  72. (r'\{', Punctuation, "root"),
  73. (r'\}', Punctuation, "#pop"),
  74. # Bracket
  75. (r'\[', Punctuation, "root"),
  76. (r'(\])(\!)', bygroups(Punctuation, Generic.Deleted), "#pop"),
  77. (r'\]', Punctuation, "#pop"),
  78. # Punctuation
  79. (r'[,;:\.@]', Punctuation),
  80. # Piping Operators
  81. (r'(\|\>)', Operator),
  82. # Branching Operators
  83. (r'(\&\&|\|\||\?\?|\&\?|\|\?|\.\?)', Operator),
  84. # Comparison Operators
  85. (r'(\<\=\>|\=\~|\=\=|\<\=|\>\=|\<|\>)', Operator),
  86. # Arithmetic Operators
  87. (r'(\+|\-|\/|\*|\%)', Operator),
  88. # Assignment Operators
  89. (r'(\=)', Operator),
  90. # Other Operators
  91. (r'(\!|\<\<|\<|\&|\|)', Operator),
  92. # Identifiers
  93. (r'\b\w+\b', Name),
  94. # Whitespace
  95. (r'[ \t\r]+\n*|\n+', Whitespace),
  96. ],
  97. # Declare (nested rules)
  98. "decl": [
  99. (r'\b[a-z_]\w*\b(?!\!)', Keyword.Declaration),
  100. (r':', Punctuation, "#pop"),
  101. (r'\n', Whitespace, "#pop"),
  102. include("root"),
  103. ],
  104. # Double-Quote String (nested rules)
  105. "string.double": [
  106. (r'\\\(', String.Interpol, "string.interpolation"),
  107. (r'\\u[0-9a-fA-F]{4}', String.Escape),
  108. (r'\\x[0-9a-fA-F]{2}', String.Escape),
  109. (r'\\[bfnrt\\\']', String.Escape),
  110. (r'\\"', String.Escape),
  111. (r'"', String.Double, "#pop"),
  112. (r'[^\\"]+', String.Double),
  113. (r'.', Error),
  114. ],
  115. # Single-Char String (nested rules)
  116. "string.char": [
  117. (r'\\u[0-9a-fA-F]{4}', String.Escape),
  118. (r'\\x[0-9a-fA-F]{2}', String.Escape),
  119. (r'\\[bfnrt\\\']', String.Escape),
  120. (r"\\'", String.Escape),
  121. (r"'", String.Char, "#pop"),
  122. (r"[^\\']+", String.Char),
  123. (r'.', Error),
  124. ],
  125. # Interpolation inside String (nested rules)
  126. "string.interpolation": [
  127. (r"\)", String.Interpol, "#pop"),
  128. include("root"),
  129. ]
  130. }