From 8dfc3c4bbe121911f9d75c4a15fa10516af9c641 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Tue, 16 May 2017 07:23:33 -0700 Subject: [PATCH] json: fix overly-greedy keyword scanning Logic error in the scanner caused it to always consume the remainder of the string. --- zcl/json/scanner.go | 2 +- zcl/json/scanner_test.go | 68 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/zcl/json/scanner.go b/zcl/json/scanner.go index c399443..7367e2f 100644 --- a/zcl/json/scanner.go +++ b/zcl/json/scanner.go @@ -165,7 +165,7 @@ Byte: for i = 0; i < len(buf); i++ { b := buf[i] switch { - case b >= 'a' || b <= 'z' || b >= 'A' || b <= 'Z' || b == '_': + case (b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') || b == '_': p.Pos.Byte++ p.Pos.Column++ default: diff --git a/zcl/json/scanner_test.go b/zcl/json/scanner_test.go index e5471c2..4275a52 100644 --- a/zcl/json/scanner_test.go +++ b/zcl/json/scanner_test.go @@ -510,6 +510,74 @@ func TestScan(t *testing.T) { }, }, }, + { + `[true]`, + []token{ + { + Type: tokenBrackO, + Bytes: []byte(`[`), + Range: zcl.Range{ + Start: zcl.Pos{ + Byte: 0, + Line: 1, + Column: 1, + }, + End: zcl.Pos{ + Byte: 1, + Line: 1, + Column: 2, + }, + }, + }, + { + Type: tokenKeyword, + Bytes: []byte(`true`), + Range: zcl.Range{ + Start: zcl.Pos{ + Byte: 1, + Line: 1, + Column: 2, + }, + End: zcl.Pos{ + Byte: 5, + Line: 1, + Column: 6, + }, + }, + }, + { + Type: tokenBrackC, + Bytes: []byte(`]`), + Range: zcl.Range{ + Start: zcl.Pos{ + Byte: 5, + Line: 1, + Column: 6, + }, + End: zcl.Pos{ + Byte: 6, + Line: 1, + Column: 7, + }, + }, + }, + { + Type: tokenEOF, + Range: zcl.Range{ + Start: zcl.Pos{ + Byte: 6, + Line: 1, + Column: 7, + }, + End: zcl.Pos{ + Byte: 6, + Line: 1, + Column: 7, + }, + }, + }, + }, + }, { `""`, []token{