|
1 # -*- coding: utf-8 -*- |
|
2 """ |
|
3 pygments.lexers.julia |
|
4 ~~~~~~~~~~~~~~~~~~~~~ |
|
5 |
|
6 Lexers for the Julia language. |
|
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 Lexer, RegexLexer, bygroups, do_insertions, \ |
|
15 words, include |
|
16 from pygments.token import Text, Comment, Operator, Keyword, Name, String, \ |
|
17 Number, Punctuation, Generic |
|
18 from pygments.util import shebang_matches, unirange |
|
19 |
|
20 __all__ = ['JuliaLexer', 'JuliaConsoleLexer'] |
|
21 |
|
22 allowed_variable = ( |
|
23 u'(?:[a-zA-Z_\u00A1-\uffff]|%s)(?:[a-zA-Z_0-9\u00A1-\uffff]|%s)*!*' % |
|
24 ((unirange(0x10000, 0x10ffff),) * 2)) |
|
25 |
|
26 |
|
27 class JuliaLexer(RegexLexer): |
|
28 """ |
|
29 For `Julia <http://julialang.org/>`_ source code. |
|
30 |
|
31 .. versionadded:: 1.6 |
|
32 """ |
|
33 |
|
34 name = 'Julia' |
|
35 aliases = ['julia', 'jl'] |
|
36 filenames = ['*.jl'] |
|
37 mimetypes = ['text/x-julia', 'application/x-julia'] |
|
38 |
|
39 flags = re.MULTILINE | re.UNICODE |
|
40 |
|
41 tokens = { |
|
42 'root': [ |
|
43 (r'\n', Text), |
|
44 (r'[^\S\n]+', Text), |
|
45 (r'#=', Comment.Multiline, "blockcomment"), |
|
46 (r'#.*$', Comment), |
|
47 (r'[\[\]{}(),;]', Punctuation), |
|
48 |
|
49 # keywords |
|
50 (r'in\b', Keyword.Pseudo), |
|
51 (r'(true|false)\b', Keyword.Constant), |
|
52 (r'(local|global|const)\b', Keyword.Declaration), |
|
53 (words([ |
|
54 'function', 'type', 'typealias', 'abstract', 'immutable', |
|
55 'baremodule', 'begin', 'bitstype', 'break', 'catch', 'ccall', |
|
56 'continue', 'do', 'else', 'elseif', 'end', 'export', 'finally', |
|
57 'for', 'if', 'import', 'importall', 'let', 'macro', 'module', |
|
58 'quote', 'return', 'try', 'using', 'while'], |
|
59 suffix=r'\b'), Keyword), |
|
60 |
|
61 # NOTE |
|
62 # Patterns below work only for definition sites and thus hardly reliable. |
|
63 # |
|
64 # functions |
|
65 # (r'(function)(\s+)(' + allowed_variable + ')', |
|
66 # bygroups(Keyword, Text, Name.Function)), |
|
67 # |
|
68 # types |
|
69 # (r'(type|typealias|abstract|immutable)(\s+)(' + allowed_variable + ')', |
|
70 # bygroups(Keyword, Text, Name.Class)), |
|
71 |
|
72 # type names |
|
73 (words([ |
|
74 'ANY', 'ASCIIString', 'AbstractArray', 'AbstractChannel', |
|
75 'AbstractFloat', 'AbstractMatrix', 'AbstractRNG', |
|
76 'AbstractSparseArray', 'AbstractSparseMatrix', |
|
77 'AbstractSparseVector', 'AbstractString', 'AbstractVecOrMat', |
|
78 'AbstractVector', 'Any', 'ArgumentError', 'Array', |
|
79 'AssertionError', 'Associative', 'Base64DecodePipe', |
|
80 'Base64EncodePipe', 'Bidiagonal', 'BigFloat', 'BigInt', |
|
81 'BitArray', 'BitMatrix', 'BitVector', 'Bool', 'BoundsError', |
|
82 'Box', 'BufferStream', 'CapturedException', 'CartesianIndex', |
|
83 'CartesianRange', 'Cchar', 'Cdouble', 'Cfloat', 'Channel', |
|
84 'Char', 'Cint', 'Cintmax_t', 'Clong', 'Clonglong', |
|
85 'ClusterManager', 'Cmd', 'Coff_t', 'Colon', 'Complex', |
|
86 'Complex128', 'Complex32', 'Complex64', 'CompositeException', |
|
87 'Condition', 'Cptrdiff_t', 'Cshort', 'Csize_t', 'Cssize_t', |
|
88 'Cstring', 'Cuchar', 'Cuint', 'Cuintmax_t', 'Culong', |
|
89 'Culonglong', 'Cushort', 'Cwchar_t', 'Cwstring', 'DataType', |
|
90 'Date', 'DateTime', 'DenseArray', 'DenseMatrix', |
|
91 'DenseVecOrMat', 'DenseVector', 'Diagonal', 'Dict', |
|
92 'DimensionMismatch', 'Dims', 'DirectIndexString', 'Display', |
|
93 'DivideError', 'DomainError', 'EOFError', 'EachLine', 'Enum', |
|
94 'Enumerate', 'ErrorException', 'Exception', 'Expr', |
|
95 'Factorization', 'FileMonitor', 'FileOffset', 'Filter', |
|
96 'Float16', 'Float32', 'Float64', 'FloatRange', 'Function', |
|
97 'GenSym', 'GlobalRef', 'GotoNode', 'HTML', 'Hermitian', 'IO', |
|
98 'IOBuffer', 'IOStream', 'IPv4', 'IPv6', 'InexactError', |
|
99 'InitError', 'Int', 'Int128', 'Int16', 'Int32', 'Int64', 'Int8', |
|
100 'IntSet', 'Integer', 'InterruptException', 'IntrinsicFunction', |
|
101 'InvalidStateException', 'Irrational', 'KeyError', 'LabelNode', |
|
102 'LambdaStaticData', 'LinSpace', 'LineNumberNode', 'LoadError', |
|
103 'LocalProcess', 'LowerTriangular', 'MIME', 'Matrix', |
|
104 'MersenneTwister', 'Method', 'MethodError', 'MethodTable', |
|
105 'Module', 'NTuple', 'NewvarNode', 'NullException', 'Nullable', |
|
106 'Number', 'ObjectIdDict', 'OrdinalRange', 'OutOfMemoryError', |
|
107 'OverflowError', 'Pair', 'ParseError', 'PartialQuickSort', |
|
108 'Pipe', 'PollingFileWatcher', 'ProcessExitedException', |
|
109 'ProcessGroup', 'Ptr', 'QuoteNode', 'RandomDevice', 'Range', |
|
110 'Rational', 'RawFD', 'ReadOnlyMemoryError', 'Real', |
|
111 'ReentrantLock', 'Ref', 'Regex', 'RegexMatch', |
|
112 'RemoteException', 'RemoteRef', 'RepString', 'RevString', |
|
113 'RopeString', 'RoundingMode', 'SegmentationFault', |
|
114 'SerializationState', 'Set', 'SharedArray', 'SharedMatrix', |
|
115 'SharedVector', 'Signed', 'SimpleVector', 'SparseMatrixCSC', |
|
116 'StackOverflowError', 'StatStruct', 'StepRange', 'StridedArray', |
|
117 'StridedMatrix', 'StridedVecOrMat', 'StridedVector', 'SubArray', |
|
118 'SubString', 'SymTridiagonal', 'Symbol', 'SymbolNode', |
|
119 'Symmetric', 'SystemError', 'TCPSocket', 'Task', 'Text', |
|
120 'TextDisplay', 'Timer', 'TopNode', 'Tridiagonal', 'Tuple', |
|
121 'Type', 'TypeConstructor', 'TypeError', 'TypeName', 'TypeVar', |
|
122 'UDPSocket', 'UInt', 'UInt128', 'UInt16', 'UInt32', 'UInt64', |
|
123 'UInt8', 'UTF16String', 'UTF32String', 'UTF8String', |
|
124 'UndefRefError', 'UndefVarError', 'UnicodeError', 'UniformScaling', |
|
125 'Union', 'UnitRange', 'Unsigned', 'UpperTriangular', 'Val', |
|
126 'Vararg', 'VecOrMat', 'Vector', 'VersionNumber', 'Void', 'WString', |
|
127 'WeakKeyDict', 'WeakRef', 'WorkerConfig', 'Zip'], suffix=r'\b'), |
|
128 Keyword.Type), |
|
129 |
|
130 # builtins |
|
131 (words([ |
|
132 u'ARGS', u'CPU_CORES', u'C_NULL', u'DevNull', u'ENDIAN_BOM', |
|
133 u'ENV', u'I', u'Inf', u'Inf16', u'Inf32', u'Inf64', |
|
134 u'InsertionSort', u'JULIA_HOME', u'LOAD_PATH', u'MergeSort', |
|
135 u'NaN', u'NaN16', u'NaN32', u'NaN64', u'OS_NAME', |
|
136 u'QuickSort', u'RoundDown', u'RoundFromZero', u'RoundNearest', |
|
137 u'RoundNearestTiesAway', u'RoundNearestTiesUp', |
|
138 u'RoundToZero', u'RoundUp', u'STDERR', u'STDIN', u'STDOUT', |
|
139 u'VERSION', u'WORD_SIZE', u'catalan', u'e', u'eu', |
|
140 u'eulergamma', u'golden', u'im', u'nothing', u'pi', u'γ', |
|
141 u'π', u'φ'], |
|
142 suffix=r'\b'), Name.Builtin), |
|
143 |
|
144 # operators |
|
145 # see: https://github.com/JuliaLang/julia/blob/master/src/julia-parser.scm |
|
146 (words([ |
|
147 # prec-assignment |
|
148 u'=', u':=', u'+=', u'-=', u'*=', u'/=', u'//=', u'.//=', u'.*=', u'./=', |
|
149 u'\\=', u'.\\=', u'^=', u'.^=', u'÷=', u'.÷=', u'%=', u'.%=', u'|=', u'&=', |
|
150 u'$=', u'=>', u'<<=', u'>>=', u'>>>=', u'~', u'.+=', u'.-=', |
|
151 # prec-conditional |
|
152 u'?', |
|
153 # prec-arrow |
|
154 u'--', u'-->', |
|
155 # prec-lazy-or |
|
156 u'||', |
|
157 # prec-lazy-and |
|
158 u'&&', |
|
159 # prec-comparison |
|
160 u'>', u'<', u'>=', u'≥', u'<=', u'≤', u'==', u'===', u'≡', u'!=', u'≠', |
|
161 u'!==', u'≢', u'.>', u'.<', u'.>=', u'.≥', u'.<=', u'.≤', u'.==', u'.!=', |
|
162 u'.≠', u'.=', u'.!', u'<:', u'>:', u'∈', u'∉', u'∋', u'∌', u'⊆', |
|
163 u'⊈', u'⊂', |
|
164 u'⊄', u'⊊', |
|
165 # prec-pipe |
|
166 u'|>', u'<|', |
|
167 # prec-colon |
|
168 u':', |
|
169 # prec-plus |
|
170 u'+', u'-', u'.+', u'.-', u'|', u'∪', u'$', |
|
171 # prec-bitshift |
|
172 u'<<', u'>>', u'>>>', u'.<<', u'.>>', u'.>>>', |
|
173 # prec-times |
|
174 u'*', u'/', u'./', u'÷', u'.÷', u'%', u'⋅', u'.%', u'.*', u'\\', u'.\\', u'&', u'∩', |
|
175 # prec-rational |
|
176 u'//', u'.//', |
|
177 # prec-power |
|
178 u'^', u'.^', |
|
179 # prec-decl |
|
180 u'::', |
|
181 # prec-dot |
|
182 u'.', |
|
183 # unary op |
|
184 u'+', u'-', u'!', u'√', u'∛', u'∜' |
|
185 ]), Operator), |
|
186 |
|
187 # chars |
|
188 (r"'(\\.|\\[0-7]{1,3}|\\x[a-fA-F0-9]{1,3}|\\u[a-fA-F0-9]{1,4}|" |
|
189 r"\\U[a-fA-F0-9]{1,6}|[^\\\'\n])'", String.Char), |
|
190 |
|
191 # try to match trailing transpose |
|
192 (r'(?<=[.\w)\]])\'+', Operator), |
|
193 |
|
194 # strings |
|
195 (r'"""', String, 'tqstring'), |
|
196 (r'"', String, 'string'), |
|
197 |
|
198 # regular expressions |
|
199 (r'r"""', String.Regex, 'tqregex'), |
|
200 (r'r"', String.Regex, 'regex'), |
|
201 |
|
202 # backticks |
|
203 (r'`', String.Backtick, 'command'), |
|
204 |
|
205 # names |
|
206 (allowed_variable, Name), |
|
207 (r'@' + allowed_variable, Name.Decorator), |
|
208 |
|
209 # numbers |
|
210 (r'(\d+(_\d+)+\.\d*|\d*\.\d+(_\d+)+)([eEf][+-]?[0-9]+)?', Number.Float), |
|
211 (r'(\d+\.\d*|\d*\.\d+)([eEf][+-]?[0-9]+)?', Number.Float), |
|
212 (r'\d+(_\d+)+[eEf][+-]?[0-9]+', Number.Float), |
|
213 (r'\d+[eEf][+-]?[0-9]+', Number.Float), |
|
214 (r'0b[01]+(_[01]+)+', Number.Bin), |
|
215 (r'0b[01]+', Number.Bin), |
|
216 (r'0o[0-7]+(_[0-7]+)+', Number.Oct), |
|
217 (r'0o[0-7]+', Number.Oct), |
|
218 (r'0x[a-fA-F0-9]+(_[a-fA-F0-9]+)+', Number.Hex), |
|
219 (r'0x[a-fA-F0-9]+', Number.Hex), |
|
220 (r'\d+(_\d+)+', Number.Integer), |
|
221 (r'\d+', Number.Integer) |
|
222 ], |
|
223 |
|
224 "blockcomment": [ |
|
225 (r'[^=#]', Comment.Multiline), |
|
226 (r'#=', Comment.Multiline, '#push'), |
|
227 (r'=#', Comment.Multiline, '#pop'), |
|
228 (r'[=#]', Comment.Multiline), |
|
229 ], |
|
230 |
|
231 'string': [ |
|
232 (r'"', String, '#pop'), |
|
233 # FIXME: This escape pattern is not perfect. |
|
234 (r'\\([\\"\'$nrbtfav]|(x|u|U)[a-fA-F0-9]+|\d+)', String.Escape), |
|
235 # Interpolation is defined as "$" followed by the shortest full |
|
236 # expression, which is something we can't parse. |
|
237 # Include the most common cases here: $word, and $(paren'd expr). |
|
238 (r'\$' + allowed_variable, String.Interpol), |
|
239 # (r'\$[a-zA-Z_]+', String.Interpol), |
|
240 (r'(\$)(\()', bygroups(String.Interpol, Punctuation), 'in-intp'), |
|
241 # @printf and @sprintf formats |
|
242 (r'%[-#0 +]*([0-9]+|[*])?(\.([0-9]+|[*]))?[hlL]?[E-GXc-giorsux%]', |
|
243 String.Interpol), |
|
244 (r'.|\s', String), |
|
245 ], |
|
246 |
|
247 'tqstring': [ |
|
248 (r'"""', String, '#pop'), |
|
249 (r'\\([\\"\'$nrbtfav]|(x|u|U)[a-fA-F0-9]+|\d+)', String.Escape), |
|
250 (r'\$' + allowed_variable, String.Interpol), |
|
251 (r'(\$)(\()', bygroups(String.Interpol, Punctuation), 'in-intp'), |
|
252 (r'.|\s', String), |
|
253 ], |
|
254 |
|
255 'regex': [ |
|
256 (r'"', String.Regex, '#pop'), |
|
257 (r'\\"', String.Regex), |
|
258 (r'.|\s', String.Regex), |
|
259 ], |
|
260 |
|
261 'tqregex': [ |
|
262 (r'"""', String.Regex, '#pop'), |
|
263 (r'.|\s', String.Regex), |
|
264 ], |
|
265 |
|
266 'command': [ |
|
267 (r'`', String.Backtick, '#pop'), |
|
268 (r'\$' + allowed_variable, String.Interpol), |
|
269 (r'(\$)(\()', bygroups(String.Interpol, Punctuation), 'in-intp'), |
|
270 (r'.|\s', String.Backtick) |
|
271 ], |
|
272 |
|
273 'in-intp': [ |
|
274 (r'\(', Punctuation, '#push'), |
|
275 (r'\)', Punctuation, '#pop'), |
|
276 include('root'), |
|
277 ] |
|
278 } |
|
279 |
|
280 def analyse_text(text): |
|
281 return shebang_matches(text, r'julia') |
|
282 |
|
283 |
|
284 class JuliaConsoleLexer(Lexer): |
|
285 """ |
|
286 For Julia console sessions. Modeled after MatlabSessionLexer. |
|
287 |
|
288 .. versionadded:: 1.6 |
|
289 """ |
|
290 name = 'Julia console' |
|
291 aliases = ['jlcon'] |
|
292 |
|
293 def get_tokens_unprocessed(self, text): |
|
294 jllexer = JuliaLexer(**self.options) |
|
295 start = 0 |
|
296 curcode = '' |
|
297 insertions = [] |
|
298 output = False |
|
299 error = False |
|
300 |
|
301 for line in text.splitlines(True): |
|
302 if line.startswith('julia>'): |
|
303 insertions.append((len(curcode), [(0, Generic.Prompt, line[:6])])) |
|
304 curcode += line[6:] |
|
305 output = False |
|
306 error = False |
|
307 elif line.startswith('help?>') or line.startswith('shell>'): |
|
308 yield start, Generic.Prompt, line[:6] |
|
309 yield start + 6, Text, line[6:] |
|
310 output = False |
|
311 error = False |
|
312 elif line.startswith(' ') and not output: |
|
313 insertions.append((len(curcode), [(0, Text, line[:6])])) |
|
314 curcode += line[6:] |
|
315 else: |
|
316 if curcode: |
|
317 for item in do_insertions( |
|
318 insertions, jllexer.get_tokens_unprocessed(curcode)): |
|
319 yield item |
|
320 curcode = '' |
|
321 insertions = [] |
|
322 if line.startswith('ERROR: ') or error: |
|
323 yield start, Generic.Error, line |
|
324 error = True |
|
325 else: |
|
326 yield start, Generic.Output, line |
|
327 output = True |
|
328 start += len(line) |
|
329 |
|
330 if curcode: |
|
331 for item in do_insertions( |
|
332 insertions, jllexer.get_tokens_unprocessed(curcode)): |
|
333 yield item |