@@ -25,18 +25,46 @@ export function parse(content: string) {
25
25
content = content . replace ( / \n v i m : [ ^ \n ] * \s * $ / , "" ) ;
26
26
27
27
const options : Option [ ] = [ ] ;
28
+ const succeeds = new Set < number > ( ) ;
29
+ const errors : Array < { name : string ; start : number } > = [ ] ;
30
+ let last = - 1 ;
28
31
for ( const match of content . matchAll ( / \* ' ( \w + ) ' \* / g) ) {
29
32
const name = match [ 1 ] ;
30
- const block = extractBlock ( content , match . index ?? 0 ) ;
33
+ const index = match . index ! ;
34
+ if ( index < last ) {
35
+ // It is contained previous block
36
+ continue ;
37
+ }
38
+ const { block, start, end } = extractBlock ( content , index ) ;
31
39
const option = parseBlock ( name , block ) ;
32
40
if ( option ) {
33
41
options . push ( option ) ;
42
+ succeeds . add ( start ) ;
43
+ last = end ;
44
+ } else {
45
+ errors . push ( { name, start } ) ;
34
46
}
35
47
}
48
+
49
+ if ( errors . length ) {
50
+ for ( const { name, start } of errors ) {
51
+ if ( ! succeeds . has ( start ) ) {
52
+ const line = content . substring ( 0 , start + 1 ) . split ( "\n" ) . length ;
53
+ console . error (
54
+ `Failed to parse option definition for ${ name } at line ${ line } ` ,
55
+ ) ;
56
+ }
57
+ }
58
+ }
59
+
36
60
return options ;
37
61
}
38
62
39
- function extractBlock ( content : string , index : number ) : string {
63
+ function extractBlock ( content : string , index : number ) : {
64
+ block : string ;
65
+ start : number ;
66
+ end : number ;
67
+ } {
40
68
const s = content . lastIndexOf ( "\n" , index ) ;
41
69
const ms = regexIndexOf ( content , / \n [ ^ < > \s ] | $ / , s ) ;
42
70
const me = regexIndexOf ( content , / \n [ ^ < > \s ] | $ / , ms + 1 ) ;
@@ -45,7 +73,7 @@ function extractBlock(content: string, index: number): string {
45
73
. substring ( s , e )
46
74
. replace ( / ( \n < ? ) (?: \s + \* \S + ?\* ) + \s * $ / , "$1" ) // Remove next block tag
47
75
. trimEnd ( ) ;
48
- return block ;
76
+ return { block, start : s , end : s + block . length } ;
49
77
}
50
78
51
79
function parseBlock ( name : string , body : string ) : Option | undefined {
0 commit comments