1
0
mirror of https://github.com/alecthomas/chroma.git synced 2025-02-09 13:23:51 +02:00

Add basic graphql support

This commit is contained in:
Adam Scarr 2019-01-14 17:10:37 +11:00 committed by Alec Thomas
parent 3c6b341f5a
commit 4b6319e2ab
4 changed files with 259 additions and 1 deletions

View File

@ -37,7 +37,7 @@ C | C, C#, C++, Cassandra CQL, CFEngine3, cfstatement/ColdFusion, CMake, COBOL,
D | Dart, Diff, Django/Jinja, Docker, DTD
E | EBNF, Elixir, Elm, EmacsLisp, Erlang
F | Factor, Fish, Forth, Fortran, FSharp
G | GAS, GDScript, GLSL, Genshi, Genshi HTML, Genshi Text, Gnuplot, Go, Go HTML Template, Go Text Template, Groovy
G | GAS, GDScript, GLSL, Genshi, Genshi HTML, Genshi Text, Gnuplot, Go, Go HTML Template, Go Text Template, GraphQL, Groovy
H | Handlebars, Haskell, Haxe, Hexdump, HTML, HTTP, Hy
I | Idris, INI, Io
J | Java, JavaScript, JSON, Jsx, Julia, Jungle

45
lexers/g/graphql.go Normal file
View File

@ -0,0 +1,45 @@
package g
import (
. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)
// Go lexer.
var Graphql = internal.Register(MustNewLexer(
&Config{
Name: "GraphQL",
Aliases: []string{"graphql", "graphqls", "gql"},
Filenames: []string{"*.graphql", "*.graphqls"},
},
Rules{
"root": {
{`(query|mutation|subscription|fragment|scalar|implements|interface|union|enum|input|type)`, KeywordDeclaration, Push("type")},
{`(on|extend|schema|directive|\.\.\.)`, KeywordDeclaration, nil},
{`(QUERY|MUTATION|SUBSCRIPTION|FIELD|FRAGMENT_DEFINITION|FRAGMENT_SPREAD|INLINE_FRAGMENT|SCHEMA|SCALAR|OBJECT|FIELD_DEFINITION|ARGUMENT_DEFINITION|INTERFACE|UNION|ENUM|ENUM_VALUE|INPUT_OBJECT|INPUT_FIELD_DEFINITION)\b`, KeywordConstant, nil},
{`[^\W\d]\w*`, NameProperty, nil},
{`\@\w+`, NameDecorator, nil},
{`:`, Punctuation, Push("type")},
{`[\(\)\{\}\[\],!\|=]`, Punctuation, nil},
{`\$\w+`, NameVariable, nil},
{`\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][+\-]?\d+|\.\d*|[eE][+\-]?\d+)`, LiteralNumberFloat, nil},
{`\.\d+([eE][+\-]?\d+)?`, LiteralNumberFloat, nil},
{`(0|[1-9][0-9]*)`, LiteralNumberInteger, nil},
{`"""[\x00-\x7F]*?"""`, LiteralString, nil},
{`"(\\["\\abfnrtv]|\\x[0-9a-fA-F]{2}|\\[0-7]{1,3}|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8}|[^\\])"`, LiteralStringChar, nil},
{`"(\\\\|\\"|[^"])*"`, LiteralString, nil},
{`"(true|false|null)*"`, Literal, nil},
{`[\r\n\s]+`, Whitespace, nil},
{`#[^\r\n]*`, Comment, nil},
},
// Treats the next word as a class, default rules it would be a property
"type": {
{`[^\W\d]\w*`, NameClass, Pop(1)},
Include("root"),
},
},
))

42
lexers/testdata/graphql.actual vendored Normal file
View File

@ -0,0 +1,42 @@
query Hero($episode: Episode, $withFriends: Boolean!) {
hero(episode: $episode) {
name
friends @include(if: $withFriends) {
name
...frag
... {
inlineFrag
}
}
}
}
fragment frag on User {
id
name
profilePic(size: 50)
}
# Switching to schema
"Description for the type"
type MyObjectType {
"""
Description for field
Supports **multi-line** description for your [API](http://example.com)!
"""
myField: String! @deprecated(reason: "Use `newField`.")
otherField(
"Description for argument"
arg: Int
)
}
input Foo {
bar [String!]! = ["baz"]
}
directive @deprecated(
reason: String = "No longer supported"
) on FIELD_DEFINITION | ENUM_VALUE

171
lexers/testdata/graphql.expected vendored Normal file
View File

@ -0,0 +1,171 @@
[
{"type":"KeywordDeclaration","value":"query"},
{"type":"TextWhitespace","value":" "},
{"type":"NameClass","value":"Hero"},
{"type":"Punctuation","value":"("},
{"type":"NameVariable","value":"$episode"},
{"type":"Punctuation","value":":"},
{"type":"TextWhitespace","value":" "},
{"type":"NameClass","value":"Episode"},
{"type":"Punctuation","value":","},
{"type":"TextWhitespace","value":" "},
{"type":"NameVariable","value":"$withFriends"},
{"type":"Punctuation","value":":"},
{"type":"TextWhitespace","value":" "},
{"type":"NameClass","value":"Boolean"},
{"type":"Punctuation","value":"!)"},
{"type":"TextWhitespace","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"TextWhitespace","value":"\n "},
{"type":"NameProperty","value":"hero"},
{"type":"Punctuation","value":"("},
{"type":"NameProperty","value":"episode"},
{"type":"Punctuation","value":":"},
{"type":"TextWhitespace","value":" "},
{"type":"NameVariable","value":"$episode"},
{"type":"Punctuation","value":")"},
{"type":"TextWhitespace","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"TextWhitespace","value":"\n "},
{"type":"NameClass","value":"name"},
{"type":"TextWhitespace","value":"\n "},
{"type":"NameProperty","value":"friends"},
{"type":"TextWhitespace","value":" "},
{"type":"NameDecorator","value":"@include"},
{"type":"Punctuation","value":"("},
{"type":"NameProperty","value":"if"},
{"type":"Punctuation","value":":"},
{"type":"TextWhitespace","value":" "},
{"type":"NameVariable","value":"$withFriends"},
{"type":"Punctuation","value":")"},
{"type":"TextWhitespace","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"TextWhitespace","value":"\n "},
{"type":"NameClass","value":"name"},
{"type":"TextWhitespace","value":"\n "},
{"type":"KeywordDeclaration","value":"..."},
{"type":"NameProperty","value":"frag"},
{"type":"TextWhitespace","value":"\n\n "},
{"type":"KeywordDeclaration","value":"..."},
{"type":"TextWhitespace","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"TextWhitespace","value":"\n "},
{"type":"NameProperty","value":"inlineFrag"},
{"type":"TextWhitespace","value":"\n "},
{"type":"Punctuation","value":"}"},
{"type":"TextWhitespace","value":"\n "},
{"type":"Punctuation","value":"}"},
{"type":"TextWhitespace","value":"\n "},
{"type":"Punctuation","value":"}"},
{"type":"TextWhitespace","value":"\n"},
{"type":"Punctuation","value":"}"},
{"type":"TextWhitespace","value":"\n\n"},
{"type":"KeywordDeclaration","value":"fragment"},
{"type":"TextWhitespace","value":" "},
{"type":"NameClass","value":"frag"},
{"type":"TextWhitespace","value":" "},
{"type":"KeywordDeclaration","value":"on"},
{"type":"TextWhitespace","value":" "},
{"type":"NameProperty","value":"User"},
{"type":"TextWhitespace","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"TextWhitespace","value":"\n "},
{"type":"NameProperty","value":"id"},
{"type":"TextWhitespace","value":"\n "},
{"type":"NameProperty","value":"name"},
{"type":"TextWhitespace","value":"\n "},
{"type":"NameProperty","value":"profilePic"},
{"type":"Punctuation","value":"("},
{"type":"NameProperty","value":"size"},
{"type":"Punctuation","value":":"},
{"type":"TextWhitespace","value":" "},
{"type":"NameClass","value":"50"},
{"type":"Punctuation","value":")"},
{"type":"TextWhitespace","value":"\n"},
{"type":"Punctuation","value":"}"},
{"type":"TextWhitespace","value":"\n\n"},
{"type":"Comment","value":"# Switching to schema"},
{"type":"TextWhitespace","value":"\n"},
{"type":"LiteralString","value":"\"Description for the type\""},
{"type":"TextWhitespace","value":"\n"},
{"type":"KeywordDeclaration","value":"type"},
{"type":"TextWhitespace","value":" "},
{"type":"NameClass","value":"MyObjectType"},
{"type":"TextWhitespace","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"TextWhitespace","value":"\n "},
{"type":"LiteralString","value":"\"\"\"\n Description for field\n Supports **multi-line** description for your [API](http://example.com)!\n \"\"\""},
{"type":"TextWhitespace","value":"\n "},
{"type":"NameProperty","value":"myField"},
{"type":"Punctuation","value":":"},
{"type":"TextWhitespace","value":" "},
{"type":"NameClass","value":"String"},
{"type":"Punctuation","value":"!"},
{"type":"TextWhitespace","value":" "},
{"type":"NameDecorator","value":"@deprecated"},
{"type":"Punctuation","value":"("},
{"type":"NameProperty","value":"reason"},
{"type":"Punctuation","value":":"},
{"type":"TextWhitespace","value":" "},
{"type":"LiteralString","value":"\"Use `newField`.\""},
{"type":"Punctuation","value":")"},
{"type":"TextWhitespace","value":"\n\n "},
{"type":"NameClass","value":"otherField"},
{"type":"Punctuation","value":"("},
{"type":"TextWhitespace","value":"\n "},
{"type":"LiteralString","value":"\"Description for argument\""},
{"type":"TextWhitespace","value":"\n "},
{"type":"NameProperty","value":"arg"},
{"type":"Punctuation","value":":"},
{"type":"TextWhitespace","value":" "},
{"type":"NameClass","value":"Int"},
{"type":"TextWhitespace","value":"\n "},
{"type":"Punctuation","value":")"},
{"type":"TextWhitespace","value":"\n"},
{"type":"Punctuation","value":"}"},
{"type":"TextWhitespace","value":"\n\n"},
{"type":"KeywordDeclaration","value":"input"},
{"type":"TextWhitespace","value":" "},
{"type":"NameClass","value":"Foo"},
{"type":"TextWhitespace","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"TextWhitespace","value":"\n "},
{"type":"NameProperty","value":"bar"},
{"type":"TextWhitespace","value":" "},
{"type":"Punctuation","value":"["},
{"type":"NameProperty","value":"String"},
{"type":"Punctuation","value":"!]!"},
{"type":"TextWhitespace","value":" "},
{"type":"Punctuation","value":"="},
{"type":"TextWhitespace","value":" "},
{"type":"Punctuation","value":"["},
{"type":"LiteralString","value":"\"baz\""},
{"type":"Punctuation","value":"]"},
{"type":"TextWhitespace","value":"\n"},
{"type":"Punctuation","value":"}"},
{"type":"TextWhitespace","value":"\n\n"},
{"type":"KeywordDeclaration","value":"directive"},
{"type":"TextWhitespace","value":" "},
{"type":"NameDecorator","value":"@deprecated"},
{"type":"Punctuation","value":"("},
{"type":"TextWhitespace","value":"\n "},
{"type":"NameProperty","value":"reason"},
{"type":"Punctuation","value":":"},
{"type":"TextWhitespace","value":" "},
{"type":"NameClass","value":"String"},
{"type":"TextWhitespace","value":" "},
{"type":"Punctuation","value":"="},
{"type":"TextWhitespace","value":" "},
{"type":"LiteralString","value":"\"No longer supported\""},
{"type":"TextWhitespace","value":"\n"},
{"type":"Punctuation","value":")"},
{"type":"TextWhitespace","value":" "},
{"type":"KeywordDeclaration","value":"on"},
{"type":"TextWhitespace","value":" "},
{"type":"KeywordConstant","value":"FIELD_DEFINITION"},
{"type":"TextWhitespace","value":" "},
{"type":"Punctuation","value":"|"},
{"type":"TextWhitespace","value":" "},
{"type":"KeywordConstant","value":"ENUM_VALUE"},
{"type":"TextWhitespace","value":"\n"}
]