70 lines
1.6 KiB
Go
70 lines
1.6 KiB
Go
//go:generate gocc -a azalea.bnf
|
|
package azalea
|
|
|
|
import (
|
|
"io"
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"hakurei.app/container/check"
|
|
)
|
|
|
|
type Parser struct {
|
|
Generator
|
|
}
|
|
|
|
func NewParser(gen Generator) *Parser {
|
|
return &Parser{
|
|
Generator: gen,
|
|
}
|
|
}
|
|
func (p Parser) Initialise() {
|
|
|
|
}
|
|
|
|
func (p Parser) Consume(ns string, file io.Reader) error {
|
|
return nil
|
|
}
|
|
|
|
// ConsumeDir walks a directory and consumes all Azalea source files within it and all its subdirectories, as long as they end with the .az extension.
|
|
func (p Parser) ConsumeDir(dir *check.Absolute) error {
|
|
ds := dir.String()
|
|
return filepath.WalkDir(ds, func(path string, d fs.DirEntry, err error) (e error) {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if d.IsDir() || !strings.HasSuffix(d.Name(), ".az") {
|
|
return
|
|
}
|
|
rel, e := filepath.Rel(ds, path)
|
|
ns := strings.TrimSuffix(rel, ".az")
|
|
f, e := os.Open(path)
|
|
return p.Consume(ns, f)
|
|
})
|
|
}
|
|
|
|
// ConsumeAll consumes all provided readers as Azalea source code, each given the namespace `r%d` where `%d` is the index of the reader in the provided arguments.
|
|
func (p Parser) ConsumeAll(in ...io.Reader) error {
|
|
for i, r := range in {
|
|
err := p.Consume("r"+strconv.FormatInt(int64(i), 10), r)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ConsumeStrings consumes all provided strings as Azalea source code, each given the namespace `s%d` where `%d` is the index of the string in the provided arugments.
|
|
func (p Parser) ConsumeStrings(in ...string) error {
|
|
for i, s := range in {
|
|
err := p.Consume("s"+strconv.FormatInt(int64(i), 10), strings.NewReader(s))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|