Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test coverage for generated lexer #269

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
SHELL=/bin/bash -o pipefail

refresh-codegen: codegen-lexer cleanup-internal-participle-cmd

internal-participle-cmd:
@echo Building private build of participle utility...
cd cmd/participle && go build -o ../../participle github.com/alecthomas/participle/v2/cmd/participle

internal-generateinternal:
@echo Updating internal test files...
cd lexer/internal/participlegeninternal && go build -o ../../../participlegeninternal github.com/alecthomas/participle/v2/lexer/internal/participlegeninternal
./participlegeninternal

codegen-lexer: internal-participle-cmd internal-generateinternal
@echo Regenerating lexers...
(./participle gen lexer --name GeneratedA internal < lexer/internal/alexer.json | gofmt > lexer/internal/alexer.go) || rm -f lexer/internal/alexer.go
(./participle gen lexer --name GeneratedBasic internal < lexer/internal/basiclexer.json | gofmt > lexer/internal/basiclexer.go) || rm -f lexer/internal/basiclexer.go
(./participle gen lexer --name GeneratedHeredoc internal < lexer/internal/heredoclexer.json | gofmt > lexer/internal/heredoclexer.go) || rm -f lexer/internal/heredoclexer.go
(./participle gen lexer --name GeneratedHeredocWithWhitespace internal < lexer/internal/heredocwithwhitespacelexer.json | gofmt > lexer/internal/heredocwithwhitespacelexer.go) || rm -f lexer/internal/heredocwithwhitespacelexer.go
(./participle gen lexer --name GeneratedInterpolated internal < lexer/internal/interpolatedlexer.json | gofmt > lexer/internal/interpolatedlexer.go) || rm -f lexer/internal/interpolatedlexer.go
(./participle gen lexer --name GeneratedInterpolatedWithWhitespace internal < lexer/internal/interpolatedwithwhitespacelexer.json | gofmt > lexer/internal/interpolatedwithwhitespacelexer.go) || rm -f lexer/internal/interpolatedwithwhitespacelexer.go
(./participle gen lexer --name GeneratedReference internal < lexer/internal/referencelexer.json | gofmt > lexer/internal/referencelexer.go) || rm -f lexer/internal/referencelexer.go

cleanup-internal-participle-cmd:
@echo Cleaning up...
rm -f ./participle
rm -f ./participlegeninternal
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,8 @@ lexing. Use `participle/experimental/codegen.GenerateLexer()` to compile a
This will generally provide around a 10x improvement in lexing performance
while producing O(1) garbage.

See Makefile for an example how to do lexer codegen.

## Options

The Parser's behaviour can be configured via [Options](https://pkg.go.dev/github.com/alecthomas/participle/v2#Option).
Expand Down
1 change: 1 addition & 0 deletions cmd/participle/gen_lexer_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ import (
)

var _ syntax.Op
var _ = utf8.DecodeRune

var {{.Name}}Lexer lexer.Definition = lexer{{.Name}}DefinitionImpl{}

Expand Down
206 changes: 206 additions & 0 deletions lexer/internal/alexer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions lexer/internal/alexer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"Root": [
{
"name": "A",
"pattern": "a"
},
{
"name": "Ident",
"pattern": "\\w+"
},
{
"name": "whitespace",
"pattern": "\\s+"
}
]
}
1 change: 1 addition & 0 deletions lexer/internal/basiclexer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions lexer/internal/heredoclexer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"Common": [
{
"name": "whitespace",
"pattern": "\\s+"
},
{
"name": "Ident",
"pattern": "\\w+"
}
],
"Heredoc": [
{
"name": "End",
"pattern": "\\b\\1\\b",
"action": {
"kind": "pop"
}
},
{
"name": "whitespace",
"pattern": "\\s+"
},
{
"name": "Ident",
"pattern": "\\w+"
}
],
"Root": [
{
"name": "Heredoc",
"pattern": "\u003c\u003c(\\w+\\b)",
"action": {
"kind": "push",
"state": "Heredoc"
}
},
{
"name": "whitespace",
"pattern": "\\s+"
},
{
"name": "Ident",
"pattern": "\\w+"
}
]
}
47 changes: 47 additions & 0 deletions lexer/internal/heredocwithwhitespacelexer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"Common": [
{
"name": "Whitespace",
"pattern": "\\s+"
},
{
"name": "Ident",
"pattern": "\\w+"
}
],
"Heredoc": [
{
"name": "End",
"pattern": "\\b\\1\\b",
"action": {
"kind": "pop"
}
},
{
"name": "Whitespace",
"pattern": "\\s+"
},
{
"name": "Ident",
"pattern": "\\w+"
}
],
"Root": [
{
"name": "Heredoc",
"pattern": "\u003c\u003c(\\w+\\b)",
"action": {
"kind": "push",
"state": "Heredoc"
}
},
{
"name": "Whitespace",
"pattern": "\\s+"
},
{
"name": "Ident",
"pattern": "\\w+"
}
]
}
Loading