Skip to content

Commit

Permalink
Add Chapel Lexer
Browse files Browse the repository at this point in the history
  • Loading branch information
DanilaFe authored and alecthomas committed Jun 23, 2022
1 parent 298b727 commit f941d46
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -38,7 +38,7 @@ Prefix | Language
:----: | --------
A | ABAP, ABNF, ActionScript, ActionScript 3, Ada, Angular2, ANTLR, ApacheConf, APL, AppleScript, Arduino, Awk
B | Ballerina, Base Makefile, Bash, Batchfile, BibTeX, Bicep, BlitzBasic, BNF, Brainfuck
C | C, C#, C++, Caddyfile, Caddyfile Directives, Cap'n Proto, Cassandra CQL, Ceylon, CFEngine3, cfstatement, ChaiScript, Cheetah, Clojure, CMake, COBOL, CoffeeScript, Common Lisp, Coq, Crystal, CSS, Cython
C | C, C#, C++, Caddyfile, Caddyfile Directives, Cap'n Proto, Cassandra CQL, Ceylon, CFEngine3, cfstatement, ChaiScript, Chapel, Cheetah, Clojure, CMake, COBOL, CoffeeScript, Common Lisp, Coq, Crystal, CSS, Cython
D | D, Dart, Diff, Django/Jinja, Docker, DTD, Dylan
E | EBNF, Elixir, Elm, EmacsLisp, Erlang
F | Factor, Fish, Forth, Fortran, FSharp
Expand Down
62 changes: 62 additions & 0 deletions lexers/chapel.go
@@ -0,0 +1,62 @@
package lexers

import (
. "github.com/alecthomas/chroma/v2" // nolint
)

// Chapel lexer.
var Chapel = Register(MustNewLexer(
&Config{
Name: "Chapel",
Aliases: []string{"chapel", "chpl"},
Filenames: []string{"*.chpl"},
MimeTypes: []string{},
},
func() Rules {
return Rules{
"root": {
{`\n`, TextWhitespace, nil},
{`\s+`, TextWhitespace, nil},
{`\\\n`, Text, nil},
{`//(.*?)\n`, CommentSingle, nil},
{`/(\\\n)?[*](.|\n)*?[*](\\\n)?/`, CommentMultiline, nil},
{Words(``, `\b`, `config`, `const`, `in`, `inout`, `out`, `param`, `ref`, `type`, `var`), KeywordDeclaration, nil},
{Words(``, `\b`, `false`, `nil`, `none`, `true`), KeywordConstant, nil},
{Words(``, `\b`, `bool`, `bytes`, `complex`, `imag`, `int`, `locale`, `nothing`, `opaque`, `range`, `real`, `string`, `uint`, `void`), KeywordType, nil},
{Words(``, `\b`, `atomic`, `single`, `sync`, `borrowed`, `owned`, `shared`, `unmanaged`, `align`, `as`, `begin`, `break`, `by`, `catch`, `cobegin`, `coforall`, `continue`, `defer`, `delete`, `dmapped`, `do`, `domain`, `else`, `enum`, `except`, `export`, `extern`, `for`, `forall`, `foreach`, `forwarding`, `if`, `implements`, `import`, `index`, `init`, `inline`, `label`, `lambda`, `let`, `lifetime`, `local`, `new`, `noinit`, `on`, `only`, `otherwise`, `override`, `pragma`, `primitive`, `private`, `prototype`, `public`, `reduce`, `require`, `return`, `scan`, `select`, `serial`, `sparse`, `subdomain`, `then`, `this`, `throw`, `throws`, `try`, `use`, `when`, `where`, `while`, `with`, `yield`, `zip`), Keyword, nil},
{`(iter)(\s+)`, ByGroups(Keyword, TextWhitespace), Push("procname")},
{`(proc)(\s+)`, ByGroups(Keyword, TextWhitespace), Push("procname")},
{`(operator)(\s+)`, ByGroups(Keyword, TextWhitespace), Push("procname")},
{`(class|interface|module|record|union)(\s+)`, ByGroups(Keyword, TextWhitespace), Push("classname")},
{`\d+i`, LiteralNumber, nil},
{`\d+\.\d*([Ee][-+]\d+)?i`, LiteralNumber, nil},
{`\.\d+([Ee][-+]\d+)?i`, LiteralNumber, nil},
{`\d+[Ee][-+]\d+i`, LiteralNumber, nil},
{`(\d*\.\d+)([eE][+-]?[0-9]+)?i?`, LiteralNumberFloat, nil},
{`\d+[eE][+-]?[0-9]+i?`, LiteralNumberFloat, nil},
{`0[bB][01]+`, LiteralNumberBin, nil},
{`0[xX][0-9a-fA-F]+`, LiteralNumberHex, nil},
{`0[oO][0-7]+`, LiteralNumberOct, nil},
{`[0-9]+`, LiteralNumberInteger, nil},
{`"(\\\\|\\"|[^"])*"`, LiteralString, nil},
{`'(\\\\|\\'|[^'])*'`, LiteralString, nil},
{`(=|\+=|-=|\*=|/=|\*\*=|%=|&=|\|=|\^=|&&=|\|\|=|<<=|>>=|<=>|<~>|\.\.|by|#|\.\.\.|&&|\|\||!|&|\||\^|~|<<|>>|==|!=|<=|>=|<|>|[+\-*/%]|\*\*)`, Operator, nil},
{`[:;,.?()\[\]{}]`, Punctuation, nil},
{`[a-zA-Z_][\w$]*`, NameOther, nil},
},
"classname": {
{`[a-zA-Z_][\w$]*`, NameClass, Pop(1)},
},
"procname": {
{`([a-zA-Z_][.\w$]*|\~[a-zA-Z_][.\w$]*|[+*/!~%<>=&^|\-:]{1,2})`, NameFunction, Pop(1)},
{`\(`, Punctuation, Push("receivertype")},
{`\)+\.`, Punctuation, nil},
},
"receivertype": {
{Words(``, `\b`, `atomic`, `single`, `sync`, `borrowed`, `owned`, `shared`, `unmanaged`), Keyword, nil},
{Words(``, `\b`, `bool`, `bytes`, `complex`, `imag`, `int`, `locale`, `nothing`, `opaque`, `range`, `real`, `string`, `uint`, `void`), KeywordType, nil},
{`[^()]*`, NameOther, Pop(1)},
},
}
},
))
6 changes: 6 additions & 0 deletions lexers/testdata/chapel.actual
@@ -0,0 +1,6 @@
config const numMessages = 100;

forall msg in 1..numMessages do
writeln("Hello, world! (from iteration ", msg, " of ", numMessages, ")");

// Oh, and a comment for good measure.
43 changes: 43 additions & 0 deletions lexers/testdata/chapel.expected
@@ -0,0 +1,43 @@
[
{"type":"KeywordDeclaration","value":"config"},
{"type":"TextWhitespace","value":" "},
{"type":"KeywordDeclaration","value":"const"},
{"type":"TextWhitespace","value":" "},
{"type":"NameOther","value":"numMessages"},
{"type":"TextWhitespace","value":" "},
{"type":"Operator","value":"="},
{"type":"TextWhitespace","value":" "},
{"type":"LiteralNumberInteger","value":"100"},
{"type":"Punctuation","value":";"},
{"type":"TextWhitespace","value":"\n\n"},
{"type":"Keyword","value":"forall"},
{"type":"TextWhitespace","value":" "},
{"type":"NameOther","value":"msg"},
{"type":"TextWhitespace","value":" "},
{"type":"KeywordDeclaration","value":"in"},
{"type":"TextWhitespace","value":" "},
{"type":"LiteralNumberInteger","value":"1"},
{"type":"Operator","value":".."},
{"type":"NameOther","value":"numMessages"},
{"type":"TextWhitespace","value":" "},
{"type":"Keyword","value":"do"},
{"type":"TextWhitespace","value":"\n "},
{"type":"NameOther","value":"writeln"},
{"type":"Punctuation","value":"("},
{"type":"LiteralString","value":"\"Hello, world! (from iteration \""},
{"type":"Punctuation","value":","},
{"type":"TextWhitespace","value":" "},
{"type":"NameOther","value":"msg"},
{"type":"Punctuation","value":","},
{"type":"TextWhitespace","value":" "},
{"type":"LiteralString","value":"\" of \""},
{"type":"Punctuation","value":","},
{"type":"TextWhitespace","value":" "},
{"type":"NameOther","value":"numMessages"},
{"type":"Punctuation","value":","},
{"type":"TextWhitespace","value":" "},
{"type":"LiteralString","value":"\")\""},
{"type":"Punctuation","value":");"},
{"type":"TextWhitespace","value":"\n\n"},
{"type":"CommentSingle","value":"// Oh, and a comment for good measure.\n"}
]

0 comments on commit f941d46

Please sign in to comment.