@@ -510,13 +510,9 @@ function parseGraph(lexer: Lexer): NormalizedGraph {
510510
511511 function parseStatement ( owner : NormalizedGraph | NormalizedSubgraph ) : void {
512512 // stmt: node_stmt | edge_stmt | attr_stmt | ID '=' ID | subgraph
513- if ( lexer . peekIsLiteral ( '{' ) ) {
514- const subgraph = owner . upsertSubgraph ( null ) ;
515- parseStatementList ( subgraph ) ;
513+ if ( lexer . peekIsLiteral ( '{' ) || lexer . optionalKeyword ( 'subgraph' ) ) {
514+ const tailNodes = parseSubgraph ( owner ) ;
516515 if ( optionalEdgeOp ( ) ) {
517- const tailNodes : NodeID [ ] = subgraph
518- . sortedNodes ( )
519- . map ( ( node ) => [ node , undefined ] ) ;
520516 parseEdges ( tailNodes , owner ) ;
521517 }
522518 return ;
@@ -547,15 +543,8 @@ function parseGraph(lexer: Lexer): NormalizedGraph {
547543 }
548544
549545 case 'subgraph' : {
550- const name = lexer . peekIsLiteral ( '{' )
551- ? null
552- : lexer . expectID ( 'subgraph name' ) . value ;
553- const subgraph = owner . upsertSubgraph ( name ) ;
554- parseStatementList ( subgraph ) ;
546+ const tailNodes = parseSubgraph ( owner ) ;
555547 if ( optionalEdgeOp ( ) ) {
556- const tailNodes : NodeID [ ] = subgraph
557- . sortedNodes ( )
558- . map ( ( node ) => [ node , undefined ] ) ;
559548 parseEdges ( tailNodes , owner ) ;
560549 }
561550 break ;
@@ -579,20 +568,27 @@ function parseGraph(lexer: Lexer): NormalizedGraph {
579568 }
580569 }
581570
571+ function parseSubgraph (
572+ owner : NormalizedGraph | NormalizedSubgraph ,
573+ ) : NodeID [ ] {
574+ const name = lexer . peekIsLiteral ( '{' )
575+ ? null
576+ : lexer . expectID ( 'subgraph name' ) . value ;
577+ const subgraph = owner . upsertSubgraph ( name ) ;
578+ parseStatementList ( subgraph ) ;
579+ return subgraph . sortedNodes ( ) . map ( ( node ) => [ node , undefined ] ) ;
580+ }
581+
582582 function parseEdges (
583583 tailNodes : NodeID [ ] ,
584584 owner : NormalizedGraph | NormalizedSubgraph ,
585585 ) {
586586 const newEdges = new Set < NormalizedEdge > ( ) ;
587587 do {
588- let headNodes : NodeID [ ] ;
589- if ( lexer . peekIsLiteral ( '{' ) ) {
590- const subgraph = owner . upsertSubgraph ( null ) ;
591- parseStatementList ( subgraph ) ;
592- headNodes = subgraph . sortedNodes ( ) . map ( ( node ) => [ node , undefined ] ) ;
593- } else {
594- headNodes = [ parseNodeID ( owner ) ] ;
595- }
588+ const headNodes =
589+ lexer . peekIsLiteral ( '{' ) || lexer . optionalKeyword ( 'subgraph' )
590+ ? parseSubgraph ( owner )
591+ : [ parseNodeID ( owner ) ] ;
596592
597593 for ( const tail of tailNodes ) {
598594 for ( const head of headNodes ) {
0 commit comments