scanner: Fixed decimal number parser; it didn't save parenthesis around period part
This commit is contained in:
parent
8eb25bbc86
commit
e7e9330b71
@ -404,6 +404,7 @@ func (self *scanner) parseNumber(firstCh byte) (tk *Token) {
|
|||||||
}
|
}
|
||||||
} else if ch == '(' {
|
} else if ch == '(' {
|
||||||
sym = SymFraction
|
sym = SymFraction
|
||||||
|
sb.WriteByte(ch)
|
||||||
ch, err = self.readChar()
|
ch, err = self.readChar()
|
||||||
for ; err == nil && (ch >= '0' && ch <= '9'); ch, err = self.readChar() {
|
for ; err == nil && (ch >= '0' && ch <= '9'); ch, err = self.readChar() {
|
||||||
sb.WriteByte(ch)
|
sb.WriteByte(ch)
|
||||||
@ -412,6 +413,7 @@ func (self *scanner) parseNumber(firstCh byte) (tk *Token) {
|
|||||||
if ch != ')' {
|
if ch != ')' {
|
||||||
err = fmt.Errorf("[%d:%d] expected ')', got '%c'", self.row, self.column, ch)
|
err = fmt.Errorf("[%d:%d] expected ')', got '%c'", self.row, self.column, ch)
|
||||||
} else {
|
} else {
|
||||||
|
sb.WriteByte(ch)
|
||||||
_, err = self.readChar()
|
_, err = self.readChar()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,7 @@ package expr
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
@ -55,15 +56,18 @@ func TestScanner(t *testing.T) {
|
|||||||
/* 33 */ {`(`, SymOpenRound, nil, nil},
|
/* 33 */ {`(`, SymOpenRound, nil, nil},
|
||||||
/* 34 */ {`)`, SymClosedRound, nil, nil},
|
/* 34 */ {`)`, SymClosedRound, nil, nil},
|
||||||
/* 35 */ {`1E+2`, SymFloat, float64(100), nil},
|
/* 35 */ {`1E+2`, SymFloat, float64(100), nil},
|
||||||
/* 36 */ {`1E+x`, SymError, errors.New("expected integer exponent"), nil},
|
/* 36 */ {`1E+x`, SymError, errors.New("[1:5] expected integer exponent, got x"), nil},
|
||||||
/* 37 */ {`$`, SymDollar, nil, nil},
|
/* 37 */ {`$`, SymDollar, nil, nil},
|
||||||
/* 38 */ {`\`, SymError, errors.New("incomplete escape sequence"), nil},
|
/* 38 */ {`\`, SymError, errors.New("incomplete escape sequence"), nil},
|
||||||
/* 39 */ {`"string"`, SymString, "string", nil},
|
/* 39 */ {`"string"`, SymString, "string", nil},
|
||||||
/* 39 */ {`identifier`, SymIdentifier, "identifier", nil},
|
/* 40 */ {`identifier`, SymIdentifier, "identifier", nil},
|
||||||
|
/* 41 */ {`1.2(3)`, SymFraction, newFraction(37, 30), nil},
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, input := range inputs {
|
for i, input := range inputs {
|
||||||
|
// if i != 40 {
|
||||||
|
// continue
|
||||||
|
// }
|
||||||
if input.wantErr == nil {
|
if input.wantErr == nil {
|
||||||
t.Log(fmt.Sprintf("[+]Test nr %2d -- %q", i+1, input.source))
|
t.Log(fmt.Sprintf("[+]Test nr %2d -- %q", i+1, input.source))
|
||||||
} else {
|
} else {
|
||||||
@ -75,7 +79,8 @@ func TestScanner(t *testing.T) {
|
|||||||
|
|
||||||
if tk := scanner.Next(); tk == nil {
|
if tk := scanner.Next(); tk == nil {
|
||||||
t.Errorf("%d: %q -> got = (nil), want %v (value %v [%T])", i+1, input.source, input.wantSym, input.wantValue, input.wantValue)
|
t.Errorf("%d: %q -> got = (nil), want %v (value %v [%T])", i+1, input.source, input.wantSym, input.wantValue, input.wantValue)
|
||||||
} else if tk.Sym != input.wantSym || tk.Value != input.wantValue {
|
// } else if tk.Sym != input.wantSym || tk.Value != input.wantValue {
|
||||||
|
} else if tk.Sym != input.wantSym || !reflect.DeepEqual(tk.Value, input.wantValue) {
|
||||||
if tk.Sym == SymError && input.wantSym == tk.Sym {
|
if tk.Sym == SymError && input.wantSym == tk.Sym {
|
||||||
if tkErr, tkOk := tk.Value.(error); tkOk {
|
if tkErr, tkOk := tk.Value.(error); tkOk {
|
||||||
if inputErr, inputOk := input.wantValue.(error); inputOk {
|
if inputErr, inputOk := input.wantValue.(error); inputOk {
|
||||||
@ -86,7 +91,7 @@ func TestScanner(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
t.Errorf("%d: %q -> got = %v (value=%v [%T), want %v (value=%v [%T)", i+1,
|
t.Errorf("%d: %q -> got = %v (value=%v [%T]), want %v (value=%v [%T])", i+1,
|
||||||
input.source, tk.Sym, tk.Value, tk.Value, input.wantSym, input.wantValue, input.wantValue)
|
input.source, tk.Sym, tk.Value, tk.Value, input.wantSym, input.wantValue, input.wantValue)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user