aboutsummaryrefslogtreecommitdiffstats
path: root/Bin
diff options
context:
space:
mode:
authorIvan Enderlin <ivan.enderlin@hoa-project.net>2013-01-05 08:10:50 +0100
committerIvan Enderlin <ivan.enderlin@hoa-project.net>2013-01-05 08:10:50 +0100
commitd0835c3a63f27c405b45ad4a1c33f37c871924e4 (patch)
treee74e2195674a2e4a44bfba875fa9fddb07d69872 /Bin
parentc186dd66ba3359e7b05b806002c9b5723dec20b0 (diff)
downloadCompiler-d0835c3a63f27c405b45ad4a1c33f37c871924e4.zip
Compiler-d0835c3a63f27c405b45ad4a1c33f37c871924e4.tar.gz
Compiler-d0835c3a63f27c405b45ad4a1c33f37c871924e4.tar.bz2
Add -t/--trace to print trace (if exists).
Diffstat (limited to 'Bin')
-rw-r--r--Bin/Pp.php49
1 files changed, 47 insertions, 2 deletions
diff --git a/Bin/Pp.php b/Bin/Pp.php
index 23a736b..70e8163 100644
--- a/Bin/Pp.php
+++ b/Bin/Pp.php
@@ -72,6 +72,7 @@ class Pp extends \Hoa\Console\Dispatcher\Kit {
protected $options = array(
array('visitor', \Hoa\Console\GetOption::REQUIRED_ARGUMENT, 'v'),
array('visitor-class', \Hoa\Console\GetOption::REQUIRED_ARGUMENT, 'c'),
+ array('trace', \Hoa\Console\GetOption::NO_ARGUMENT, 't'),
array('help', \Hoa\Console\GetOption::NO_ARGUMENT, 'h'),
array('help', \Hoa\Console\GetOption::NO_ARGUMENT, '?')
);
@@ -87,7 +88,7 @@ class Pp extends \Hoa\Console\Dispatcher\Kit {
public function main ( ) {
$visitor = null;
- $debug = false;
+ $trace = false;
while(false !== $c = $this->getOption($v)) switch($c) {
@@ -107,6 +108,10 @@ class Pp extends \Hoa\Console\Dispatcher\Kit {
$visitor = str_replace('.', '\\', $v);
break;
+ case 't':
+ $trace = true;
+ break;
+
case '__ambiguous':
$this->resolveOptionAmbiguity($v);
break;
@@ -127,7 +132,23 @@ class Pp extends \Hoa\Console\Dispatcher\Kit {
new \Hoa\File\Read($grammar)
);
$data = new \Hoa\File\Read($language);
- $ast = $compiler->parse($data->readAll());
+
+ try {
+
+ $ast = $compiler->parse($data->readAll());
+ }
+ catch ( \Hoa\Compiler\Exception $e ) {
+
+ $this->printTrace($compiler);
+ throw $e;
+
+ return 1;
+ }
+
+ if(true === $trace) {
+
+ $this->printTrace($compiler);
+ }
if(null !== $visitor) {
@@ -139,6 +160,29 @@ class Pp extends \Hoa\Console\Dispatcher\Kit {
}
/**
+ * Print trace.
+ *
+ * @access protected
+ * @param \Hoa\Compiler\Llk\Parser $compiler Compiler.
+ * @return void
+ */
+ protected function printTrace ( \Hoa\Compiler\Llk\Parser $compiler ) {
+
+ $i = 0;
+
+ foreach($compiler->getTrace() as $element)
+ if($element instanceof \Hoa\Compiler\Llk\Rule\Entry)
+ echo str_repeat('> ', ++$i), 'enter ', $element->getRule(), "\n";
+ elseif($element instanceof \Hoa\Compiler\Llk\Rule\Token)
+ echo str_repeat(' ', $i + 1), 'token ',$element->getTokenName(),
+ ', consumed ', $element->getValue(), "\n";
+ else
+ echo str_repeat('< ', $i--), 'ekzit ', $element->getRule(), "\n";
+
+ return;
+ }
+
+ /**
* The command usage.
*
* @access public
@@ -151,6 +195,7 @@ class Pp extends \Hoa\Console\Dispatcher\Kit {
$this->makeUsageOptionsList(array(
'v' => 'Visitor name (only “dump” is supported).',
'c' => 'Visitor classname (using . instead of \ works).',
+ 't' => 'Print trace.',
'help' => 'This help.'
)), "\n";