Skip to content

Commit

Permalink
fix(parser): error reporting
Browse files Browse the repository at this point in the history
emmy now displays the correct starting position of faulty token as well
as an ^ (arrow) at the position of a single character token while
displaying multiple arrows for the length of multi-character token:

old behaviour:
    $ emmy "1+-test"
    2023/06/19 21:37:55 error: unexpected '-' (MINUS) at position 2

            1+-test
             ^  unexpected token

    2023/06/19 21:37:55 error: unexpected 'test' (IDENTIFIER) at position 3

            1+-test
              ^   unexpected token

    2023/06/19 21:37:55 semantic errors found, no evaluation of ast possible
    = NaN

new behaviour:
    $ emmy "1+-test"
    2023/06/19 21:37:05 error: unexpected '-' (MINUS) at position 2

            1+-test
              ^
              unexpected token

    2023/06/19 21:37:05 error: unexpected 'test' (IDENTIFIER) at position 3

            1+-test
               ^^^^
               unexpected token

    2023/06/19 21:37:05 semantic errors found, no evaluation of ast possible
    -> NaN

The prompt was changed from '=' to '->'
  • Loading branch information
xNaCly committed Jun 19, 2023
1 parent 359f28d commit 4fdf19f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
4 changes: 3 additions & 1 deletion emmy.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func run(in string, s *lexer.Scanner, p *parser.Parser) {
if DEBUG {
fmt.Println(lexer.String(t), parser.String(stmts))
}
fmt.Println("=", p.Eval(stmts))
fmt.Println("->", p.Eval(stmts))
}

func main() {
Expand All @@ -33,6 +33,8 @@ func main() {
}

fmt.Println("Welcome to the emmy repl")
fmt.Println("\nCommand overview:")
fmt.Println(consts.HELP_MSG)
prompt := "ε> "
reader := bufio.NewReader(os.Stdin)

Expand Down
16 changes: 11 additions & 5 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,20 @@ func (p *Parser) error() {
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)
log.Printf("error: unexpected '%v' (%s) at position %d\n", val, consts.KIND_LOOKUP[tk], t.Pos)
}

fmt.Printf("\n\t%s\n\t%s%s%s%s\n\n",
arrows := 1

if len(t.Raw) != 0 {
arrows = len(t.Raw)
}

fmt.Printf("\n\t%s\n\t%s%s\n\t%s%s\n\n",
p.inputString,
strings.Repeat(" ", p.pos),
"^",
strings.Repeat(" ", p.pos),
strings.Repeat(" ", t.Pos),
strings.Repeat("^", arrows),
strings.Repeat(" ", t.Pos),
"unexpected token",
)
}
Expand Down
3 changes: 1 addition & 2 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
func testHelper(t *testing.T, m map[string]any, matcher func(k string, v any) (bool, any)) {
for k, v := range m {
ok, val := matcher(k, v)
// t.Logf("PASSED: [wanted=%v;got=%v;key=%s]", v, val, k)
if !ok {
t.Errorf("wanted %v: got %v, for %s", v, val, k)
}
Expand Down Expand Up @@ -83,7 +82,7 @@ func TestParserPemdas(t *testing.T) {
// examples taken from https://pemdas.info/
tests := map[string]any{
"1+2*3": 7.0,
// TODO: this is not correct!, emmy returns 7, not 8, somehow +3/3 is not included in the resulting ast
// BUG: this is not correct!, emmy returns 7, not 8, somehow +3/3 is not included in the resulting ast
// "7-1*0+3/3": 8.0,
"1-2*3*4": -23.0,
"3+4/2-4": 1.0,
Expand Down

0 comments on commit 4fdf19f

Please sign in to comment.