Php Lexer

Uses Grammars to parse complex strings/files

Notice

  • Existing grammars are for version v0.3 and have not been updated.
  • The JsonGrammar is v0.5 compatible, but its incomplete. I won't finish it, because json_decode is good & faster.
  • The PhpGrammar is the next I'll write.
  • BashGrammar will be next, but minimal.
  • JavascriptGrammar will probably be after that.

Install

For development, it depends upon taeluf/php/php-tests and taeluf/php/CodeScrawl, but they're not setup on packigist yet.

composer require taeluf/lexer v0.5.x-dev   

Use it

To lex a file, just use lex($filePath). Also, the JsonGrammar is very incomplete & cannot parse most json.

$json = '["Cool",["yes","please"],"okay"]';  
$lexer = new \Tlf\Lexer();  
// $lexer->debug = true;  
// $lexer->inspect_loop = 28;  
// $lexer->useCache = false; // useCache only matters when lexing a file  
$lexer->addGrammar($jsonGrammar = new \Tlf\Lexer\JsonGrammar());  
  
//this is the root ast  
$ast = new \Tlf\Lexer\JsonAst("json"); //"json" is the type  
$ast->source = $json; //Not required, but I like keeping source in the ast root.  
$ast = $lexer->lexAst($ast, $json);  
  
// $tree = $ast->getTree(); // not what we normally want with json  
$data = $ast->getJsonData(); // custom method on the JsonAst class  

Contribute

  • Want to write a grammar? Check out the JsonGrammar, read docs/Unsorted.md, and/or contact me on twitter at TaelufDev.
  • Got questions? Dig through the code & document your answers in .docsrc/. Use Unsorted.src.md if you're not sure how to organize your docs.
  • Need features? Check out the Status.md document and see what needs to be done. Open up an issue if you're working on something, so we don't double efforts.