31
31
import java .io .IOException ;
32
32
import java .io .Reader ;
33
33
import java .io .StringReader ;
34
+ import java .util .Scanner ;
34
35
35
36
/**
36
37
* A reader with some special look ahead reading.
@@ -41,7 +42,7 @@ class LessLookAheadReader extends LessObject implements Closeable {
41
42
42
43
private final StringBuilder cache = new StringBuilder ();
43
44
44
- private StringReader blockMarkReader ;
45
+ private StringReader cache2 ;
45
46
46
47
private final boolean isReference , isMultiple ;
47
48
@@ -71,12 +72,12 @@ class LessLookAheadReader extends LessObject implements Closeable {
71
72
* if an I/O error occur
72
73
*/
73
74
private int readCharBlockMarker () throws IOException {
74
- if ( blockMarkReader != null ) {
75
- int ch = blockMarkReader .read ();
75
+ if ( cache2 != null ) {
76
+ int ch = cache2 .read ();
76
77
if ( ch != -1 ) {
77
78
return ch ;
78
79
}
79
- blockMarkReader = null ;
80
+ cache2 = null ;
80
81
}
81
82
return reader .read ();
82
83
}
@@ -90,7 +91,15 @@ private int readCharBlockMarker() throws IOException {
90
91
*/
91
92
int nextBlockMarker () throws LessException {
92
93
if ( cachePos < cache .length () ) {
93
- blockMarkReader = new StringReader ( cache .substring ( cachePos ) );
94
+ String str = cache .substring ( cachePos );
95
+ if ( cache2 != null ) { // occur with detached rulset inside another detached ruleset
96
+ try (Scanner scanner = new Scanner ( cache2 ).useDelimiter ( "\\ A" )) {
97
+ if ( scanner .hasNext () ) {
98
+ str = scanner .next () + str ;
99
+ }
100
+ }
101
+ }
102
+ cache2 = new StringReader ( str );
94
103
}
95
104
cache .setLength ( cachePos = 0 );
96
105
int parenthesis = 0 ;
@@ -149,13 +158,21 @@ int nextBlockMarker() throws LessException {
149
158
}
150
159
}
151
160
if ( !isBlock ) {
161
+ int braces = 1 ;
152
162
do {
153
163
ch = readCharBlockMarker ();
154
- if ( ch < 0 ) {
155
- throw createException ( "Unrecognized input: '" + cache .toString ().trim () + "'" );
164
+ switch ( ch ) {
165
+ case -1 :
166
+ throw createException ( "Unrecognized input: '" + cache .toString ().trim () + "'" );
167
+ case '}' :
168
+ braces --;
169
+ break ;
170
+ case '{' :
171
+ braces ++;
172
+ break ;
156
173
}
157
174
cache .append ( (char )ch );
158
- } while ( ch != '}' );
175
+ } while ( braces > 0 );
159
176
break ;
160
177
}
161
178
//$FALL-THROUGH$
0 commit comments