Skip to content

Commit

Permalink
Improve Go lexer to detect functions (NameFunction)
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrosland authored and alecthomas committed Oct 23, 2018
1 parent 01e1883 commit 5a47317
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions lexers/g/go.go
Expand Up @@ -42,6 +42,7 @@ var Go = internal.Register(MustNewLexer(
{"(`)([^`]*)(`)", ByGroups(LiteralString, Using(TypeRemappingLexer(GoTextTemplate, TypeMapping{{Other, LiteralString, nil}})), LiteralString), nil},
{`"(\\\\|\\"|[^"])*"`, LiteralString, nil},
{`(<<=|>>=|<<|>>|<=|>=|&\^=|&\^|\+=|-=|\*=|/=|%=|&=|\|=|&&|\|\||<-|\+\+|--|==|!=|:=|\.\.\.|[+\-*/%&])`, Operator, nil},
{`([a-zA-Z_]\w*)(\s*)(\()`, ByGroups(NameFunction, UsingSelf("root"), Punctuation), nil},
{`[|^<>=!()\[\]{}.,;:]`, Punctuation, nil},
{`[^\W\d]\w*`, NameOther, nil},
},
Expand Down
15 changes: 15 additions & 0 deletions lexers/testdata/go.actual
@@ -0,0 +1,15 @@
package main

import "fmt"

func main() {
fmt.Println("Hello World!")
}

func hello(a int) {
fmt.Println("Hello World!").Hello()

return func() int {
return i
}
}
62 changes: 62 additions & 0 deletions lexers/testdata/go.expected
@@ -0,0 +1,62 @@
[
{"type":"KeywordNamespace","value":"package"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"main"},
{"type":"Text","value":"\n\n"},
{"type":"KeywordNamespace","value":"import"},
{"type":"Text","value":" "},
{"type":"LiteralString","value":"\"fmt\""},
{"type":"Text","value":"\n\n"},
{"type":"KeywordDeclaration","value":"func"},
{"type":"Text","value":" "},
{"type":"NameFunction","value":"main"},
{"type":"Punctuation","value":"()"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":"\n "},
{"type":"NameOther","value":"fmt"},
{"type":"Punctuation","value":"."},
{"type":"NameFunction","value":"Println"},
{"type":"Punctuation","value":"("},
{"type":"LiteralString","value":"\"Hello World!\""},
{"type":"Punctuation","value":")"},
{"type":"Text","value":"\n"},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"\n\n"},
{"type":"KeywordDeclaration","value":"func"},
{"type":"Text","value":" "},
{"type":"NameFunction","value":"hello"},
{"type":"Punctuation","value":"("},
{"type":"NameOther","value":"a"},
{"type":"Text","value":" "},
{"type":"KeywordType","value":"int"},
{"type":"Punctuation","value":")"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":"\n\t"},
{"type":"NameOther","value":"fmt"},
{"type":"Punctuation","value":"."},
{"type":"NameFunction","value":"Println"},
{"type":"Punctuation","value":"("},
{"type":"LiteralString","value":"\"Hello World!\""},
{"type":"Punctuation","value":")."},
{"type":"NameFunction","value":"Hello"},
{"type":"Punctuation","value":"()"},
{"type":"Text","value":"\n\n "},
{"type":"Keyword","value":"return"},
{"type":"Text","value":" "},
{"type":"KeywordDeclaration","value":"func"},
{"type":"Punctuation","value":"()"},
{"type":"Text","value":" "},
{"type":"KeywordType","value":"int"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":"\n "},
{"type":"Keyword","value":"return"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"i"},
{"type":"Text","value":"\n "},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"\n"},
{"type":"Punctuation","value":"}"}
]

0 comments on commit 5a47317

Please sign in to comment.