@@ -170,12 +170,14 @@ Parser.prototype.choice = function(varName, options) {
170170 options = varName ;
171171 varName = null ;
172172 }
173+
173174 if ( ! options . tag ) {
174175 throw new Error ( "Tag option of array is not defined." ) ;
175176 }
176177 if ( ! options . choices ) {
177178 throw new Error ( "Choices option of array is not defined." ) ;
178179 }
180+
179181 Object . keys ( options . choices ) . forEach ( function ( key ) {
180182 if ( isNaN ( parseInt ( key , 10 ) ) ) {
181183 throw new Error ( "Key of choices must be a number." ) ;
@@ -203,13 +205,22 @@ Parser.prototype.choice = function(varName, options) {
203205} ;
204206
205207Parser . prototype . nest = function ( varName , options ) {
208+ if ( arguments . length == 1 && typeof varName === "object" ) {
209+ options = varName ;
210+ varName = null ;
211+ }
212+
206213 if ( ! options . type ) {
207214 throw new Error ( "Type option of nest is not defined." ) ;
208215 }
209-
210216 if ( ! ( options . type instanceof Parser ) && ! aliasRegistry [ options . type ] ) {
211217 throw new Error ( "Type option of nest must be a Parser object." ) ;
212218 }
219+ if ( ! ( options . type instanceof Parser ) && ! varName ) {
220+ throw new Error (
221+ "options.type must be a object if variable name is omitted."
222+ ) ;
223+ }
213224
214225 return this . setNextParser ( "nest" , varName , options ) ;
215226} ;
@@ -701,8 +712,11 @@ Parser.prototype.generateChoice = function(ctx) {
701712
702713Parser . prototype . generateNest = function ( ctx ) {
703714 var nestVar = ctx . generateVariable ( this . varName ) ;
715+
704716 if ( this . options . type instanceof Parser ) {
705- ctx . pushCode ( "{0} = {};" , nestVar ) ;
717+ if ( this . varName ) {
718+ ctx . pushCode ( "{0} = {};" , nestVar ) ;
719+ }
706720 ctx . pushPath ( this . varName ) ;
707721 this . options . type . generate ( ctx ) ;
708722 ctx . popPath ( this . varName ) ;
0 commit comments