Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/pygments/lexers/go.py: 100%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""
2 pygments.lexers.go
3 ~~~~~~~~~~~~~~~~~~
5 Lexers for the Google Go language.
7 :copyright: Copyright 2006-present by the Pygments team, see AUTHORS.
8 :license: BSD, see LICENSE for details.
9"""
11from pygments.lexer import RegexLexer, bygroups, words
12from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
13 Number, Punctuation, Whitespace
15__all__ = ['GoLexer']
18class GoLexer(RegexLexer):
19 """
20 For Go source.
21 """
22 name = 'Go'
23 url = 'https://go.dev/'
24 filenames = ['*.go']
25 aliases = ['go', 'golang']
26 mimetypes = ['text/x-gosrc']
27 version_added = '1.2'
29 tokens = {
30 'root': [
31 (r'\n', Whitespace),
32 (r'\s+', Whitespace),
33 (r'(\\)(\n)', bygroups(Text, Whitespace)), # line continuations
34 (r'//(.*?)$', Comment.Single),
35 (r'/(\\\n)?[*][\s\S]*?[*](\\\n)?/', Comment.Multiline),
36 (r'(import|package)\b', Keyword.Namespace),
37 (r'(var|func|struct|type|interface|const)\b',
38 Keyword.Declaration),
39 (words((
40 'break', 'default', 'select', 'case', 'defer', 'go',
41 'else', 'goto', 'switch', 'fallthrough', 'if', 'range',
42 'continue', 'for', 'return'), suffix=r'\b'),
43 Keyword),
44 (r'(true|false|iota|nil)\b', Keyword.Constant),
45 (words((
46 'print', 'println', 'panic', 'recover', 'close', 'complex',
47 'real', 'imag', 'len', 'cap', 'append', 'copy', 'delete',
48 'new', 'make', 'min', 'max', 'clear'), suffix=r'\b(\()'),
49 bygroups(Name.Builtin, Punctuation)),
50 (words((
51 'uint', 'uint8', 'uint16', 'uint32', 'uint64',
52 'int', 'int8', 'int16', 'int32', 'int64',
53 'float32', 'float64', 'map', 'chan',
54 'complex64', 'complex128', 'byte', 'rune',
55 'string', 'bool', 'error', 'uintptr', 'any', 'comparable'), suffix=r'\b'),
56 Keyword.Type),
57 # imaginary_lit
58 (r'\d+i', Number),
59 (r'\d+\.\d*([Ee][-+]\d+)?i', Number),
60 (r'\.\d+([Ee][-+]\d+)?i', Number),
61 (r'\d+[Ee][-+]\d+i', Number),
62 # float_lit
63 (r'\d+(\.\d+[eE][+\-]?\d+|'
64 r'\.\d*|[eE][+\-]?\d+)', Number.Float),
65 (r'\.\d+([eE][+\-]?\d+)?', Number.Float),
66 # int_lit
67 # -- binary_lit
68 (r'0[bB](_?[01])+', Number.Bin),
69 # -- octal_lit
70 (r'0[oO]?(_?[0-7])+', Number.Oct),
71 # -- hex_lit
72 (r'0[xX](_?[0-9a-fA-F])+', Number.Hex),
73 # -- decimal_lit
74 (r'(0|[1-9](_?[0-9])*)', Number.Integer),
75 # char_lit
76 (r"""'(\\['"\\abfnrtv]|\\x[0-9a-fA-F]{2}|\\[0-7]{1,3}"""
77 r"""|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8}|[^\\])'""",
78 String.Char),
79 # StringLiteral
80 # -- raw_string_lit
81 (r'`[^`]*`', String),
82 # -- interpreted_string_lit
83 (r'"(\\\\|\\[^\\]|[^"\\])*"', String),
84 # Tokens
85 (r'(<<=|>>=|<<|>>|<=|>=|&\^=|&\^|\+=|-=|\*=|/=|%=|&=|\|=|\^=|&&|\|\|'
86 r'|<-|\+\+|--|==|!=|:=|[+\-*/%&!=<>|^])', Operator),
87 (r'(\.\.\.|[()\[\]{}.,;:~])', Punctuation),
88 # identifier
89 (r'[^\W\d]\w*', Name.Other),
90 ]
91 }