From 5d75c5a14370258e54afaf1513e33874c9d69a92 Mon Sep 17 00:00:00 2001 From: Fatih Arslan Date: Sun, 4 Oct 2015 23:21:34 +0300 Subject: [PATCH] scanner: initial number lexing --- scanner/scanner.go | 27 ++++++++++++-- scanner/scanner_test.go | 81 ++++++++++++++++++++--------------------- 2 files changed, 63 insertions(+), 45 deletions(-) diff --git a/scanner/scanner.go b/scanner/scanner.go index 394eeb0..fda5265 100644 --- a/scanner/scanner.go +++ b/scanner/scanner.go @@ -112,9 +112,8 @@ func (s *Scanner) Scan() (tok token.Token) { tok = token.BOOL } - case isDigit(ch): - s.scanNumber() - // TODO(arslan) + case isDecimal(ch): + tok = s.scanNumber(ch) default: switch ch { case eof: @@ -129,7 +128,23 @@ func (s *Scanner) Scan() (tok token.Token) { 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 @@ -249,6 +264,10 @@ func isDigit(ch rune) bool { 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 func isWhitespace(ch rune) bool { return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' diff --git a/scanner/scanner_test.go b/scanner/scanner_test.go index 4d30050..8ce08c0 100644 --- a/scanner/scanner_test.go +++ b/scanner/scanner_test.go @@ -100,7 +100,6 @@ func TestString(t *testing.T) { } func TestNumber(t *testing.T) { - t.SkipNow() var tokenList = []tokenPair{ {token.NUMBER, "0"}, {token.NUMBER, "1"}, @@ -112,46 +111,46 @@ func TestNumber(t *testing.T) { {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.FLOAT, "0."}, - {token.FLOAT, "1."}, - {token.FLOAT, "42."}, - {token.FLOAT, "01234567890."}, - {token.FLOAT, ".0"}, - {token.FLOAT, ".1"}, - {token.FLOAT, ".42"}, - {token.FLOAT, ".0123456789"}, - {token.FLOAT, "0.0"}, - {token.FLOAT, "1.0"}, - {token.FLOAT, "42.0"}, - {token.FLOAT, "01234567890.0"}, - {token.FLOAT, "0e0"}, - {token.FLOAT, "1e0"}, - {token.FLOAT, "42e0"}, - {token.FLOAT, "01234567890e0"}, - {token.FLOAT, "0E0"}, - {token.FLOAT, "1E0"}, - {token.FLOAT, "42E0"}, - {token.FLOAT, "01234567890E0"}, - {token.FLOAT, "0e+10"}, - {token.FLOAT, "1e-10"}, - {token.FLOAT, "42e+10"}, - {token.FLOAT, "01234567890e-10"}, - {token.FLOAT, "0E+10"}, - {token.FLOAT, "1E-10"}, - {token.FLOAT, "42E+10"}, - {token.FLOAT, "01234567890E-10"}, + // {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.FLOAT, "0."}, + // {token.FLOAT, "1."}, + // {token.FLOAT, "42."}, + // {token.FLOAT, "01234567890."}, + // {token.FLOAT, ".0"}, + // {token.FLOAT, ".1"}, + // {token.FLOAT, ".42"}, + // {token.FLOAT, ".0123456789"}, + // {token.FLOAT, "0.0"}, + // {token.FLOAT, "1.0"}, + // {token.FLOAT, "42.0"}, + // {token.FLOAT, "01234567890.0"}, + // {token.FLOAT, "0e0"}, + // {token.FLOAT, "1e0"}, + // {token.FLOAT, "42e0"}, + // {token.FLOAT, "01234567890e0"}, + // {token.FLOAT, "0E0"}, + // {token.FLOAT, "1E0"}, + // {token.FLOAT, "42E0"}, + // {token.FLOAT, "01234567890E0"}, + // {token.FLOAT, "0e+10"}, + // {token.FLOAT, "1e-10"}, + // {token.FLOAT, "42e+10"}, + // {token.FLOAT, "01234567890e-10"}, + // {token.FLOAT, "0E+10"}, + // {token.FLOAT, "1E-10"}, + // {token.FLOAT, "42E+10"}, + // {token.FLOAT, "01234567890E-10"}, } testTokenList(t, tokenList)