Instantly share code, notes, and snippets.

@alecthomas /output.go Secret
Created Sep 9, 2018

Embed
What would you like to do?
&main.Script{
Command: []*main.Command{
&main.Command{
Name: "open",
Arg: &main.Value{
Var: &"loginPage",
},
},
&main.Command{
Name: "validate",
Arg: &main.Value{
List: []string{
"emailBox",
"passBox",
"loginButton",
},
},
},
&main.Command{
Name: "type",
Arg: &main.Value{
Var: &"emailCred",
},
On: "emailBox",
},
&main.Command{
Name: "type",
Arg: &main.Value{
Var: &"passCred",
},
On: "passwordBox",
},
&main.Command{
Name: "click",
Arg: &main.Value{
Var: &"loginButton",
},
},
&main.Command{
Name: "letLoad",
Arg: &main.Value{
Var: &"validate",
},
},
&main.Command{
Name: "leftNav",
Arg: &main.Value{
Var: &"validate",
},
},
&main.Command{
Name: "topNav",
Arg: &main.Value{
Var: &"end",
},
},
},
}
package main
import (
"github.com/alecthomas/participle"
"github.com/alecthomas/repr"
)
const source = `
open loginPage
validate [emailBox, passBox, loginButton]
type emailCred on emailBox
type passCred on passwordBox
click loginButton
letLoad
validate leftNav
validate topNav
end
`
type Script struct {
Command []*Command `{ @@ }`
}
type Command struct {
Name string `@Ident`
Arg *Value `[ @@`
On string ` [ "on" @Ident ] ]`
}
type Value struct {
Var *string ` @Ident`
List []string `| "[" [ @Ident { "," @Ident } ] "]"`
}
func main() {
parser := participle.MustBuild(&Script{})
ast := &Script{}
err := parser.ParseString(source, ast)
if err != nil {
panic(err)
}
repr.Println(ast)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment