|
1 # -*- coding: utf-8 -*- |
|
2 """ |
|
3 pygments.lexers.julia |
|
4 ~~~~~~~~~~~~~~~~~~~~~ |
|
5 |
|
6 Lexers for the Julia 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 Lexer, RegexLexer, bygroups, combined, do_insertions |
|
15 from pygments.token import Text, Comment, Operator, Keyword, Name, String, \ |
|
16 Number, Punctuation, Generic |
|
17 from pygments.util import shebang_matches |
|
18 |
|
19 __all__ = ['JuliaLexer', 'JuliaConsoleLexer'] |
|
20 |
|
21 |
|
22 class JuliaLexer(RegexLexer): |
|
23 """ |
|
24 For `Julia <http://julialang.org/>`_ source code. |
|
25 |
|
26 .. versionadded:: 1.6 |
|
27 """ |
|
28 name = 'Julia' |
|
29 aliases = ['julia', 'jl'] |
|
30 filenames = ['*.jl'] |
|
31 mimetypes = ['text/x-julia', 'application/x-julia'] |
|
32 |
|
33 builtins = [ |
|
34 'exit', 'whos', 'edit', 'load', 'is', 'isa', 'isequal', 'typeof', 'tuple', |
|
35 'ntuple', 'uid', 'hash', 'finalizer', 'convert', 'promote', 'subtype', |
|
36 'typemin', 'typemax', 'realmin', 'realmax', 'sizeof', 'eps', 'promote_type', |
|
37 'method_exists', 'applicable', 'invoke', 'dlopen', 'dlsym', 'system', |
|
38 'error', 'throw', 'assert', 'new', 'Inf', 'Nan', 'pi', 'im', |
|
39 ] |
|
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 (r'\\\n', Text), |
|
49 (r'\\', Text), |
|
50 |
|
51 # keywords |
|
52 (r'(begin|while|for|in|return|break|continue|' |
|
53 r'macro|quote|let|if|elseif|else|try|catch|end|' |
|
54 r'bitstype|ccall|do|using|module|import|export|' |
|
55 r'importall|baremodule|immutable)\b', Keyword), |
|
56 (r'(local|global|const)\b', Keyword.Declaration), |
|
57 (r'(Bool|Int|Int8|Int16|Int32|Int64|Uint|Uint8|Uint16|Uint32|Uint64' |
|
58 r'|Float32|Float64|Complex64|Complex128|Any|Nothing|None)\b', |
|
59 Keyword.Type), |
|
60 |
|
61 # functions |
|
62 (r'(function)((?:\s|\\\s)+)', |
|
63 bygroups(Keyword, Name.Function), 'funcname'), |
|
64 |
|
65 # types |
|
66 (r'(type|typealias|abstract)((?:\s|\\\s)+)', |
|
67 bygroups(Keyword, Name.Class), 'typename'), |
|
68 |
|
69 # operators |
|
70 (r'==|!=|<=|>=|->|&&|\|\||::|<:|[-~+/*%=<>&^|.?!$]', Operator), |
|
71 (r'\.\*|\.\^|\.\\|\.\/|\\', Operator), |
|
72 |
|
73 # builtins |
|
74 ('(' + '|'.join(builtins) + r')\b', Name.Builtin), |
|
75 |
|
76 # backticks |
|
77 (r'`(?s).*?`', String.Backtick), |
|
78 |
|
79 # chars |
|
80 (r"'(\\.|\\[0-7]{1,3}|\\x[a-fA-F0-9]{1,3}|\\u[a-fA-F0-9]{1,4}|" |
|
81 r"\\U[a-fA-F0-9]{1,6}|[^\\\'\n])'", String.Char), |
|
82 |
|
83 # try to match trailing transpose |
|
84 (r'(?<=[.\w)\]])\'+', Operator), |
|
85 |
|
86 # strings |
|
87 (r'(?:[IL])"', String, 'string'), |
|
88 (r'[E]?"', String, combined('stringescape', 'string')), |
|
89 |
|
90 # names |
|
91 (r'@[\w.]+', Name.Decorator), |
|
92 (r'[a-zA-Z_]\w*', Name), |
|
93 |
|
94 # numbers |
|
95 (r'(\d+(_\d+)+\.\d*|\d*\.\d+(_\d+)+)([eEf][+-]?[0-9]+)?', Number.Float), |
|
96 (r'(\d+\.\d*|\d*\.\d+)([eEf][+-]?[0-9]+)?', Number.Float), |
|
97 (r'\d+(_\d+)+[eEf][+-]?[0-9]+', Number.Float), |
|
98 (r'\d+[eEf][+-]?[0-9]+', Number.Float), |
|
99 (r'0b[01]+(_[01]+)+', Number.Bin), |
|
100 (r'0b[01]+', Number.Bin), |
|
101 (r'0o[0-7]+(_[0-7]+)+', Number.Oct), |
|
102 (r'0o[0-7]+', Number.Oct), |
|
103 (r'0x[a-fA-F0-9]+(_[a-fA-F0-9]+)+', Number.Hex), |
|
104 (r'0x[a-fA-F0-9]+', Number.Hex), |
|
105 (r'\d+(_\d+)+', Number.Integer), |
|
106 (r'\d+', Number.Integer) |
|
107 ], |
|
108 |
|
109 'funcname': [ |
|
110 ('[a-zA-Z_]\w*', Name.Function, '#pop'), |
|
111 ('\([^\s\w{]{1,2}\)', Operator, '#pop'), |
|
112 ('[^\s\w{]{1,2}', Operator, '#pop'), |
|
113 ], |
|
114 |
|
115 'typename': [ |
|
116 ('[a-zA-Z_]\w*', Name.Class, '#pop') |
|
117 ], |
|
118 |
|
119 'stringescape': [ |
|
120 (r'\\([\\abfnrtv"\']|\n|N\{.*?\}|u[a-fA-F0-9]{4}|' |
|
121 r'U[a-fA-F0-9]{8}|x[a-fA-F0-9]{2}|[0-7]{1,3})', String.Escape) |
|
122 ], |
|
123 "blockcomment": [ |
|
124 (r'[^=#]', Comment.Multiline), |
|
125 (r'#=', Comment.Multiline, '#push'), |
|
126 (r'=#', Comment.Multiline, '#pop'), |
|
127 (r'[=#]', Comment.Multiline), |
|
128 ], |
|
129 'string': [ |
|
130 (r'"', String, '#pop'), |
|
131 (r'\\\\|\\"|\\\n', String.Escape), # included here for raw strings |
|
132 (r'\$(\(\w+\))?[-#0 +]*([0-9]+|[*])?(\.([0-9]+|[*]))?', |
|
133 String.Interpol), |
|
134 (r'[^\\"$]+', String), |
|
135 # quotes, dollar signs, and backslashes must be parsed one at a time |
|
136 (r'["\\]', String), |
|
137 # unhandled string formatting sign |
|
138 (r'\$', String) |
|
139 ], |
|
140 } |
|
141 |
|
142 def analyse_text(text): |
|
143 return shebang_matches(text, r'julia') |
|
144 |
|
145 |
|
146 line_re = re.compile('.*?\n') |
|
147 |
|
148 |
|
149 class JuliaConsoleLexer(Lexer): |
|
150 """ |
|
151 For Julia console sessions. Modeled after MatlabSessionLexer. |
|
152 |
|
153 .. versionadded:: 1.6 |
|
154 """ |
|
155 name = 'Julia console' |
|
156 aliases = ['jlcon'] |
|
157 |
|
158 def get_tokens_unprocessed(self, text): |
|
159 jllexer = JuliaLexer(**self.options) |
|
160 |
|
161 curcode = '' |
|
162 insertions = [] |
|
163 |
|
164 for match in line_re.finditer(text): |
|
165 line = match.group() |
|
166 |
|
167 if line.startswith('julia>'): |
|
168 insertions.append((len(curcode), |
|
169 [(0, Generic.Prompt, line[:6])])) |
|
170 curcode += line[6:] |
|
171 |
|
172 elif line.startswith(' '): |
|
173 |
|
174 idx = len(curcode) |
|
175 |
|
176 # without is showing error on same line as before...? |
|
177 line = "\n" + line |
|
178 token = (0, Generic.Traceback, line) |
|
179 insertions.append((idx, [token])) |
|
180 |
|
181 else: |
|
182 if curcode: |
|
183 for item in do_insertions( |
|
184 insertions, jllexer.get_tokens_unprocessed(curcode)): |
|
185 yield item |
|
186 curcode = '' |
|
187 insertions = [] |
|
188 |
|
189 yield match.start(), Generic.Output, line |
|
190 |
|
191 if curcode: # or item: |
|
192 for item in do_insertions( |
|
193 insertions, jllexer.get_tokens_unprocessed(curcode)): |
|
194 yield item |