1"""
2 pygments.lexers.spice
3 ~~~~~~~~~~~~~~~~~~~~~
4
5 Lexers for the Spice programming 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, bygroups, words
12from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
13 Number, Punctuation, Whitespace
14
15__all__ = ['SpiceLexer']
16
17
18class SpiceLexer(RegexLexer):
19 """
20 For Spice source.
21 """
22 name = 'Spice'
23 url = 'https://www.spicelang.com'
24 filenames = ['*.spice']
25 aliases = ['spice', 'spicelang']
26 mimetypes = ['text/x-spice']
27 version_added = '2.11'
28
29 tokens = {
30 'root': [
31 (r'\n', Whitespace),
32 (r'\s+', Whitespace),
33 (r'\\\n', Text),
34 # comments
35 (r'//(.*?)\n', Comment.Single),
36 (r'/(\\\n)?[*]{2}(.|\n)*?[*](\\\n)?/', String.Doc),
37 (r'/(\\\n)?[*](.|\n)*?[*](\\\n)?/', Comment.Multiline),
38 # keywords
39 (r'(import|as)\b', Keyword.Namespace),
40 (r'(f|p|type|struct|interface|enum|alias|operator)\b', Keyword.Declaration),
41 (words(('if', 'else', 'switch', 'case', 'default', 'for', 'foreach', 'do',
42 'while', 'break', 'continue', 'fallthrough', 'return', 'assert',
43 'unsafe', 'ext'), suffix=r'\b'), Keyword),
44 (words(('const', 'signed', 'unsigned', 'inline', 'public', 'heap', 'compose'),
45 suffix=r'\b'), Keyword.Pseudo),
46 (words(('new', 'yield', 'stash', 'pick', 'sync', 'class'), suffix=r'\b'),
47 Keyword.Reserved),
48 (r'(true|false|nil)\b', Keyword.Constant),
49 (words(('double', 'int', 'short', 'long', 'byte', 'char', 'string',
50 'bool', 'dyn'), suffix=r'\b'), Keyword.Type),
51 (words(('printf', 'sizeof', 'alignof', 'len', 'panic'), suffix=r'\b(\()'),
52 bygroups(Name.Builtin, Punctuation)),
53 # numeric literals
54 (r'[-]?[0-9]*[.][0-9]+([eE][+-]?[0-9]+)?', Number.Double),
55 (r'0[bB][01]+[slu]?', Number.Bin),
56 (r'0[oO][0-7]+[slu]?', Number.Oct),
57 (r'0[xXhH][0-9a-fA-F]+[slu]?', Number.Hex),
58 (r'(0[dD])?[0-9]+[slu]?', Number.Integer),
59 # string literal
60 (r'"(\\\\|\\[^\\]|[^"\\])*"', String),
61 # char literal
62 (r'\'(\\\\|\\[^\\]|[^\'\\])\'', String.Char),
63 # tokens
64 (r'<<=|>>=|<<|>>|<=|>=|\+=|-=|\*=|/=|\%=|\|=|&=|\^=|&&|\|\||&|\||'
65 r'\+\+|--|\%|\^|\~|==|!=|->|::|[.]{3}|#!|#|[+\-*/&]', Operator),
66 (r'[|<>=!()\[\]{}.,;:\?]', Punctuation),
67 # identifiers
68 (r'[^\W\d]\w*', Name.Other),
69 ]
70 }