|
1 # -*- coding: utf-8 -*- |
|
2 """ |
|
3 pygments.lexers.nimrod |
|
4 ~~~~~~~~~~~~~~~~~~~~~~ |
|
5 |
|
6 Lexer for the Nim language (formerly known as Nimrod). |
|
7 |
|
8 :copyright: Copyright 2006-2017 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, include, default |
|
15 from pygments.token import Text, Comment, Operator, Keyword, Name, String, \ |
|
16 Number, Punctuation, Error |
|
17 |
|
18 __all__ = ['NimrodLexer'] |
|
19 |
|
20 |
|
21 class NimrodLexer(RegexLexer): |
|
22 """ |
|
23 For `Nim <http://nim-lang.org/>`_ source code. |
|
24 |
|
25 .. versionadded:: 1.5 |
|
26 """ |
|
27 |
|
28 name = 'Nimrod' |
|
29 aliases = ['nim', 'nimrod'] |
|
30 filenames = ['*.nim', '*.nimrod'] |
|
31 mimetypes = ['text/x-nim'] |
|
32 |
|
33 flags = re.MULTILINE | re.IGNORECASE | re.UNICODE |
|
34 |
|
35 def underscorize(words): |
|
36 newWords = [] |
|
37 new = "" |
|
38 for word in words: |
|
39 for ch in word: |
|
40 new += (ch + "_?") |
|
41 newWords.append(new) |
|
42 new = "" |
|
43 return "|".join(newWords) |
|
44 |
|
45 keywords = [ |
|
46 'addr', 'and', 'as', 'asm', 'atomic', 'bind', 'block', 'break', 'case', |
|
47 'cast', 'concept', 'const', 'continue', 'converter', 'defer', 'discard', |
|
48 'distinct', 'div', 'do', 'elif', 'else', 'end', 'enum', 'except', |
|
49 'export', 'finally', 'for', 'func', 'if', 'in', 'yield', 'interface', |
|
50 'is', 'isnot', 'iterator', 'let', 'macro', 'method', 'mixin', 'mod', |
|
51 'not', 'notin', 'object', 'of', 'or', 'out', 'proc', 'ptr', 'raise', |
|
52 'ref', 'return', 'shared', 'shl', 'shr', 'static', 'template', 'try', |
|
53 'tuple', 'type', 'when', 'while', 'with', 'without', 'xor' |
|
54 ] |
|
55 |
|
56 keywordsPseudo = [ |
|
57 'nil', 'true', 'false' |
|
58 ] |
|
59 |
|
60 opWords = [ |
|
61 'and', 'or', 'not', 'xor', 'shl', 'shr', 'div', 'mod', 'in', |
|
62 'notin', 'is', 'isnot' |
|
63 ] |
|
64 |
|
65 types = [ |
|
66 'int', 'int8', 'int16', 'int32', 'int64', 'float', 'float32', 'float64', |
|
67 'bool', 'char', 'range', 'array', 'seq', 'set', 'string' |
|
68 ] |
|
69 |
|
70 tokens = { |
|
71 'root': [ |
|
72 (r'##.*$', String.Doc), |
|
73 (r'#.*$', Comment), |
|
74 (r'[*=><+\-/@$~&%!?|\\\[\]]', Operator), |
|
75 (r'\.\.|\.|,|\[\.|\.\]|\{\.|\.\}|\(\.|\.\)|\{|\}|\(|\)|:|\^|`|;', |
|
76 Punctuation), |
|
77 |
|
78 # Strings |
|
79 (r'(?:[\w]+)"', String, 'rdqs'), |
|
80 (r'"""', String, 'tdqs'), |
|
81 ('"', String, 'dqs'), |
|
82 |
|
83 # Char |
|
84 ("'", String.Char, 'chars'), |
|
85 |
|
86 # Keywords |
|
87 (r'(%s)\b' % underscorize(opWords), Operator.Word), |
|
88 (r'(p_?r_?o_?c_?\s)(?![(\[\]])', Keyword, 'funcname'), |
|
89 (r'(%s)\b' % underscorize(keywords), Keyword), |
|
90 (r'(%s)\b' % underscorize(['from', 'import', 'include']), |
|
91 Keyword.Namespace), |
|
92 (r'(v_?a_?r)\b', Keyword.Declaration), |
|
93 (r'(%s)\b' % underscorize(types), Keyword.Type), |
|
94 (r'(%s)\b' % underscorize(keywordsPseudo), Keyword.Pseudo), |
|
95 # Identifiers |
|
96 (r'\b((?![_\d])\w)(((?!_)\w)|(_(?!_)\w))*', Name), |
|
97 # Numbers |
|
98 (r'[0-9][0-9_]*(?=([e.]|\'f(32|64)))', |
|
99 Number.Float, ('float-suffix', 'float-number')), |
|
100 (r'0x[a-f0-9][a-f0-9_]*', Number.Hex, 'int-suffix'), |
|
101 (r'0b[01][01_]*', Number.Bin, 'int-suffix'), |
|
102 (r'0o[0-7][0-7_]*', Number.Oct, 'int-suffix'), |
|
103 (r'[0-9][0-9_]*', Number.Integer, 'int-suffix'), |
|
104 # Whitespace |
|
105 (r'\s+', Text), |
|
106 (r'.+$', Error), |
|
107 ], |
|
108 'chars': [ |
|
109 (r'\\([\\abcefnrtvl"\']|x[a-f0-9]{2}|[0-9]{1,3})', String.Escape), |
|
110 (r"'", String.Char, '#pop'), |
|
111 (r".", String.Char) |
|
112 ], |
|
113 'strings': [ |
|
114 (r'(?<!\$)\$(\d+|#|\w+)+', String.Interpol), |
|
115 (r'[^\\\'"$\n]+', String), |
|
116 # quotes, dollars and backslashes must be parsed one at a time |
|
117 (r'[\'"\\]', String), |
|
118 # unhandled string formatting sign |
|
119 (r'\$', String) |
|
120 # newlines are an error (use "nl" state) |
|
121 ], |
|
122 'dqs': [ |
|
123 (r'\\([\\abcefnrtvl"\']|\n|x[a-f0-9]{2}|[0-9]{1,3})', |
|
124 String.Escape), |
|
125 (r'"', String, '#pop'), |
|
126 include('strings') |
|
127 ], |
|
128 'rdqs': [ |
|
129 (r'"(?!")', String, '#pop'), |
|
130 (r'""', String.Escape), |
|
131 include('strings') |
|
132 ], |
|
133 'tdqs': [ |
|
134 (r'"""(?!")', String, '#pop'), |
|
135 include('strings'), |
|
136 include('nl') |
|
137 ], |
|
138 'funcname': [ |
|
139 (r'((?![\d_])\w)(((?!_)\w)|(_(?!_)\w))*', Name.Function, '#pop'), |
|
140 (r'`.+`', Name.Function, '#pop') |
|
141 ], |
|
142 'nl': [ |
|
143 (r'\n', String) |
|
144 ], |
|
145 'float-number': [ |
|
146 (r'\.(?!\.)[0-9_]*', Number.Float), |
|
147 (r'e[+-]?[0-9][0-9_]*', Number.Float), |
|
148 default('#pop') |
|
149 ], |
|
150 'float-suffix': [ |
|
151 (r'\'f(32|64)', Number.Float), |
|
152 default('#pop') |
|
153 ], |
|
154 'int-suffix': [ |
|
155 (r'\'i(32|64)', Number.Integer.Long), |
|
156 (r'\'i(8|16)', Number.Integer), |
|
157 default('#pop') |
|
158 ], |
|
159 } |