|
1 # -*- coding: utf-8 -*- |
|
2 """ |
|
3 pygments.lexers.go |
|
4 ~~~~~~~~~~~~~~~~~~ |
|
5 |
|
6 Lexers for the Google Go language. |
|
7 |
|
8 :copyright: Copyright 2006-2014 by the Pygments team, see AUTHORS. |
|
9 :license: BSD, see LICENSE for details. |
|
10 """ |
|
11 |
|
12 import re |
|
13 |
|
14 from pygments.lexer import RegexLexer, bygroups, words |
|
15 from pygments.token import Text, Comment, Operator, Keyword, Name, String, \ |
|
16 Number, Punctuation |
|
17 |
|
18 __all__ = ['GoLexer'] |
|
19 |
|
20 |
|
21 class GoLexer(RegexLexer): |
|
22 """ |
|
23 For `Go <http://golang.org>`_ source. |
|
24 |
|
25 .. versionadded:: 1.2 |
|
26 """ |
|
27 name = 'Go' |
|
28 filenames = ['*.go'] |
|
29 aliases = ['go'] |
|
30 mimetypes = ['text/x-gosrc'] |
|
31 |
|
32 flags = re.MULTILINE | re.UNICODE |
|
33 |
|
34 tokens = { |
|
35 'root': [ |
|
36 (r'\n', Text), |
|
37 (r'\s+', Text), |
|
38 (r'\\\n', Text), # line continuations |
|
39 (r'//(.*?)\n', Comment.Single), |
|
40 (r'/(\\\n)?[*](.|\n)*?[*](\\\n)?/', Comment.Multiline), |
|
41 (r'(import|package)\b', Keyword.Namespace), |
|
42 (r'(var|func|struct|map|chan|type|interface|const)\b', |
|
43 Keyword.Declaration), |
|
44 (words(( |
|
45 'break', 'default', 'select', 'case', 'defer', 'go', |
|
46 'else', 'goto', 'switch', 'fallthrough', 'if', 'range', |
|
47 'continue', 'for', 'return'), suffix=r'\b'), |
|
48 Keyword), |
|
49 (r'(true|false|iota|nil)\b', Keyword.Constant), |
|
50 # It seems the builtin types aren't actually keywords, but |
|
51 # can be used as functions. So we need two declarations. |
|
52 (words(( |
|
53 'uint', 'uint8', 'uint16', 'uint32', 'uint64', |
|
54 'int', 'int8', 'int16', 'int32', 'int64', |
|
55 'float', 'float32', 'float64', |
|
56 'complex64', 'complex128', 'byte', 'rune', |
|
57 'string', 'bool', 'error', 'uintptr', |
|
58 'print', 'println', 'panic', 'recover', 'close', 'complex', |
|
59 'real', 'imag', 'len', 'cap', 'append', 'copy', 'delete', |
|
60 'new', 'make'), suffix=r'\b(\()'), |
|
61 bygroups(Name.Builtin, Punctuation)), |
|
62 (words(( |
|
63 'uint', 'uint8', 'uint16', 'uint32', 'uint64', |
|
64 'int', 'int8', 'int16', 'int32', 'int64', |
|
65 'float', 'float32', 'float64', |
|
66 'complex64', 'complex128', 'byte', 'rune', |
|
67 'string', 'bool', 'error', 'uintptr'), suffix=r'\b'), |
|
68 Keyword.Type), |
|
69 # imaginary_lit |
|
70 (r'\d+i', Number), |
|
71 (r'\d+\.\d*([Ee][-+]\d+)?i', Number), |
|
72 (r'\.\d+([Ee][-+]\d+)?i', Number), |
|
73 (r'\d+[Ee][-+]\d+i', Number), |
|
74 # float_lit |
|
75 (r'\d+(\.\d+[eE][+\-]?\d+|' |
|
76 r'\.\d*|[eE][+\-]?\d+)', Number.Float), |
|
77 (r'\.\d+([eE][+\-]?\d+)?', Number.Float), |
|
78 # int_lit |
|
79 # -- octal_lit |
|
80 (r'0[0-7]+', Number.Oct), |
|
81 # -- hex_lit |
|
82 (r'0[xX][0-9a-fA-F]+', Number.Hex), |
|
83 # -- decimal_lit |
|
84 (r'(0|[1-9][0-9]*)', Number.Integer), |
|
85 # char_lit |
|
86 (r"""'(\\['"\\abfnrtv]|\\x[0-9a-fA-F]{2}|\\[0-7]{1,3}""" |
|
87 r"""|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8}|[^\\])'""", |
|
88 String.Char), |
|
89 # StringLiteral |
|
90 # -- raw_string_lit |
|
91 (r'`[^`]*`', String), |
|
92 # -- interpreted_string_lit |
|
93 (r'"(\\\\|\\"|[^"])*"', String), |
|
94 # Tokens |
|
95 (r'(<<=|>>=|<<|>>|<=|>=|&\^=|&\^|\+=|-=|\*=|/=|%=|&=|\|=|&&|\|\|' |
|
96 r'|<-|\+\+|--|==|!=|:=|\.\.\.|[+\-*/%&])', Operator), |
|
97 (r'[|^<>=!()\[\]{}.,;:]', Punctuation), |
|
98 # identifier |
|
99 (r'[^\W\d]\w*', Name.Other), |
|
100 ] |
|
101 } |