{"id":16877617,"url":"https://github.com/nikic/phlexy","last_synced_at":"2025-04-05T06:07:12.125Z","repository":{"id":4921537,"uuid":"6078002","full_name":"nikic/Phlexy","owner":"nikic","description":"Lexing experiments in PHP","archived":false,"fork":false,"pushed_at":"2021-08-24T18:55:09.000Z","size":79,"stargazers_count":161,"open_issues_count":1,"forks_count":11,"subscribers_count":15,"default_branch":"master","last_synced_at":"2024-10-13T23:29:21.392Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nikic.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-10-04T16:06:28.000Z","updated_at":"2024-05-04T10:48:44.000Z","dependencies_parsed_at":"2022-09-10T16:51:55.451Z","dependency_job_id":null,"html_url":"https://github.com/nikic/Phlexy","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nikic%2FPhlexy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nikic%2FPhlexy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nikic%2FPhlexy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nikic%2FPhlexy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nikic","download_url":"https://codeload.github.com/nikic/Phlexy/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247294536,"owners_count":20915340,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-13T15:44:28.840Z","updated_at":"2025-04-05T06:07:12.107Z","avatar_url":"https://github.com/nikic.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"Phlexy\n======\n\nThis project is a followup to [my post on fast lexing in PHP][lexing_blog_post]. It contains a few lexer implementations\n(both stateless and stateful) and related performance tests.\n\nUsage\n-----\n\nLexers are created from a lexer definition using a factory class.\n\nFor example, if you want to create a MARK based stateless CSV lexer, you can use the following code:\n\n```php\n\u003c?php\nrequire 'path/to/vendor/autoload.php';\n\n$factory = new Phlexy\\LexerFactory\\Stateless\\UsingMarks(\n    new Phlexy\\LexerDataGenerator\n);\n\n$lexer = $factory-\u003ecreateLexer(array(\n    '[^\",\\r\\n]+'                     =\u003e 0, // 0, 1, 2, 3 are the tokens\n    '\"[^\"\\\\\\\\]*(?:\\\\\\\\.[^\"\\\\\\\\]*)*\"' =\u003e 1, // they should really be constants\n    ','                              =\u003e 2,\n    '\\r?\\n'                          =\u003e 3,\n));\n\n$tokens = $lexer-\u003elex(\"hallo world,foo bar,more foo,more bar,\\\"rare , escape\\\",some more,stuff\\n...\");\n```\n\nSimilarly a stateful lexer:\n\n```php\n\u003c?php\nrequire 'path/to/lib/Phlexy/bootstrap.php';\n\n$factory = new Phlexy\\LexerFactory\\Stateful\\UsingMarks(\n    new Phlexy\\LexerDataGenerator\n);\n\n// The \"i\" is an additional modifier (all createLexer methods accept it)\n$lexer = $factory-\u003ecreateLexer($lexerDefinition, 'i');\n```\n\nFor an example of a stateful lexer definition, you can look the [definition for lexing PHP source\ncode][php_lexer_definition].\n\nPerformance\n-----------\n\nA performance comparison for the different lexer implementations can be done using the [performance testing\nscript][performance_test_file]:\n\n```\n$ php-7.2 examples/performanceTests.php\n\nTiming lexing of CVS data:\nTook 0.55736708641052 seconds (Phlexy\\Lexer\\Stateless\\Simple)\nTook 0.526859998703 seconds (Phlexy\\Lexer\\Stateless\\WithCapturingGroups)\nTook 0.49272608757019 seconds (Phlexy\\Lexer\\Stateless\\WithoutCapturingGroups)\nTook 0.5570011138916 seconds (Phlexy\\Lexer\\Stateless\\UsingPregReplace)\nTook 0.46333193778992 seconds (Phlexy\\Lexer\\Stateless\\UsingMarks)\n\nTiming alphabet lexing of all \"a\":\nTook 0.58650183677673 seconds (Phlexy\\Lexer\\Stateless\\Simple)\nTook 0.754310131073 seconds (Phlexy\\Lexer\\Stateless\\WithCapturingGroups)\nTook 0.70682787895203 seconds (Phlexy\\Lexer\\Stateless\\WithoutCapturingGroups)\nTook 0.76406478881836 seconds (Phlexy\\Lexer\\Stateless\\UsingPregReplace)\nTook 0.62837815284729 seconds (Phlexy\\Lexer\\Stateless\\UsingMarks)\n\nTiming alphabet lexing of all \"z\":\nTook 0.79967403411865 seconds (Phlexy\\Lexer\\Stateless\\Simple)\nTook 0.30202317237854 seconds (Phlexy\\Lexer\\Stateless\\WithCapturingGroups)\nTook 0.29198718070984 seconds (Phlexy\\Lexer\\Stateless\\WithoutCapturingGroups)\nTook 0.36609601974487 seconds (Phlexy\\Lexer\\Stateless\\UsingPregReplace)\nTook 0.12433409690857 seconds (Phlexy\\Lexer\\Stateless\\UsingMarks)\n\nTiming alphabet lexing of random string:\nTook 1.1720998287201 seconds (Phlexy\\Lexer\\Stateless\\Simple)\nTook 0.5946900844574 seconds (Phlexy\\Lexer\\Stateless\\WithCapturingGroups)\nTook 0.55696296691895 seconds (Phlexy\\Lexer\\Stateless\\WithoutCapturingGroups)\nTook 0.6708779335022 seconds (Phlexy\\Lexer\\Stateless\\UsingPregReplace)\nTook 0.33155107498169 seconds (Phlexy\\Lexer\\Stateless\\UsingMarks)\n\nTiming PHP lexing of this file:\nTook 0.151211977005 seconds (Phlexy\\Lexer\\Stateful\\Simple)\nTook 0.025480031967163 seconds (Phlexy\\Lexer\\Stateful\\UsingCompiledRegex)\nTook 0.007037878036499 seconds (Phlexy\\Lexer\\Stateful\\UsingMarks)\n\nTiming PHP lexing of larger TestAbstract file:\nTook 0.49794602394104 seconds (Phlexy\\Lexer\\Stateful\\Simple)\nTook 0.083348035812378 seconds (Phlexy\\Lexer\\Stateful\\UsingCompiledRegex)\nTook 0.019592046737671 seconds (Phlexy\\Lexer\\Stateful\\UsingMarks)\n```\n\n`Stateless\\Simple` and `Stateful\\Simple` are trivial lexer implementations (which loop through the regular expressions).\n\n`Stateless\\WithoutCapturingGroups`, `Stateless\\WithCapturingGroups` and `Stateful\\UsingCompiledRegex` use the compiled\nregex approach described in the blog post mentioned above.\n\n`Stateless\\UsingPregReplace` is an extension of the compiled regex approach, where the looping through the regular\nexpression is done by (mis)using `preg_replace_callback`.\n\n`Stateless\\UsingMarks` and `Stateful\\UsingMark` use the `(*MARK)` mechanism that was exposed in PHP 5.5.\n\nAs the above performance measurments show, the `Simple` approach is a good bit slower than using a compiled regex approach. Mark based implementation perform much better than group offset based ones. The benefits increase with lexer size: For the CSV lexer there is relatively little difference, while for the PHP lexer the mark based implementation is 25x faster than the naive one.\n\n [lexing_blog_post]: http://nikic.github.com/2011/10/23/Improving-lexing-performance-in-PHP.html\n [php_lexer_definition]: https://github.com/nikic/Phlexy/blob/master/examples/phpLexerDefinition.php\n [performance_test_file]: https://github.com/nikic/Phlexy/blob/master/examples/performanceTests.php\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnikic%2Fphlexy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnikic%2Fphlexy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnikic%2Fphlexy/lists"}