Skip to content

Commit

Permalink
add support for matlab
Browse files Browse the repository at this point in the history
  • Loading branch information
ggarlic authored and alecthomas committed Mar 7, 2019
1 parent c67f8fe commit 564f7d3
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 0 deletions.
51 changes: 51 additions & 0 deletions lexers/m/matlab.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package m

import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)

// Matlab lexer.
var Matlab = internal.Register(MustNewLexer(
&Config{
Name: "Matlab",
Aliases: []string{"matlab"},
Filenames: []string{"*.m"},
MimeTypes: []string{"text/matlab"},
},
Rules{
"root": {
{`\n`, Text, nil},
{`^!.*`, LiteralStringOther, nil},
{`%\{\s*\n`, CommentMultiline, Push("blockcomment")},
{`%.*$`, Comment, nil},
{`^\s*function`, Keyword, Push("deffunc")},
{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},
{`(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},
{`\.\.\..*$`, Comment, nil},
{`-|==|~=|<|>|<=|>=|&&|&|~|\|\|?`, Operator, nil},
{`\.\*|\*|\+|\.\^|\.\\|\.\/|\/|\\`, Operator, nil},
{`\[|\]|\(|\)|\{|\}|:|@|\.|,`, Punctuation, nil},
{`=|:|;`, Punctuation, nil},
{`(?<=[\w)\].])\'+`, Operator, nil},
{`(\d+\.\d*|\d*\.\d+)([eEf][+-]?[0-9]+)?`, LiteralNumberFloat, nil},
{`\d+[eEf][+-]?[0-9]+`, LiteralNumberFloat, nil},
{`\d+`, LiteralNumberInteger, nil},
{`(?<![\w)\].])\'`, LiteralString, Push("string")},
{`[a-zA-Z_]\w*`, Name, nil},
{`.`, Text, nil},
},
"string": {
{`[^\']*\'`, LiteralString, Pop(1)},
},
"blockcomment": {
{`^\s*%\}`, CommentMultiline, Pop(1)},
{`^.*\n`, CommentMultiline, nil},
{`.`, CommentMultiline, nil},
},
"deffunc": {
{`(\s*)(?:(.+)(\s*)(=)(\s*))?(.+)(\()(.*)(\))(\s*)`, ByGroups(TextWhitespace, Text, TextWhitespace, Punctuation, TextWhitespace, NameFunction, Punctuation, Text, Punctuation, TextWhitespace), Pop(1)},
{`(\s*)([a-zA-Z_]\w*)`, ByGroups(Text, NameFunction), Pop(1)},
},
},
))
10 changes: 10 additions & 0 deletions lexers/testdata/matlab.actual
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
% a sum function
function p = vector_sum(x)
p = 0;
for k1 = 1:length(x)
p = p + x(k1);
end
end

z = 1:10;
sum_from_1_to_10 = vector_sum(z)
69 changes: 69 additions & 0 deletions lexers/testdata/matlab.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
[
{"type":"Comment","value":"% a sum function"},
{"type":"Text","value":"\n"},
{"type":"Keyword","value":"function"},
{"type":"TextWhitespace","value":" "},
{"type":"Text","value":"p "},
{"type":"Punctuation","value":"="},
{"type":"TextWhitespace","value":" "},
{"type":"NameFunction","value":"vector_sum"},
{"type":"Punctuation","value":"("},
{"type":"Text","value":"x"},
{"type":"Punctuation","value":")"},
{"type":"TextWhitespace","value":"\n"},
{"type":"Name","value":"p"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"="},
{"type":"Text","value":" "},
{"type":"LiteralNumberInteger","value":"0"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n"},
{"type":"Keyword","value":"for"},
{"type":"Text","value":" "},
{"type":"Name","value":"k1"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"="},
{"type":"Text","value":" "},
{"type":"LiteralNumberInteger","value":"1"},
{"type":"Punctuation","value":":"},
{"type":"NameBuiltin","value":"length"},
{"type":"Punctuation","value":"("},
{"type":"Name","value":"x"},
{"type":"Punctuation","value":")"},
{"type":"Text","value":"\n "},
{"type":"Name","value":"p"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"="},
{"type":"Text","value":" "},
{"type":"Name","value":"p"},
{"type":"Text","value":" "},
{"type":"Operator","value":"+"},
{"type":"Text","value":" "},
{"type":"Name","value":"x"},
{"type":"Punctuation","value":"("},
{"type":"Name","value":"k1"},
{"type":"Punctuation","value":");"},
{"type":"Text","value":"\n"},
{"type":"Keyword","value":"end"},
{"type":"Text","value":"\n"},
{"type":"Keyword","value":"end"},
{"type":"Text","value":"\n\n"},
{"type":"Name","value":"z"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"="},
{"type":"Text","value":" "},
{"type":"LiteralNumberInteger","value":"1"},
{"type":"Punctuation","value":":"},
{"type":"LiteralNumberInteger","value":"10"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n"},
{"type":"Name","value":"sum_from_1_to_10"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"="},
{"type":"Text","value":" "},
{"type":"Name","value":"vector_sum"},
{"type":"Punctuation","value":"("},
{"type":"Name","value":"z"},
{"type":"Punctuation","value":")"},
{"type":"Text","value":"\n"}
]

0 comments on commit 564f7d3

Please sign in to comment.