update main file parsing error handling

This commit is contained in:
Neemek 2024-10-09 09:15:43 +02:00
parent 9055a09e43
commit 9f17543d3e
Signed by: neemek
GPG key ID: 28360A8951CD0E9B

20
main.go
View file

@ -44,17 +44,25 @@ func (cmd *RunCmd) Run(ctx *Context) error {
log.Fatal(err) log.Fatal(err)
} }
if len(tokens) <= 1 {
log.Fatal("Empty file")
}
if ctx.Debug { if ctx.Debug {
log.Println("Initialized parser") log.Printf("Lexed %d tokens", len(tokens))
} }
p := NewParser(tokens) p := NewParser(tokens)
if ctx.Debug { if ctx.Debug {
log.Println("Parsed tree") log.Println("Initialized parser")
} }
defer func() { defer func() {
if r := recover(); r != nil { if r := recover(); r != nil {
if r != "no more tokens" { // if the panic was not caused by the parser, it should not be recovered.
panic(r)
}
for _, e := range p.errors { for _, e := range p.errors {
e.Print(src) e.Print(src)
} }
@ -63,6 +71,14 @@ func (cmd *RunCmd) Run(ctx *Context) error {
tree := p.Parse() tree := p.Parse()
// if there were parsing errors, print them out
if len(p.errors) > 0 {
for _, e := range p.errors {
e.Print(src)
}
log.Fatal("Parsing had errors")
}
if ctx.Debug { if ctx.Debug {
log.Println("Initialized compiler") log.Println("Initialized compiler")
} }