1
0
treerack/cmd/treerack/check.go

67 lines
1.6 KiB
Go

package main
import (
"code.squareroundforest.org/arpio/treerack"
"errors"
"io"
"os"
)
type checkOptions struct {
// Syntax specifies the filename of the syntax definition file.
Syntax *string
// SyntaxString specifies the syntax as an inline string.
SyntaxString *string
// Input specifies the filename of the input content to be validated.
Input *string
// InputString specifies the input content as an inline string.
InputString *string
// MaxTraceLength enables tracing when set to a positive integer.
MaxTraceLength int
}
// check parses input content against the provided syntax definition and fails if the input does not match.
// Syntax can be provided via a filename option or an inline string option. Input can be provided via a filename
// option, a positional argument filename, an inline string option, or piped from standard input.
func check(o checkOptions, stdin io.Reader, args ...string) error {
syntax, finalizeSyntax, err := initInput(o.Syntax, o.SyntaxString, nil, nil)
if err != nil {
return err
}
defer finalizeSyntax()
input, finalizeInput, err := initInput(o.Input, o.InputString, stdin, args)
if err != nil {
return err
}
defer finalizeInput()
s := &treerack.Syntax{MaxTraceLength: o.MaxTraceLength}
if err := s.ReadSyntax(syntax); err != nil {
if terr := treerack.Trace(os.Stderr, err); terr != nil {
err = errors.Join(err, terr)
}
return err
}
if err := s.Init(); err != nil {
return err
}
if _, err := s.Parse(input); err != nil {
if terr := treerack.Trace(os.Stderr, err); terr != nil {
err = errors.Join(err, terr)
}
return err
}
return nil
}