From 52befe20932bf3453549dddc569d5445542daf7f Mon Sep 17 00:00:00 2001 From: Fatih Arslan Date: Sun, 4 Oct 2015 20:19:39 +0300 Subject: [PATCH] scanner: use new hcl/token package --- scanner/scanner.go | 12 +++-- scanner/{scanner_text.go => scanner_test.go} | 50 ++++++++++---------- 2 files changed, 33 insertions(+), 29 deletions(-) rename scanner/{scanner_text.go => scanner_test.go} (76%) diff --git a/scanner/scanner.go b/scanner/scanner.go index c3dd565..b223a86 100644 --- a/scanner/scanner.go +++ b/scanner/scanner.go @@ -6,6 +6,8 @@ import ( "io/ioutil" "log" "unicode" + + "github.com/fatih/hcl/token" ) // eof represents a marker rune for the end of the reader. @@ -83,7 +85,7 @@ func (s *Scanner) peek() rune { } // Scan scans the next token and returns the token. -func (s *Scanner) Scan() (tok Token) { +func (s *Scanner) Scan() (tok token.Token) { ch := s.next() // skip white space @@ -96,10 +98,10 @@ func (s *Scanner) Scan() (tok Token) { s.tokPos = s.currPos.Offset - s.lastCharLen if isLetter(ch) { - tok = IDENT + tok = token.IDENT lit := s.scanIdentifier() if lit == "true" || lit == "false" { - tok = BOOL + tok = token.BOOL } } @@ -110,9 +112,9 @@ func (s *Scanner) Scan() (tok Token) { switch ch { case eof: - tok = EOF + tok = token.EOF case '"': - tok = STRING + tok = token.STRING s.scanString() } diff --git a/scanner/scanner_text.go b/scanner/scanner_test.go similarity index 76% rename from scanner/scanner_text.go rename to scanner/scanner_test.go index 2c5f80c..3ea41fd 100644 --- a/scanner/scanner_text.go +++ b/scanner/scanner_test.go @@ -4,17 +4,19 @@ import ( "bytes" "fmt" "testing" + + "github.com/fatih/hcl/token" ) var f100 = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" -type token struct { - tok Token +type tokenPair struct { + tok token.Token text string } func TestBool(t *testing.T) { - var tokenList = []token{ + var tokenList = []tokenPair{ {token.BOOL, "true"}, {token.BOOL, "false"}, } @@ -44,23 +46,23 @@ func TestBool(t *testing.T) { } func TestIdent(t *testing.T) { - var tokenList = []token{ - {IDENT, "a"}, - {IDENT, "a0"}, - {IDENT, "foobar"}, - {IDENT, "abc123"}, - {IDENT, "LGTM"}, - {IDENT, "_"}, - {IDENT, "_abc123"}, - {IDENT, "abc123_"}, - {IDENT, "_abc_123_"}, - {IDENT, "_äöü"}, - {IDENT, "_本"}, - {IDENT, "äöü"}, - {IDENT, "本"}, - {IDENT, "a۰۱۸"}, - {IDENT, "foo६४"}, - {IDENT, "bar9876"}, + var tokenList = []tokenPair{ + {token.IDENT, "a"}, + {token.IDENT, "a0"}, + {token.IDENT, "foobar"}, + {token.IDENT, "abc123"}, + {token.IDENT, "LGTM"}, + {token.IDENT, "_"}, + {token.IDENT, "_abc123"}, + {token.IDENT, "abc123_"}, + {token.IDENT, "_abc_123_"}, + {token.IDENT, "_äöü"}, + {token.IDENT, "_本"}, + {token.IDENT, "äöü"}, + {token.IDENT, "本"}, + {token.IDENT, "a۰۱۸"}, + {token.IDENT, "foo६४"}, + {token.IDENT, "bar9876"}, } // create artifical source code @@ -88,10 +90,10 @@ func TestIdent(t *testing.T) { } func TestString(t *testing.T) { - var tokenList = []token{ - {STRING, `" "`}, - {STRING, `"a"`}, - {STRING, `"本"`}, + var tokenList = []tokenPair{ + {token.STRING, `" "`}, + {token.STRING, `"a"`}, + {token.STRING, `"本"`}, // {STRING, `"\a"`}, // {STRING, `"\b"`}, // {STRING, `"\f"`},