3131import java .io .IOException ;
3232import java .io .Reader ;
3333import java .io .StringReader ;
34+ import java .util .Scanner ;
3435
3536/**
3637 * A reader with some special look ahead reading.
@@ -41,7 +42,7 @@ class LessLookAheadReader extends LessObject implements Closeable {
4142
4243 private final StringBuilder cache = new StringBuilder ();
4344
44- private StringReader blockMarkReader ;
45+ private StringReader cache2 ;
4546
4647 private final boolean isReference , isMultiple ;
4748
@@ -71,12 +72,12 @@ class LessLookAheadReader extends LessObject implements Closeable {
7172 * if an I/O error occur
7273 */
7374 private int readCharBlockMarker () throws IOException {
74- if ( blockMarkReader != null ) {
75- int ch = blockMarkReader .read ();
75+ if ( cache2 != null ) {
76+ int ch = cache2 .read ();
7677 if ( ch != -1 ) {
7778 return ch ;
7879 }
79- blockMarkReader = null ;
80+ cache2 = null ;
8081 }
8182 return reader .read ();
8283 }
@@ -90,7 +91,15 @@ private int readCharBlockMarker() throws IOException {
9091 */
9192 int nextBlockMarker () throws LessException {
9293 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 );
94103 }
95104 cache .setLength ( cachePos = 0 );
96105 int parenthesis = 0 ;
@@ -149,13 +158,21 @@ int nextBlockMarker() throws LessException {
149158 }
150159 }
151160 if ( !isBlock ) {
161+ int braces = 1 ;
152162 do {
153163 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 ;
156173 }
157174 cache .append ( (char )ch );
158- } while ( ch != '}' );
175+ } while ( braces > 0 );
159176 break ;
160177 }
161178 //$FALL-THROUGH$
0 commit comments