ucl/cmdlang/ast.go

58 lines
1.3 KiB
Go
Raw Normal View History

2024-04-10 10:45:58 +00:00
package cmdlang
import (
"github.com/alecthomas/participle/v2"
2024-04-12 23:25:16 +00:00
"github.com/alecthomas/participle/v2/lexer"
2024-04-10 10:45:58 +00:00
"io"
)
type astLiteral struct {
Str *string `parser:"@String"`
Ident *string `parser:" | @Ident"`
}
type astCmdArg struct {
2024-04-10 12:19:11 +00:00
Literal *astLiteral `parser:"@@"`
2024-04-12 23:25:16 +00:00
Var *string `parser:"| DOLLAR @Ident"`
Sub *astPipeline `parser:"| LP @@ RP"`
2024-04-10 10:45:58 +00:00
}
type astCmd struct {
Name string `parser:"@Ident"`
Args []astCmdArg `parser:"@@*"`
}
type astPipeline struct {
First *astCmd `parser:"@@"`
2024-04-12 23:25:16 +00:00
Rest []*astCmd `parser:"( PIPE @@ )*"`
}
2024-04-11 12:05:05 +00:00
type astStatements struct {
First *astPipeline `parser:"@@"`
2024-04-12 23:25:16 +00:00
Rest []*astPipeline `parser:"( (SEMICL | NL)+ @@ )*"` // TODO: also add support for newlines
2024-04-11 12:05:05 +00:00
}
2024-04-12 23:25:16 +00:00
type astBlock struct {
Statements *astStatements `parser:"'{' "`
}
var scanner = lexer.MustStateful(lexer.Rules{
"Root": {
{"Whitespace", `[ ]`, nil},
{"NL", `\n\s*`, nil},
{"String", `"(\\"|[^"])*"`, nil},
{"DOLLAR", `\$`, nil},
{"LP", `\(`, nil},
{"RP", `\)`, nil},
{"SEMICL", `;`, nil},
{"PIPE", `\|`, nil},
{"Ident", `\w+`, nil},
},
})
var parser = participle.MustBuild[astStatements](participle.Lexer(scanner),
participle.Elide("Whitespace"))
2024-04-10 10:45:58 +00:00
2024-04-11 12:05:05 +00:00
func parse(r io.Reader) (*astStatements, error) {
2024-04-10 10:45:58 +00:00
return parser.Parse("test", r)
}