scanner: initial number lexing

This commit is contained in:
Fatih Arslan 2015-10-04 23:21:34 +03:00
parent 9629512599
commit 5d75c5a143
2 changed files with 63 additions and 45 deletions

View File

@ -112,9 +112,8 @@ func (s *Scanner) Scan() (tok token.Token) {
tok = token.BOOL tok = token.BOOL
} }
case isDigit(ch): case isDecimal(ch):
s.scanNumber() tok = s.scanNumber(ch)
// TODO(arslan)
default: default:
switch ch { switch ch {
case eof: case eof:
@ -129,7 +128,23 @@ func (s *Scanner) Scan() (tok token.Token) {
return tok return tok
} }
func (s *Scanner) scanNumber() { // scanNumber scans a HCL number definition starting with the given rune
func (s *Scanner) scanNumber(ch rune) token.Token {
if ch == '0' {
// check hexadecimal or float
// ch = s.next()
// return token.ILLEGAL
}
s.scanMantissa(ch)
return token.NUMBER
}
func (s *Scanner) scanMantissa(ch rune) {
for isDecimal(ch) {
ch = s.next()
}
s.unread()
} }
// scanString scans a quoted string // scanString scans a quoted string
@ -249,6 +264,10 @@ func isDigit(ch rune) bool {
return '0' <= ch && ch <= '9' || ch >= 0x80 && unicode.IsDigit(ch) return '0' <= ch && ch <= '9' || ch >= 0x80 && unicode.IsDigit(ch)
} }
func isDecimal(ch rune) bool {
return '0' <= ch && ch <= '9'
}
// isWhitespace returns true if the rune is a space, tab, newline or carriage return // isWhitespace returns true if the rune is a space, tab, newline or carriage return
func isWhitespace(ch rune) bool { func isWhitespace(ch rune) bool {
return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r'

View File

@ -100,7 +100,6 @@ func TestString(t *testing.T) {
} }
func TestNumber(t *testing.T) { func TestNumber(t *testing.T) {
t.SkipNow()
var tokenList = []tokenPair{ var tokenList = []tokenPair{
{token.NUMBER, "0"}, {token.NUMBER, "0"},
{token.NUMBER, "1"}, {token.NUMBER, "1"},
@ -112,46 +111,46 @@ func TestNumber(t *testing.T) {
{token.NUMBER, "07"}, {token.NUMBER, "07"},
{token.NUMBER, "042"}, {token.NUMBER, "042"},
{token.NUMBER, "01234567"}, {token.NUMBER, "01234567"},
{token.NUMBER, "0x0"}, // {token.NUMBER, "0x0"},
{token.NUMBER, "0x1"}, // {token.NUMBER, "0x1"},
{token.NUMBER, "0xf"}, // {token.NUMBER, "0xf"},
{token.NUMBER, "0x42"}, // {token.NUMBER, "0x42"},
{token.NUMBER, "0x123456789abcDEF"}, // {token.NUMBER, "0x123456789abcDEF"},
{token.NUMBER, "0x" + f100}, // {token.NUMBER, "0x" + f100},
{token.NUMBER, "0X0"}, // {token.NUMBER, "0X0"},
{token.NUMBER, "0X1"}, // {token.NUMBER, "0X1"},
{token.NUMBER, "0XF"}, // {token.NUMBER, "0XF"},
{token.NUMBER, "0X42"}, // {token.NUMBER, "0X42"},
{token.NUMBER, "0X123456789abcDEF"}, // {token.NUMBER, "0X123456789abcDEF"},
{token.NUMBER, "0X" + f100}, // {token.NUMBER, "0X" + f100},
{token.FLOAT, "0."}, // {token.FLOAT, "0."},
{token.FLOAT, "1."}, // {token.FLOAT, "1."},
{token.FLOAT, "42."}, // {token.FLOAT, "42."},
{token.FLOAT, "01234567890."}, // {token.FLOAT, "01234567890."},
{token.FLOAT, ".0"}, // {token.FLOAT, ".0"},
{token.FLOAT, ".1"}, // {token.FLOAT, ".1"},
{token.FLOAT, ".42"}, // {token.FLOAT, ".42"},
{token.FLOAT, ".0123456789"}, // {token.FLOAT, ".0123456789"},
{token.FLOAT, "0.0"}, // {token.FLOAT, "0.0"},
{token.FLOAT, "1.0"}, // {token.FLOAT, "1.0"},
{token.FLOAT, "42.0"}, // {token.FLOAT, "42.0"},
{token.FLOAT, "01234567890.0"}, // {token.FLOAT, "01234567890.0"},
{token.FLOAT, "0e0"}, // {token.FLOAT, "0e0"},
{token.FLOAT, "1e0"}, // {token.FLOAT, "1e0"},
{token.FLOAT, "42e0"}, // {token.FLOAT, "42e0"},
{token.FLOAT, "01234567890e0"}, // {token.FLOAT, "01234567890e0"},
{token.FLOAT, "0E0"}, // {token.FLOAT, "0E0"},
{token.FLOAT, "1E0"}, // {token.FLOAT, "1E0"},
{token.FLOAT, "42E0"}, // {token.FLOAT, "42E0"},
{token.FLOAT, "01234567890E0"}, // {token.FLOAT, "01234567890E0"},
{token.FLOAT, "0e+10"}, // {token.FLOAT, "0e+10"},
{token.FLOAT, "1e-10"}, // {token.FLOAT, "1e-10"},
{token.FLOAT, "42e+10"}, // {token.FLOAT, "42e+10"},
{token.FLOAT, "01234567890e-10"}, // {token.FLOAT, "01234567890e-10"},
{token.FLOAT, "0E+10"}, // {token.FLOAT, "0E+10"},
{token.FLOAT, "1E-10"}, // {token.FLOAT, "1E-10"},
{token.FLOAT, "42E+10"}, // {token.FLOAT, "42E+10"},
{token.FLOAT, "01234567890E-10"}, // {token.FLOAT, "01234567890E-10"},
} }
testTokenList(t, tokenList) testTokenList(t, tokenList)