Skip to content

Commit 7143a3d

Browse files
authored
Merge pull request #11 from MarcelBolten/develop
dev into next
2 parents f1e037e + 127bdf4 commit 7143a3d

89 files changed

Lines changed: 4934 additions & 8228 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: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,49 @@ 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 PHP"
24+
uses: "shivammathur/setup-php@v2"
25+
with:
26+
coverage: "none"
27+
php-version: "8.1"
28+
tools: composer:v2
29+
ini-values: error_reporting=-1
30+
31+
- name: Install pnpm
32+
uses: pnpm/action-setup@v4
33+
2234
- name: Use Node.js ${{ matrix.node-version }}
23-
uses: actions/setup-node@v3
35+
uses: actions/setup-node@v4
2436
with:
2537
node-version: ${{ matrix.node-version }}
26-
cache: npm
38+
cache: pnpm
39+
2740
- name: Install dependencies
28-
run: npm ci
41+
run: pnpm install
42+
2943
- name: Test
3044
run: npm run test
3145

3246
PHP:
3347

3448
strategy:
3549
matrix:
36-
php-version: [8.0, 8.1, 8.2]
50+
php-version: [8.1, 8.2, 8.3, 8.4]
3751
os: [ubuntu-latest]
3852

3953
runs-on: ${{ matrix.os }}
4054

4155
steps:
4256
- name: "Checkout"
43-
uses: "actions/checkout@v3"
57+
uses: "actions/checkout@v4"
4458

4559
- name: "Install PHP"
4660
uses: "shivammathur/setup-php@v2"

.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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,27 @@ Change Log
33

44
This file documents all notable changes to PHPeggy.
55

6+
3.0.0
7+
-----
8+
9+
Released: TBD
10+
11+
### Breaking Changes
12+
13+
- Node.js v20+ is now required
14+
- `mbstring` extension is required now. The `mbstringAllowed` option is removed.
15+
- Drop support for Internet Explorer
16+
17+
### Major Changes
18+
19+
- Add wider support for unicode in grammar files:
20+
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+
627
2.0.1
728
-----
829

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: 61 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

@@ -160,21 +192,21 @@ maintain compatibility with PCRE versions that are missing Unicode support
160192
array (one array element per UTF-8 character) and pass this array into
161193
`$parser->parse()` instead of the string input.
162194

163-
Grammar Syntax and Semantics
164-
----------------------------
195+
## Grammar Syntax and Semantics
165196

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

168199
* action and predicate blocks should be written in PHP.
169200
* 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`).
170201
* the _global initializer_ code block can be used to add use statements, classes, functions, constants, ...
202+
* [Importing External Rules](https://peggyjs.org/documentation.html#importing-external-rules) works only from the Command Line.
171203

172204
Original Peggy rule:
173205

174206
```js
175207
media_list = head:medium tail:("," S* medium)* {
176-
var result = [head];
177-
for (var i = 0; i < tail.length; i++) {
208+
let result = [head];
209+
for (let i = 0; i < tail.length; i++) {
178210
result.push(tail[i][2]);
179211
}
180212
return result;
@@ -206,8 +238,8 @@ media_list = head:medium tail:("," S* medium)* {
206238
return $result;
207239
?> **/
208240

209-
var result = [head];
210-
for (var i = 0; i < tail.length; i++) {
241+
let result = [head];
242+
for (let i = 0; i < tail.length; i++) {
211243
result.push(tail[i][2]);
212244
}
213245
return result;
@@ -221,8 +253,7 @@ You can also use the following utility functions in PHP action blocks:
221253
- `ord_unicode($code)` - return the UTF-8 code for a character (analogue of
222254
JavaScript's `String.prototype.charCodeAt(0)` function).
223255

224-
Guide for converting Peggy action blocks to PHPeggy
225-
---------------------------------------------------
256+
## Guide for converting Peggy action blocks to PHPeggy
226257

227258
| Javascript code | PHP analogue |
228259
| --------------------------------- | ----------------------------------------- |

0 commit comments

Comments
 (0)