json: true, false, null lex [GH-22]
This commit is contained in:
parent
e51eabcdf8
commit
2c88e7ea6e
42
json/lex.go
42
json/lex.go
@ -74,10 +74,50 @@ func (x *jsonLex) Lex(yylval *jsonSymType) int {
|
|||||||
case '"':
|
case '"':
|
||||||
return x.lexString(yylval)
|
return x.lexString(yylval)
|
||||||
default:
|
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
|
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
|
// lexNumber lexes out a number
|
||||||
|
@ -3,5 +3,8 @@
|
|||||||
"bar": 7,
|
"bar": 7,
|
||||||
"baz": [1,2,3],
|
"baz": [1,2,3],
|
||||||
"foo": -12,
|
"foo": -12,
|
||||||
"bar": 3.14159
|
"bar": 3.14159,
|
||||||
|
"foo": true,
|
||||||
|
"bar": false,
|
||||||
|
"foo": null
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user