Skip to content

Commit

Permalink
Add basic graphql support
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Scarr authored and alecthomas committed Jan 24, 2019
1 parent 3c6b341 commit 4b6319e
Show file tree
Hide file tree
Showing 4 changed files with 259 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 45 additions & 0 deletions lexers/g/graphql.go
Original file line number Diff line number Diff line change
@@ -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 changes: 42 additions & 0 deletions lexers/testdata/graphql.actual
Original file line number Diff line number Diff line change
@@ -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 changes: 171 additions & 0 deletions lexers/testdata/graphql.expected
Original file line number Diff line number Diff line change
@@ -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"}
]

0 comments on commit 4b6319e

Please sign in to comment.