From c27cd9b2e8db7b8c664f1933e54eb63285925b65 Mon Sep 17 00:00:00 2001 From: Maxim Date: Fri, 19 Apr 2019 19:24:50 +0300 Subject: [PATCH] hcl/json: Fix incorrect alphabetical check in scanner --- hcl/json/scanner.go | 8 ++++++-- hcl/json/scanner_test.go | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/hcl/json/scanner.go b/hcl/json/scanner.go index 0a8378b..da72884 100644 --- a/hcl/json/scanner.go +++ b/hcl/json/scanner.go @@ -153,7 +153,7 @@ func byteCanStartKeyword(b byte) bool { // in the parser, where we can generate better diagnostics. // So e.g. we want to be able to say: // unrecognized keyword "True". Did you mean "true"? - case b >= 'a' || b <= 'z' || b >= 'A' || b <= 'Z': + case isAlphabetical(b): return true default: return false @@ -167,7 +167,7 @@ Byte: for i = 0; i < len(buf); i++ { b := buf[i] switch { - case (b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') || b == '_': + case isAlphabetical(b) || b == '_': p.Pos.Byte++ p.Pos.Column++ default: @@ -291,3 +291,7 @@ func posRange(start, end pos) hcl.Range { func (t token) GoString() string { return fmt.Sprintf("json.token{json.%s, []byte(%q), %#v}", t.Type, t.Bytes, t.Range) } + +func isAlphabetical(b byte) bool { + return (b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') +} diff --git a/hcl/json/scanner_test.go b/hcl/json/scanner_test.go index 8526847..23650c5 100644 --- a/hcl/json/scanner_test.go +++ b/hcl/json/scanner_test.go @@ -810,6 +810,27 @@ func TestScan(t *testing.T) { }, }, }, + { + `&`, + []token{ + { + Type: tokenInvalid, + Bytes: []byte(`&`), + Range: hcl.Range{ + Start: hcl.Pos{ + Byte: 0, + Line: 1, + Column: 1, + }, + End: hcl.Pos{ + Byte: 1, + Line: 1, + Column: 2, + }, + }, + }, + }, + }, } for _, test := range tests {