2022-05-12 02:11:05 +00:00
|
|
|
package argh
|
|
|
|
|
2022-05-14 00:58:55 +00:00
|
|
|
import (
|
2022-05-15 00:58:09 +00:00
|
|
|
"fmt"
|
2022-05-14 00:58:55 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
2022-05-15 00:58:09 +00:00
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2022-05-14 00:58:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
tracingEnabled = os.Getenv("ARGH_TRACING") == "enabled"
|
2022-05-15 00:58:09 +00:00
|
|
|
traceLogger *log.Logger
|
2022-05-14 00:58:55 +00:00
|
|
|
)
|
|
|
|
|
2022-05-15 00:58:09 +00:00
|
|
|
func init() {
|
|
|
|
if !tracingEnabled {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
traceLogger = log.New(os.Stderr, "ARGH TRACING: ", 0)
|
|
|
|
}
|
|
|
|
|
2022-05-12 02:11:05 +00:00
|
|
|
type Argh struct {
|
2022-05-14 00:58:55 +00:00
|
|
|
ParseTree *ParseTree `json:"parse_tree"`
|
|
|
|
}
|
|
|
|
|
2022-05-15 00:58:09 +00:00
|
|
|
func (a *Argh) TypedAST() []TypedNode {
|
|
|
|
return a.ParseTree.typedAST()
|
2022-05-14 00:58:55 +00:00
|
|
|
}
|
|
|
|
|
2022-05-15 00:58:09 +00:00
|
|
|
func (a *Argh) AST() []Node {
|
|
|
|
return a.ParseTree.ast()
|
2022-05-14 00:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func tracef(format string, v ...any) {
|
|
|
|
if !tracingEnabled {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-15 00:58:09 +00:00
|
|
|
if _, file, line, ok := runtime.Caller(2); ok {
|
|
|
|
format = fmt.Sprintf("%v:%v ", filepath.Base(file), line) + format
|
|
|
|
}
|
|
|
|
|
|
|
|
traceLogger.Printf(format, v...)
|
2022-05-12 02:11:05 +00:00
|
|
|
}
|