GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - wouterj/peg: A PEG parser written in PHP

BranchesTags
Open more actions menu

Latest commit

 

History

15 Commits
15 Commits

Folders and files

NameName
Last commit message
Last commit date
src
src
 
 
tests
tests
 
 
.gitignore
.gitignore
 
 
.travis.yml
.travis.yml
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
composer.json
composer.json
 
 
phpunit.xml.dist
phpunit.xml.dist
 
 

Repository files navigation

PHP PEG Parser

PEG is a generic PEG parser written in PHP. This parser allows you to write parsers using the Parsing Expression Grammar.

Installation

Install PEG using Composer:

$ composer require wouterj/peg

Usage

Use the Grammar class to specify the Parsing Expression Grammar by creating Definition instances:

use WouterJ\Peg\Grammar;
use WouterJ\Peg\Definition;

// specifies that Float is the main definition
$grammar = new Grammar('Float', [
    // matches any Digits (next definition), followed by a . (dot) and any Digits
    new Definition('Float', ['sequence', [
        ['identifier', 'Digits'],
        ['literal', '.'],
        ['identifier', 'Digits'],
    ]]),

    // matches any Digit (next definition) one or more times
    new Definition('Digits', ['repeat', ['identifier', 'Digit'], 1]),

    // matches 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9
    new Definition('Digit', ['characterClass', '0-9']),
]);

Use Grammar#parse() to parse input strings using this grammar. The return value is the part of the string that matched or null when there is no match:

// ...

echo $grammar->parse('1.2'); // 1.2
echo $grammar->parse('1039.50'); // 1039.50
echo $grammar->parse('abc'); // NULL

// please note that it doesn't have to match the full string
echo $grammar->parse('1.2a'); // 1.2

Using the PEG syntax

Of course, using arrays to define the grammar is really ugly. This is why PEG comes with PegGrammar, a parser that is able to parse PEG syntax. It returns a ready to use grammar instance using the parsed grammar.

use WouterJ\Peg\PegGrammar;

$pegGrammar = new PegGrammar();

// our float parser (equal to the previous one)
$grammar = $pegGrammar->parse(<<<EOG
Float  <- Digits '.' Digits
Digits <- Digit+
Digit  <- [0-9]
EOG
);

$grammar->parse('1039.50'); // 1039.50

For information about the PEG syntax, see Parsing Expression Grammars: A Recognition-Based Syntactic Foundation by Bryan Ford.

License

The project is released under the BSD-3 Clause license. For the full copyright and license information, please view the LICENSE file that was distributed with this source code.

Contributing

Whether it's just a reference, online high-five, bug report or feature proposal, all contributions are welcome. The main project is located at GitHub.

About

A PEG parser written in PHP

Resources

Readme
BSD-3-Clause license
Activity

Stars

24 stars

Watchers

1 watching

Forks

6 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.