savi.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. """
  2. pygments.lexers.savi
  3. ~~~~~~~~~~~~~~~~~~~~
  4. Lexer for Savi.
  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, 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. version_added = ''
  35. tokens = {
  36. "root": [
  37. # Line Comment
  38. (r'//.*?$', Comment.Single),
  39. # Doc Comment
  40. (r'::.*?$', Comment.Single),
  41. # Capability Operator
  42. (r'(\')(\w+)(?=[^\'])', bygroups(Operator, Name)),
  43. # Double-Quote String
  44. (r'\w?"', String.Double, "string.double"),
  45. # Single-Char String
  46. (r"'", String.Char, "string.char"),
  47. # Type Name
  48. (r'(_?[A-Z]\w*)', Name.Class),
  49. # Nested Type Name
  50. (r'(\.)(\s*)(_?[A-Z]\w*)', bygroups(Punctuation, Whitespace, Name.Class)),
  51. # Declare
  52. (r'^([ \t]*)(:\w+)',
  53. bygroups(Whitespace, Name.Tag),
  54. "decl"),
  55. # Error-Raising Calls/Names
  56. (r'((\w+|\+|\-|\*)\!)', Generic.Deleted),
  57. # Numeric Values
  58. (r'\b\d([\d_]*(\.[\d_]+)?)\b', Number),
  59. # Hex Numeric Values
  60. (r'\b0x([0-9a-fA-F_]+)\b', Number.Hex),
  61. # Binary Numeric Values
  62. (r'\b0b([01_]+)\b', Number.Bin),
  63. # Function Call (with braces)
  64. (r'\w+(?=\()', Name.Function),
  65. # Function Call (with receiver)
  66. (r'(\.)(\s*)(\w+)', bygroups(Punctuation, Whitespace, Name.Function)),
  67. # Function Call (with self receiver)
  68. (r'(@)(\w+)', bygroups(Punctuation, Name.Function)),
  69. # Parenthesis
  70. (r'\(', Punctuation, "root"),
  71. (r'\)', Punctuation, "#pop"),
  72. # Brace
  73. (r'\{', Punctuation, "root"),
  74. (r'\}', Punctuation, "#pop"),
  75. # Bracket
  76. (r'\[', Punctuation, "root"),
  77. (r'(\])(\!)', bygroups(Punctuation, Generic.Deleted), "#pop"),
  78. (r'\]', Punctuation, "#pop"),
  79. # Punctuation
  80. (r'[,;:\.@]', Punctuation),
  81. # Piping Operators
  82. (r'(\|\>)', Operator),
  83. # Branching Operators
  84. (r'(\&\&|\|\||\?\?|\&\?|\|\?|\.\?)', Operator),
  85. # Comparison Operators
  86. (r'(\<\=\>|\=\~|\=\=|\<\=|\>\=|\<|\>)', Operator),
  87. # Arithmetic Operators
  88. (r'(\+|\-|\/|\*|\%)', Operator),
  89. # Assignment Operators
  90. (r'(\=)', Operator),
  91. # Other Operators
  92. (r'(\!|\<\<|\<|\&|\|)', Operator),
  93. # Identifiers
  94. (r'\b\w+\b', Name),
  95. # Whitespace
  96. (r'[ \t\r]+\n*|\n+', Whitespace),
  97. ],
  98. # Declare (nested rules)
  99. "decl": [
  100. (r'\b[a-z_]\w*\b(?!\!)', Keyword.Declaration),
  101. (r':', Punctuation, "#pop"),
  102. (r'\n', Whitespace, "#pop"),
  103. include("root"),
  104. ],
  105. # Double-Quote String (nested rules)
  106. "string.double": [
  107. (r'\\\(', String.Interpol, "string.interpolation"),
  108. (r'\\u[0-9a-fA-F]{4}', String.Escape),
  109. (r'\\x[0-9a-fA-F]{2}', String.Escape),
  110. (r'\\[bfnrt\\\']', String.Escape),
  111. (r'\\"', String.Escape),
  112. (r'"', String.Double, "#pop"),
  113. (r'[^\\"]+', String.Double),
  114. (r'.', Error),
  115. ],
  116. # Single-Char String (nested rules)
  117. "string.char": [
  118. (r'\\u[0-9a-fA-F]{4}', String.Escape),
  119. (r'\\x[0-9a-fA-F]{2}', String.Escape),
  120. (r'\\[bfnrt\\\']', String.Escape),
  121. (r"\\'", String.Escape),
  122. (r"'", String.Char, "#pop"),
  123. (r"[^\\']+", String.Char),
  124. (r'.', Error),
  125. ],
  126. # Interpolation inside String (nested rules)
  127. "string.interpolation": [
  128. (r"\)", String.Interpol, "#pop"),
  129. include("root"),
  130. ]
  131. }