@@ -9,13 +9,7 @@ export const grammarsSource = String.raw`
99 // Example:
1010 //+ "//+ \"x\"\nG {\n//- \"\"\nstart = \"x\"}"
1111 OhmWithExamples <: Ohm {
12- // The default start rule for Ohm is 'Grammars', which is syntactic rule.
13- // When the start rule is a syntactic rule, there's no way to get access to
14- // leading space (including comments). So, for this grammar to be useful,
15- // you have to explicit use this rule as the start rule.
16- grammarsWithExamples = (exampleComments applySyntactic<Grammar>)*
17-
18- Grammar := ident SuperGrammar? "{" (#exampleComments Rule)* "}"
12+ Grammar := ident SuperGrammar? "{" (#exampleComments Rule)* #exampleComments "}"
1913
2014 exampleComments = (spacesNoExampleComment exampleComment)*
2115
@@ -79,19 +73,10 @@ const semantics = grammars.OhmWithExamples.createSemantics().addOperation('hasEx
7973} ) ;
8074
8175semantics . addOperation ( 'examples' , {
82- grammarsWithExamples ( exampleCommentsIter , grammarIter ) {
83- const result = [ ] ;
84- for ( const [ i , child ] of Object . entries ( grammarIter . children ) ) {
85- if ( exampleCommentsIter . hasExamples ( ) ) {
86- const defaultExamples = exampleCommentsIter . child ( i ) . examples ( ) ;
87- const grammar = child . grammarName ( ) ;
88- result . push ( ...defaultExamples . map ( ex => ( { ...ex , grammar, rule : '' } ) ) ) ;
89- }
90- result . push ( ...child . examples ( ) ) ;
91- }
92- return result ;
76+ Grammars ( grammarIter ) {
77+ return grammarIter . children . flatMap ( c => c . examples ( ) ) ;
9378 } ,
94- Grammar ( name , _ , _open , exampleCommentsIter , ruleIter , _close ) {
79+ Grammar ( name , _ , _open , exampleCommentsIter , ruleIter , trailingCommentsIter , _close ) {
9580 const result = [ ] ;
9681 const grammar = this . grammarName ( ) ;
9782 for ( let i = 0 ; i < ruleIter . numChildren ; i ++ ) {
@@ -103,6 +88,10 @@ semantics.addOperation('examples', {
10388
10489 result . push ( ...augmentedExamples ) ;
10590 }
91+ if ( trailingCommentsIter . hasExamples ( ) ) {
92+ const defaultExamples = trailingCommentsIter . examples ( ) ;
93+ result . push ( ...defaultExamples . map ( ex => ( { ...ex , grammar, rule : '' } ) ) ) ;
94+ }
10695 return result ;
10796 } ,
10897 exampleComments ( _ , commentIter ) {
@@ -128,7 +117,7 @@ semantics.addOperation('examples', {
128117} ) ;
129118
130119semantics . addOperation ( 'grammarName' , {
131- Grammar ( name , _ , _open , exampleCommentsIter , ruleIter , _close ) {
120+ Grammar ( name , _ , _open , exampleCommentsIter , ruleIter , trailingCommentsIter , _close ) {
132121 return name . sourceString ;
133122 } ,
134123} ) ;
@@ -152,7 +141,7 @@ semantics.addOperation('ruleName', {
152141 * @return {[Example] }
153142 */
154143export function extractExamples ( grammarsDef ) {
155- const matchResult = grammars . OhmWithExamples . match ( grammarsDef , 'grammarsWithExamples' ) ;
144+ const matchResult = grammars . OhmWithExamples . match ( grammarsDef ) ;
156145 if ( matchResult . failed ( ) ) {
157146 throw new Error ( matchResult . message ) ;
158147 }
0 commit comments