Back to index

moin  1.9.0~rc2
math.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002 """
00003     pygments.lexers.math
00004     ~~~~~~~~~~~~~~~~~~~~
00005 
00006     Lexers for math languages.
00007 
00008     :copyright: Copyright 2006-2009 by the Pygments team, see AUTHORS.
00009     :license: BSD, see LICENSE for details.
00010 """
00011 
00012 import re
00013 try:
00014     set
00015 except NameError:
00016     from sets import Set as set
00017 
00018 from pygments.lexer import Lexer, RegexLexer, bygroups, include, do_insertions
00019 from pygments.token import Comment, String, Punctuation, Keyword, Name, \
00020     Operator, Number, Text, Generic
00021 
00022 from pygments.lexers.agile import PythonLexer
00023 
00024 __all__ = ['MuPADLexer', 'MatlabLexer', 'MatlabSessionLexer', 'NumPyLexer',
00025            'SLexer']
00026 
00027 
00028 class MuPADLexer(RegexLexer):
00029     """
00030     A `MuPAD <http://www.mupad.com>`_ lexer.
00031     Contributed by Christopher Creutzig <christopher@creutzig.de>.
00032 
00033     *New in Pygments 0.8.*
00034     """
00035     name = 'MuPAD'
00036     aliases = ['mupad']
00037     filenames = ['*.mu']
00038 
00039     tokens = {
00040       'root' : [
00041         (r'//.*?$', Comment.Single),
00042         (r'/\*', Comment.Multiline, 'comment'),
00043         (r'"(?:[^"\\]|\\.)*"', String),
00044         (r'\(|\)|\[|\]|\{|\}', Punctuation),
00045         (r'''(?x)\b(?:
00046             next|break|end|
00047             axiom|end_axiom|category|end_category|domain|end_domain|inherits|
00048             if|%if|then|elif|else|end_if|
00049             case|of|do|otherwise|end_case|
00050             while|end_while|
00051             repeat|until|end_repeat|
00052             for|from|to|downto|step|end_for|
00053             proc|local|option|save|begin|end_proc|
00054             delete|frame
00055           )\b''', Keyword),
00056         (r'''(?x)\b(?:
00057             DOM_ARRAY|DOM_BOOL|DOM_COMPLEX|DOM_DOMAIN|DOM_EXEC|DOM_EXPR|
00058             DOM_FAIL|DOM_FLOAT|DOM_FRAME|DOM_FUNC_ENV|DOM_HFARRAY|DOM_IDENT|
00059             DOM_INT|DOM_INTERVAL|DOM_LIST|DOM_NIL|DOM_NULL|DOM_POLY|DOM_PROC|
00060             DOM_PROC_ENV|DOM_RAT|DOM_SET|DOM_STRING|DOM_TABLE|DOM_VAR
00061           )\b''', Name.Class),
00062         (r'''(?x)\b(?:
00063             PI|EULER|E|CATALAN|
00064             NIL|FAIL|undefined|infinity|
00065             TRUE|FALSE|UNKNOWN
00066           )\b''',
00067           Name.Constant),
00068         (r'\b(?:dom|procname)\b', Name.Builtin.Pseudo),
00069         (r'\.|,|:|;|=|\+|-|\*|/|\^|@|>|<|\$|\||!|\'|%|~=', Operator),
00070         (r'''(?x)\b(?:
00071             and|or|not|xor|
00072             assuming|
00073             div|mod|
00074             union|minus|intersect|in|subset
00075           )\b''',
00076           Operator.Word),
00077         (r'\b(?:I|RDN_INF|RD_NINF|RD_NAN)\b', Number),
00078         #(r'\b(?:adt|linalg|newDomain|hold)\b', Name.Builtin),
00079         (r'''(?x)
00080           ((?:[a-zA-Z_#][a-zA-Z_#0-9]*|`[^`]*`)
00081           (?:::[a-zA-Z_#][a-zA-Z_#0-9]*|`[^`]*`)*)\s*([(])''',
00082           bygroups(Name.Function, Punctuation)),
00083         (r'''(?x)
00084           (?:[a-zA-Z_#][a-zA-Z_#0-9]*|`[^`]*`)
00085           (?:::[a-zA-Z_#][a-zA-Z_#0-9]*|`[^`]*`)*''', Name.Variable),
00086         (r'[0-9]+(?:\.[0-9]*)?(?:e[0-9]+)?', Number),
00087         (r'\.[0-9]+(?:e[0-9]+)?', Number),
00088         (r'.', Text)
00089       ],
00090       'comment' : [
00091         (r'[^*/]', Comment.Multiline),
00092         (r'/\*', Comment.Multiline, '#push'),
00093         (r'\*/', Comment.Multiline, '#pop'),
00094         (r'[*/]', Comment.Multiline)
00095       ]
00096     }
00097 
00098 
00099 class MatlabLexer(RegexLexer):
00100     """
00101     For Matlab (or GNU Octave) source code.
00102     Contributed by Ken Schutte <kschutte@csail.mit.edu>.
00103 
00104     *New in Pygments 0.10.*
00105     """
00106     name = 'Matlab'
00107     aliases = ['matlab', 'octave']
00108     filenames = ['*.m']
00109     mimetypes = ['text/matlab']
00110 
00111     #
00112     # These lists are generated automatically.
00113     # Run the following in bash shell:
00114     #
00115     # for f in elfun specfun elmat; do
00116     #   echo -n "$f = "
00117     #   matlab -nojvm -r "help $f;exit;" | perl -ne \
00118     #   'push(@c,$1) if /^    (\w+)\s+-/; END {print q{["}.join(q{","},@c).qq{"]\n};}'
00119     # done
00120     #
00121     # elfun: Elementary math functions
00122     # specfun: Special Math functions
00123     # elmat: Elementary matrices and matrix manipulation
00124     #
00125     # taken from Matlab version 7.4.0.336 (R2007a)
00126     #
00127     elfun = ["sin","sind","sinh","asin","asind","asinh","cos","cosd","cosh",
00128              "acos","acosd","acosh","tan","tand","tanh","atan","atand","atan2",
00129              "atanh","sec","secd","sech","asec","asecd","asech","csc","cscd",
00130              "csch","acsc","acscd","acsch","cot","cotd","coth","acot","acotd",
00131              "acoth","hypot","exp","expm1","log","log1p","log10","log2","pow2",
00132              "realpow","reallog","realsqrt","sqrt","nthroot","nextpow2","abs",
00133              "angle","complex","conj","imag","real","unwrap","isreal","cplxpair",
00134              "fix","floor","ceil","round","mod","rem","sign"]
00135     specfun = ["airy","besselj","bessely","besselh","besseli","besselk","beta",
00136                "betainc","betaln","ellipj","ellipke","erf","erfc","erfcx",
00137                "erfinv","expint","gamma","gammainc","gammaln","psi","legendre",
00138                "cross","dot","factor","isprime","primes","gcd","lcm","rat",
00139                "rats","perms","nchoosek","factorial","cart2sph","cart2pol",
00140                "pol2cart","sph2cart","hsv2rgb","rgb2hsv"]
00141     elmat = ["zeros","ones","eye","repmat","rand","randn","linspace","logspace",
00142              "freqspace","meshgrid","accumarray","size","length","ndims","numel",
00143              "disp","isempty","isequal","isequalwithequalnans","cat","reshape",
00144              "diag","blkdiag","tril","triu","fliplr","flipud","flipdim","rot90",
00145              "find","end","sub2ind","ind2sub","bsxfun","ndgrid","permute",
00146              "ipermute","shiftdim","circshift","squeeze","isscalar","isvector",
00147              "ans","eps","realmax","realmin","pi","i","inf","nan","isnan",
00148              "isinf","isfinite","j","why","compan","gallery","hadamard","hankel",
00149              "hilb","invhilb","magic","pascal","rosser","toeplitz","vander",
00150              "wilkinson"]
00151 
00152     tokens = {
00153         'root': [
00154             # line starting with '!' is sent as a system command.  not sure what
00155             # label to use...
00156             (r'^!.*', String.Other),
00157             (r'%.*$', Comment),
00158             (r'^\s*function', Keyword, 'deffunc'),
00159 
00160             # from 'iskeyword' on version 7.4.0.336 (R2007a):
00161             (r'(break|case|catch|classdef|continue|else|elseif|end|for|function|'
00162              r'global|if|otherwise|parfor|persistent|return|switch|try|while)\b',
00163              Keyword),
00164 
00165             ("(" + "|".join(elfun+specfun+elmat) + r')\b',  Name.Builtin),
00166 
00167             # operators:
00168             (r'-|==|~=|<|>|<=|>=|&&|&|~|\|\|?', Operator),
00169             # operators requiring escape for re:
00170             (r'\.\*|\*|\+|\.\^|\.\\|\.\/|\/|\\', Operator),
00171 
00172             # punctuation:
00173             (r'\[|\]|\(|\)|\{|\}|:|@|\.|,', Punctuation),
00174             (r'=|:|;', Punctuation),
00175 
00176             # quote can be transpose, instead of string:
00177             # (not great, but handles common cases...)
00178             (r'(?<=[\w\)\]])\'', Operator),
00179 
00180             (r'(?<![\w\)\]])\'', String, 'string'),
00181             ('[a-zA-Z_][a-zA-Z0-9_]*', Name),
00182             (r'.', Text),
00183         ],
00184         'string': [
00185             (r'[^\']*\'', String, '#pop')
00186         ],
00187         'deffunc': [
00188             (r'(\s*)(?:(.+)(\s*)(=)(\s*))?(.+)(\()(.*)(\))(\s*)',
00189              bygroups(Text.Whitespace, Text, Text.Whitespace, Punctuation,
00190                       Text.Whitespace, Name.Function, Punctuation, Text,
00191                       Punctuation, Text.Whitespace), '#pop'),
00192         ],
00193     }
00194 
00195     def analyse_text(text):
00196         if re.match('^\s*%', text, re.M): # comment
00197             return 0.9
00198         elif re.match('^!\w+', text, re.M): # system cmd
00199             return 0.9
00200         return 0.1
00201 
00202 line_re  = re.compile('.*?\n')
00203 
00204 class MatlabSessionLexer(Lexer):
00205     """
00206     For Matlab (or GNU Octave) sessions.  Modeled after PythonConsoleLexer.
00207     Contributed by Ken Schutte <kschutte@csail.mit.edu>.
00208 
00209     *New in Pygments 0.10.*
00210     """
00211     name = 'Matlab session'
00212     aliases = ['matlabsession']
00213 
00214     def get_tokens_unprocessed(self, text):
00215         mlexer = MatlabLexer(**self.options)
00216 
00217         curcode = ''
00218         insertions = []
00219 
00220         for match in line_re.finditer(text):
00221             line = match.group()
00222 
00223             if line.startswith('>>'):
00224                 insertions.append((len(curcode),
00225                                    [(0, Generic.Prompt, line[:3])]))
00226                 curcode += line[3:]
00227 
00228             elif line.startswith('???'):
00229 
00230                 idx = len(curcode)
00231 
00232                 # without is showing error on same line as before...?
00233                 line = "\n" + line
00234                 token = (0, Generic.Traceback, line)
00235                 insertions.append(  (idx, [token,]) )
00236 
00237             else:
00238                 if curcode:
00239                     for item in do_insertions(
00240                         insertions, mlexer.get_tokens_unprocessed(curcode)):
00241                         yield item
00242                     curcode = ''
00243                     insertions = []
00244 
00245                 yield match.start(), Generic.Output, line
00246 
00247         if curcode: # or item:
00248             for item in do_insertions(
00249                 insertions, mlexer.get_tokens_unprocessed(curcode)):
00250                 yield item
00251 
00252 
00253 class NumPyLexer(PythonLexer):
00254     '''
00255     A Python lexer recognizing Numerical Python builtins.
00256 
00257     *New in Pygments 0.10.*
00258     '''
00259 
00260     name = 'NumPy'
00261     aliases = ['numpy']
00262 
00263     # override the mimetypes to not inherit them from python
00264     mimetypes = []
00265     filenames = []
00266 
00267     EXTRA_KEYWORDS = set([
00268         'abs', 'absolute', 'accumulate', 'add', 'alen', 'all', 'allclose',
00269         'alltrue', 'alterdot', 'amax', 'amin', 'angle', 'any', 'append',
00270         'apply_along_axis', 'apply_over_axes', 'arange', 'arccos', 'arccosh',
00271         'arcsin', 'arcsinh', 'arctan', 'arctan2', 'arctanh', 'argmax', 'argmin',
00272         'argsort', 'argwhere', 'around', 'array', 'array2string', 'array_equal',
00273         'array_equiv', 'array_repr', 'array_split', 'array_str', 'arrayrange',
00274         'asanyarray', 'asarray', 'asarray_chkfinite', 'ascontiguousarray',
00275         'asfarray', 'asfortranarray', 'asmatrix', 'asscalar', 'astype',
00276         'atleast_1d', 'atleast_2d', 'atleast_3d', 'average', 'bartlett',
00277         'base_repr', 'beta', 'binary_repr', 'bincount', 'binomial',
00278         'bitwise_and', 'bitwise_not', 'bitwise_or', 'bitwise_xor', 'blackman',
00279         'bmat', 'broadcast', 'byte_bounds', 'bytes', 'byteswap', 'c_',
00280         'can_cast', 'ceil', 'choose', 'clip', 'column_stack', 'common_type',
00281         'compare_chararrays', 'compress', 'concatenate', 'conj', 'conjugate',
00282         'convolve', 'copy', 'corrcoef', 'correlate', 'cos', 'cosh', 'cov',
00283         'cross', 'cumprod', 'cumproduct', 'cumsum', 'delete', 'deprecate',
00284         'diag', 'diagflat', 'diagonal', 'diff', 'digitize', 'disp', 'divide',
00285         'dot', 'dsplit', 'dstack', 'dtype', 'dump', 'dumps', 'ediff1d', 'empty',
00286         'empty_like', 'equal', 'exp', 'expand_dims', 'expm1', 'extract', 'eye',
00287         'fabs', 'fastCopyAndTranspose', 'fft', 'fftfreq', 'fftshift', 'fill',
00288         'finfo', 'fix', 'flat', 'flatnonzero', 'flatten', 'fliplr', 'flipud',
00289         'floor', 'floor_divide', 'fmod', 'frexp', 'fromarrays', 'frombuffer',
00290         'fromfile', 'fromfunction', 'fromiter', 'frompyfunc', 'fromstring',
00291         'generic', 'get_array_wrap', 'get_include', 'get_numarray_include',
00292         'get_numpy_include', 'get_printoptions', 'getbuffer', 'getbufsize',
00293         'geterr', 'geterrcall', 'geterrobj', 'getfield', 'gradient', 'greater',
00294         'greater_equal', 'gumbel', 'hamming', 'hanning', 'histogram',
00295         'histogram2d', 'histogramdd', 'hsplit', 'hstack', 'hypot', 'i0',
00296         'identity', 'ifft', 'imag', 'index_exp', 'indices', 'inf', 'info',
00297         'inner', 'insert', 'int_asbuffer', 'interp', 'intersect1d',
00298         'intersect1d_nu', 'inv', 'invert', 'iscomplex', 'iscomplexobj',
00299         'isfinite', 'isfortran', 'isinf', 'isnan', 'isneginf', 'isposinf',
00300         'isreal', 'isrealobj', 'isscalar', 'issctype', 'issubclass_',
00301         'issubdtype', 'issubsctype', 'item', 'itemset', 'iterable', 'ix_',
00302         'kaiser', 'kron', 'ldexp', 'left_shift', 'less', 'less_equal', 'lexsort',
00303         'linspace', 'load', 'loads', 'loadtxt', 'log', 'log10', 'log1p', 'log2',
00304         'logical_and', 'logical_not', 'logical_or', 'logical_xor', 'logspace',
00305         'lstsq', 'mat', 'matrix', 'max', 'maximum', 'maximum_sctype',
00306         'may_share_memory', 'mean', 'median', 'meshgrid', 'mgrid', 'min',
00307         'minimum', 'mintypecode', 'mod', 'modf', 'msort', 'multiply', 'nan',
00308         'nan_to_num', 'nanargmax', 'nanargmin', 'nanmax', 'nanmin', 'nansum',
00309         'ndenumerate', 'ndim', 'ndindex', 'negative', 'newaxis', 'newbuffer',
00310         'newbyteorder', 'nonzero', 'not_equal', 'obj2sctype', 'ogrid', 'ones',
00311         'ones_like', 'outer', 'permutation', 'piecewise', 'pinv', 'pkgload',
00312         'place', 'poisson', 'poly', 'poly1d', 'polyadd', 'polyder', 'polydiv',
00313         'polyfit', 'polyint', 'polymul', 'polysub', 'polyval', 'power', 'prod',
00314         'product', 'ptp', 'put', 'putmask', 'r_', 'randint', 'random_integers',
00315         'random_sample', 'ranf', 'rank', 'ravel', 'real', 'real_if_close',
00316         'recarray', 'reciprocal', 'reduce', 'remainder', 'repeat', 'require',
00317         'reshape', 'resize', 'restoredot', 'right_shift', 'rint', 'roll',
00318         'rollaxis', 'roots', 'rot90', 'round', 'round_', 'row_stack', 's_',
00319         'sample', 'savetxt', 'sctype2char', 'searchsorted', 'seed', 'select',
00320         'set_numeric_ops', 'set_printoptions', 'set_string_function',
00321         'setbufsize', 'setdiff1d', 'seterr', 'seterrcall', 'seterrobj',
00322         'setfield', 'setflags', 'setmember1d', 'setxor1d', 'shape',
00323         'show_config', 'shuffle', 'sign', 'signbit', 'sin', 'sinc', 'sinh',
00324         'size', 'slice', 'solve', 'sometrue', 'sort', 'sort_complex', 'source',
00325         'split', 'sqrt', 'square', 'squeeze', 'standard_normal', 'std',
00326         'subtract', 'sum', 'svd', 'swapaxes', 'take', 'tan', 'tanh', 'tensordot',
00327         'test', 'tile', 'tofile', 'tolist', 'tostring', 'trace', 'transpose',
00328         'trapz', 'tri', 'tril', 'trim_zeros', 'triu', 'true_divide', 'typeDict',
00329         'typename', 'uniform', 'union1d', 'unique', 'unique1d', 'unravel_index',
00330         'unwrap', 'vander', 'var', 'vdot', 'vectorize', 'view', 'vonmises',
00331         'vsplit', 'vstack', 'weibull', 'where', 'who', 'zeros', 'zeros_like'
00332     ])
00333 
00334     def get_tokens_unprocessed(self, text):
00335         for index, token, value in \
00336                 PythonLexer.get_tokens_unprocessed(self, text):
00337             if token is Name and value in self.EXTRA_KEYWORDS:
00338                 yield index, Keyword.Pseudo, value
00339             else:
00340                 yield index, token, value
00341 
00342 
00343 class SLexer(RegexLexer):
00344     """
00345     For S, S-plus, and R source code.
00346 
00347     *New in Pygments 0.10.*
00348     """
00349 
00350     name = 'S'
00351     aliases = ['splus', 's', 'r']
00352     filenames = ['*.S', '*.R']
00353     mimetypes = ['text/S-plus', 'text/S', 'text/R']
00354 
00355     tokens = {
00356         'comments': [
00357             (r'#.*$', Comment.Single),
00358         ],
00359         'valid_name': [
00360             (r'[a-zA-Z][0-9a-zA-Z\._]+', Text),
00361             (r'`.+`', String.Backtick),
00362         ],
00363         'punctuation': [
00364             (r'\[|\]|\[\[|\]\]|\$|\(|\)|@|:::?|;|,', Punctuation),
00365         ],
00366         'keywords': [
00367             (r'for(?=\s*\()|while(?=\s*\()|if(?=\s*\()|(?<=\s)else|'
00368              r'(?<=\s)break(?=;|$)|return(?=\s*\()|function(?=\s*\()',
00369              Keyword.Reserved)
00370         ],
00371         'operators': [
00372             (r'<-|-|==|<=|>=|<|>|&&|&|!=|\|\|?', Operator),
00373             (r'\*|\+|\^|/|%%|%/%|=', Operator),
00374             (r'%in%|%*%', Operator)
00375         ],
00376         'builtin_symbols': [
00377             (r'(NULL|NA|TRUE|FALSE|NaN)\b', Keyword.Constant),
00378             (r'(T|F)\b', Keyword.Variable),
00379         ],
00380         'numbers': [
00381             (r'(?<![0-9a-zA-Z\)\}\]`\"])(?=\s*)[-\+]?[0-9]+'
00382              r'(\.[0-9]*)?(E[0-9][-\+]?(\.[0-9]*)?)?', Number),
00383             (r'\.[0-9]*(E[0-9][-\+]?(\.[0-9]*)?)?', Number),
00384         ],
00385         'statements': [
00386             include('comments'),
00387             # whitespaces
00388             (r'\s+', Text),
00389             (r'\'', String, 'string_squote'),
00390             (r'\"', String, 'string_dquote'),
00391             include('builtin_symbols'),
00392             include('numbers'),
00393             include('keywords'),
00394             include('punctuation'),
00395             include('operators'),
00396             include('valid_name'),
00397         ],
00398         'root': [
00399             include('statements'),
00400             # blocks:
00401             (r'\{|\}', Punctuation),
00402             #(r'\{', Punctuation, 'block'),
00403             (r'.', Text),
00404         ],
00405         #'block': [
00406         #    include('statements'),
00407         #    ('\{', Punctuation, '#push'),
00408         #    ('\}', Punctuation, '#pop')
00409         #],
00410         'string_squote': [
00411             (r'[^\']*\'', String, '#pop'),
00412         ],
00413         'string_dquote': [
00414             (r'[^\"]*\"', String, '#pop'),
00415         ],
00416     }
00417 
00418     def analyse_text(text):
00419         return '<-' in text