Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
better error printing for properties that originated in other files
  • Loading branch information
leafo committed Feb 26, 2012
1 parent 801eeca commit be402da
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions lessc.inc.php
Expand Up @@ -55,6 +55,10 @@ class lessc {
public $imPrefix = '!'; // special character to add !important
public $parentSelector = '&';

// set the the parser that generated the current line when compiling
// so we know how to create error messages
protected $sourceParser = null;

static protected $precedence = array(
'=<' => 0,
'>=' => 0,
Expand Down Expand Up @@ -982,7 +986,8 @@ function mixImports($block) {
if ($prop[0] == 'import') {
list(, $path) = $prop;
$this->addParsedFile($path);
$root = $this->createChild($path)->parseTree();
$child_less = $this->createChild($path);
$root = $child_less->parseTree();

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

// splice in all the props
foreach ($root->props as $sub_prop) {
// TODO fix the position to point to right file
if (isset($sub_prop[-1])) {
// leave a reference to the imported file for error messages
$sub_prop[-1] = array($child_less, $sub_prop[-1]);
}
$props[] = $sub_prop;
}
} else {
Expand Down Expand Up @@ -1264,8 +1272,16 @@ function zipSetArgs($args, $values) {

// compile a prop and update $lines or $blocks appropriately
function compileProp($prop, $block, $tags, &$_lines, &$_blocks) {
// set error position context
if (isset($prop[-1])) {
$this->count = $prop[-1];
if (is_array($prop[-1])) {
list($less, $count) = $prop[-1];
$parentParser = $this->sourceParser;
$this->sourceParser = $less;
$this->count = $count;
} else {
$this->count = $prop[-1];
}
} else {
$this->count = -1;
}
Expand Down Expand Up @@ -1335,6 +1351,10 @@ function compileProp($prop, $block, $tags, &$_lines, &$_blocks) {
default:
$this->throwError("unknown op: {$prop[0]}\n");
}

if (isset($parentParser)) {
$this->sourceParser = $parentParser;
}
}


Expand Down Expand Up @@ -2252,7 +2272,10 @@ function parse($str = null, $initial_variables = null) {
* Uses the current value of $this->count to show line and line number
*/
function throwError($msg = 'parse error') {
if ($this->count > 0) {
if (!empty($this->sourceParser)) {
$this->sourceParser->count = $this->count;
return $this->sourceParser->throwError($msg);
} elseif ($this->count > 0) {
$line = $this->line + substr_count(substr($this->buffer, 0, $this->count), "\n");
if (isset($this->fileName)) {
$loc = $this->fileName.' on line '.$line;
Expand All @@ -2262,9 +2285,9 @@ function throwError($msg = 'parse error') {

if ($this->peek("(.*?)(\n|$)", $m))
throw new exception($msg.': failed at `'.$m[1].'` '.$loc);
} else {
throw new exception($msg);
}

throw new exception($msg);
}

/**
Expand Down

0 comments on commit be402da

Please sign in to comment.