Skip to content

Commit

Permalink
change os.exit to panic and recover (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
agnieszka-miszkurka authored Mar 28, 2019
1 parent 20195d8 commit 6a82235
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package parser

import (
"fmt"
"os"
"strconv"
"strings"

"github.com/radlinskii/interpreter/ast"
"github.com/radlinskii/interpreter/lexer"
Expand Down Expand Up @@ -140,19 +140,29 @@ func (p *Parser) ParseProgram() *ast.Program {
// checkIfIllegal kills the parser if illegal character was found.
func (p *Parser) checkIfIllegal() {
if p.curToken.Type == token.ILLEGAL {
var errMsg string
if p.curToken.Literal == "\x00" {
fmt.Printf("FATAL ERROR: comment not terminated at line: %d\n\n", p.curToken.LineNumber)
errMsg = fmt.Sprintf("FATAL ERROR: comment not terminated at line: %d\n\n", p.curToken.LineNumber)

} else {
fmt.Printf("FATAL ERROR: illegal character: %q at line: %d\n\n", p.curToken.Literal, p.curToken.LineNumber)
errMsg = fmt.Sprintf("FATAL ERROR: illegal character: %q at line: %d\n\n", p.curToken.Literal, p.curToken.LineNumber)
}

os.Exit(1)
defer func() {
if err := recover(); err != nil {
p.errors = append(p.errors, errMsg)
}
}()
panic(errMsg)
}
}

func (p *Parser) printErrors() {
if len(p.errors) != 0 {
for _, msg := range p.Errors() {
if strings.HasPrefix(msg, "FATAL") {
fmt.Println(msg)
break
}
fmt.Println("ERROR: " + msg)
}
fmt.Println("")
Expand Down

0 comments on commit 6a82235

Please sign in to comment.