Skip to content

Commit 146375d

Browse files
committed
Some more explicit top-level namespacing and some formatting.
1 parent d4422d0 commit 146375d

File tree

9 files changed

+115
-107
lines changed

9 files changed

+115
-107
lines changed

lib/hafriedlander/Peg/Compiler/PHPBuilder.php

Lines changed: 73 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -5,125 +5,126 @@
55
class PHPBuilder {
66

77
static function build () {
8-
return new PHPBuilder() ;
8+
return new PHPBuilder();
99
}
1010

1111
function __construct() {
12-
$this->lines = [] ;
12+
$this->lines = [];
1313
}
1414

1515
function l() {
16-
foreach ( \func_get_args() as $lines ) {
17-
if ( !$lines ) continue ;
16+
foreach (\func_get_args() as $lines) {
17+
if (!$lines) continue;
1818

19-
if ( is_string( $lines ) ) $lines = preg_split( '/\r\n|\r|\n/', $lines ) ;
20-
if ( !$lines ) continue ;
19+
if (is_string($lines)) $lines = preg_split('/\r\n|\r|\n/', $lines);
20+
if (!$lines) continue;
2121

22-
if ( $lines instanceof PHPBuilder ) $lines = $lines->lines ;
23-
else $lines = \array_map( 'ltrim', $lines ) ;
24-
if ( !$lines ) continue ;
22+
if ($lines instanceof PHPBuilder) $lines = $lines->lines;
23+
else $lines = \array_map('ltrim', $lines);
24+
if (!$lines) continue;
2525

26-
$this->lines = \array_merge( $this->lines, $lines ) ;
26+
$this->lines = \array_merge($this->lines, $lines);
2727
}
28-
return $this ;
28+
return $this;
2929
}
3030

3131
function b() {
32-
$args = \func_get_args() ;
33-
$entry = \array_shift( $args ) ;
32+
$args = \func_get_args();
33+
$entry = \array_shift($args);
3434

35-
$block = new PHPBuilder() ;
36-
call_user_func_array( array( $block, 'l' ), $args ) ;
35+
$block = new PHPBuilder();
36+
call_user_func_array(array($block, 'l'), $args);
3737

38-
$this->lines[] = array( $entry, $block->lines ) ;
38+
$this->lines[] = array($entry, $block->lines);
3939

40-
return $this ;
40+
return $this;
4141
}
4242

43-
function replace( $replacements, &$array = \null ) {
44-
if ( $array === \null ) {
45-
unset( $array ) ;
46-
$array =& $this->lines ;
43+
function replace($replacements, &$array = \null) {
44+
if ($array === \null) {
45+
unset($array);
46+
$array =& $this->lines;
4747
}
4848

49-
$i = 0 ;
50-
while ( $i < \count( $array ) ) {
49+
$i = 0;
50+
while ($i < \count($array)) {
5151

5252
/* Recurse into blocks */
53-
if ( \is_array( $array[$i] ) ) {
54-
$this->replace( $replacements, $array[$i][1] ) ;
53+
if (\is_array($array[$i])) {
54+
$this->replace($replacements, $array[$i][1]);
5555

56-
if ( \count( $array[$i][1] ) == 0 ) {
57-
$nextelse = isset( $array[$i+1] ) && \is_array( $array[$i+1] ) && \preg_match( '/^\s*else\s*$/i', $array[$i+1][0] ) ;
56+
if (\count($array[$i][1]) == 0) {
57+
$nextelse = isset($array[$i+1]) && \is_array($array[$i+1]) && \preg_match('/^\s*else\s*$/i', $array[$i+1][0]);
5858

59-
$delete = \preg_match( '/^\s*else\s*$/i', $array[$i][0] ) ;
60-
$delete = $delete || ( \preg_match( '/^\s*if\s*\(/i', $array[$i][0] ) && !$nextelse ) ;
59+
$delete = \preg_match('/^\s*else\s*$/i', $array[$i][0]);
60+
$delete = $delete || (\preg_match('/^\s*if\s*\(/i', $array[$i][0]) && !$nextelse);
6161

62-
if ( $delete ) {
62+
if ($delete) {
6363
// Is this always safe? Not if the expression has side-effects.
64-
// print "/* REMOVING EMPTY BLOCK: " . $array[$i][0] . "*/\n" ;
65-
\array_splice( $array, $i, 1 ) ;
66-
continue ;
64+
// print "/* REMOVING EMPTY BLOCK: " . $array[$i][0] . "*/\n";
65+
\array_splice($array, $i, 1);
66+
continue;
6767
}
6868
}
69-
}
70-
71-
/* Handle replacing lines with \null to remove, or string, array of strings or PHPBuilder to replace */
72-
else {
73-
if ( \array_key_exists( $array[$i], $replacements ) ) {
74-
$rep = $replacements[$array[$i]] ;
75-
76-
if ( $rep === \null ) {
77-
\array_splice( $array, $i, 1 ) ;
78-
continue ;
69+
} else {
70+
/* Handle replacing lines with \null to remove, or string, array of strings or PHPBuilder to replace */
71+
if (\array_key_exists($array[$i], $replacements)) {
72+
$rep = $replacements[$array[$i]];
73+
74+
if ($rep === \null) {
75+
\array_splice($array, $i, 1);
76+
continue;
7977
}
8078

81-
if ( is_string( $rep ) ) {
82-
$array[$i] = $rep ;
83-
$i++ ;
84-
continue ;
79+
if (is_string($rep)) {
80+
$array[$i] = $rep;
81+
$i++ ;
82+
continue;
8583
}
8684

87-
if ( $rep instanceof PHPBuilder ) $rep = $rep->lines ;
85+
if ($rep instanceof PHPBuilder) $rep = $rep->lines;
8886

89-
if ( \is_array( $rep ) ) {
90-
\array_splice( $array, $i, 1, $rep ) ; $i += \count( $rep ) + 1 ;
91-
continue ;
87+
if (\is_array($rep)) {
88+
\array_splice($array, $i, 1, $rep); $i += \count($rep) + 1;
89+
continue;
9290
}
9391

94-
throw new \Exception('Unknown type passed to PHPBuilder#replace') ;
92+
throw new \Exception('Unknown type passed to PHPBuilder#replace');
9593
}
9694
}
9795

98-
$i++ ;
96+
$i++;
9997
}
10098

101-
return $this ;
99+
return $this;
102100
}
103101

104-
function render( $array = \null, $indent = "" ) {
105-
if ( $array === \null ) $array = $this->lines ;
102+
function render($array = \null, $indent = "") {
106103

107-
$out = [] ;
108-
foreach( $array as $line ) {
109-
if ( \is_array( $line ) ) {
110-
[$entry, $block] = $line ;
111-
$str = $this->render( $block, $indent . "\t" ) ;
104+
if ($array === \null) {
105+
$array = $this->lines;
106+
}
112107

113-
if ( \strlen( $str ) < 40 ) {
114-
$out[] = $indent . $entry . ' { ' . \ltrim( $str ) . ' }' ;
108+
$out = [];
109+
foreach($array as $line) {
110+
if (\is_array($line)) {
111+
[$entry, $block] = $line;
112+
$str = $this->render($block, $indent . "\t");
113+
114+
if (\strlen($str) < 40) {
115+
$out[] = $indent . $entry . ' { ' . \ltrim($str) . ' }';
116+
} else {
117+
$out[] = $indent . $entry . ' {';
118+
$out[] = $str;
119+
$out[] = $indent . '}';
115120
}
116-
else {
117-
$out[] = $indent . $entry . ' {' ;
118-
$out[] = $str ;
119-
$out[] = $indent . '}' ;
120-
}
121-
}
122-
else {
123-
$out[] = $indent . $line ;
121+
} else {
122+
$out[] = $indent . $line;
124123
}
125124
}
126125

127-
return \implode(\PHP_EOL, $out ) ;
126+
return \implode(\PHP_EOL, $out);
127+
128128
}
129+
129130
}

lib/hafriedlander/Peg/Compiler/Rule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ function compile($indent) {
298298
$match->l("protected \$match_{$function_name}_typestack = $typestack;");
299299

300300
$match->b( "function match_{$function_name} (\$stack = [])",
301-
'$matchrule = "'.$function_name.'"; $result = $this->construct($matchrule, $matchrule, '.$arguments.');$newStack = \array_merge($stack, [$result]);',
301+
'$matchrule = "'.$function_name.'"; $result = $this->construct($matchrule, $matchrule, '.$arguments.'); $newStack = \array_merge($stack, [$result]);',
302302
$this->parsed->compile()->replace(array(
303303
'MATCH' => 'return $this->finalise($result);',
304304
'FAIL' => 'return \false;'

lib/hafriedlander/Peg/Compiler/Token/Recurse.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,22 @@ function match_code( $value ) {
2121
if ( \hafriedlander\Peg\Compiler::$debug ) {
2222
$debug_header = PHPBuilder::build()
2323
->l(
24-
'$indent = str_repeat( "\e[90m| \e[0m", $this->depth / 2);',
24+
'$indent = str_repeat("\e[90m| \e[0m", $this->depth / 2);',
2525
'$this->depth += 2;',
26-
'$sub = ( strlen( $this->string ) - $this->pos > 40 ) ? ( substr( $this->string, $this->pos, 40 ) . "..." ) : substr( $this->string, $this->pos );',
27-
'$sub = preg_replace( \'/(\r|\n)+/\', " {NL} ", $sub );',
28-
sprintf('print( $indent."Matching \e[32m%s\e[0m \"\e[36m".$sub."\e[0m\" \n" );', trim($function, "'"))
26+
'$sub = (strlen( $this->string ) - $this->pos > 40) ? substr($this->string, $this->pos, 40) . "...") : substr($this->string, $this->pos);',
27+
'$sub = preg_replace(\'/(\r|\n)+/\', " {NL} ", $sub);',
28+
sprintf('print $indent . "Matching \e[32m%s\e[0m \"\e[36m".$sub."\e[0m\" \n";', trim($function, "'"))
2929
);
3030

3131
$debug_match = PHPBuilder::build()
3232
->l(
33-
'print( $indent."\e[1m\e[42mOK\n\e[0m" );',
33+
'print $indent . "\e[1m\e[42mOK\n\e[0m";',
3434
'$this->depth -= 2;'
3535
);
3636

3737
$debug_fail = PHPBuilder::build()
3838
->l(
39-
'print( $indent."-\n" );',
39+
'print $indent . "-\n";',
4040
'$this->depth -= 2;'
4141
);
4242
}
@@ -52,8 +52,8 @@ function match_code( $value ) {
5252
PHPBuilder::build()->l(
5353
$debug_match,
5454
$this->tag === \false ?
55-
'$this->store( $result, $subres );' :
56-
'$this->store( $result, $subres, "'.$storetag.'" );'
55+
'$this->store($result, $subres);' :
56+
'$this->store($result, $subres, "'.$storetag.'");'
5757
),
5858
PHPBuilder::build()->l(
5959
$debug_fail

lib/hafriedlander/Peg/Compiler/Token/Regex.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace hafriedlander\Peg\Compiler\Token;
44

5-
use hafriedlander\Peg\Compiler\PHPBuilder;
6-
75
class Regex extends Expressionable {
86
static function escape( $rx ) {
97
$rx = \str_replace( "'", "\\'", $rx ) ;

lib/hafriedlander/Peg/Compiler/Token/Sequence.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,26 @@
66
use hafriedlander\Peg\Compiler\PHPBuilder;
77

88
class Sequence extends Token {
9-
function __construct( $value ) {
10-
parent::__construct( 'sequence', $value ) ;
9+
10+
function __construct($value) {
11+
parent::__construct('sequence', $value);
1112
}
1213

13-
function match_code( $value ) {
14-
$code = PHPBuilder::build() ;
15-
foreach( $value as $token ) {
14+
function match_code($value) {
15+
$code = PHPBuilder::build();
16+
17+
foreach ($value as $token) {
1618
$code->l(
1719
$token->compile()->replace([
1820
'MATCH' => \null,
1921
'FAIL' => 'FBREAK'
2022
])
2123
);
2224
}
23-
$code->l( 'MBREAK' );
2425

25-
return $this->match_fail_block( $code ) ;
26+
$code->l('MBREAK');
27+
return $this->match_fail_block($code);
28+
2629
}
30+
2731
}

lib/hafriedlander/Peg/Compiler/Token/Terminal.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55
use hafriedlander\Peg\Compiler\Token;
66

77
abstract class Terminal extends Token {
8-
function set_text( $text ) {
8+
9+
function set_text($text) {
910
return $this->silent ? \null : '$result["text"] .= ' . $text . ';';
1011
}
1112

12-
protected function match_code( $value ) {
13-
return $this->match_fail_conditional( '( $subres = $this->'.$this->type.'( '.$value.' ) ) !== \false',
13+
protected function match_code($value) {
14+
return $this->match_fail_conditional(
15+
'($subres = $this->' . $this->type . '(' . $value . ')) !== \false',
1416
$this->set_text('$subres')
1517
);
1618
}
19+
1720
}

lib/hafriedlander/Peg/Compiler/Token/Whitespace.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
namespace hafriedlander\Peg\Compiler\Token;
44

55
class Whitespace extends Terminal {
6-
function __construct( $optional ) {
7-
parent::__construct( 'whitespace', $optional ) ;
6+
7+
function __construct($optional) {
8+
parent::__construct('whitespace', $optional);
89
}
910

1011
/* Call recursion indirectly */
11-
function match_code( $value ) {
12-
$code = parent::match_code( '' ) ;
13-
return $value ? $code->replace( ['FAIL' => \null] ) : $code ;
12+
function match_code($value) {
13+
$code = parent::match_code('');
14+
return $value ? $code->replace(['FAIL' => \null]) : $code;
1415
}
16+
1517
}

lib/hafriedlander/Peg/Parser/Basic.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,21 @@ function expression( $result, $stack, $value ) {
4848
$stack[] = $result; $rv = \false;
4949

5050
/* Search backwards through the sub-expression stacks */
51-
for ( $i = count($stack) - 1 ; $i >= 0 ; $i-- ) {
51+
for ( $i = \count($stack) - 1 ; $i >= 0 ; $i-- ) {
5252
$node = $stack[$i];
5353

5454
if ( isset($node[$value]) ) { $rv = $node[$value]; break; }
5555

5656
foreach ($this->typestack($node['_matchrule']) as $type) {
5757
$callback = array($this, "{$type}_DLR{$value}");
58-
if ( is_callable( $callback ) ) { $rv = call_user_func( $callback ) ; if ($rv !== \false) break; }
58+
if ( is_callable( $callback ) ) { $rv = \call_user_func( $callback ) ; if ($rv !== \false) break; }
5959
}
6060
}
6161

6262
if ($rv === \false) $rv = @$this->$value;
6363
if ($rv === \false) $rv = @$this->$value();
6464

65-
return is_array($rv) ? $rv['text'] : ($rv ? $rv : '');
65+
return \is_array($rv) ? $rv['text'] : ($rv ? $rv : '');
6666
}
6767

6868
function packhas( $key, $pos ) {
@@ -92,7 +92,7 @@ function construct( $matchrule, $name, $arguments = \null ) {
9292
foreach ($this->typestack($matchrule) as $type) {
9393
$callback = array( $this, "{$type}__construct" ) ;
9494
if ( \is_callable( $callback ) ) {
95-
call_user_func_array( $callback, array( &$result ) ) ;
95+
\call_user_func_array( $callback, array( &$result ) ) ;
9696
break;
9797
}
9898
}
@@ -105,7 +105,7 @@ function finalise( &$result ) {
105105
foreach ($this->typestack($result['_matchrule']) as $type) {
106106
$callback = array( $this, "{$type}__finalise" ) ;
107107
if ( \is_callable( $callback ) ) {
108-
call_user_func_array( $callback, array( &$result ) ) ;
108+
\call_user_func_array( $callback, array( &$result ) ) ;
109109
break;
110110
}
111111
}
@@ -121,13 +121,13 @@ function store ( &$result, $subres, $storetag = \null ) {
121121
foreach ($this->typestack($result['_matchrule']) as $type) {
122122
$callback = array( $this, $storetag ? "{$type}_{$storetag}" : "{$type}_{$subres['name']}" ) ;
123123
if ( \is_callable( $callback ) ) {
124-
call_user_func_array( $callback, array( &$result, $subres ) ) ;
124+
\call_user_func_array( $callback, array( &$result, $subres ) ) ;
125125
$storecalled = \true; break;
126126
}
127127

128128
$globalcb = array( $this, "{$type}_STR" ) ;
129129
if ( \is_callable( $globalcb ) ) {
130-
call_user_func_array( $globalcb, array( &$result, $subres ) ) ;
130+
\call_user_func_array( $globalcb, array( &$result, $subres ) ) ;
131131
$storecalled = \true; break;
132132
}
133133
}

0 commit comments

Comments
 (0)