Skip to content

Instantly share code, notes, and snippets.

@hbagdi

hbagdi/main.go Secret

Created August 4, 2022 04:32
Show Gist options
  • Save hbagdi/9c3a3384b80f396c77348b5d29a93db1 to your computer and use it in GitHub Desktop.
Save hbagdi/9c3a3384b80f396c77348b5d29a93db1 to your computer and use it in GitHub Desktop.
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