Skip to content

Commit 6df0220

Browse files
authored
Merge pull request #14 from MarcelBolten/next
v3.0.0
2 parents f1e037e + 47649bc commit 6df0220

93 files changed

Lines changed: 5601 additions & 7873 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.eslintrc.js

Lines changed: 0 additions & 15 deletions
This file was deleted.

.github/workflows/CI-tests.yml

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,58 @@ jobs:
1212

1313
strategy:
1414
matrix:
15-
node-version: [14.x, 16.x, 18.x, 19.x]
15+
node-version: [20.x, 22.x, 24.x]
1616
os: [ubuntu-latest, windows-latest, macos-latest]
1717

1818
runs-on: ${{ matrix.os }}
1919

2020
steps:
21-
- uses: actions/checkout@v3
21+
- uses: actions/checkout@v4
22+
23+
- name: Install pnpm
24+
uses: pnpm/action-setup@v4
25+
2226
- name: Use Node.js ${{ matrix.node-version }}
23-
uses: actions/setup-node@v3
27+
uses: actions/setup-node@v4
2428
with:
2529
node-version: ${{ matrix.node-version }}
26-
cache: npm
30+
cache: pnpm
31+
2732
- name: Install dependencies
28-
run: npm ci
29-
- name: Test
30-
run: npm run test
33+
run: pnpm install
34+
35+
- name: Check coding standards
36+
if: matrix.node-version == '24.x' && matrix.os == 'ubuntu-latest'
37+
run: pnpm run lint
38+
39+
- name: Test parser generation
40+
run: npm test
41+
env:
42+
ONLY_GENERATE_PARSERS: 'y'
3143

3244
PHP:
3345

3446
strategy:
3547
matrix:
36-
php-version: [8.0, 8.1, 8.2]
48+
php-version: [8.1, 8.2, 8.3, 8.4]
3749
os: [ubuntu-latest]
3850

3951
runs-on: ${{ matrix.os }}
4052

4153
steps:
4254
- name: "Checkout"
43-
uses: "actions/checkout@v3"
55+
uses: "actions/checkout@v4"
56+
57+
- name: Install pnpm
58+
uses: pnpm/action-setup@v4
59+
60+
- name: Use Node.js
61+
uses: actions/setup-node@v4
62+
with:
63+
cache: pnpm
64+
65+
- name: Install dependencies
66+
run: pnpm install
4467

4568
- name: "Install PHP"
4669
uses: "shivammathur/setup-php@v2"
@@ -53,5 +76,11 @@ jobs:
5376
- name: "Install dependencies"
5477
run: "composer install --no-interaction --no-progress"
5578

56-
- name: "PHP Tests"
79+
- name: "Check coding standards"
80+
run: npm run php:csfix
81+
82+
- name: "Static analysis"
83+
run: npm run php:static
84+
85+
- name: "Test Parsers"
5786
run: npm run php:test

.gitignore

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
/.phpdoc
2-
/phpdoc
3-
/docker/xdebug_out
4-
/manual-test/output
5-
/node_modules
6-
/vendor
1+
.phpdoc/
2+
.pnpm-store/
3+
.vscode/
4+
phpdoc/
5+
docker/xdebug_out/
6+
manual-test/output/
7+
node_modules/
8+
vendor/
79
composer.lock

.npmignore

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
.git/
22
.github/
3-
.phpdoc
3+
.phpdoc/
4+
.pnpm-store/
5+
.vscode/
46
docker/
57
manual-test/
68
node_modules/
7-
phpdoc
9+
phpdoc/
810
src/.eslintrc.json
911
test/
1012
vendor/
1113
.dockerignore
12-
.eslintrc.js
1314
.gitattributes
1415
.gitignore
1516
.nvmrc
@@ -18,5 +19,7 @@ vendor/
1819
composer.json
1920
composer.lock
2021
docker-compose.yml
22+
eslint.config.js
2123
phpstan.neon
24+
pnpm-lock.yaml
2225
psalm.xml

.nvmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v16.13.0
1+
v24.0.0

.php-cs-fixer.dist.manual.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77

88
return $config->setRules(array(
99
'@PSR12' => true,
10-
'@PHP80Migration' => true,
11-
'braces' => false,
10+
'@PHP81Migration' => true,
1211
'method_argument_space' => false,
1312
))
1413
->setFinder($finder)

.php-cs-fixer.dist.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
return $config->setRules(array(
99
'@PSR12' => true,
10-
'@PHP80Migration' => true,
10+
'@PHP81Migration' => true,
1111
))
1212
->setFinder($finder)
1313
;

CHANGELOG.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,48 @@ Change Log
33

44
This file documents all notable changes to PHPeggy.
55

6+
3.0.0
7+
-----
8+
9+
Released: 2025-06-11
10+
11+
### Breaking Changes
12+
13+
- Node.js v20+ is now required
14+
- `mbstring` extension is required. The `mbstringAllowed` option is removed.
15+
- Drop support for Internet Explorer
16+
17+
### Major Changes
18+
19+
- Support for peggy 5
20+
- Add wider support for unicode in grammar files
21+
22+
### Minor Changes
23+
24+
- Fix a bug with named NEVER_MATCH expressions.
25+
compare to https://github.com/peggyjs/peggy/pull/454
26+
27+
3.0.0-alpha
28+
-----
29+
30+
Released: 2025-06-06
31+
32+
### Breaking Changes
33+
34+
- Node.js v20+ is now required
35+
- `mbstring` extension is required. The `mbstringAllowed` option is removed.
36+
- Drop support for Internet Explorer
37+
38+
### Major Changes
39+
40+
- Support for peggy 5
41+
- Add wider support for unicode in grammar files
42+
43+
### Minor Changes
44+
45+
- Fix a bug with named NEVER_MATCH expressions.
46+
compare to https://github.com/peggyjs/peggy/pull/454
47+
648
2.0.1
749
-----
850

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2014-2023 The PHPeggy AUTHORS
3+
Copyright (c) 2014-2025 The PHPeggy AUTHORS
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 62 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22
[![npm version](https://img.shields.io/npm/v/phpeggy)](https://www.npmjs.com/package/phpeggy)
33
[![License](https://img.shields.io/badge/license-mit-blue)](https://opensource.org/licenses/MIT)
44

5+
- [Migrating from phpegjs](#migrating-from-phpegjs)
6+
- [Requirements](#requirements)
7+
- [Installation](#installation)
8+
- [Usage](#usage)
9+
- [Generating a Parser](#generating-a-parser)
10+
- [JS API](#js-api)
11+
- [Command Line](#command-line)
12+
- [Using the Parser](#using-the-parser)
13+
- [Grammar Syntax and Semantics](#grammar-syntax-and-semantics)
14+
- [Conversion Guide - Peggy action blocks to PHPeggy](#guide-for-converting-peggy-action-blocks-to-phpeggy)
15+
516
# PHPeggy
617

718
A PHP code generation plugin for
@@ -34,36 +45,38 @@ Follow these steps to upgrade:
3445

3546
## Requirements
3647

37-
* [Peggy](https://peggyjs.org/) (known compatible with v3.0.0)
48+
* [Peggy](https://peggyjs.org/) (known compatible with version 5)
49+
* PHP version >=8 for the created parser
50+
* `mbstring` extension enabled
3851

39-
Installation
40-
------------
52+
## Installation
4153

4254
### Node.js
4355

44-
Install Peggy with `phpeggy` plugin
56+
Install Peggy with PHPeggy plugin
4557

4658
```sh
47-
$ npm install peggy@3.0.0 phpeggy
59+
$ npm install peggy@^5.0.0 phpeggy
4860
```
4961

50-
Usage
51-
-----
62+
## Usage
5263

5364
### Generating a Parser
5465

55-
In Node.js, require both the Peggy parser generator and the `phpeggy` plugin:
66+
#### JS API
67+
68+
In Node.js, require both the Peggy parser generator and the PHPeggy plugin:
5669

5770
```js
58-
var peggy = require("peggy");
59-
var phpeggy = require("phpeggy");
71+
const peggy = require("peggy");
72+
const phpeggy = require("phpeggy");
6073
```
6174

62-
To generate a PHP parser, pass both the `phpeggy` plugin and your grammar to
75+
To generate a PHP parser, pass both the PHPeggy plugin and your grammar to
6376
`peggy.generate`:
6477

6578
```js
66-
var parser = peggy.generate("start = ('a' / 'b')+", {
79+
const parser = peggy.generate("start = ('a' / 'b')+", {
6780
plugins: [phpeggy]
6881
});
6982
```
@@ -88,7 +101,7 @@ Supported options of `peggy.generate`:
88101
You can also pass options specific to the PHPeggy plugin as follows:
89102

90103
```js
91-
var parser = peggy.generate("start = ('a' / 'b')+", {
104+
const parser = peggy.generate("start = ('a' / 'b')+", {
92105
plugins: [phpeggy],
93106
phpeggy: { /* phpeggy-specific options */ }
94107
});
@@ -99,17 +112,36 @@ Here are the options available to pass this way:
99112
* `parserNamespace` - namespace of generated parser (default: `PHPeggy`). If
100113
value is `''` or `null`, no namespace will be used.
101114
* `parserClassName` - name of generated class for parser (default: `Parser`).
102-
* `mbstringAllowed` - whether to allow usage of PHP's `mb_*` functions which
103-
depend on the `mbstring` extension being installed (default: `true`). This
104-
can be disabled for compatibility with a wider range of PHP configurations,
105-
but this will also disable several features of Peggy (case-insensitive
106-
string matching, case-insensitive character classes, and empty character
107-
classes). Attempting to use these features with `mbstringAllowed: false`
108-
will cause `passes.check` to throw an error.
109115
* `header` - you can provide a custom header that will be added at the top of the parser, e.g. `/* My custom header */`.
110116

111-
Using the Parser
112-
----------------
117+
#### Command Line
118+
119+
To generate a parser from your grammar, use the peggy command:
120+
121+
```bash
122+
npx peggy --plugin /path/to/phpeggy/src/phpeggy.js arithmetics.pegjs
123+
```
124+
125+
The following options might be of interest in the context of PHPeggy:
126+
127+
- `--allowed-start-rules <rules>`
128+
- `--cache`
129+
- `--extra-options <options>`
130+
- `-c, --extra-options-file <file>`
131+
- `-o, --output <file>`
132+
- `-S, --start-rule <rule>`
133+
134+
`--format` is irrelevant as PHPeggy will only provide PHP source code.
135+
136+
Here is a more complex example:
137+
138+
```bash
139+
npx peggy -o arithmeticsParser.php --plugin /path/to/phpeggy/src/phpeggy.js arithmetics.pegjs --cache --extra-option '{ "phpeggy" : { "parserNamespace" : "MyNameSpace", "parserClassName" : "ArithmeticsParser", "header" : "/* My custom header */" } }'
140+
```
141+
142+
A more detailed description of the different options can be found in the [peggy documentation](https://peggyjs.org/documentation.html#generating-a-parser-command-line).
143+
144+
## Using the Parser
113145

114146
1) Save parser generated by `peggy.generate` to a file
115147

@@ -145,6 +177,7 @@ catch (PHPeggy\SyntaxError $e) {
145177

146178
Which will look similar to:
147179

180+
<!-- eslint-disable-next-line markdown/fenced-code-language -->
148181
```
149182
SyntaxError: Expected "a" but "b" found.
150183
--> Input string:1:1
@@ -160,21 +193,21 @@ maintain compatibility with PCRE versions that are missing Unicode support
160193
array (one array element per UTF-8 character) and pass this array into
161194
`$parser->parse()` instead of the string input.
162195

163-
Grammar Syntax and Semantics
164-
----------------------------
196+
## Grammar Syntax and Semantics
165197

166198
See [documentation of Peggy](https://peggyjs.org/documentation.html) with following differences:
167199

168200
* action and predicate blocks should be written in PHP.
169201
* the _per-parse initializer_ code block is used to provide additional methods, properties and constants to the Parser class. A special method `function initialize()` can be provided and resembles the Peggy per-parse initializer i.e. this method is called before the generated parser starts parsing (see [examples/fizzbuzz.pegjs](examples/fizzbuzz.pegjs)). All methods have access to the input (`$this->input`) and the options (`$this->options`).
170202
* the _global initializer_ code block can be used to add use statements, classes, functions, constants, ...
203+
* [Importing External Rules](https://peggyjs.org/documentation.html#importing-external-rules) works only from the Command Line.
171204

172205
Original Peggy rule:
173206

174207
```js
175208
media_list = head:medium tail:("," S* medium)* {
176-
var result = [head];
177-
for (var i = 0; i < tail.length; i++) {
209+
let result = [head];
210+
for (let i = 0; i < tail.length; i++) {
178211
result.push(tail[i][2]);
179212
}
180213
return result;
@@ -206,8 +239,8 @@ media_list = head:medium tail:("," S* medium)* {
206239
return $result;
207240
?> **/
208241

209-
var result = [head];
210-
for (var i = 0; i < tail.length; i++) {
242+
let result = [head];
243+
for (let i = 0; i < tail.length; i++) {
211244
result.push(tail[i][2]);
212245
}
213246
return result;
@@ -221,8 +254,7 @@ You can also use the following utility functions in PHP action blocks:
221254
- `ord_unicode($code)` - return the UTF-8 code for a character (analogue of
222255
JavaScript's `String.prototype.charCodeAt(0)` function).
223256

224-
Guide for converting Peggy action blocks to PHPeggy
225-
---------------------------------------------------
257+
## Guide for converting Peggy action blocks to PHPeggy
226258

227259
| Javascript code | PHP analogue |
228260
| --------------------------------- | ----------------------------------------- |

0 commit comments

Comments
 (0)