2017 © Pedro Peláez
 

library lex

Functional Lexing library

image

krak/lex

Functional Lexing library

  • Friday, December 30, 2016
  • by ragboyjr
  • Repository
  • 1 Watchers
  • 0 Stars
  • 748 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

Lex

Lex is a library for lexical analysis in PHP. Currently, only simple regular expression lexers are available; but considering that you shouldn't be lexing anything complex in php, this should be fine :)., (*1)

Installation

composer require krak/lex

Usage

<?php

require_once __DIR__ . '/vendor/autoload.php';

use function Krak\Lex\lexer,
    Krak\Lex\skipLexer;

const TOK_INT = 'int';
const TOK_PLUS = 'plus';
const TOK_MINUS = 'minus';
const TOK_WS = 'whitespace';

// creates a lexer that will use these RE's to match input
// the A (anchor flag) is required
$lex = lexer([
    '/\d+/A' => TOK_INT,
    '/\+/A' => TOK_PLUS,
    '/\-/A' => TOK_MINUS,
    '/\s+/A' => TOK_WS
]);

// decorator for skipping tokens, in this case, just throw away the whitespace tokens
$lex = skipLexer($lex, [TOK_WS]);

// lex the input and return an iterator of tokens
$toks = $lex('1 + 2 - 3');

foreach ($toks as $matched_tok) {
    printf(
        "Matched token '%s' with input '%s' at offset %d\n",
        $matched_tok->token,
        $matched_tok->match,
        $matched_tok->offset
    );
}

The following program would output, (*2)

Matched token 'int' with input '1' at offset 0
Matched token 'plus' with input '+' at offset 2
Matched token 'int' with input '2' at offset 4
Matched token 'minus' with input '-' at offset 6
Matched token 'int' with input '3' at offset 8

TokenStream

A token stream is a simple interface for consuming one token at a time. This is very useful for Recursive Decent Parsers, (*3)

<?php

use function Krak\Lex\lexer,
    Krak\Lex\tokenStreamLexer;

$lex = lexer(['/a/A' => 'a', '/b/A' => 'b']);
$lex = tokenStreamLexer($lex);
$stream = $lex('aba');

assert($stream->peek() == 'a');
assert($stream->getToken() == 'a');
assert($stream->getToken() == 'b');
assert($stream->getToken() == 'a');
assert($stream->isEmpty());

API

Lexers

Each lexer will accept a string input and then return an iterable of MatchedToken, (*4)

lexer($token_map, $throw = true)

Main lexer which lexes the strings based off of the $token_map. $throw determines whether or not the lexer should throw an exception on unrecognized input., (*5)

skipLexer($lex, $tokens)

Lexer decorator which will skip any Matched Tokens in the set of the $tokens passed in., (*6)

tokenStreamLexer($lex)

Lexer decorator that will convert the output of the $lex into a TokenStream, (*7)

mockLexer($tokens)

Returns $tokens as is., (*8)

class MatchedToken

$match

Returns the text that was matched., (*9)

$token

Returns the token name that was matched., (*10)

$offset

Returns the string offset at which the match started., (*11)

interface TokenStream extends \IteratorAggregate

getToken()

Returns the current token and advances the internal pointer up by one., (*12)

peek()

Returns the current token but does not advance the internal pointer, (*13)

isEmpty()

returns true if the token stream is empty, false if not., (*14)

The Versions

30/12 2016

dev-master

9999999-dev

Functional Lexing library

  Sources   Download

MIT

The Requires

 

The Development Requires

30/12 2016

v0.2.0

0.2.0.0

Functional Lexing library

  Sources   Download

MIT

The Requires

 

The Development Requires

26/04 2016

v0.1

0.1.0.0

Functional Lexing library

  Sources   Download

MIT

The Requires

 

The Development Requires