wikipeg 4.0.2 → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/VERSION CHANGED
@@ -1 +1 @@
1
- 4.0.2
1
+ 5.0.0
@@ -225,7 +225,7 @@ let javascript = {
225
225
  if (opts.params.length) {
226
226
  keyParts = keyParts.concat(opts.params);
227
227
  }
228
- let storeRefs = opts.storeRefs.filter(function(part) {
228
+ const storeRefs = opts.storeRefs.filter(function(part) {
229
229
  return part !== '';
230
230
  }).map(function(part) {
231
231
  return ' ' + part;
package/lib/peg.js CHANGED
@@ -5,7 +5,7 @@ var arrays = require("./utils/arrays"),
5
5
 
6
6
  var PEG = {
7
7
  /* WikiPEG version (uses semantic versioning). */
8
- VERSION: "4.0.2",
8
+ VERSION: "5.0.0",
9
9
 
10
10
  GrammarError: require("./grammar-error"),
11
11
  parser: require("./parser"),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wikipeg",
3
- "version": "4.0.2",
3
+ "version": "5.0.0",
4
4
  "description": "Parser generator for JavaScript and PHP",
5
5
  "license": "MIT",
6
6
  "homepage": "https://gerrit.wikimedia.org/r/plugins/gitiles/wikipeg/",
@@ -40,7 +40,7 @@
40
40
  "url": "https://gerrit.wikimedia.org/r/wikipeg"
41
41
  },
42
42
  "devDependencies": {
43
- "eslint": "8.31.0",
43
+ "eslint": "8.57.0",
44
44
  "jasmine-node": "3.0.0"
45
45
  },
46
46
  "engines": {
@@ -5,31 +5,31 @@ namespace Wikimedia\WikiPEG;
5
5
  use InvalidArgumentException;
6
6
 
7
7
  class DefaultTracer implements Tracer {
8
- private $indentLevel = 0;
8
+ protected int $indentLevel = 0;
9
9
 
10
- public function trace( $event ) {
10
+ public function trace( array $event ): void {
11
11
  switch ( $event['type'] ) {
12
- case 'rule.enter':
13
- $this->log( $event );
14
- $this->indentLevel++;
15
- break;
12
+ case 'rule.enter':
13
+ $this->log( $event );
14
+ $this->indentLevel++;
15
+ break;
16
16
 
17
- case 'rule.match':
18
- $this->indentLevel--;
19
- $this->log( $event );
20
- break;
17
+ case 'rule.match':
18
+ $this->indentLevel--;
19
+ $this->log( $event );
20
+ break;
21
21
 
22
- case 'rule.fail':
23
- $this->indentLevel--;
24
- $this->log( $event );
25
- break;
22
+ case 'rule.fail':
23
+ $this->indentLevel--;
24
+ $this->log( $event );
25
+ break;
26
26
 
27
- default:
28
- throw new InvalidArgumentException( "Invalid event type {$event['type']}" );
27
+ default:
28
+ throw new InvalidArgumentException( "Invalid event type {$event['type']}" );
29
29
  }
30
30
  }
31
31
 
32
- private function log( $event ) {
32
+ protected function log( array $event ) {
33
33
  print str_pad(
34
34
  '' . $event['location'],
35
35
  20
@@ -40,7 +40,7 @@ class DefaultTracer implements Tracer {
40
40
  . "\n";
41
41
  }
42
42
 
43
- private function formatArgs( $argMap ) {
43
+ protected function formatArgs( ?array $argMap ): string {
44
44
  if ( !$argMap ) {
45
45
  return '';
46
46
  }
@@ -2,29 +2,31 @@
2
2
 
3
3
  namespace Wikimedia\WikiPEG;
4
4
 
5
+ use stdClass;
6
+
5
7
  abstract class PEGParserBase {
6
- protected static $FAILED;
7
- protected static $UNDEFINED;
8
- protected $currPos;
9
- protected $savedPos;
10
- protected $input;
11
- protected $inputLength;
12
- protected $options;
8
+ protected static ?stdClass $FAILED = null;
9
+ protected static ?stdClass $UNDEFINED = null;
10
+ protected int $currPos;
11
+ protected int $savedPos;
12
+ protected string $input;
13
+ protected int $inputLength;
14
+ protected array $options;
15
+ /** @var array */
13
16
  protected $cache;
14
17
 
15
18
  /** @var array<int,array{line:int,column:int,seenCR:bool}> */
16
- protected $posDetailsCache;
17
- protected $maxFailPos;
18
- protected $maxFailExpected;
19
+ protected array $posDetailsCache;
20
+ protected int $maxFailPos;
21
+ protected array $maxFailExpected;
19
22
 
20
23
  /** @var array Associative arrays of expectation info */
21
24
  protected $expectations;
22
25
 
23
26
  /** @var Expectation[] */
24
- private $expectationCache;
27
+ private array $expectationCache;
25
28
 
26
- /** @var Tracer */
27
- protected $tracer;
29
+ protected Tracer $tracer;
28
30
 
29
31
  public function __construct() {
30
32
  if ( !self::$FAILED ) {
@@ -35,7 +37,8 @@ abstract class PEGParserBase {
35
37
  }
36
38
  }
37
39
 
38
- protected function traceCall( $parseFunc, $name, $argNames, $args ) {
40
+ /** @return mixed */
41
+ protected function traceCall( callable $parseFunc, string $name, array $argNames, array $args ) {
39
42
  $argMap = [];
40
43
  foreach ( $args as $i => $argValue ) {
41
44
  $argMap[$argNames[$i]] = $argValue;
@@ -47,7 +50,7 @@ abstract class PEGParserBase {
47
50
  'location' => $this->computeLocation( $startPos, $startPos ),
48
51
  'args' => $argMap
49
52
  ] );
50
- $result = call_user_func_array( $parseFunc, $args );
53
+ $result = $parseFunc( ...$args );
51
54
  if ( $result !== self::$FAILED ) {
52
55
  $this->tracer->trace( [
53
56
  'type' => 'rule.match',
@@ -65,11 +68,11 @@ abstract class PEGParserBase {
65
68
  return $result;
66
69
  }
67
70
 
68
- protected function text() {
71
+ protected function text(): string {
69
72
  return substr( $this->input, $this->savedPos, $this->currPos - $this->savedPos );
70
73
  }
71
74
 
72
- protected function location() {
75
+ protected function location(): LocationRange {
73
76
  return $this->computeLocation( $this->savedPos, $this->currPos );
74
77
  }
75
78
 
@@ -99,7 +102,7 @@ abstract class PEGParserBase {
99
102
  );
100
103
  }
101
104
 
102
- public static function charAt( $s, $byteOffset ) {
105
+ public static function charAt( string $s, int $byteOffset ): string {
103
106
  if ( !isset( $s[$byteOffset] ) ) {
104
107
  return '';
105
108
  }
@@ -117,7 +120,7 @@ abstract class PEGParserBase {
117
120
  return $char;
118
121
  }
119
122
 
120
- public static function charsAt( $s, $byteOffset, $numChars ) {
123
+ public static function charsAt( string $s, int $byteOffset, int $numChars ): string {
121
124
  $ret = '';
122
125
  for ( $i = 0; $i < $numChars; $i++ ) {
123
126
  $ret .= self::consumeChar( $s, $byteOffset );
@@ -125,7 +128,7 @@ abstract class PEGParserBase {
125
128
  return $ret;
126
129
  }
127
130
 
128
- public static function consumeChar( $s, &$byteOffset ) {
131
+ public static function consumeChar( string $s, int &$byteOffset ): string {
129
132
  if ( !isset( $s[$byteOffset] ) ) {
130
133
  return '';
131
134
  }
@@ -143,6 +146,10 @@ abstract class PEGParserBase {
143
146
  return $char;
144
147
  }
145
148
 
149
+ /**
150
+ * @param mixed $value
151
+ * @return mixed
152
+ */
146
153
  public static function &newRef( $value ) {
147
154
  return $value;
148
155
  }
@@ -165,7 +172,8 @@ abstract class PEGParserBase {
165
172
  while ( $p < $pos ) {
166
173
  $ch = self::charAt( $this->input, $p );
167
174
  if ( $ch === "\n" ) {
168
- if ( !$details['seenCR'] ) { $details['line']++;
175
+ if ( !$details['seenCR'] ) {
176
+ $details['line']++;
169
177
  }
170
178
  $details['column'] = 1;
171
179
  $details['seenCR'] = false;
@@ -185,7 +193,7 @@ abstract class PEGParserBase {
185
193
  return $details;
186
194
  }
187
195
 
188
- protected function computeLocation( $startPos, $endPos ) {
196
+ protected function computeLocation( int $startPos, int $endPos ): LocationRange {
189
197
  if ( $endPos > $this->inputLength ) {
190
198
  $endPos--;
191
199
  }
@@ -202,7 +210,7 @@ abstract class PEGParserBase {
202
210
  );
203
211
  }
204
212
 
205
- protected function fail( $expected ) {
213
+ protected function fail( int $expected ) {
206
214
  if ( $this->currPos < $this->maxFailPos ) {
207
215
  return;
208
216
  }
@@ -234,7 +242,7 @@ abstract class PEGParserBase {
234
242
  return $expanded;
235
243
  }
236
244
 
237
- private function buildMessage( $expected, $found ) {
245
+ private function buildMessage( array $expected, ?string $found ): string {
238
246
  $expectedDescs = [];
239
247
 
240
248
  foreach ( $expected as $info ) {
@@ -251,7 +259,9 @@ abstract class PEGParserBase {
251
259
  return "Expected " . $expectedDesc . " but " . $foundDesc . " found.";
252
260
  }
253
261
 
254
- protected function buildException( $message, $expected, $found, $location ) {
262
+ protected function buildException(
263
+ ?string $message, ?array $expected, ?string $found, LocationRange $location
264
+ ): SyntaxError {
255
265
  if ( $expected !== null ) {
256
266
  sort( $expected );
257
267
  $expected = array_unique( $expected );
@@ -271,7 +281,7 @@ abstract class PEGParserBase {
271
281
  );
272
282
  }
273
283
 
274
- protected function buildParseException() {
284
+ protected function buildParseException(): SyntaxError {
275
285
  $char = self::charAt( $this->input, $this->maxFailPos );
276
286
  return $this->buildException(
277
287
  null,
@@ -284,7 +294,7 @@ abstract class PEGParserBase {
284
294
  protected function initialize() {
285
295
  }
286
296
 
287
- protected function initInternal( $input, $options ) {
297
+ protected function initInternal( string $input, array $options ) {
288
298
  $this->currPos = 0;
289
299
  $this->savedPos = 0;
290
300
  $this->input = $input;
@@ -299,5 +309,6 @@ abstract class PEGParserBase {
299
309
  $this->initialize();
300
310
  }
301
311
 
302
- abstract public function parse( $input, $options = [] );
312
+ /** @return mixed */
313
+ abstract public function parse( string $input, array $options = [] );
303
314
  }
@@ -3,9 +3,9 @@
3
3
  namespace Wikimedia\WikiPEG;
4
4
 
5
5
  class SyntaxError extends \Exception implements \JsonSerializable {
6
- public $expected;
7
- public $found;
8
- public $location;
6
+ public array $expected;
7
+ public ?string $found;
8
+ public LocationRange $location;
9
9
 
10
10
  /**
11
11
  * @param string $message
@@ -13,7 +13,7 @@ class SyntaxError extends \Exception implements \JsonSerializable {
13
13
  * @param string|null $found
14
14
  * @param LocationRange $location
15
15
  */
16
- public function __construct( string $message, array $expected, $found, LocationRange $location ) {
16
+ public function __construct( string $message, array $expected, ?string $found, LocationRange $location ) {
17
17
  parent::__construct( $message );
18
18
  $this->expected = $expected;
19
19
  $this->found = $found;
package/src/Tracer.php CHANGED
@@ -3,5 +3,5 @@
3
3
  namespace Wikimedia\WikiPEG;
4
4
 
5
5
  interface Tracer {
6
- public function trace( $event );
6
+ public function trace( array $event ): void;
7
7
  }