@@ -80,13 +80,61 @@ var Parser = function Parser(context, imports, fileInfo) {
8080 } ;
8181 }
8282
83+ /**
84+ * Used after initial parsing to create nodes on the fly
85+ *
86+ * @param {String } str - string to parse
87+ * @param {Array } parseList - array of parsers to run input through e.g. ["value", "important"]
88+ * @param {Number } currentIndex - start number to begin indexing
89+ * @param {Object } fileInfo - fileInfo to attach to created nodes
90+ */
91+ function parseNode ( str , parseList , currentIndex , fileInfo , callback ) {
92+ var result , returnNodes = [ ] ;
93+ var parser = parserInput ;
94+
95+ try {
96+ parser . start ( str , false , function fail ( msg , index ) {
97+ callback ( {
98+ message : msg ,
99+ index : index + currentIndex
100+ } ) ;
101+ } ) ;
102+ for ( var x = 0 , p , i ; ( p = parseList [ x ] ) ; x ++ ) {
103+ i = parser . i ;
104+ result = parsers [ p ] ( ) ;
105+ if ( result ) {
106+ result . _index = i + currentIndex ;
107+ result . _fileInfo = fileInfo ;
108+ returnNodes . push ( result ) ;
109+ }
110+ else {
111+ returnNodes . push ( null ) ;
112+ }
113+ }
114+
115+ var endInfo = parser . end ( ) ;
116+ if ( endInfo . isFinished ) {
117+ callback ( null , returnNodes ) ;
118+ }
119+ else {
120+ callback ( true , null ) ;
121+ }
122+ } catch ( e ) {
123+ throw new LessError ( {
124+ index : e . index + currentIndex ,
125+ message : e . message
126+ } , imports , fileInfo . filename ) ;
127+ }
128+ }
129+
83130 //
84131 // The Parser
85132 //
86133 return {
87134 parserInput : parserInput ,
88135 imports : imports ,
89136 fileInfo : fileInfo ,
137+ parseNode : parseNode ,
90138 //
91139 // Parse an input string into an abstract syntax tree,
92140 // @param str A string containing 'less' markup
@@ -133,8 +181,8 @@ var Parser = function Parser(context, imports, fileInfo) {
133181 } ) ;
134182
135183 tree . Node . prototype . parse = this ;
136-
137- root = new ( tree . Ruleset ) ( null , this . parsers . primary ( ) ) ;
184+ root = new tree . Ruleset ( null , this . parsers . primary ( ) ) ;
185+ tree . Node . prototype . rootNode = root ;
138186 root . root = true ;
139187 root . firstRoot = true ;
140188
@@ -1008,7 +1056,7 @@ var Parser = function Parser(context, imports, fileInfo) {
10081056 if ( ! e ) {
10091057 parserInput . save ( ) ;
10101058 if ( parserInput . $char ( '(' ) ) {
1011- if ( ( v = this . selector ( ) ) && parserInput . $char ( ')' ) ) {
1059+ if ( ( v = this . selector ( false ) ) && parserInput . $char ( ')' ) ) {
10121060 e = new ( tree . Paren ) ( v ) ;
10131061 parserInput . forget ( ) ;
10141062 } else {
@@ -1059,14 +1107,8 @@ var Parser = function Parser(context, imports, fileInfo) {
10591107 }
10601108 } ,
10611109 //
1062- // A CSS selector (see selector below)
1063- // with less extensions e.g. the ability to extend and guard
1064- //
1065- lessSelector : function ( ) {
1066- return this . selector ( true ) ;
1067- } ,
1068- //
10691110 // A CSS Selector
1111+ // with less extensions e.g. the ability to extend and guard
10701112 //
10711113 // .class > div + h1
10721114 // li a:hover
@@ -1075,7 +1117,7 @@ var Parser = function Parser(context, imports, fileInfo) {
10751117 //
10761118 selector : function ( isLess ) {
10771119 var index = parserInput . i , elements , extendList , c , e , allExtends , when , condition ;
1078-
1120+ isLess = isLess !== false ;
10791121 while ( ( isLess && ( extendList = this . extend ( ) ) ) || ( isLess && ( when = parserInput . $str ( "when" ) ) ) || ( e = this . element ( ) ) ) {
10801122 if ( when ) {
10811123 condition = expect ( this . conditions , 'expected condition' ) ;
@@ -1165,7 +1207,7 @@ var Parser = function Parser(context, imports, fileInfo) {
11651207 }
11661208
11671209 while ( true ) {
1168- s = this . lessSelector ( ) ;
1210+ s = this . selector ( ) ;
11691211 if ( ! s ) {
11701212 break ;
11711213 }
0 commit comments