kapitancho / walex
There is no license information available for the latest version (0.0.1) of this package.
0.0.1
2024-02-29 00:03 UTC
Requires
- php: >=8.2
This package is not auto-updated.
Last update: 2024-10-25 01:25:39 UTC
README
A lightweight lexer, written in PHP
Installation
To install the latest version, use the following command:
$ composer require kapitancho/walex
Usage
Walex is the lexer of the Walnut language. Here is an example based on real code:
<?php use Walnut\Lib\Walex\Lexer; use Walnut\Lib\Walex\Pattern; use Walnut\Lib\Walex\Rule; use Walnut\Lib\Walex\SpecialRuleTag; $lexer = new Lexer([ new Rule(new Pattern('(0|(\-?[1-9][0-9]*))\.[0-9]+'), 'real_number'), new Rule(new Pattern('[\+\-\*\/]'), 'arithmetic_op'), new Rule(new Pattern('[\n]'), SpecialRuleTag::newLine), new Rule(new Pattern('.'), SpecialRuleTag::skip) ]); foreach($lexer->getTokensFor("3.141 + 42 * \n -5.6 / 7.8") as $token) { echo $token, PHP_EOL; } # Output: # Token at line: 1, column: 1, offset: 0 of type real_number matching 3.141 # Token at line: 1, column: 7, offset: 6 of type arithmetic_op matching + # Token at line: 1, column: 12, offset: 11 of type arithmetic_op matching * # Token at line: 2, column: 2, offset: 15 of type real_number matching -5.6 # Token at line: 2, column: 7, offset: 20 of type arithmetic_op matching / # Token at line: 2, column: 9, offset: 22 of type real_number matching 7.8 # Token at line: 2, column: 12, offset: 25 of type eof matching ?>