2017 © Pedro Peláez
 

library lexer

Lexical analyzer with individual token definition with regular expressions

image

tmilos/lexer

Lexical analyzer with individual token definition with regular expressions

  • Wednesday, March 22, 2017
  • by tmilos
  • Repository
  • 2 Watchers
  • 8 Stars
  • 483 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 4 Versions
  • 22 % Grown

The README.md

PHP lexical analyzer

PHP implementation of Lexical Analyzer., (*1)

Author Build Status Coverage Status License SensioLabsInsight, (*2)

Warning This is not a GENERATOR like classical lex is. It does not produce any php code. It's a simple plain scanner of the given input string and tokenizer into given set of tokens by matching regular expressions. Thus, at runtime you can change the token definition and use one same code for any token set., (*3)

Token Definition

Tokens are defined with TokenDefinition class that holds token name and regular expression. Token name can be empty, and in that case, lexer will ignore/skip such tokens., (*4)

Lexer Configuration

The lexer configuration holds a list of all token definitions. With LexerArrayConfig it can be easily created from an array where keys are regular expressions and values are names of tokens., (*5)

Full scan

Lexer's static method scan($config, $input) can be used to scan given input string and return an array of tokens., (*6)

Lexer with state

Instance of the Lexer class be used to walk trough scanned tokens with single look-ahead token., (*7)

It's similar in API to doctrine/lexer, just tokens are defined and scanned differently, w/out the need for recognizing the token type/name from the tokenized value - rather the token type/name is given by the same TokenDefn that gave the regex to recognize the token., (*8)

Examples

``` php <?php $config = new LexerArrayConfig([ '\s' => '', '\d+' => 'number', '\+' => 'plus', '-' => 'minus', '\*' => 'mul', '/' => 'div', ]);, (*9)

// static scan method that returns an array of $tokens = Lexer::scan($config, '2 + 3'); array_map(function ($t) { return $t->getName(); }, $tokens); // ['number', 'plus', 'number'], (*10)

// lexer instance $lexer = new Lexer($config); $lexer->setInput('2 + 3'); $lexer->moveNext(); while ($lexer->getLookahead()) { print $lexer->getLookahead()->getName(); $lexer->moveNext(); } ```, (*11)

The Versions

22/03 2017

dev-master

9999999-dev

Lexical analyzer with individual token definition with regular expressions

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

parser lexer

21/12 2016

1.0.2

1.0.2.0

Lexical analyzer with individual token definition with regular expressions

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

parser lexer

20/12 2016
19/12 2016