70 "ans", "eps", "realmax", "realmin", "pi", "i", "inf", "nan", "isnan", |
70 "ans", "eps", "realmax", "realmin", "pi", "i", "inf", "nan", "isnan", |
71 "isinf", "isfinite", "j", "why", "compan", "gallery", "hadamard", "hankel", |
71 "isinf", "isfinite", "j", "why", "compan", "gallery", "hadamard", "hankel", |
72 "hilb", "invhilb", "magic", "pascal", "rosser", "toeplitz", "vander", |
72 "hilb", "invhilb", "magic", "pascal", "rosser", "toeplitz", "vander", |
73 "wilkinson") |
73 "wilkinson") |
74 |
74 |
|
75 _operators = r'-|==|~=|<=|>=|<|>|&&|&|~|\|\|?|\.\*|\*|\+|\.\^|\.\\|\.\/|\/|\\' |
|
76 |
75 tokens = { |
77 tokens = { |
76 'root': [ |
78 'root': [ |
77 # line starting with '!' is sent as a system command. not sure what |
79 # line starting with '!' is sent as a system command. not sure what |
78 # label to use... |
80 # label to use... |
79 (r'^!.*', String.Other), |
81 (r'^!.*', String.Other), |
80 (r'%\{\s*\n', Comment.Multiline, 'blockcomment'), |
82 (r'%\{\s*\n', Comment.Multiline, 'blockcomment'), |
81 (r'%.*$', Comment), |
83 (r'%.*$', Comment), |
82 (r'^\s*function', Keyword, 'deffunc'), |
84 (r'^\s*function\b', Keyword, 'deffunc'), |
83 |
85 |
84 # from 'iskeyword' on version 7.11 (R2010): |
86 # from 'iskeyword' on version 7.11 (R2010): |
85 (words(( |
87 # Check that there is no preceding dot, as keywords are valid field |
86 'break', 'case', 'catch', 'classdef', 'continue', 'else', 'elseif', |
88 # names. |
87 'end', 'enumerated', 'events', 'for', 'function', 'global', 'if', |
89 (words(('break', 'case', 'catch', 'classdef', 'continue', 'else', |
88 'methods', 'otherwise', 'parfor', 'persistent', 'properties', |
90 'elseif', 'end', 'enumerated', 'events', 'for', 'function', |
89 'return', 'spmd', 'switch', 'try', 'while'), suffix=r'\b'), |
91 'global', 'if', 'methods', 'otherwise', 'parfor', |
|
92 'persistent', 'properties', 'return', 'spmd', 'switch', |
|
93 'try', 'while'), |
|
94 prefix=r'(?<!\.)', suffix=r'\b'), |
90 Keyword), |
95 Keyword), |
91 |
96 |
92 ("(" + "|".join(elfun + specfun + elmat) + r')\b', Name.Builtin), |
97 ("(" + "|".join(elfun + specfun + elmat) + r')\b', Name.Builtin), |
93 |
98 |
94 # line continuation with following comment: |
99 # line continuation with following comment: |
95 (r'\.\.\..*$', Comment), |
100 (r'\.\.\..*$', Comment), |
96 |
101 |
|
102 # command form: |
|
103 # "How MATLAB Recognizes Command Syntax" specifies that an operator |
|
104 # is recognized if it is either surrounded by spaces or by no |
|
105 # spaces on both sides; only the former case matters for us. (This |
|
106 # allows distinguishing `cd ./foo` from `cd ./ foo`.) |
|
107 (r'(?:^|(?<=;))(\s*)(\w+)(\s+)(?!=|\(|(%s)\s+)' % _operators, |
|
108 bygroups(Text, Name, Text), 'commandargs'), |
|
109 |
97 # operators: |
110 # operators: |
98 (r'-|==|~=|<|>|<=|>=|&&|&|~|\|\|?', Operator), |
111 (_operators, Operator), |
99 # operators requiring escape for re: |
112 |
100 (r'\.\*|\*|\+|\.\^|\.\\|\.\/|\/|\\', Operator), |
113 # numbers (must come before punctuation to handle `.5`; cannot use |
|
114 # `\b` due to e.g. `5. + .5`). |
|
115 (r'(?<!\w)((\d+\.\d*)|(\d*\.\d+))([eEf][+-]?\d+)?(?!\w)', Number.Float), |
|
116 (r'\b\d+[eEf][+-]?[0-9]+\b', Number.Float), |
|
117 (r'\b\d+\b', Number.Integer), |
101 |
118 |
102 # punctuation: |
119 # punctuation: |
103 (r'\[|\]|\(|\)|\{|\}|:|@|\.|,', Punctuation), |
120 (r'\[|\]|\(|\)|\{|\}|:|@|\.|,', Punctuation), |
104 (r'=|:|;', Punctuation), |
121 (r'=|:|;', Punctuation), |
105 |
122 |
106 # quote can be transpose, instead of string: |
123 # quote can be transpose, instead of string: |
107 # (not great, but handles common cases...) |
124 # (not great, but handles common cases...) |
108 (r'(?<=[\w)\].])\'+', Operator), |
125 (r'(?<=[\w)\].])\'+', Operator), |
109 |
126 |
110 (r'(\d+\.\d*|\d*\.\d+)([eEf][+-]?[0-9]+)?', Number.Float), |
127 (r'"(""|[^"])*"', String), |
111 (r'\d+[eEf][+-]?[0-9]+', Number.Float), |
|
112 (r'\d+', Number.Integer), |
|
113 |
128 |
114 (r'(?<![\w)\].])\'', String, 'string'), |
129 (r'(?<![\w)\].])\'', String, 'string'), |
115 (r'[a-zA-Z_]\w*', Name), |
130 (r'[a-zA-Z_]\w*', Name), |
116 (r'.', Text), |
131 (r'.', Text), |
117 ], |
|
118 'string': [ |
|
119 (r'[^\']*\'', String, '#pop') |
|
120 ], |
132 ], |
121 'blockcomment': [ |
133 'blockcomment': [ |
122 (r'^\s*%\}', Comment.Multiline, '#pop'), |
134 (r'^\s*%\}', Comment.Multiline, '#pop'), |
123 (r'^.*\n', Comment.Multiline), |
135 (r'^.*\n', Comment.Multiline), |
124 (r'.', Comment.Multiline), |
136 (r'.', Comment.Multiline), |
129 Whitespace, Name.Function, Punctuation, Text, |
141 Whitespace, Name.Function, Punctuation, Text, |
130 Punctuation, Whitespace), '#pop'), |
142 Punctuation, Whitespace), '#pop'), |
131 # function with no args |
143 # function with no args |
132 (r'(\s*)([a-zA-Z_]\w*)', bygroups(Text, Name.Function), '#pop'), |
144 (r'(\s*)([a-zA-Z_]\w*)', bygroups(Text, Name.Function), '#pop'), |
133 ], |
145 ], |
|
146 'string': [ |
|
147 (r"[^']*'", String, '#pop'), |
|
148 ], |
|
149 'commandargs': [ |
|
150 (r"[ \t]+", Text), |
|
151 ("'[^']*'", String), |
|
152 (r"[^';\s]+", String), |
|
153 (";?", Punctuation, '#pop'), |
|
154 ] |
134 } |
155 } |
135 |
156 |
136 def analyse_text(text): |
157 def analyse_text(text): |
137 if re.match(r'^\s*%', text, re.M): # comment |
158 # function declaration. |
|
159 first_non_comment = next((line for line in text.splitlines() |
|
160 if not re.match(r'^\s*%', text)), '').strip() |
|
161 if (first_non_comment.startswith('function') |
|
162 and '{' not in first_non_comment): |
|
163 return 1. |
|
164 # comment |
|
165 elif re.search(r'^\s*%', text, re.M): |
138 return 0.2 |
166 return 0.2 |
139 elif re.match(r'^!\w+', text, re.M): # system cmd |
167 # system cmd |
|
168 elif re.search(r'^!\w+', text, re.M): |
140 return 0.2 |
169 return 0.2 |
141 |
170 |
142 |
171 |
143 line_re = re.compile('.*?\n') |
172 line_re = re.compile('.*?\n') |
144 |
173 |
607 mimetypes = ['text/scilab'] |
636 mimetypes = ['text/scilab'] |
608 |
637 |
609 tokens = { |
638 tokens = { |
610 'root': [ |
639 'root': [ |
611 (r'//.*?$', Comment.Single), |
640 (r'//.*?$', Comment.Single), |
612 (r'^\s*function', Keyword, 'deffunc'), |
641 (r'^\s*function\b', Keyword, 'deffunc'), |
613 |
642 |
614 (words(( |
643 (words(( |
615 '__FILE__', '__LINE__', 'break', 'case', 'catch', 'classdef', 'continue', 'do', 'else', |
644 '__FILE__', '__LINE__', 'break', 'case', 'catch', 'classdef', 'continue', 'do', 'else', |
616 'elseif', 'end', 'end_try_catch', 'end_unwind_protect', 'endclassdef', |
645 'elseif', 'end', 'end_try_catch', 'end_unwind_protect', 'endclassdef', |
617 'endevents', 'endfor', 'endfunction', 'endif', 'endmethods', 'endproperties', |
646 'endevents', 'endfor', 'endfunction', 'endif', 'endmethods', 'endproperties', |