Skip to content

Commit be402da

Browse files
committed
better error printing for properties that originated in other files
1 parent 801eeca commit be402da

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

lessc.inc.php

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ class lessc {
5555
public $imPrefix = '!'; // special character to add !important
5656
public $parentSelector = '&';
5757

58+
// set the the parser that generated the current line when compiling
59+
// so we know how to create error messages
60+
protected $sourceParser = null;
61+
5862
static protected $precedence = array(
5963
'=<' => 0,
6064
'>=' => 0,
@@ -982,7 +986,8 @@ function mixImports($block) {
982986
if ($prop[0] == 'import') {
983987
list(, $path) = $prop;
984988
$this->addParsedFile($path);
985-
$root = $this->createChild($path)->parseTree();
989+
$child_less = $this->createChild($path);
990+
$root = $child_less->parseTree();
986991

987992
$root->parent = $block;
988993
$this->mixImports($root);
@@ -992,7 +997,10 @@ function mixImports($block) {
992997

993998
// splice in all the props
994999
foreach ($root->props as $sub_prop) {
995-
// TODO fix the position to point to right file
1000+
if (isset($sub_prop[-1])) {
1001+
// leave a reference to the imported file for error messages
1002+
$sub_prop[-1] = array($child_less, $sub_prop[-1]);
1003+
}
9961004
$props[] = $sub_prop;
9971005
}
9981006
} else {
@@ -1264,8 +1272,16 @@ function zipSetArgs($args, $values) {
12641272

12651273
// compile a prop and update $lines or $blocks appropriately
12661274
function compileProp($prop, $block, $tags, &$_lines, &$_blocks) {
1275+
// set error position context
12671276
if (isset($prop[-1])) {
1268-
$this->count = $prop[-1];
1277+
if (is_array($prop[-1])) {
1278+
list($less, $count) = $prop[-1];
1279+
$parentParser = $this->sourceParser;
1280+
$this->sourceParser = $less;
1281+
$this->count = $count;
1282+
} else {
1283+
$this->count = $prop[-1];
1284+
}
12691285
} else {
12701286
$this->count = -1;
12711287
}
@@ -1335,6 +1351,10 @@ function compileProp($prop, $block, $tags, &$_lines, &$_blocks) {
13351351
default:
13361352
$this->throwError("unknown op: {$prop[0]}\n");
13371353
}
1354+
1355+
if (isset($parentParser)) {
1356+
$this->sourceParser = $parentParser;
1357+
}
13381358
}
13391359

13401360

@@ -2252,7 +2272,10 @@ function parse($str = null, $initial_variables = null) {
22522272
* Uses the current value of $this->count to show line and line number
22532273
*/
22542274
function throwError($msg = 'parse error') {
2255-
if ($this->count > 0) {
2275+
if (!empty($this->sourceParser)) {
2276+
$this->sourceParser->count = $this->count;
2277+
return $this->sourceParser->throwError($msg);
2278+
} elseif ($this->count > 0) {
22562279
$line = $this->line + substr_count(substr($this->buffer, 0, $this->count), "\n");
22572280
if (isset($this->fileName)) {
22582281
$loc = $this->fileName.' on line '.$line;
@@ -2262,9 +2285,9 @@ function throwError($msg = 'parse error') {
22622285

22632286
if ($this->peek("(.*?)(\n|$)", $m))
22642287
throw new exception($msg.': failed at `'.$m[1].'` '.$loc);
2265-
} else {
2266-
throw new exception($msg);
22672288
}
2289+
2290+
throw new exception($msg);
22682291
}
22692292

22702293
/**

0 commit comments

Comments
 (0)