{ "info": { "author": "Jon Coppeard", "author_email": "jon@coppeard.net", "bugtrack_url": null, "classifiers": [ "Development Status :: 4 - Beta", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", "Programming Language :: Python", "Programming Language :: Python :: 3", "Topic :: Software Development :: Libraries :: Python Modules", "Topic :: Text Processing :: General" ], "description": "Varas\r\n=====\r\n\r\nVaras is a python Pratt parser framework, which aims to be general\r\npurpose and easy to use.\r\n\r\nPratt parsers (also known as top down operator precendence parsers) are\r\nsimple to use and resonably efficent. There are a few articles\r\ndescribing them on the web:\r\n\r\n http://javascript.crockford.com/tdop/tdop.html\r\n\r\n http://eli.thegreenplace.net/2010/01/02/top-down-operator-precedence-parsing/\r\n\r\nInstallation\r\n------------\r\n\r\nThe source is available for download from github here:\r\n\r\n https://github.com/JonCoppeard/varas/downloads\r\n\r\nYou can run the test code like this:\r\n\r\n python run_tests.py\r\n\r\nAnd you can install it using python distutils in the normal way:\r\n\r\n $ python setup.py install\r\n\r\nA simple example\r\n----------------\r\n\r\nAs a simple example, we can build a calculator that computes numerical expressions.\r\n\r\nFirst we create a tokenizer. There will be two types of tokens - literal\r\nnumbers and mathematical operators. Tokens are matched by regular expressions.\r\n\r\n >>> from varas import *\r\n >>> tok = Tokenizer( (\"\\d+\", \"NUMBER\"),\r\n ... (\"[-+*/^]\", None) )\r\n\r\n >>> list(tok.tokenize(\"2 +\"))\r\n [Token('NUMBER', '2'), Token('+', '+'), Token(Token.END_TOKEN, '')]\r\n\r\nNext we set up an expression spec to tell the parser what to do when it\r\nencounters tokens.\r\n\r\n >>> expr = ExprSpec()\r\n\r\nTo deal with number literals we add an action that just calls the int() function\r\non the content of the token:\r\n\r\n >>> expr.add_word(\"NUMBER\", lambda token: int(token.content))\r\n\r\nTo parse an expression and calculate the answer, we need to plug the parser and\r\ntokenizer together. This is done by creating a Parser object which takes both\r\nthe expression spec and a token generator. We can then call the parse() method\r\nto actually parse the input:\r\n\r\n >>> def calc(text):\r\n ... return Parser(expr, tok.tokenize(text)).parse()\r\n\r\nWe can check that this parses numbers:\r\n\r\n >>> calc(\"42\")\r\n 42\r\n\r\nOperators for addition and subtraction can be added to the expression spec like\r\nthis:\r\n\r\n >>> expr.add_binary_op(\"+\", 10, Assoc.LEFT,\r\n ... lambda token, left, right: left + right)\r\n >>> expr.add_binary_op(\"-\", 10, Assoc.LEFT,\r\n ... lambda token, left, right: left - right)\r\n\r\nThis allows us to process simple expressions:\r\n\r\n >>> calc(\"2 + 2\")\r\n 4\r\n\r\nThe number '10' in the lines above indicates the precedence of the operator. To\r\nadd multiplication and division operators, we need to set a higher\r\nprecendence value to ensure the correct result:\r\n\r\n >>> expr.add_binary_op(\"*\", 20, Assoc.LEFT,\r\n ... lambda token, left, right: left * right)\r\n >>> expr.add_binary_op(\"/\", 20, Assoc.LEFT,\r\n ... lambda token, left, right: left / right)\r\n\r\n >>> calc(\"1 + 2 * 3\")\r\n 7\r\n\r\nA more detailed example can be found in the file test/calc_example.py.\r\n", "description_content_type": null, "docs_url": null, "download_url": "UNKNOWN", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/JonCoppeard/varas", "keywords": null, "license": "UNKNOWN", "maintainer": null, "maintainer_email": null, "name": "Varas", "package_url": "https://pypi.org/project/Varas/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/Varas/", "project_urls": { "Download": "UNKNOWN", "Homepage": "https://github.com/JonCoppeard/varas" }, "release_url": "https://pypi.org/project/Varas/1.1/", "requires_dist": null, "requires_python": null, "summary": "Pratt parser framework", "version": "1.1" }, "last_serial": 785957, "releases": { "1.0": [ { "comment_text": "", "digests": { "md5": "f5fd7995c6ea058cff99a3c9e2f00cf3", "sha256": "4c681919b83ba55793a04a63f36d29ad2cd51a5a61fcd45e29b16fd0e7b927b2" }, "downloads": -1, "filename": "Varas-1.0.tar.gz", "has_sig": false, "md5_digest": "f5fd7995c6ea058cff99a3c9e2f00cf3", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7641, "upload_time": "2012-04-17T17:19:19", "url": "https://files.pythonhosted.org/packages/3e/0c/0d52b7f6eed21484e2ee2a87446965f6bbe0323951fb2f949955f9d2b21b/Varas-1.0.tar.gz" } ], "1.1": [ { "comment_text": "", "digests": { "md5": "7c26e15a666f3fe18283f1443a819f7c", "sha256": "51b101e0729de6cb84c3010278382db993314deb5b20bda502daa08b5207eccd" }, "downloads": -1, "filename": "Varas-1.1.tar.gz", "has_sig": false, "md5_digest": "7c26e15a666f3fe18283f1443a819f7c", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 9133, "upload_time": "2012-04-24T16:46:44", "url": "https://files.pythonhosted.org/packages/48/8d/ddf6c06c7857d9d33d4b4eac046434a4fadcdfc0dbeae933c09444e66175/Varas-1.1.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "7c26e15a666f3fe18283f1443a819f7c", "sha256": "51b101e0729de6cb84c3010278382db993314deb5b20bda502daa08b5207eccd" }, "downloads": -1, "filename": "Varas-1.1.tar.gz", "has_sig": false, "md5_digest": "7c26e15a666f3fe18283f1443a819f7c", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 9133, "upload_time": "2012-04-24T16:46:44", "url": "https://files.pythonhosted.org/packages/48/8d/ddf6c06c7857d9d33d4b4eac046434a4fadcdfc0dbeae933c09444e66175/Varas-1.1.tar.gz" } ] }