@@ -12,73 +12,148 @@ const fs = require('fs');
12
12
13
13
const TEST_FOLDER = 'src/test/rustdoc-js/' ;
14
14
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
+
15
29
// Stupid function extractor based on indent.
16
30
function extractFunction ( content , functionName ) {
17
- var x = content . split ( '\n' ) ;
18
- var in_func = false ;
19
31
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
+ }
34
72
}
73
+ pos += 1 ;
35
74
}
75
+ content = content . slice ( start + 1 ) ;
36
76
}
37
77
return null ;
38
78
}
39
79
40
80
// Stupid function extractor for array.
41
81
function 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 ) ;
57
117
}
118
+ pos += 1 ;
58
119
}
120
+ content = content . slice ( start + 1 ) ;
59
121
}
60
122
return null ;
61
123
}
62
124
63
125
// Stupid function extractor for variable.
64
126
function 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 ) ;
80
153
}
154
+ pos += 1 ;
81
155
}
156
+ content = content . slice ( start + 1 ) ;
82
157
}
83
158
return null ;
84
159
}
@@ -101,7 +176,7 @@ function loadThings(thingsToLoad, kindOfLoad, funcToCall, fileContent) {
101
176
for ( var i = 0 ; i < thingsToLoad . length ; ++ i ) {
102
177
var tmp = funcToCall ( fileContent , thingsToLoad [ i ] ) ;
103
178
if ( tmp === null ) {
104
- console . error ( 'enable to find ' + kindOfLoad + ' "' + thingsToLoad [ i ] + '"' ) ;
179
+ console . error ( 'unable to find ' + kindOfLoad + ' "' + thingsToLoad [ i ] + '"' ) ;
105
180
process . exit ( 1 ) ;
106
181
}
107
182
content += tmp ;
0 commit comments