matlab.go

 1package m
 2
 3import (
 4	. "github.com/alecthomas/chroma" // nolint
 5	"github.com/alecthomas/chroma/lexers/internal"
 6)
 7
 8// Matlab lexer.
 9var Matlab = internal.Register(MustNewLexer(
10	&Config{
11		Name:      "Matlab",
12		Aliases:   []string{"matlab"},
13		Filenames: []string{"*.m"},
14		MimeTypes: []string{"text/matlab"},
15	},
16	Rules{
17		"root": {
18			{`\n`, Text, nil},
19			{`^!.*`, LiteralStringOther, nil},
20			{`%\{\s*\n`, CommentMultiline, Push("blockcomment")},
21			{`%.*$`, Comment, nil},
22			{`^\s*function`, Keyword, Push("deffunc")},
23			{Words(``, `\b`, `break`, `case`, `catch`, `classdef`, `continue`, `else`, `elseif`, `end`, `enumerated`, `events`, `for`, `function`, `global`, `if`, `methods`, `otherwise`, `parfor`, `persistent`, `properties`, `return`, `spmd`, `switch`, `try`, `while`), Keyword, nil},
24			{`(sin|sind|sinh|asin|asind|asinh|cos|cosd|cosh|acos|acosd|acosh|tan|tand|tanh|atan|atand|atan2|atanh|sec|secd|sech|asec|asecd|asech|csc|cscd|csch|acsc|acscd|acsch|cot|cotd|coth|acot|acotd|acoth|hypot|exp|expm1|log|log1p|log10|log2|pow2|realpow|reallog|realsqrt|sqrt|nthroot|nextpow2|abs|angle|complex|conj|imag|real|unwrap|isreal|cplxpair|fix|floor|ceil|round|mod|rem|sign|airy|besselj|bessely|besselh|besseli|besselk|beta|betainc|betaln|ellipj|ellipke|erf|erfc|erfcx|erfinv|expint|gamma|gammainc|gammaln|psi|legendre|cross|dot|factor|isprime|primes|gcd|lcm|rat|rats|perms|nchoosek|factorial|cart2sph|cart2pol|pol2cart|sph2cart|hsv2rgb|rgb2hsv|zeros|ones|eye|repmat|rand|randn|linspace|logspace|freqspace|meshgrid|accumarray|size|length|ndims|numel|disp|isempty|isequal|isequalwithequalnans|cat|reshape|diag|blkdiag|tril|triu|fliplr|flipud|flipdim|rot90|find|end|sub2ind|ind2sub|bsxfun|ndgrid|permute|ipermute|shiftdim|circshift|squeeze|isscalar|isvector|ans|eps|realmax|realmin|pi|i|inf|nan|isnan|isinf|isfinite|j|why|compan|gallery|hadamard|hankel|hilb|invhilb|magic|pascal|rosser|toeplitz|vander|wilkinson)\b`, NameBuiltin, nil},
25			{`\.\.\..*$`, Comment, nil},
26			{`-|==|~=|<|>|<=|>=|&&|&|~|\|\|?`, Operator, nil},
27			{`\.\*|\*|\+|\.\^|\.\\|\.\/|\/|\\`, Operator, nil},
28			{`\[|\]|\(|\)|\{|\}|:|@|\.|,`, Punctuation, nil},
29			{`=|:|;`, Punctuation, nil},
30			{`(?<=[\w)\].])\'+`, Operator, nil},
31			{`(\d+\.\d*|\d*\.\d+)([eEf][+-]?[0-9]+)?`, LiteralNumberFloat, nil},
32			{`\d+[eEf][+-]?[0-9]+`, LiteralNumberFloat, nil},
33			{`\d+`, LiteralNumberInteger, nil},
34			{`(?<![\w)\].])\'`, LiteralString, Push("string")},
35			{`[a-zA-Z_]\w*`, Name, nil},
36			{`.`, Text, nil},
37		},
38		"string": {
39			{`[^\']*\'`, LiteralString, Pop(1)},
40		},
41		"blockcomment": {
42			{`^\s*%\}`, CommentMultiline, Pop(1)},
43			{`^.*\n`, CommentMultiline, nil},
44			{`.`, CommentMultiline, nil},
45		},
46		"deffunc": {
47			{`(\s*)(?:(.+)(\s*)(=)(\s*))?(.+)(\()(.*)(\))(\s*)`, ByGroups(TextWhitespace, Text, TextWhitespace, Punctuation, TextWhitespace, NameFunction, Punctuation, Text, Punctuation, TextWhitespace), Pop(1)},
48			{`(\s*)([a-zA-Z_]\w*)`, ByGroups(Text, NameFunction), Pop(1)},
49		},
50	},
51))