diff --git a/scanner/scanner.go b/scanner/scanner.go index b5c3679..cf558e4 100644 --- a/scanner/scanner.go +++ b/scanner/scanner.go @@ -189,7 +189,12 @@ func (s *Scanner) Scan() token.Token { case '+': tok = token.ADD case '-': - tok = token.SUB + if isDecimal(s.peek()) { + ch := s.next() + tok = s.scanNumber(ch) + } else { + tok = token.SUB + } default: s.err("illegal char") } diff --git a/scanner/scanner_test.go b/scanner/scanner_test.go index 56ae526..6e2f7ab 100644 --- a/scanner/scanner_test.go +++ b/scanner/scanner_test.go @@ -52,7 +52,7 @@ var tokenLists = map[string][]tokenPair{ {token.BOOL, "true"}, {token.BOOL, "false"}, }, - "identoken.t": []tokenPair{ + "ident": []tokenPair{ {token.IDENT, "a"}, {token.IDENT, "a0"}, {token.IDENT, "foobar"}, @@ -70,7 +70,7 @@ var tokenLists = map[string][]tokenPair{ {token.IDENT, "foo६४"}, {token.IDENT, "bar9876"}, }, - "stritoken.ng": []tokenPair{ + "string": []tokenPair{ {token.STRING, `" "`}, {token.STRING, `"a"`}, {token.STRING, `"本"`}, @@ -92,7 +92,7 @@ var tokenLists = map[string][]tokenPair{ {token.STRING, `"\U0000ffAB"`}, {token.STRING, `"` + f100 + `"`}, }, - "numbtoken.er": []tokenPair{ + "number": []tokenPair{ {token.NUMBER, "0"}, {token.NUMBER, "1"}, {token.NUMBER, "9"}, @@ -131,8 +131,46 @@ var tokenLists = map[string][]tokenPair{ {token.NUMBER, "1E-10"}, {token.NUMBER, "42E+10"}, {token.NUMBER, "01234567890E-10"}, + {token.NUMBER, "-0"}, + {token.NUMBER, "-1"}, + {token.NUMBER, "-9"}, + {token.NUMBER, "-42"}, + {token.NUMBER, "-1234567890"}, + {token.NUMBER, "-00"}, + {token.NUMBER, "-01"}, + {token.NUMBER, "-07"}, + {token.NUMBER, "-042"}, + {token.NUMBER, "-01234567"}, + {token.NUMBER, "-0x0"}, + {token.NUMBER, "-0x1"}, + {token.NUMBER, "-0xf"}, + {token.NUMBER, "-0x42"}, + {token.NUMBER, "-0x123456789abcDEF"}, + {token.NUMBER, "-0x" + f100}, + {token.NUMBER, "-0X0"}, + {token.NUMBER, "-0X1"}, + {token.NUMBER, "-0XF"}, + {token.NUMBER, "-0X42"}, + {token.NUMBER, "-0X123456789abcDEF"}, + {token.NUMBER, "-0X" + f100}, + {token.NUMBER, "-0e0"}, + {token.NUMBER, "-1e0"}, + {token.NUMBER, "-42e0"}, + {token.NUMBER, "-01234567890e0"}, + {token.NUMBER, "-0E0"}, + {token.NUMBER, "-1E0"}, + {token.NUMBER, "-42E0"}, + {token.NUMBER, "-01234567890E0"}, + {token.NUMBER, "-0e+10"}, + {token.NUMBER, "-1e-10"}, + {token.NUMBER, "-42e+10"}, + {token.NUMBER, "-01234567890e-10"}, + {token.NUMBER, "-0E+10"}, + {token.NUMBER, "-1E-10"}, + {token.NUMBER, "-42E+10"}, + {token.NUMBER, "-01234567890E-10"}, }, - "floatoken.t": []tokenPair{ + "float": []tokenPair{ {token.FLOAT, "0."}, {token.FLOAT, "1."}, {token.FLOAT, "42."}, @@ -161,6 +199,26 @@ var tokenLists = map[string][]tokenPair{ {token.FLOAT, "1.1E-10"}, {token.FLOAT, "42.1E+10"}, {token.FLOAT, "01234567890.1E-10"}, + {token.FLOAT, "-0.0"}, + {token.FLOAT, "-1.0"}, + {token.FLOAT, "-42.0"}, + {token.FLOAT, "-01234567890.0"}, + {token.FLOAT, "-01.8e0"}, + {token.FLOAT, "-1.4e0"}, + {token.FLOAT, "-42.2e0"}, + {token.FLOAT, "-01234567890.12e0"}, + {token.FLOAT, "-0.E0"}, + {token.FLOAT, "-1.12E0"}, + {token.FLOAT, "-42.123E0"}, + {token.FLOAT, "-01234567890.213E0"}, + {token.FLOAT, "-0.2e+10"}, + {token.FLOAT, "-1.2e-10"}, + {token.FLOAT, "-42.54e+10"}, + {token.FLOAT, "-01234567890.98e-10"}, + {token.FLOAT, "-0.1E+10"}, + {token.FLOAT, "-1.1E-10"}, + {token.FLOAT, "-42.1E+10"}, + {token.FLOAT, "-01234567890.1E-10"}, }, }