Git Product home page Git Product logo

php-pegjs's Introduction

PHP PEG.js

** This repo is not maintained for a while please use phpegjs module instead **

PHP PEG.js is a php code generation plugin for PEG.js.

Requirements

Installation

Node.js

Install PEG.js with php-pegjs plugin

$ npm install php-pegjs

Usage

Generating a Parser

In Node.js, require both the PEG.js parser generator and the php-pegjs plugin:

var pegjs = require("pegjs");
var phppegjs = require("php-pegjs");

To generate a php parser, pass to pegjs.buildParser php-pegjs plugin and your grammar:

var parser = pegjs.buildParser("start = ('a' / 'b')+", {
    plugins: [phppegjs]
});

The method will return source code of generated parser as a string. Unlike original PEG.js, generated php parser will be a class, not a function.

Supported options of pegjs.buildParser:

  • cache — if true, makes the parser cache results, avoiding exponential parsing time in pathological cases but making the parser slower (default: false). In case of php, this is strongly recommended for big grammars (like javascript.pegjs or css.pegjs in example folder)
  • allowedStartRules — rules the parser will be allowed to start parsing from (default: the first rule in the grammar)

Additional PHP PEG.js plugin options:

  • phppegjs.parserNamespace - namespace of generated parser (default: "PhpPegJs"). If value is "", parser will be in global namespace
  • phppegjs.parserClassName - name of generated class for parser (default: "Parser")

Using the Parser

  1. Save parser generated by pegjs.buildParser to a file

  2. In php code:

    include "your.parser.file.php";

    try { $parser = new PhpPegJs\Parser; $result = $parser->parse($input); } catch (PhpPegJs\SyntaxError $ex) { // Handle parsing error // [...] }

You can use following snippet to format parsing error:

catch (PhpPegJs\SyntaxError $ex)
{
    $message = "Syntax error: " . $ex->getMessage() . ' At line ' . $ex->grammarLine . ' column ' . $ex->grammarColumn . ' offset ' . $ex->grammarOffset;
}

Grammar Syntax and Semantics

See documentation of PEG.js with one difference: action blocks should be written on PHP.

Original PEG.js rule:

media_list
  = head:medium tail:("," S* medium)* {
      var result = [head];
      for (var i = 0; i < tail.length; i++) {
        result.push(tail[i][2]);
      }
      return result;
    }

PHP PEG.js rule:

media_list
  = head:medium tail:("," S* medium)* {
      $result = array($head);
      for ($i = 0; $i < count($tail); $i++) {
        $result[] = $tail[$i][2];
      }
      return $result;
    }

You can also use following util functions in action blocks:

chr_unicode($code) - return character by its UTF-8 code (Analogue of javascript String.fromCharCode)

Guide of converting PEG.js action blocks to PHP PEG.js

Javascript code PHP analogue
some_var $some_var
{f1: "val1", f2: "val2"} array("f1" => "val1", "f2" => "val2")
["val1", "val2"] array("val1", "val2")
some_array.push("val") $some_array[] = "val"
some_array.length count($some_array)
some_array.join("") join("", $some_array)
some_array1.concat(some_array2) array_merge($some_array1, $some_array2)
parseInt("23") intval("23")
parseFloat("23.1") floatval("23.1")
some_str.length mb_strlen(some_str, "UTF-8")
some_str.replace("b", "\b") str_replace("b", "\b", $some_str)
String.fromCharCode(2323) chr_unicode(2323)
text() $this->text()
location() $this->location()

Function $this->location() returns object with following structure:

{
  'start' => {
     'offset' => int,
     'line'   => int,
     'column' => int,
  },
  'end' => {
     'offset' => int,
     'line'   => int,
     'column' => int,
  },
}

License

The MIT License (MIT)

php-pegjs's People

Contributors

nordth avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

php-pegjs's Issues

utf8 - ndash in grammar not recognised at runtime

if I include a n-long dash in the grammar ("–") I'm getting a parse error that "\u2013" is expected but "–" was found

Syntax error: Expected " ", "\u2013" or end of input but "–" found. At line 1 column 7 offset 6

I'm not that familiar with php support for unicode, what should I do to parse input with "–"?

syntax error if the parse() is called multiple times

i've got a pet grammar

start = digits:[0-9]+ {
  return intval(join('', $digits));
}
include "pet.php";
try {
    $parser = new PhpPegJs\Parser;
    $result = $parser->parse('26');
    $result = $parser->parse('27'); // HERE IT CHOKES
} catch (PhpPegJs\SyntaxError $ex) {
    $message = "Syntax error: " . $ex->getMessage() . ' At line ' . $ex->grammarLine . ' column ' . $ex->grammarColumn . ' offset ' . $ex->grammarOffset . "\n";
    echo $message;
}
Syntax error: Expected [0-9] but end of input found. At line 1 column 3 offset 2

this is independent of the grammar I use

Strings with $ in the grammar don't escape $ in PHP

For example,

$callback from the grammar at https://github.com/auth0/node-odata-parser/blob/master/src/odata.pegjs on line 182.

results in

$this->peg_c117 = "$callback=";
$this->peg_c118 = array( "type" => "literal", "value" => "$callback=", "description" => "\"$callback=\"" );

but should result in

$this->peg_c117 = "\$callback=";
$this->peg_c118 = array( "type" => "literal", "value" => "\$callback=", "description" => "\"\$callback=\"" );

Without the escaped character, PHP tries to interpret $callback as a variable since it is in a double-quote string.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.