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