hcl/json: Fix incorrect alphabetical check in scanner

This commit is contained in:
Maxim 2019-04-19 19:24:50 +03:00 committed by Martin Atkins
parent f7764c6954
commit c27cd9b2e8
2 changed files with 27 additions and 2 deletions

View File

@ -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')
}

View File

@ -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 {