update main file parsing error handling
This commit is contained in:
parent
9055a09e43
commit
9f17543d3e
1 changed files with 18 additions and 2 deletions
20
main.go
20
main.go
|
|
@ -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")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue