json: true, false, null lex [GH-22]

This commit is contained in:
Mitchell Hashimoto 2014-11-21 10:59:03 -08:00
parent e51eabcdf8
commit 2c88e7ea6e
2 changed files with 45 additions and 2 deletions

View File

@ -74,10 +74,50 @@ func (x *jsonLex) Lex(yylval *jsonSymType) int {
case '"':
return x.lexString(yylval)
default:
x.createErr(fmt.Sprintf("unexpected character: %c", c))
x.backup()
return x.lexId(yylval)
}
}
}
// lexId lexes an identifier
func (x *jsonLex) lexId(yylval *jsonSymType) int {
var b bytes.Buffer
first := true
for {
c := x.next()
if c == lexEOF {
break
}
if !unicode.IsDigit(c) && !unicode.IsLetter(c) && c != '_' && c != '-' {
x.backup()
if first {
x.createErr("Invalid identifier")
return lexEOF
}
break
}
first = false
if _, err := b.WriteRune(c); err != nil {
return lexEOF
}
}
switch v := b.String(); v {
case "true":
return TRUE
case "false":
return FALSE
case "null":
return NULL
default:
x.createErr(fmt.Sprintf("Invalid identifier: %s", v))
return lexEOF
}
}
// lexNumber lexes out a number

View File

@ -3,5 +3,8 @@
"bar": 7,
"baz": [1,2,3],
"foo": -12,
"bar": 3.14159
"bar": 3.14159,
"foo": true,
"bar": false,
"foo": null
}