{"id":19483274,"url":"https://github.com/yosymfony/parser-utils","last_synced_at":"2025-04-25T16:31:56.829Z","repository":{"id":57087956,"uuid":"99709552","full_name":"yosymfony/parser-utils","owner":"yosymfony","description":"A library for writing recursive descent parsers in PHP.","archived":false,"fork":false,"pushed_at":"2018-06-29T15:36:28.000Z","size":21,"stargazers_count":20,"open_issues_count":0,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-11-08T01:08:02.350Z","etag":null,"topics":["lexer","parser","php","recursive-descent-parser"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yosymfony.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2017-08-08T15:44:17.000Z","updated_at":"2024-08-14T00:37:32.000Z","dependencies_parsed_at":"2022-08-20T15:31:13.743Z","dependency_job_id":null,"html_url":"https://github.com/yosymfony/parser-utils","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/yosymfony%2Fparser-utils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yosymfony%2Fparser-utils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yosymfony%2Fparser-utils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yosymfony%2Fparser-utils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yosymfony","download_url":"https://codeload.github.com/yosymfony/parser-utils/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224007877,"owners_count":17240294,"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":["lexer","parser","php","recursive-descent-parser"],"created_at":"2024-11-10T20:14:16.000Z","updated_at":"2024-11-10T20:14:17.065Z","avatar_url":"https://github.com/yosymfony.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"A library for writing [recursive descent parsers](https://en.wikipedia.org/wiki/Recursive_descent_parser)\r\nin PHP.\r\n\r\n[![Build Status](https://travis-ci.org/yosymfony/parser-utils.svg?branch=master)](https://travis-ci.org/yosymfony/parser-utils)\r\n\r\n## requires\r\n\r\n* PHP \u003e= 7.1\r\n\r\n## Installation\r\n\r\nThe preferred installation method is [composer](https://getcomposer.org):\r\n\r\n```bash\r\ncomposer require yosymfony/parser-utils\r\n```\r\n\r\n## An example\r\n\r\nFirst, you need to create a lexer. This one will recognize tokens\r\n\r\n```php\r\nuse Yosymfony\\ParserUtils\\BasicLexer;\r\n\r\n$lexer = new BasicLexer([\r\n    '/^([0-9]+)/x' =\u003e 'T_NUMBER',\r\n    '/^(\\+)/x' =\u003e 'T_PLUS',\r\n    '/^(-)/x' =\u003e 'T_MINUS',\r\n    '/^\\s+/' =\u003e 'T_SPACE',  // We do not surround it with parentheses because\r\n                            // this is not meaningful for us in this case\r\n]);\r\n```\r\n\r\nSecond, you need a parser for consuming the tokens provided by the lexer.\r\nThe `AbstractParser` class contains an abstract method called `parseImplementation`\r\nthat receives a `TokenStream` as an argument.\r\n\r\n```php\r\nuse Yosymfony\\ParserUtils\\AbstractParser;\r\n\r\nclass Parser extends AbstractParser\r\n{\r\n    protected function parseImplementation(TokenStream $stream)\r\n    {\r\n        $result = $stream-\u003ematchNext('T_NUMBER');\r\n\r\n        while ($stream-\u003eisNextAny(['T_PLUS', 'T_MINUS'])) {\r\n            switch ($stream-\u003emoveNext()-\u003egetName()) {\r\n                case 'T_PLUS':\r\n                    $result += $stream-\u003ematchNext('T_NUMBER');\r\n                    break;\r\n                case 'T_MINUS':\r\n                    $result -= $stream-\u003ematchNext('T_NUMBER');\r\n                    break;\r\n                default:\r\n                    throw new SyntaxErrorException(\"Something went wrong\");\r\n                    break;\r\n            }\r\n        }\r\n\r\n        return $result;\r\n    }\r\n}\r\n```\r\n\r\nNow, you can see the results:\r\n\r\n```php\r\n$parser = new Parser($lexer);\r\n$parser-\u003eparse('1 + 1');          // 2\r\n```\r\n\r\n### The BasicLexer class\r\n\r\nThe lexer has the responsibility of recognizing tokens. This one works line by\r\nline. If you want to generate an special `T_NEWLINE` token for each line\r\nof the input, call `$lexer-\u003egenerateNewlineTokens()` before tokenizing. You can set the\r\nname of this special token using the method `setNewlineTokenName`.\r\n\r\n```php\r\n$lexer = new BasicLexer([...]);\r\n$lexer-\u003egenerateNewlineTokens()\r\n      -\u003esetNewlineTokenName('T_NL');\r\n\r\n$lexer-\u003etokenize('...');\r\n```\r\n\r\nAdditionally, there is another special token `T_EOS` that determines the end of the input\r\nstring. To enable this feature call `$lexer-\u003egenerateEosToken()` before tokenizing.\r\nYou can set the name of this special token using the method `setEosTokenName`.\r\n\r\n```php\r\n$lexer = new BasicLexer([...]);\r\n$lexer-\u003egenerateEosToken()\r\n      -\u003esetEosTokenName('T_MY_EOS');\r\n\r\n$lexer-\u003etokenize('...');\r\n```\r\n\r\n### The TokenStream class\r\n\r\nThis class let you treat with the list of tokens returned by the lexer.\r\n\r\n* **moveNext**: Moves the pointer one token forward. Returns a `Token` object or\r\n`null` if there are not more tokens. e.g: `$ts-\u003emoveNext()`.\r\n* **matchNext**: Matches the next token and returns its value. This method moves\r\nthe pointer one token forward. It will throw an `SyntaxErrorException` exception\r\nif the next token does not match. e.g: `$number = $ts-\u003ematchNext('T_NUMBER')`.\r\n* **isNext**: Checks if the next token matches with the token name passed as argument.\r\ne.g: `$ts-\u003eisNext('T_PLUS') // true or false`.\r\n* **skipWhile**: Skips tokens while they match with the token name passed\r\nas argument. This method moves the pointer \"n\" tokens forward until the\r\nlast one that match with the token name. e.g: `$ts-\u003eskipWhile('T_PLUS')`\r\n* **skipWhileAny**: Skips tokens while they match with one of the token\r\nnames passed as argument. This method moves the pointer \"n\" tokens\r\nforward until the last one that match with one of the token names\r\ne.g: `$ts-\u003eskipWhileAny(['T_PLUS', 'T_MINUS'])`\r\n\r\n* **isNextSequence**: Checks if the following tokens in the stream match with\r\nthe sequence of tokens. e.g: `$ts-\u003eisNextSequence(['T_NUMBER', 'T_PLUS', 'T_NUMBER']) // true or false`.\r\n* **isNextAny**: Checks if one of the tokens passed as argument is the next token.\r\ne.g: `$fs-\u003eisNextAny(['T_PLUS', 'T_SUB']) // true or false`\r\n* **hasPendingTokens**: Has pending tokens? e.g: `$fs-\u003ehasPendingTokens() // true or false`.\r\n* **reset**: Resets the stream to the beginning.\r\n\r\n### Tokens\r\n\r\nTokens are instances of `Token` class, a class than contains the following methods:\r\n\r\n* **getName**: returns the name of the toke. e.g: `T_SUM`.\r\n* **getValue**: returns the value of the token.\r\n* **getLine**: returns the line in where the token is found.\r\n\r\n## Unit tests\r\n\r\nYou can run the unit tests with the following command:\r\n\r\n```bash\r\n$ cd parser-utils\r\n$ composer test\r\n```\r\n\r\n## License\r\n\r\nThis library is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT).\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyosymfony%2Fparser-utils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyosymfony%2Fparser-utils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyosymfony%2Fparser-utils/lists"}