aboutsummaryrefslogtreecommitdiffstats
path: root/Bin
diff options
context:
space:
mode:
authorIvan Enderlin <ivan.enderlin@hoa-project.net>2015-05-04 20:11:09 +0200
committerIvan Enderlin <ivan.enderlin@hoa-project.net>2015-05-05 09:40:42 +0200
commit5b971e8745c6428d9bd7662e1ed2610681c1851e (patch)
tree914269afc3030c0384be6a3ca28345bfcd1d3372 /Bin
parent366ccd14d612fdc172e4ad5bae7bf24eb0d25caa (diff)
downloadCompiler-5b971e8745c6428d9bd7662e1ed2610681c1851e.zip
Compiler-5b971e8745c6428d9bd7662e1ed2610681c1851e.tar.gz
Compiler-5b971e8745c6428d9bd7662e1ed2610681c1851e.tar.bz2
Move to PSR-1 and PSR-2.
Diffstat (limited to 'Bin')
-rw-r--r--Bin/Pp.php162
1 files changed, 81 insertions, 81 deletions
diff --git a/Bin/Pp.php b/Bin/Pp.php
index df0c2e3..d2252b8 100644
--- a/Bin/Pp.php
+++ b/Bin/Pp.php
@@ -8,7 +8,7 @@
*
* New BSD License
*
- * Copyright © 2007-2015, Ivan Enderlin. All rights reserved.
+ * Copyright © 2007-2015, Hoa community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -45,17 +45,15 @@ use Hoa\File;
*
* Play with PP.
*
- * @author Ivan Enderlin <ivan.enderlin@hoa-project.net>
- * @copyright Copyright © 2007-2015 Ivan Enderlin.
+ * @copyright Copyright © 2007-2015 Hoa community
* @license New BSD License
*/
-
-class Pp extends Console\Dispatcher\Kit {
-
+class Pp extends Console\Dispatcher\Kit
+{
/**
* Options description.
*
- * @var \Hoa\Compiler\Bin\Pp array
+ * @var array
*/
protected $options = [
['visitor', Console\GetOption::REQUIRED_ARGUMENT, 'v'],
@@ -71,68 +69,69 @@ class Pp extends Console\Dispatcher\Kit {
/**
* The entry method.
*
- * @access public
* @return int
*/
- public function main ( ) {
-
+ public function main()
+ {
$visitor = null;
$tokenSequence = false;
$trace = false;
- while(false !== $c = $this->getOption($v)) switch($c) {
+ while (false !== $c = $this->getOption($v)) {
+ switch ($c) {
+ case 'v':
+ switch (strtolower($v)) {
+ case 'dump':
+ $visitor = 'Hoa\Compiler\Visitor\Dump';
- case 'v':
- switch(strtolower($v)) {
+ break;
- case 'dump':
- $visitor = 'Hoa\Compiler\Visitor\Dump';
- break;
+ default:
+ return $this->usage();
+ }
- default:
- return $this->usage();
- }
- break;
+ break;
+
+ case 'c':
+ $visitor = str_replace('.', '\\', $v);
+
+ break;
+
+ case 's':
+ $tokenSequence = true;
- case 'c':
- $visitor = str_replace('.', '\\', $v);
- break;
+ break;
- case 's':
- $tokenSequence = true;
- break;
+ case 't':
+ $trace = true;
- case 't':
- $trace = true;
- break;
+ break;
- case '__ambiguous':
- $this->resolveOptionAmbiguity($v);
- break;
+ case '__ambiguous':
+ $this->resolveOptionAmbiguity($v);
- case 'h':
- case '?':
- default:
- return $this->usage();
- break;
+ break;
+
+ case 'h':
+ case '?':
+ default:
+ return $this->usage();
+ }
}
$this->parser->listInputs($grammar, $language);
- if(empty($grammar) || (empty($language) && '0' !== $language))
+ if (empty($grammar) || (empty($language) && '0' !== $language)) {
return $this->usage();
+ }
$compiler = Compiler\Llk::load(new File\Read($grammar));
$data = new File\Read($language);
try {
-
$ast = $compiler->parse($data->readAll());
- }
- catch ( Compiler\Exception $e ) {
-
- if(true === $tokenSequence) {
-
+ } catch (Compiler\Exception $e) {
+ if (true === $tokenSequence) {
$this->printTokenSequence($compiler);
echo "\n\n";
}
@@ -142,20 +141,17 @@ class Pp extends Console\Dispatcher\Kit {
return 1;
}
- if(true === $tokenSequence) {
-
+ if (true === $tokenSequence) {
$this->printTokenSequence($compiler);
echo "\n\n";
}
- if(true === $trace) {
-
+ if (true === $trace) {
$this->printTrace($compiler);
echo "\n\n";
}
- if(null !== $visitor) {
-
+ if (null !== $visitor) {
$visitor = dnew($visitor);
echo $visitor->visit($ast);
}
@@ -166,32 +162,36 @@ class Pp extends Console\Dispatcher\Kit {
/**
* Print trace.
*
- * @access protected
* @param \Hoa\Compiler\Llk\Parser $compiler Compiler.
* @return void
*/
- protected function printTrace ( Compiler\Llk\Parser $compiler ) {
-
+ protected function printTrace(Compiler\Llk\Parser $compiler)
+ {
$i = 0;
- foreach($compiler->getTrace() as $element)
- if($element instanceof Compiler\Llk\Rule\Entry) {
-
+ foreach ($compiler->getTrace() as $element) {
+ if ($element instanceof Compiler\Llk\Rule\Entry) {
$ruleName = $element->getRule();
$rule = $compiler->getRule($ruleName);
echo str_repeat('> ', ++$i), 'enter ', $ruleName;
- if(null !== $id = $rule->getNodeId())
+ if (null !== $id = $rule->getNodeId()) {
echo ' (', $id, ')';
+ }
echo "\n";
- }
- elseif($element instanceof Compiler\Llk\Rule\Token)
- echo str_repeat(' ', $i + 1), 'token ',$element->getTokenName(),
+ } elseif ($element instanceof 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";
+ } else {
+ echo
+ str_repeat('< ', $i--),
+ 'ekzit ', $element->getRule(), "\n";
+ }
+ }
return;
}
@@ -199,12 +199,11 @@ class Pp extends Console\Dispatcher\Kit {
/**
* Print token sequence.
*
- * @access protected
* @param \Hoa\Compiler\Llk\Parser $compiler Compiler.
* @return void
*/
- protected function printTokenSequence ( Compiler\Llk\Parser $compiler ) {
-
+ protected function printTokenSequence(Compiler\Llk\Parser $compiler)
+ {
$sequence = $compiler->getTokenSequence();
$format = '%' . (strlen((string) count($sequence)) + 1) . 's ' .
'%-13s %-20s %s %6s' . "\n";
@@ -220,7 +219,7 @@ class Pp extends Console\Dispatcher\Kit {
echo $header, str_repeat('-', strlen($header)), "\n";
- foreach($sequence as $i => $token)
+ foreach ($sequence as $i => $token) {
printf(
$format,
$i,
@@ -229,11 +228,12 @@ class Pp extends Console\Dispatcher\Kit {
30 < $token['length']
? mb_substr($token['value'], 0, 29) . '…'
: 'EOF' === $token['token']
- ? str_repeat(' ', 30)
- : $token['value'] .
- str_repeat(' ', 30 - $token['length']),
+ ? str_repeat(' ', 30)
+ : $token['value'] .
+ str_repeat(' ', 30 - $token['length']),
$token['offset']
);
+ }
return;
}
@@ -241,20 +241,20 @@ class Pp extends Console\Dispatcher\Kit {
/**
* The command usage.
*
- * @access public
* @return int
*/
- public function usage ( ) {
-
- echo 'Usage : compiler:pp <options> [grammar.pp] [language]', "\n",
- 'Options :', "\n",
- $this->makeUsageOptionsList([
- 'v' => 'Visitor name (only “dump” is supported).',
- 'c' => 'Visitor classname (using . instead of \ works).',
- 's' => 'Print token sequence.',
- 't' => 'Print trace.',
- 'help' => 'This help.'
- ]), "\n";
+ public function usage()
+ {
+ echo
+ 'Usage : compiler:pp <options> [grammar.pp] [language]', "\n",
+ 'Options :', "\n",
+ $this->makeUsageOptionsList([
+ 'v' => 'Visitor name (only “dump” is supported).',
+ 'c' => 'Visitor classname (using . instead of \ works).',
+ 's' => 'Print token sequence.',
+ 't' => 'Print trace.',
+ 'help' => 'This help.'
+ ]), "\n";
return;
}