hare.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. """
  2. pygments.lexers.hare
  3. ~~~~~~~~~~~~~~~~~~~~
  4. Lexers for the Hare language.
  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, include, words
  9. from pygments.token import Comment, Operator, Keyword, Name, String, \
  10. Number, Punctuation, Whitespace
  11. __all__ = ['HareLexer']
  12. class HareLexer(RegexLexer):
  13. """
  14. Lexer for the Hare programming language.
  15. """
  16. name = 'Hare'
  17. url = 'https://harelang.org/'
  18. aliases = ['hare']
  19. filenames = ['*.ha']
  20. mimetypes = ['text/x-hare']
  21. version_added = '2.19'
  22. _ws = r'(?:\s|//.*?\n|/[*].*?[*]/)+'
  23. _ws1 = r'\s*(?:/[*].*?[*]/\s*)?'
  24. tokens = {
  25. 'whitespace': [
  26. (r'^use.*;', Comment.Preproc),
  27. (r'@[a-z]+', Comment.Preproc),
  28. (r'\n', Whitespace),
  29. (r'\s+', Whitespace),
  30. (r'//.*?$', Comment.Single),
  31. ],
  32. 'statements': [
  33. (r'"', String, 'string'),
  34. (r'`[^`]*`', String),
  35. (r"'(\\.|\\[0-7]{1,3}|\\x[a-fA-F0-9]{1,2}|[^\\\'\n])'", String.Char),
  36. (r'(\d+\.\d*|\.\d+|\d+)[eE][+-]?\d+[LlUu]*', Number.Float),
  37. (r'(\d+\.\d*|\.\d+|\d+[fF])[fF]?', Number.Float),
  38. (r'0x[0-9a-fA-F]+[LlUu]*', Number.Hex),
  39. (r'0o[0-7]+[LlUu]*', Number.Oct),
  40. (r'\d+[zui]?(\d+)?', Number.Integer),
  41. (r'[~!%^&*+=|?:<>/-]', Operator),
  42. (words(('as', 'is', '=>', '..', '...')), Operator),
  43. (r'[()\[\],.{};]+', Punctuation),
  44. (words(('abort', 'align', 'alloc', 'append', 'assert', 'case',
  45. 'const', 'def', 'defer', 'delete', 'else', 'enum', 'export',
  46. 'fn', 'for', 'free', 'if', 'let', 'len', 'match', 'offset',
  47. 'return', 'static', 'struct', 'switch', 'type', 'union',
  48. 'yield', 'vastart', 'vaarg', 'vaend'),
  49. suffix=r'\b'), Keyword),
  50. (r'(bool|int|uint|uintptr|u8|u16|u32|u64|i8|i16|i32|i64|f32|f64|null|done|never|void|nullable|rune|size|valist)\b',
  51. Keyword.Type),
  52. (r'(true|false|null)\b', Name.Builtin),
  53. (r'[a-zA-Z_]\w*', Name),
  54. ],
  55. 'string': [
  56. (r'"', String, '#pop'),
  57. (r'\\([\\abfnrtv"\']|x[a-fA-F0-9]{2,4}|'
  58. r'u[a-fA-F0-9]{4}|U[a-fA-F0-9]{8}|[0-7]{1,3})', String.Escape),
  59. (r'[^\\"\n]+', String), # all other characters
  60. (r'\\', String), # stray backslash
  61. ],
  62. 'root': [
  63. include('whitespace'),
  64. include('statements'),
  65. ],
  66. }