1
0
mirror of https://github.com/alecthomas/chroma.git synced 2025-03-17 20:58:08 +02:00

add monkeyc lexer

This commit is contained in:
pzl 2018-06-23 08:53:29 -04:00
parent 1e3d94a832
commit 8387f77374
No known key found for this signature in database
GPG Key ID: D02DF5211139C048
3 changed files with 452 additions and 0 deletions

62
lexers/m/monkeyc.go Normal file
View File

@ -0,0 +1,62 @@
package m
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
var MonkeyC = internal.Register(MustNewLexer(
&Config{
Name: "MonkeyC",
Aliases: []string{"monkeyc"},
Filenames: []string{"*.mc"},
MimeTypes: []string{"text/x-monkeyc"},
},
Rules{
"root": {
{`[^\S\n]+`, Text, nil},
{`\n`, Text, nil},
{`//(\n|[\w\W]*?[^\\]\n)`, CommentSingle, nil},
{`/(\\\n)?[*][\w\W]*?[*](\\\n)?/`, CommentMultiline, nil},
{`/(\\\n)?[*][\w\W]*`, CommentMultiline, nil},
{`:[a-zA-Z_][\w_\.]*`, StringSymbol, nil},
{`[{}\[\]\(\),;:\.]`, Punctuation, nil},
{`[&~\|\^!+\-*\/%=?]`, Operator, nil},
{`=>|[+-]=|&&|\|\||>>|<<|[<>]=?|[!=]=`, Operator, nil},
{`\b(and|or|instanceof|has|extends|new)`, OperatorWord, nil},
{Words(``, `\b`, `NaN`, `null`, `true`, `false`), KeywordConstant, nil},
{`(using)((?:\s|\\\\s)+)`, ByGroups(KeywordNamespace, Text), Push("import")},
{`(class)((?:\s|\\\\s)+)`, ByGroups(KeywordDeclaration, Text), Push("class")},
{`(function)((?:\s|\\\\s)+)`, ByGroups(KeywordDeclaration, Text), Push("function")},
{`(module)((?:\s|\\\\s)+)`, ByGroups(KeywordDeclaration, Text), Push("module")},
{`\b(if|else|for|switch|case|while|break|continue|default|do|try|catch|finally|return|throw|extends|function)\b`, Keyword, nil},
{`\b(const|enum|hidden|public|protected|private|static)\b`, KeywordType, nil},
{`\bvar\b`, KeywordDeclaration, nil},
{`\b(Activity(Monitor|Recording)?|Ant(Plus)?|Application|Attention|Background|Communications|Cryptography|FitContributor|Graphics|Gregorian|Lang|Math|Media|Persisted(Content|Locations)|Position|Properties|Sensor(History|Logging)?|Storage|StringUtil|System|Test|Time(r)?|Toybox|UserProfile|WatchUi|Rez|Drawables|Strings|Fonts|method)\b`, NameBuiltin, nil},
{`\b(me|self|\$)\b`, NameBuiltinPseudo, nil},
{`"(\\\\|\\"|[^"])*"`, LiteralStringDouble, nil},
{`'(\\\\|\\'|[^''])*'`, LiteralStringSingle, nil},
{`-?(0x[0-9a-fA-F]+l?)`, NumberHex, nil},
{`-?([0-9]+(\.[0-9]+[df]?|[df]))\b`, NumberFloat, nil},
{`-?([0-9]+l?)`, NumberInteger, nil},
{`[a-zA-Z_]\w*`, Name, nil},
},
"import": {
{`([a-zA-Z_][\w_\.]*)(?:(\s+)(as)(\s+)([a-zA-Z_][\w_]*))?`, ByGroups(NameNamespace, Text, KeywordNamespace, Text, NameNamespace), nil},
Default(Pop(1)),
},
"class": {
{`([a-zA-Z_][\w_\.]*)(?:(\s+)(extends)(\s+)([a-zA-Z_][\w_\.]*))?`, ByGroups(NameClass, Text, KeywordDeclaration, Text, NameClass), nil},
Default(Pop(1)),
},
"function": {
{`initialize`, NameFunctionMagic, nil},
{`[a-zA-Z_][\w_\.]*`, NameFunction, nil},
Default(Pop(1)),
},
"module": {
{`[a-zA-Z_][\w_\.]*`, NameNamespace, nil},
Default(Pop(1)),
},
},
))

47
lexers/testdata/monkeyc.actual vendored Normal file
View File

@ -0,0 +1,47 @@
using Toybox.Application as App;
using Toybox.System;
const PI = 3.14;
class MyProjectApp extends App.AppBase {
protected var y;
function initialize () {
me.y = "Hello";
self.y = "World";
var x = add( 3, 4 );
var array = new [x];
// Initialize the sub-arrays
for( var i = 0; i < x; i += 1 ) {
array[i] = new [5];
}
var dict = { "a" => 1, "b" => 2 };
var person = { :firstName=>"Bob", :lastName=>"Jones" };
}
public function onStart(state) {
var v = new Foo();
var m = v.method(:op);
m.invoke(1,2l);
}
function getInitialView() {
return [ new MyProjectView() ];
}
function add( a, b ) {
return a + b;
}
function thisFunctionUsesAdd() {
var a = add( 1, 0x03f ); // Return 4
var b = add( "Hello ", "World" ); // Returns "Hello World"
}
}
class Foo {
function op(a, b) {}
}

343
lexers/testdata/monkeyc.expected vendored Normal file
View File

@ -0,0 +1,343 @@
[
{"type":"KeywordNamespace","value":"using"},
{"type":"Text","value":" "},
{"type":"NameNamespace","value":"Toybox.Application"},
{"type":"Text","value":" "},
{"type":"KeywordNamespace","value":"as"},
{"type":"Text","value":" "},
{"type":"NameNamespace","value":"App"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n"},
{"type":"KeywordNamespace","value":"using"},
{"type":"Text","value":" "},
{"type":"NameNamespace","value":"Toybox.System"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n\n"},
{"type":"KeywordType","value":"const"},
{"type":"Text","value":" "},
{"type":"Name","value":"PI"},
{"type":"Text","value":" "},
{"type":"Operator","value":"="},
{"type":"Text","value":" "},
{"type":"LiteralNumberFloat","value":"3.14"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n\n"},
{"type":"KeywordDeclaration","value":"class"},
{"type":"Text","value":" "},
{"type":"NameClass","value":"MyProjectApp"},
{"type":"Text","value":" "},
{"type":"KeywordDeclaration","value":"extends"},
{"type":"Text","value":" "},
{"type":"NameClass","value":"App.AppBase"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":"\n "},
{"type":"KeywordType","value":"protected"},
{"type":"Text","value":" "},
{"type":"KeywordDeclaration","value":"var"},
{"type":"Text","value":" "},
{"type":"Name","value":"y"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n\n "},
{"type":"KeywordDeclaration","value":"function"},
{"type":"Text","value":" "},
{"type":"NameFunctionMagic","value":"initialize"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"()"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":"\n "},
{"type":"NameBuiltinPseudo","value":"me"},
{"type":"Punctuation","value":"."},
{"type":"Name","value":"y"},
{"type":"Text","value":" "},
{"type":"Operator","value":"="},
{"type":"Text","value":" "},
{"type":"LiteralStringDouble","value":"\"Hello\""},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n "},
{"type":"NameBuiltinPseudo","value":"self"},
{"type":"Punctuation","value":"."},
{"type":"Name","value":"y"},
{"type":"Text","value":" "},
{"type":"Operator","value":"="},
{"type":"Text","value":" "},
{"type":"LiteralStringDouble","value":"\"World\""},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n "},
{"type":"KeywordDeclaration","value":"var"},
{"type":"Text","value":" "},
{"type":"Name","value":"x"},
{"type":"Text","value":" "},
{"type":"Operator","value":"="},
{"type":"Text","value":" "},
{"type":"Name","value":"add"},
{"type":"Punctuation","value":"("},
{"type":"Text","value":" "},
{"type":"LiteralNumberInteger","value":"3"},
{"type":"Punctuation","value":","},
{"type":"Text","value":" "},
{"type":"LiteralNumberInteger","value":"4"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":");"},
{"type":"Text","value":"\n "},
{"type":"KeywordDeclaration","value":"var"},
{"type":"Text","value":" "},
{"type":"Name","value":"array"},
{"type":"Text","value":" "},
{"type":"Operator","value":"="},
{"type":"Text","value":" "},
{"type":"OperatorWord","value":"new"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"["},
{"type":"Name","value":"x"},
{"type":"Punctuation","value":"];"},
{"type":"Text","value":"\n\n "},
{"type":"CommentSingle","value":"// Initialize the sub-arrays\n"},
{"type":"Text","value":" "},
{"type":"Keyword","value":"for"},
{"type":"Punctuation","value":"("},
{"type":"Text","value":" "},
{"type":"KeywordDeclaration","value":"var"},
{"type":"Text","value":" "},
{"type":"Name","value":"i"},
{"type":"Text","value":" "},
{"type":"Operator","value":"="},
{"type":"Text","value":" "},
{"type":"LiteralNumberInteger","value":"0"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":" "},
{"type":"Name","value":"i"},
{"type":"Text","value":" "},
{"type":"Operator","value":"\u003c"},
{"type":"Text","value":" "},
{"type":"Name","value":"x"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":" "},
{"type":"Name","value":"i"},
{"type":"Text","value":" "},
{"type":"Operator","value":"+="},
{"type":"Text","value":" "},
{"type":"LiteralNumberInteger","value":"1"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":")"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":"\n "},
{"type":"Name","value":"array"},
{"type":"Punctuation","value":"["},
{"type":"Name","value":"i"},
{"type":"Punctuation","value":"]"},
{"type":"Text","value":" "},
{"type":"Operator","value":"="},
{"type":"Text","value":" "},
{"type":"OperatorWord","value":"new"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"["},
{"type":"LiteralNumberInteger","value":"5"},
{"type":"Punctuation","value":"];"},
{"type":"Text","value":"\n "},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"\n\n "},
{"type":"KeywordDeclaration","value":"var"},
{"type":"Text","value":" "},
{"type":"Name","value":"dict"},
{"type":"Text","value":" "},
{"type":"Operator","value":"="},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":" "},
{"type":"LiteralStringDouble","value":"\"a\""},
{"type":"Text","value":" "},
{"type":"Operator","value":"=\u003e"},
{"type":"Text","value":" "},
{"type":"LiteralNumberInteger","value":"1"},
{"type":"Punctuation","value":","},
{"type":"Text","value":" "},
{"type":"LiteralStringDouble","value":"\"b\""},
{"type":"Text","value":" "},
{"type":"Operator","value":"=\u003e"},
{"type":"Text","value":" "},
{"type":"LiteralNumberInteger","value":"2"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"};"},
{"type":"Text","value":"\n "},
{"type":"KeywordDeclaration","value":"var"},
{"type":"Text","value":" "},
{"type":"Name","value":"person"},
{"type":"Text","value":" "},
{"type":"Operator","value":"="},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":" "},
{"type":"LiteralStringSymbol","value":":firstName"},
{"type":"Operator","value":"=\u003e"},
{"type":"LiteralStringDouble","value":"\"Bob\""},
{"type":"Punctuation","value":","},
{"type":"Text","value":" "},
{"type":"LiteralStringSymbol","value":":lastName"},
{"type":"Operator","value":"=\u003e"},
{"type":"LiteralStringDouble","value":"\"Jones\""},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"};"},
{"type":"Text","value":"\n "},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"\n\n "},
{"type":"KeywordType","value":"public"},
{"type":"Text","value":" "},
{"type":"KeywordDeclaration","value":"function"},
{"type":"Text","value":" "},
{"type":"NameFunction","value":"onStart"},
{"type":"Punctuation","value":"("},
{"type":"Name","value":"state"},
{"type":"Punctuation","value":")"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":"\n "},
{"type":"KeywordDeclaration","value":"var"},
{"type":"Text","value":" "},
{"type":"Name","value":"v"},
{"type":"Text","value":" "},
{"type":"Operator","value":"="},
{"type":"Text","value":" "},
{"type":"OperatorWord","value":"new"},
{"type":"Text","value":" "},
{"type":"Name","value":"Foo"},
{"type":"Punctuation","value":"();"},
{"type":"Text","value":"\n "},
{"type":"KeywordDeclaration","value":"var"},
{"type":"Text","value":" "},
{"type":"Name","value":"m"},
{"type":"Text","value":" "},
{"type":"Operator","value":"="},
{"type":"Text","value":" "},
{"type":"Name","value":"v"},
{"type":"Punctuation","value":"."},
{"type":"NameBuiltin","value":"method"},
{"type":"Punctuation","value":"("},
{"type":"LiteralStringSymbol","value":":op"},
{"type":"Punctuation","value":");"},
{"type":"Text","value":"\n "},
{"type":"Name","value":"m"},
{"type":"Punctuation","value":"."},
{"type":"Name","value":"invoke"},
{"type":"Punctuation","value":"("},
{"type":"LiteralNumberInteger","value":"1"},
{"type":"Punctuation","value":","},
{"type":"LiteralNumberInteger","value":"2l"},
{"type":"Punctuation","value":");"},
{"type":"Text","value":"\n "},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"\n\n "},
{"type":"KeywordDeclaration","value":"function"},
{"type":"Text","value":" "},
{"type":"NameFunction","value":"getInitialView"},
{"type":"Punctuation","value":"()"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":"\n "},
{"type":"Keyword","value":"return"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"["},
{"type":"Text","value":" "},
{"type":"OperatorWord","value":"new"},
{"type":"Text","value":" "},
{"type":"Name","value":"MyProjectView"},
{"type":"Punctuation","value":"()"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"];"},
{"type":"Text","value":"\n "},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"\n\n "},
{"type":"KeywordDeclaration","value":"function"},
{"type":"Text","value":" "},
{"type":"NameFunction","value":"add"},
{"type":"Punctuation","value":"("},
{"type":"Text","value":" "},
{"type":"Name","value":"a"},
{"type":"Punctuation","value":","},
{"type":"Text","value":" "},
{"type":"Name","value":"b"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":")"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":"\n "},
{"type":"Keyword","value":"return"},
{"type":"Text","value":" "},
{"type":"Name","value":"a"},
{"type":"Text","value":" "},
{"type":"Operator","value":"+"},
{"type":"Text","value":" "},
{"type":"Name","value":"b"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n "},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"\n\n "},
{"type":"KeywordDeclaration","value":"function"},
{"type":"Text","value":" "},
{"type":"NameFunction","value":"thisFunctionUsesAdd"},
{"type":"Punctuation","value":"()"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":"\n "},
{"type":"KeywordDeclaration","value":"var"},
{"type":"Text","value":" "},
{"type":"Name","value":"a"},
{"type":"Text","value":" "},
{"type":"Operator","value":"="},
{"type":"Text","value":" "},
{"type":"Name","value":"add"},
{"type":"Punctuation","value":"("},
{"type":"Text","value":" "},
{"type":"LiteralNumberInteger","value":"1"},
{"type":"Punctuation","value":","},
{"type":"Text","value":" "},
{"type":"LiteralNumberHex","value":"0x03f"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":");"},
{"type":"Text","value":" "},
{"type":"CommentSingle","value":"// Return 4\n"},
{"type":"Text","value":" "},
{"type":"KeywordDeclaration","value":"var"},
{"type":"Text","value":" "},
{"type":"Name","value":"b"},
{"type":"Text","value":" "},
{"type":"Operator","value":"="},
{"type":"Text","value":" "},
{"type":"Name","value":"add"},
{"type":"Punctuation","value":"("},
{"type":"Text","value":" "},
{"type":"LiteralStringDouble","value":"\"Hello \""},
{"type":"Punctuation","value":","},
{"type":"Text","value":" "},
{"type":"LiteralStringDouble","value":"\"World\""},
{"type":"Text","value":" "},
{"type":"Punctuation","value":");"},
{"type":"Text","value":" "},
{"type":"CommentSingle","value":"// Returns \"Hello World\"\n"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"\n \n"},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"\n\n"},
{"type":"KeywordDeclaration","value":"class"},
{"type":"Text","value":" "},
{"type":"NameClass","value":"Foo"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":"\n "},
{"type":"KeywordDeclaration","value":"function"},
{"type":"Text","value":" "},
{"type":"NameFunction","value":"op"},
{"type":"Punctuation","value":"("},
{"type":"Name","value":"a"},
{"type":"Punctuation","value":","},
{"type":"Text","value":" "},
{"type":"Name","value":"b"},
{"type":"Punctuation","value":")"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{}"},
{"type":"Text","value":"\n"},
{"type":"Punctuation","value":"}"}
]