Skip to content
This repository has been archived by the owner on Apr 25, 2018. It is now read-only.

Commit

Permalink
Starting on object literals. Have a few simple cases down.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikewest committed May 22, 2009
1 parent cdba3ab commit 2ba1a5b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
16 changes: 15 additions & 1 deletion javascriptparser.py
Expand Up @@ -59,9 +59,23 @@ def led( self, left ):
parser.next( ',' )
parser.next( ')' )
return self

self.new_symbol( '(', 150, nud=nud, led=led )

def nud( self ):
self.first = []
if parser.current_symbol.id != '}':
while 1:
a = parser.expression( 10 )
parser.next( ':' )
b = parser.expression( 10 )
self.first.append( ( a, b ) )
if ( parser.current_symbol.id != ',' ):
break
parser.next( ',' )
parser.next( '}' )
return self
self.new_symbol( '{', 150, nud=nud )

#####################################################################(140)
# negation/increment
self.prefix( '++', 140 ) # Pre-increment
Expand Down
20 changes: 18 additions & 2 deletions tests/TestJavaScriptParser.py
Expand Up @@ -125,9 +125,19 @@ def assertEqualTree( self, expected, result ):
),
(
### Thanks to jQuery for this appalling example
'e[ val == "toggle" ? hidden ? "show" : "hide" : val ]', '(`[` (IDENTIFIER e) (`?` (`==` (IDENTIFIER val) (STRING toggle)) (`?` (IDENTIFIER hidden) (STRING show) (STRING hide)) (IDENTIFIER val)))'
'e[ val == "toggle" ? hidden ? "show" : "hide" : val ]( param )', '(`(` (`[` (IDENTIFIER e) (`?` (`==` (IDENTIFIER val) (STRING toggle)) (`?` (IDENTIFIER hidden) (STRING show) (STRING hide)) (IDENTIFIER val))) [(IDENTIFIER param)])'
)

]
JSONKnownValues = [
(
'{"a": "b"}', '(`{` [((STRING a), (STRING b))])'
),
(
'{"a": "b", "c": "d"}', '(`{` [((STRING a), (STRING b)), ((STRING c), (STRING d))])'
),
(
'{"a": "b", "c": {"d": "e"}}', '(`{` [((STRING a), (STRING b)), ((STRING c), (`{` [((STRING d), (STRING e))]))])'
),
]
def testJavaScriptParserKnownSimpleValues( self ):
"""Parser.parse_tree should give known result with known input"""
Expand Down Expand Up @@ -159,6 +169,12 @@ def testJavaScriptParserKnownCallValues( self ):
result = JavaScriptParser( string ).parse_tree
self.assertEqualTree( tree, result )

def testJavaScriptParserKnownJSONValues( self ):
"""Parser.parse_tree should give known result with known JSON structures"""
for string, tree in self.JSONKnownValues:
result = JavaScriptParser( string ).parse_tree
self.assertEqualTree( tree, result )

if __name__ == "__main__":
unittest.main()

0 comments on commit 2ba1a5b

Please sign in to comment.