1
0
mirror of https://github.com/alecthomas/chroma.git synced 2025-02-15 13:33:12 +02:00

Add protobuf and thrift.

This commit is contained in:
Alec Thomas 2017-06-07 19:46:10 +10:00
parent 7ae55eb265
commit 7a7b85b4b3
4 changed files with 126 additions and 1 deletions

View File

@ -18,7 +18,7 @@ import (
)
// {{upper_name}} lexer.
var {{upper_name}} = Register(NewLexer(
var {{upper_name}} = Register(MustNewLexer(
&Config{
Name: "{{name}}",
Aliases: []string{ {{#aliases}}"{{.}}", {{/aliases}} },

52
lexers/protobuf.go Normal file
View File

@ -0,0 +1,52 @@
package lexers
import (
. "github.com/alecthomas/chroma" // nolint
)
// Protocol Buffer lexer.
var ProtocolBuffer = Register(MustNewLexer(
&Config{
Name: "Protocol Buffer",
Aliases: []string{"protobuf", "proto"},
Filenames: []string{"*.proto"},
MimeTypes: []string{},
},
Rules{
"root": {
{`[ \t]+`, Text, nil},
{`[,;{}\[\]()<>]`, Punctuation, nil},
{`/(\\\n)?/(\n|(.|\n)*?[^\\]\n)`, CommentSingle, nil},
{`/(\\\n)?\*(.|\n)*?\*(\\\n)?/`, CommentMultiline, nil},
{`\b(?:import|option|optional|required|repeated|default|packed|ctype|extensions|to|max|rpc|returns|oneof)\b`, Keyword, nil},
{`(?:int32|int64|uint32|uint64|sint32|sint64|fixed32|fixed64|sfixed32|sfixed64|float|double|bool|string|bytes)\b`, KeywordType, nil},
{`(true|false)\b`, KeywordConstant, nil},
{`(package)(\s+)`, ByGroups(KeywordNamespace, Text), Push("package")},
{`(message|extend)(\s+)`, ByGroups(KeywordDeclaration, Text), Push("message")},
{`(enum|group|service)(\s+)`, ByGroups(KeywordDeclaration, Text), Push("type")},
{`\".*?\"`, LiteralString, nil},
{`\'.*?\'`, LiteralString, nil},
{`(\d+\.\d*|\.\d+|\d+)[eE][+-]?\d+[LlUu]*`, LiteralNumberFloat, nil},
{`(\d+\.\d*|\.\d+|\d+[fF])[fF]?`, LiteralNumberFloat, nil},
{`(\-?(inf|nan))\b`, LiteralNumberFloat, nil},
{`0x[0-9a-fA-F]+[LlUu]*`, LiteralNumberHex, nil},
{`0[0-7]+[LlUu]*`, LiteralNumberOct, nil},
{`\d+[LlUu]*`, LiteralNumberInteger, nil},
{`[+-=]`, Operator, nil},
{`([a-zA-Z_][\w.]*)([ \t]*)(=)`, ByGroups(Name, Text, Operator), nil},
{`[a-zA-Z_][\w.]*`, Name, nil},
},
"package": {
{`[a-zA-Z_]\w*`, NameNamespace, Pop(1)},
Default(Pop(1)),
},
"message": {
{`[a-zA-Z_]\w*`, NameClass, Pop(1)},
Default(Pop(1)),
},
"type": {
{`[a-zA-Z_]\w*`, Name, Pop(1)},
Default(Pop(1)),
},
},
))

72
lexers/thrift.go Normal file
View File

@ -0,0 +1,72 @@
package lexers
import (
. "github.com/alecthomas/chroma" // nolint
)
// Thrift lexer.
var Thrift = Register(MustNewLexer(
&Config{
Name: "Thrift",
Aliases: []string{"thrift"},
Filenames: []string{"*.thrift"},
MimeTypes: []string{"application/x-thrift"},
},
Rules{
"root": {
Include("whitespace"),
Include("comments"),
{`"`, LiteralStringDouble, Combined("stringescape", "dqs")},
{`\'`, LiteralStringSingle, Combined("stringescape", "sqs")},
{`(namespace)(\s+)`, ByGroups(KeywordNamespace, TextWhitespace), Push("namespace")},
{`(enum|union|struct|service|exception)(\s+)`, ByGroups(KeywordDeclaration, TextWhitespace), Push("class")},
{`((?:(?:[^\W\d]|\$)[\w.\[\]$<>]*\s+)+?)((?:[^\W\d]|\$)[\w$]*)(\s*)(\()`, ByGroups(UsingSelf("root"), NameFunction, Text, Operator), nil},
Include("keywords"),
Include("numbers"),
{`[&=]`, Operator, nil},
{`[:;,{}()<>\[\]]`, Punctuation, nil},
{`[a-zA-Z_](\.\w|\w)*`, Name, nil},
},
"whitespace": {
{`\n`, TextWhitespace, nil},
{`\s+`, TextWhitespace, nil},
},
"comments": {
{`#.*$`, Comment, nil},
{`//.*?\n`, Comment, nil},
{`/\*[\w\W]*?\*/`, CommentMultiline, nil},
},
"stringescape": {
{`\\([\\nrt"\'])`, LiteralStringEscape, nil},
},
"dqs": {
{`"`, LiteralStringDouble, Pop(1)},
{`[^\\"\n]+`, LiteralStringDouble, nil},
},
"sqs": {
{`'`, LiteralStringSingle, Pop(1)},
{`[^\\\'\n]+`, LiteralStringSingle, nil},
},
"namespace": {
{`[a-z*](\.\w|\w)*`, NameNamespace, Pop(1)},
Default(Pop(1)),
},
"class": {
{`[a-zA-Z_]\w*`, NameClass, Pop(1)},
Default(Pop(1)),
},
"keywords": {
{`(async|oneway|extends|throws|required|optional)\b`, Keyword, nil},
{`(true|false)\b`, KeywordConstant, nil},
{`(const|typedef)\b`, KeywordDeclaration, nil},
{`(?:cpp_namespace|cpp_include|cpp_type|java_package|cocoa_prefix|csharp_namespace|delphi_namespace|php_namespace|py_module|perl_package|ruby_namespace|smalltalk_category|smalltalk_prefix|xsd_all|xsd_optional|xsd_nillable|xsd_namespace|xsd_attrs|include)\b`, KeywordNamespace, nil},
{`(?:void|bool|byte|i16|i32|i64|double|string|binary|map|list|set|slist|senum)\b`, KeywordType, nil},
{`\b(?:BEGIN|END|__CLASS__|__DIR__|__FILE__|__FUNCTION__|__LINE__|__METHOD__|__NAMESPACE__|abstract|alias|and|args|as|assert|begin|break|case|catch|class|clone|continue|declare|def|default|del|delete|do|dynamic|elif|else|elseif|elsif|end|enddeclare|endfor|endforeach|endif|endswitch|endwhile|ensure|except|exec|finally|float|for|foreach|function|global|goto|if|implements|import|in|inline|instanceof|interface|is|lambda|module|native|new|next|nil|not|or|pass|public|print|private|protected|raise|redo|rescue|retry|register|return|self|sizeof|static|super|switch|synchronized|then|this|throw|transient|try|undef|unless|unsigned|until|use|var|virtual|volatile|when|while|with|xor|yield)\b`, KeywordReserved, nil},
},
"numbers": {
{`[+-]?(\d+\.\d+([eE][+-]?\d+)?|\.?\d+[eE][+-]?\d+)`, LiteralNumberFloat, nil},
{`[+-]?0x[0-9A-Fa-f]+`, LiteralNumberHex, nil},
{`[+-]?[0-9]+`, LiteralNumberInteger, nil},
},
},
))

View File

@ -17,4 +17,5 @@ var SwapOff = Register(chroma.NewStyle("swapoff", map[chroma.TokenType]string{
chroma.GenericUnderline: "underline",
chroma.NameTag: "bold",
chroma.NameAttribute: "#ansiteal",
chroma.Error: "#ansired",
}))