@@ -12,73 +12,148 @@ const fs = require('fs');
1212
1313const TEST_FOLDER = 'src/test/rustdoc-js/' ;
1414
15+ function getNextStep ( content , pos , stop ) {
16+ while ( pos < content . length && content [ pos ] !== stop &&
17+ ( content [ pos ] === ' ' || content [ pos ] === '\t' || content [ pos ] === '\n' ) ) {
18+ pos += 1 ;
19+ }
20+ if ( pos >= content . length ) {
21+ return null ;
22+ }
23+ if ( content [ pos ] !== stop ) {
24+ return pos * - 1 ;
25+ }
26+ return pos ;
27+ }
28+
1529// Stupid function extractor based on indent.
1630function extractFunction ( content , functionName ) {
17- var x = content . split ( '\n' ) ;
18- var in_func = false ;
1931 var indent = 0 ;
20- var lines = [ ] ;
21-
22- for ( var i = 0 ; i < x . length ; ++ i ) {
23- if ( in_func === false ) {
24- var splitter = "function " + functionName + "(" ;
25- if ( x [ i ] . trim ( ) . startsWith ( splitter ) ) {
26- in_func = true ;
27- indent = x [ i ] . split ( splitter ) [ 0 ] . length ;
28- lines . push ( x [ i ] ) ;
29- }
30- } else {
31- lines . push ( x [ i ] ) ;
32- if ( x [ i ] . trim ( ) === "}" && x [ i ] . split ( "}" ) [ 0 ] . length === indent ) {
33- return lines . join ( "\n" ) ;
32+ var splitter = "function " + functionName + "(" ;
33+
34+ while ( true ) {
35+ var start = content . indexOf ( splitter ) ;
36+ if ( start === - 1 ) {
37+ break ;
38+ }
39+ var pos = start ;
40+ while ( pos < content . length && content [ pos ] !== ')' ) {
41+ pos += 1 ;
42+ }
43+ if ( pos >= content . length ) {
44+ break ;
45+ }
46+ pos = getNextStep ( content , pos + 1 , '{' ) ;
47+ if ( pos === null ) {
48+ break ;
49+ } else if ( pos < 0 ) {
50+ content = content . slice ( - pos ) ;
51+ continue ;
52+ }
53+ while ( pos < content . length ) {
54+ if ( content [ pos ] === '"' || content [ pos ] === "'" ) {
55+ var stop = content [ pos ] ;
56+ var is_escaped = false ;
57+ do {
58+ if ( content [ pos ] === '\\' ) {
59+ pos += 2 ;
60+ } else {
61+ pos += 1 ;
62+ }
63+ } while ( pos < content . length &&
64+ ( content [ pos ] !== stop || content [ pos - 1 ] === '\\' ) ) ;
65+ } else if ( content [ pos ] === '{' ) {
66+ indent += 1 ;
67+ } else if ( content [ pos ] === '}' ) {
68+ indent -= 1 ;
69+ if ( indent === 0 ) {
70+ return content . slice ( start , pos + 1 ) ;
71+ }
3472 }
73+ pos += 1 ;
3574 }
75+ content = content . slice ( start + 1 ) ;
3676 }
3777 return null ;
3878}
3979
4080// Stupid function extractor for array.
4181function extractArrayVariable ( content , arrayName ) {
42- var x = content . split ( '\n' ) ;
43- var found_var = false ;
44- var lines = [ ] ;
45-
46- for ( var i = 0 ; i < x . length ; ++ i ) {
47- if ( found_var === false ) {
48- var splitter = "var " + arrayName + " = [" ;
49- if ( x [ i ] . trim ( ) . startsWith ( splitter ) ) {
50- found_var = true ;
51- i -= 1 ;
52- }
53- } else {
54- lines . push ( x [ i ] ) ;
55- if ( x [ i ] . endsWith ( '];' ) ) {
56- return lines . join ( "\n" ) ;
82+ var splitter = "var " + arrayName ;
83+ while ( true ) {
84+ var start = content . indexOf ( splitter ) ;
85+ if ( start === - 1 ) {
86+ break ;
87+ }
88+ var pos = getNextStep ( content , start , '=' ) ;
89+ if ( pos === null ) {
90+ break ;
91+ } else if ( pos < 0 ) {
92+ content = content . slice ( - pos ) ;
93+ continue ;
94+ }
95+ pos = getNextStep ( content , pos , '[' ) ;
96+ if ( pos === null ) {
97+ break ;
98+ } else if ( pos < 0 ) {
99+ content = content . slice ( - pos ) ;
100+ continue ;
101+ }
102+ while ( pos < content . length ) {
103+ if ( content [ pos ] === '"' || content [ pos ] === "'" ) {
104+ var stop = content [ pos ] ;
105+ do {
106+ if ( content [ pos ] === '\\' ) {
107+ pos += 2 ;
108+ } else {
109+ pos += 1 ;
110+ }
111+ } while ( pos < content . length &&
112+ ( content [ pos ] !== stop || content [ pos - 1 ] === '\\' ) ) ;
113+ } else if ( content [ pos ] === ']' &&
114+ pos + 1 < content . length &&
115+ content [ pos + 1 ] === ';' ) {
116+ return content . slice ( start , pos + 2 ) ;
57117 }
118+ pos += 1 ;
58119 }
120+ content = content . slice ( start + 1 ) ;
59121 }
60122 return null ;
61123}
62124
63125// Stupid function extractor for variable.
64126function extractVariable ( content , varName ) {
65- var x = content . split ( '\n' ) ;
66- var found_var = false ;
67- var lines = [ ] ;
68-
69- for ( var i = 0 ; i < x . length ; ++ i ) {
70- if ( found_var === false ) {
71- var splitter = "var " + varName + " = " ;
72- if ( x [ i ] . trim ( ) . startsWith ( splitter ) ) {
73- found_var = true ;
74- i -= 1 ;
75- }
76- } else {
77- lines . push ( x [ i ] ) ;
78- if ( x [ i ] . endsWith ( ';' ) ) {
79- return lines . join ( "\n" ) ;
127+ var splitter = "var " + varName ;
128+ while ( true ) {
129+ var start = content . indexOf ( splitter ) ;
130+ if ( start === - 1 ) {
131+ break ;
132+ }
133+ var pos = getNextStep ( content , start , '=' ) ;
134+ if ( pos === null ) {
135+ break ;
136+ } else if ( pos < 0 ) {
137+ content = content . slice ( - pos ) ;
138+ continue ;
139+ }
140+ while ( pos < content . length ) {
141+ if ( content [ pos ] === '"' || content [ pos ] === "'" ) {
142+ var stop = content [ pos ] ;
143+ do {
144+ if ( content [ pos ] === '\\' ) {
145+ pos += 2 ;
146+ } else {
147+ pos += 1 ;
148+ }
149+ } while ( pos < content . length &&
150+ ( content [ pos ] !== stop || content [ pos - 1 ] === '\\' ) ) ;
151+ } else if ( content [ pos ] === ';' ) {
152+ return content . slice ( start , pos + 1 ) ;
80153 }
154+ pos += 1 ;
81155 }
156+ content = content . slice ( start + 1 ) ;
82157 }
83158 return null ;
84159}
@@ -101,7 +176,7 @@ function loadThings(thingsToLoad, kindOfLoad, funcToCall, fileContent) {
101176 for ( var i = 0 ; i < thingsToLoad . length ; ++ i ) {
102177 var tmp = funcToCall ( fileContent , thingsToLoad [ i ] ) ;
103178 if ( tmp === null ) {
104- console . error ( 'enable to find ' + kindOfLoad + ' "' + thingsToLoad [ i ] + '"' ) ;
179+ console . error ( 'unable to find ' + kindOfLoad + ' "' + thingsToLoad [ i ] + '"' ) ;
105180 process . exit ( 1 ) ;
106181 }
107182 content += tmp ;
0 commit comments