1
0
mirror of https://github.com/robertkrimen/otto synced 2025-09-28 18:45:22 +08:00

Fix parsing of null, etc. on Object intialization

true, false, etc.
This commit is contained in:
Robert Krimen 2012-10-19 16:24:14 -07:00
parent d6bb7b6285
commit 05ee181c34
5 changed files with 55 additions and 4 deletions

View File

@ -2,4 +2,12 @@ function $ERROR(message) {
console.log(message)
}
function runTestCase(fn) {
if (fn()) {
console.log("pass")
} else {
console.log("=== fail")
}
}
// ---

View File

@ -433,18 +433,19 @@ func (self *_lexer) scanIdentifierKeyword() (token _token) {
chr := convertHexadecimalRune(string(read[2:]))
if chr == utf8.RuneError {
word = append(word, 'u')
self.skip(2)
self.skip(2) // Skip \u
} else {
if chr == '\\' || !identifierCheck(chr) {
return
}
word = append(word, chr)
self.skip(6)
self.skip(6) // Skip \u????
}
} else {
return
}
} else {
// Basically a skip of 1
word = append(word, self.next())
}
default:
@ -467,6 +468,7 @@ func (self *_lexer) scanIdentifierKeyword() (token _token) {
// Now we're looking at the body of the identiifer
identifierCheck = isIdentifierPart
}
return
}

View File

@ -2022,3 +2022,29 @@ func TestUnicode(t *testing.T) {
}
}
func TestDotMember(t *testing.T) {
Terst(t)
test := runTest()
test(`
abc = {
ghi: 11,
}
abc.def = "Xyzzy"
abc.null = "Nothing happens."
`)
test(`abc.def`, "Xyzzy")
test(`abc.null`, "Nothing happens.")
test(`abc.ghi`, "11")
test(`
abc = {
null: 11,
}
`)
test(`abc.def`, "undefined")
test(`abc.null`, "11")
test(`abc.ghi`, "undefined")
}

View File

@ -49,7 +49,11 @@ func (self *_parser) ParseObjectPropertyKey() string {
} else if self.Match("string") {
return toString(self.ConsumeString().Value)
}
panic(self.Unexpected(self.Peek()))
token := self.Next()
if !isIdentifierName(token) {
panic(self.Unexpected(token))
}
return token.Text
}
func (self *_parser) ParseObjectProperty() *_objectPropertyNode {
@ -147,7 +151,11 @@ func (self *_parser) ParseCallExpression(left _node) _node {
func (self *_parser) ParseDotMember(left _node) _node {
self.Expect(".")
member := self.ConsumeIdentifier().Value
token := self.Next()
member := token.Text
if !isIdentifierName(token) {
panic(token.newSyntaxError("Unexpected token %s", token.Kind))
}
node := newDotMemberNode(left, member)
self.markNode(node)
return node

View File

@ -297,3 +297,10 @@ func (self *_parser) markNode(node _node) {
node.setPosition(self.lexer.lineCount)
}
func isIdentifierName(token _token) bool {
switch token.Kind {
case "identifier", "boolean":
return true
}
return keywordTable[token.Kind]
}