Skip to content

Commit 3830761

Browse files
committed
tree dumper
1 parent c546803 commit 3830761

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

lessc.inc.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -1675,7 +1675,8 @@ protected function createChild($fname) {
16751675
return $less;
16761676
}
16771677

1678-
protected function parseTree($str = null) {
1678+
// parse code and return intermediate tree
1679+
public function parseTree($str = null) {
16791680
$this->prepareParser(is_null($str) ? $this->buffer : $str);
16801681
while (false !== $this->parseChunk());
16811682

plessc

+41-5
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,14 @@ foreach ($argv as $loc => $a) {
4747
}
4848
}
4949

50-
function has($o, &$loc = null)
51-
{
50+
function has($o, &$loc = null) {
5251
global $opts;
5352
if (!isset($opts[$o])) return false;
5453
$loc = $opts[$o];
5554
return true;
5655
}
5756

58-
function hasValue($o, &$value = null)
59-
{
57+
function hasValue($o, &$value = null) {
6058
global $argv;
6159
if (!has($o,$loc)) return false;
6260
if (!isset($argv[$loc+1])) return false;
@@ -136,9 +134,47 @@ if (!$fname = array_shift($argv)) {
136134
exit(1);
137135
}
138136

137+
function dumpValue($node, $depth = 0) {
138+
if (is_object($node)) {
139+
$indent = str_repeat(" ", $depth);
140+
$out = array();
141+
foreach ($node->props as $prop) {
142+
$out[] = $indent . dumpValue($prop, $depth + 1);
143+
}
144+
$out = implode("\n", $out);
145+
if (!empty($node->tags)) {
146+
$out = "+ ".implode(", ", $node->tags)."\n".$out;
147+
}
148+
return $out;
149+
} elseif (is_array($node)) {
150+
$type = $node[0];
151+
if ($type == "block")
152+
return dumpValue($node[1], $depth);
153+
154+
$out = array();
155+
foreach ($node as $value) {
156+
$out[] = dumpValue($value, $depth);
157+
}
158+
return "{ ".implode(", ", $out)." }";
159+
} else {
160+
if (is_string($node) && preg_match("/[\s,]/", $node)) {
161+
return '"'.$node.'"';
162+
}
163+
return $node; // normal value
164+
}
165+
}
166+
139167
try {
140168
$l = new lessc($fname);
141-
$out = $l->parse();
169+
if (has("T") || has("X")) {
170+
$t = $l->parseTree();
171+
if (has("X"))
172+
$out = print_r($t, 1);
173+
else
174+
$out = dumpValue($t)."\n";
175+
} else {
176+
$out = $l->parse();
177+
}
142178

143179
if (!$fout = array_shift($argv)) {
144180
echo $out;

0 commit comments

Comments
 (0)