-
-
Save hbagdi/9c3a3384b80f396c77348b5d29a93db1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"github.com/alecthomas/participle/v2" | |
"github.com/alecthomas/participle/v2/lexer" | |
"github.com/alecthomas/repr" | |
) | |
var hitLexer = lexer.MustSimple([]lexer.SimpleRule{ | |
{"comment", `[#][^\n]*`}, | |
{"Ident", `[a-zA-Z_]\w*`}, | |
{"Path", `/.*`}, | |
{"Punct", `[[!@#~$%^&*()+_={}\|:;"'<,>.?/]|]`}, | |
{"Whitespace", `[\t]+`}, | |
{"NL", `\n`}, | |
{"SingleSpace", `[ ]{1}`}, | |
}) | |
type File struct { | |
Requests []*Requests `@@*` | |
} | |
type Requests struct { | |
Id string `"@"@Ident` | |
NL string `| @NL` | |
Method string `@Ident` | |
Space string `@SingleSpace` | |
Path string `@Path` | |
NL1 string `@NL` | |
Start string `"~"` | |
// Body includes new lines | |
Body string `(@Ident@NL@SingleSpace)*` | |
End string `"~"` | |
NL2 string `| @NL+` | |
} | |
var parser = participle.MustBuild(&File{}, | |
participle.Lexer(hitLexer), | |
) | |
var input = `# comment | |
@c1 | |
GET /foo | |
~ | |
foobar | |
body | |
~ | |
` | |
func main() { | |
hitFile := &File{} | |
err := parser.ParseString("", input, hitFile) | |
if err != nil { | |
panic(err) | |
} | |
repr.Println(hitFile, repr.Indent(" "), repr.OmitEmpty(true)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment