Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pygments/lexers/futhark.py: 100%
17 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:16 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:16 +0000
1"""
2 pygments.lexers.futhark
3 ~~~~~~~~~~~~~~~~~~~~~~~
5 Lexer for the Futhark language
7 :copyright: Copyright 2006-2023 by the Pygments team, see AUTHORS.
8 :license: BSD, see LICENSE for details.
9"""
11from pygments.lexer import RegexLexer, bygroups
12from pygments.token import Comment, Operator, Keyword, Name, String, \
13 Number, Punctuation, Whitespace
14from pygments import unistring as uni
16__all__ = ['FutharkLexer']
19class FutharkLexer(RegexLexer):
20 """
21 A Futhark lexer
23 .. versionadded:: 2.8
24 """
25 name = 'Futhark'
26 url = 'https://futhark-lang.org/'
27 aliases = ['futhark']
28 filenames = ['*.fut']
29 mimetypes = ['text/x-futhark']
31 num_types = ('i8', 'i16', 'i32', 'i64', 'u8', 'u16', 'u32', 'u64', 'f32', 'f64')
33 other_types = ('bool', )
35 reserved = ('if', 'then', 'else', 'def', 'let', 'loop', 'in', 'with',
36 'type', 'type~', 'type^',
37 'val', 'entry', 'for', 'while', 'do', 'case', 'match',
38 'include', 'import', 'module', 'open', 'local', 'assert', '_')
40 ascii = ('NUL', 'SOH', '[SE]TX', 'EOT', 'ENQ', 'ACK',
41 'BEL', 'BS', 'HT', 'LF', 'VT', 'FF', 'CR', 'S[OI]', 'DLE',
42 'DC[1-4]', 'NAK', 'SYN', 'ETB', 'CAN',
43 'EM', 'SUB', 'ESC', '[FGRU]S', 'SP', 'DEL')
45 num_postfix = r'(%s)?' % '|'.join(num_types)
47 identifier_re = '[a-zA-Z_][a-zA-Z_0-9\']*'
49 # opstart_re = '+\-\*/%=\!><\|&\^'
51 tokens = {
52 'root': [
53 (r'--(.*?)$', Comment.Single),
54 (r'\s+', Whitespace),
55 (r'\(\)', Punctuation),
56 (r'\b(%s)(?!\')\b' % '|'.join(reserved), Keyword.Reserved),
57 (r'\b(%s)(?!\')\b' % '|'.join(num_types + other_types), Keyword.Type),
59 # Identifiers
60 (r'#\[([a-zA-Z_\(\) ]*)\]', Comment.Preproc),
61 (r'[#!]?(%s\.)*%s' % (identifier_re, identifier_re), Name),
63 (r'\\', Operator),
64 (r'[-+/%=!><|&*^][-+/%=!><|&*^.]*', Operator),
65 (r'[][(),:;`{}?.\'~^]', Punctuation),
67 # Numbers
68 (r'0[xX]_*[\da-fA-F](_*[\da-fA-F])*_*[pP][+-]?\d(_*\d)*' + num_postfix,
69 Number.Float),
70 (r'0[xX]_*[\da-fA-F](_*[\da-fA-F])*\.[\da-fA-F](_*[\da-fA-F])*'
71 r'(_*[pP][+-]?\d(_*\d)*)?' + num_postfix, Number.Float),
72 (r'\d(_*\d)*_*[eE][+-]?\d(_*\d)*' + num_postfix, Number.Float),
73 (r'\d(_*\d)*\.\d(_*\d)*(_*[eE][+-]?\d(_*\d)*)?' + num_postfix, Number.Float),
74 (r'0[bB]_*[01](_*[01])*' + num_postfix, Number.Bin),
75 (r'0[xX]_*[\da-fA-F](_*[\da-fA-F])*' + num_postfix, Number.Hex),
76 (r'\d(_*\d)*' + num_postfix, Number.Integer),
78 # Character/String Literals
79 (r"'", String.Char, 'character'),
80 (r'"', String, 'string'),
81 # Special
82 (r'\[[a-zA-Z_\d]*\]', Keyword.Type),
83 (r'\(\)', Name.Builtin),
84 ],
85 'character': [
86 # Allows multi-chars, incorrectly.
87 (r"[^\\']'", String.Char, '#pop'),
88 (r"\\", String.Escape, 'escape'),
89 ("'", String.Char, '#pop'),
90 ],
91 'string': [
92 (r'[^\\"]+', String),
93 (r"\\", String.Escape, 'escape'),
94 ('"', String, '#pop'),
95 ],
97 'escape': [
98 (r'[abfnrtv"\'&\\]', String.Escape, '#pop'),
99 (r'\^[][' + uni.Lu + r'@^_]', String.Escape, '#pop'),
100 ('|'.join(ascii), String.Escape, '#pop'),
101 (r'o[0-7]+', String.Escape, '#pop'),
102 (r'x[\da-fA-F]+', String.Escape, '#pop'),
103 (r'\d+', String.Escape, '#pop'),
104 (r'(\s+)(\\)', bygroups(Whitespace, String.Escape), '#pop'),
105 ],
106 }