Skip to content

Commit

Permalink
fix: error recovery on NewInput call
Browse files Browse the repository at this point in the history
  • Loading branch information
xNaCly committed May 10, 2023
1 parent d62da25 commit 359f28d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
1 change: 1 addition & 0 deletions lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func NewScanner() *Scanner {

// Provides the lexer.Scanner struct with new input
func (s *Scanner) NewInput(input string) *Scanner {
s.hasError = false
s.p = 0
s.in = []rune(input)
if len(input) == 0 {
Expand Down
6 changes: 5 additions & 1 deletion parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func NewParser() *Parser {
}

func (p *Parser) NewInput(in []consts.Token, input string) *Parser {
p.err = false
p.input = in
p.pos = 0
p.inputString = input
Expand Down Expand Up @@ -65,22 +66,24 @@ func (p *Parser) statement() consts.Expression {
return p.binaryStmt()
}

// TODO: needs to unwind to stop the repl
func (p *Parser) error() {
p.err = true
t := p.peek()
tk := t.Kind

var val any
if t.Content != nil {
val = t.Content
} else {
val = consts.TOKEN_LOOKUP[tk]
}

if tk == consts.EOF {
log.Print("error: unexpected end of expression")
} else {
log.Printf("error: unexpected '%v' (%s) at position %d\n", val, consts.KIND_LOOKUP[tk], p.pos)
}

fmt.Printf("\n\t%s\n\t%s%s%s%s\n\n",
p.inputString,
strings.Repeat(" ", p.pos),
Expand Down Expand Up @@ -127,6 +130,7 @@ func (p *Parser) pointStatement() consts.Expression {

func (p *Parser) lineStatement() consts.Expression {
lhs := p.pointStatement()

for p.match(consts.PLUS) {
lhs = consts.PlusExpression{
Lhs: lhs,
Expand Down

0 comments on commit 359f28d

Please sign in to comment.