|
1 # -*- coding: utf-8 -*- |
|
2 """ |
|
3 pygments.lexers.algebra |
|
4 ~~~~~~~~~~~~~~~~~~~~~~~ |
|
5 |
|
6 Lexers for computer algebra systems. |
|
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__ = ['GAPLexer', 'MathematicaLexer', 'MuPADLexer'] |
|
19 |
|
20 |
|
21 class GAPLexer(RegexLexer): |
|
22 """ |
|
23 For `GAP <http://www.gap-system.org>`_ source code. |
|
24 |
|
25 .. versionadded:: 2.0 |
|
26 """ |
|
27 name = 'GAP' |
|
28 aliases = ['gap'] |
|
29 filenames = ['*.g', '*.gd', '*.gi', '*.gap'] |
|
30 |
|
31 tokens = { |
|
32 'root': [ |
|
33 (r'#.*$', Comment.Single), |
|
34 (r'"(?:[^"\\]|\\.)*"', String), |
|
35 (r'\(|\)|\[|\]|\{|\}', Punctuation), |
|
36 (r'''(?x)\b(?: |
|
37 if|then|elif|else|fi| |
|
38 for|while|do|od| |
|
39 repeat|until| |
|
40 break|continue| |
|
41 function|local|return|end| |
|
42 rec| |
|
43 quit|QUIT| |
|
44 IsBound|Unbind| |
|
45 TryNextMethod| |
|
46 Info|Assert |
|
47 )\b''', Keyword), |
|
48 (r'''(?x)\b(?: |
|
49 true|false|fail|infinity |
|
50 )\b''', |
|
51 Name.Constant), |
|
52 (r'''(?x)\b(?: |
|
53 (Declare|Install)([A-Z][A-Za-z]+)| |
|
54 BindGlobal|BIND_GLOBAL |
|
55 )\b''', |
|
56 Name.Builtin), |
|
57 (r'\.|,|:=|;|=|\+|-|\*|/|\^|>|<', Operator), |
|
58 (r'''(?x)\b(?: |
|
59 and|or|not|mod|in |
|
60 )\b''', |
|
61 Operator.Word), |
|
62 (r'''(?x) |
|
63 (?:\w+|`[^`]*`) |
|
64 (?:::\w+|`[^`]*`)*''', Name.Variable), |
|
65 (r'[0-9]+(?:\.[0-9]*)?(?:e[0-9]+)?', Number), |
|
66 (r'\.[0-9]+(?:e[0-9]+)?', Number), |
|
67 (r'.', Text) |
|
68 ] |
|
69 } |
|
70 |
|
71 |
|
72 class MathematicaLexer(RegexLexer): |
|
73 """ |
|
74 Lexer for `Mathematica <http://www.wolfram.com/mathematica/>`_ source code. |
|
75 |
|
76 .. versionadded:: 2.0 |
|
77 """ |
|
78 name = 'Mathematica' |
|
79 aliases = ['mathematica', 'mma', 'nb'] |
|
80 filenames = ['*.nb', '*.cdf', '*.nbp', '*.ma'] |
|
81 mimetypes = ['application/mathematica', |
|
82 'application/vnd.wolfram.mathematica', |
|
83 'application/vnd.wolfram.mathematica.package', |
|
84 'application/vnd.wolfram.cdf'] |
|
85 |
|
86 # http://reference.wolfram.com/mathematica/guide/Syntax.html |
|
87 operators = ( |
|
88 ";;", "=", "=.", "!=" "==", ":=", "->", ":>", "/.", "+", "-", "*", "/", |
|
89 "^", "&&", "||", "!", "<>", "|", "/;", "?", "@", "//", "/@", "@@", |
|
90 "@@@", "~~", "===", "&", "<", ">", "<=", ">=", |
|
91 ) |
|
92 |
|
93 punctuation = (",", ";", "(", ")", "[", "]", "{", "}") |
|
94 |
|
95 def _multi_escape(entries): |
|
96 return '(%s)' % ('|'.join(re.escape(entry) for entry in entries)) |
|
97 |
|
98 tokens = { |
|
99 'root': [ |
|
100 (r'(?s)\(\*.*?\*\)', Comment), |
|
101 |
|
102 (r'([a-zA-Z]+[A-Za-z0-9]*`)', Name.Namespace), |
|
103 (r'([A-Za-z0-9]*_+[A-Za-z0-9]*)', Name.Variable), |
|
104 (r'#\d*', Name.Variable), |
|
105 (r'([a-zA-Z]+[a-zA-Z0-9]*)', Name), |
|
106 |
|
107 (r'-?[0-9]+\.[0-9]*', Number.Float), |
|
108 (r'-?[0-9]*\.[0-9]+', Number.Float), |
|
109 (r'-?[0-9]+', Number.Integer), |
|
110 |
|
111 (words(operators), Operator), |
|
112 (words(punctuation), Punctuation), |
|
113 (r'".*?"', String), |
|
114 (r'\s+', Text.Whitespace), |
|
115 ], |
|
116 } |
|
117 |
|
118 |
|
119 class MuPADLexer(RegexLexer): |
|
120 """ |
|
121 A `MuPAD <http://www.mupad.com>`_ lexer. |
|
122 Contributed by Christopher Creutzig <christopher@creutzig.de>. |
|
123 |
|
124 .. versionadded:: 0.8 |
|
125 """ |
|
126 name = 'MuPAD' |
|
127 aliases = ['mupad'] |
|
128 filenames = ['*.mu'] |
|
129 |
|
130 tokens = { |
|
131 'root': [ |
|
132 (r'//.*?$', Comment.Single), |
|
133 (r'/\*', Comment.Multiline, 'comment'), |
|
134 (r'"(?:[^"\\]|\\.)*"', String), |
|
135 (r'\(|\)|\[|\]|\{|\}', Punctuation), |
|
136 (r'''(?x)\b(?: |
|
137 next|break|end| |
|
138 axiom|end_axiom|category|end_category|domain|end_domain|inherits| |
|
139 if|%if|then|elif|else|end_if| |
|
140 case|of|do|otherwise|end_case| |
|
141 while|end_while| |
|
142 repeat|until|end_repeat| |
|
143 for|from|to|downto|step|end_for| |
|
144 proc|local|option|save|begin|end_proc| |
|
145 delete|frame |
|
146 )\b''', Keyword), |
|
147 (r'''(?x)\b(?: |
|
148 DOM_ARRAY|DOM_BOOL|DOM_COMPLEX|DOM_DOMAIN|DOM_EXEC|DOM_EXPR| |
|
149 DOM_FAIL|DOM_FLOAT|DOM_FRAME|DOM_FUNC_ENV|DOM_HFARRAY|DOM_IDENT| |
|
150 DOM_INT|DOM_INTERVAL|DOM_LIST|DOM_NIL|DOM_NULL|DOM_POLY|DOM_PROC| |
|
151 DOM_PROC_ENV|DOM_RAT|DOM_SET|DOM_STRING|DOM_TABLE|DOM_VAR |
|
152 )\b''', Name.Class), |
|
153 (r'''(?x)\b(?: |
|
154 PI|EULER|E|CATALAN| |
|
155 NIL|FAIL|undefined|infinity| |
|
156 TRUE|FALSE|UNKNOWN |
|
157 )\b''', |
|
158 Name.Constant), |
|
159 (r'\b(?:dom|procname)\b', Name.Builtin.Pseudo), |
|
160 (r'\.|,|:|;|=|\+|-|\*|/|\^|@|>|<|\$|\||!|\'|%|~=', Operator), |
|
161 (r'''(?x)\b(?: |
|
162 and|or|not|xor| |
|
163 assuming| |
|
164 div|mod| |
|
165 union|minus|intersect|in|subset |
|
166 )\b''', |
|
167 Operator.Word), |
|
168 (r'\b(?:I|RDN_INF|RD_NINF|RD_NAN)\b', Number), |
|
169 # (r'\b(?:adt|linalg|newDomain|hold)\b', Name.Builtin), |
|
170 (r'''(?x) |
|
171 ((?:[a-zA-Z_#][\w#]*|`[^`]*`) |
|
172 (?:::[a-zA-Z_#][\w#]*|`[^`]*`)*)(\s*)([(])''', |
|
173 bygroups(Name.Function, Text, Punctuation)), |
|
174 (r'''(?x) |
|
175 (?:[a-zA-Z_#][\w#]*|`[^`]*`) |
|
176 (?:::[a-zA-Z_#][\w#]*|`[^`]*`)*''', Name.Variable), |
|
177 (r'[0-9]+(?:\.[0-9]*)?(?:e[0-9]+)?', Number), |
|
178 (r'\.[0-9]+(?:e[0-9]+)?', Number), |
|
179 (r'.', Text) |
|
180 ], |
|
181 'comment': [ |
|
182 (r'[^*/]', Comment.Multiline), |
|
183 (r'/\*', Comment.Multiline, '#push'), |
|
184 (r'\*/', Comment.Multiline, '#pop'), |
|
185 (r'[*/]', Comment.Multiline) |
|
186 ] |
|
187 } |