{ "info": { "author": "Rapha\u00ebl Barrois", "author_email": "raphael.barrois+tdparser@polytechnique.org", "bugtrack_url": null, "classifiers": [ "Development Status :: 4 - Beta", "Intended Audience :: Developers", "License :: OSI Approved :: BSD License", "Operating System :: OS Independent", "Programming Language :: Python", "Programming Language :: Python :: 2.6", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3", "Topic :: Software Development :: Libraries :: Python Modules" ], "description": "tdparser\n========\n\n\nThis library aims to provide an efficient way to write simple lexer/parsers in Python, using the\n`Top-Down parsing algorithm `_.\n\nCode is maintained on `GitHub `_, documentation is available on `ReadTheDocs `_.\n\nOther python libraries provide parsing/lexing tools (see http://nedbatchelder.com/text/python-parsers.html for a few examples); distinctive features of tdparser are:\n\n- Avoid docstring-based grammar definitions\n- Provide a generic parser structure, able to handle any grammar\n- Don't generate code\n- Let the user decide the nature of parsing results: abstract syntax tree, final expression, ...\n\n\nExample\n=======\n\nHere is the definition for a simple arithmetic parser::\n\n import re\n\n from tdparser import Lexer, Token\n\n class Integer(Token):\n def __init__(self, text):\n self.value = int(text)\n\n def nud(self, context):\n \"\"\"What the token evaluates to\"\"\"\n return self.value\n\n class Addition(Token):\n lbp = 10 # Precedence\n\n def led(self, left, context):\n \"\"\"Compute the value of this token when between two expressions.\"\"\"\n # Fetch the expression to the right, stoping at the next boundary\n # of same precedence\n right_side = context.expression(self.lbp)\n return left + right_side\n\n class Substraction(Token):\n lbp = 10 # Same precedence as addition\n\n def led(self, left, context):\n return left - context.expression(self.lbp)\n\n def nud(self, context):\n \"\"\"When a '-' is present on the left of an expression.\"\"\"\n # This means that we are returning the opposite of the next expression\n return - context.expression(self.lbp)\n\n class Multiplication(Token):\n lbp = 20 # Higher precedence than addition/substraction\n\n def led(self, left, context):\n return left * context.expression(self.lbp)\n\n\n lexer = Lexer(with_parens=True)\n lexer.register_token(Integer, re.compile(r'\\d+'))\n lexer.register_token(Addition, re.compile(r'\\+'))\n lexer.register_token(Substraction, re.compile(r'-'))\n lexer.register_token(Multiplication, re.compile(r'\\*'))\n\n def parse(text):\n return lexer.parse(text)\n\nUsing it returns the expected value::\n\n >>> parse(\"1+1\")\n 2\n >>> parse(\"1 + -2 * 3\")\n -5\n\nAdding new tokens is straightforward::\n\n class Division(Token):\n lbp = 20 # Same precedence as Multiplication\n\n def led(self, left, context):\n return left // context.expression(self.lbp)\n\n lexer.register_token(Division, re.compile(r'/'))\n\nAnd using it::\n\n >>> parse(\"3 + 12 / 3\")\n 7\n\nLet's add the exponentiation operator::\n\n class Power(Token):\n lbp = 30 # Higher than mult\n\n def led(self, left, context):\n # We pick expressions with a lower precedence, so that\n # 2 ** 3 ** 2 computes as 2 ** (3 ** 2)\n return left ** context.expression(self.lbp - 1)\n\n lexer.register_token(Power, re.compile(r'\\*\\*'))\n\nAnd use it::\n\n >>> parse(\"2 ** 3 ** 2\")\n 512\n", "description_content_type": null, "docs_url": null, "download_url": "http://pypi.python.org/pypi/tdparser/", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "http://github.com/rbarrois/tdparser", "keywords": "parser,lexer,token,topdown", "license": "BSD", "maintainer": null, "maintainer_email": null, "name": "tdparser", "package_url": "https://pypi.org/project/tdparser/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/tdparser/", "project_urls": { "Download": "http://pypi.python.org/pypi/tdparser/", "Homepage": "http://github.com/rbarrois/tdparser" }, "release_url": "https://pypi.org/project/tdparser/1.1.6/", "requires_dist": null, "requires_python": null, "summary": "A very simple parsing library, based on the Top-Down algorithm.", "version": "1.1.6" }, "last_serial": 865478, "releases": { "0.1.0": [], "1.1.0": [ { "comment_text": "", "digests": { "md5": "0d8893df67dc6b0b6e4bb188a2c74f1b", "sha256": "fe39949f7b15208d6c35fdcea28e74c6b47927a3d0373716087870432df9ba5a" }, "downloads": -1, "filename": "tdparser-1.1.0.tar.gz", "has_sig": false, "md5_digest": "0d8893df67dc6b0b6e4bb188a2c74f1b", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 9742, "upload_time": "2012-08-24T16:54:00", "url": "https://files.pythonhosted.org/packages/d7/6f/1d01b152e48904ab0e1e4bdd7cc04f4b7558e58c3e5e858d5bb04e5b5ce6/tdparser-1.1.0.tar.gz" } ], "1.1.1": [ { "comment_text": "", "digests": { "md5": "ceaea3d8469c09c4defd6abb25808dcc", "sha256": "5be49f97b809aed4d074c4d886e9004852b1e6ad9c95217424c7626e78e09f8c" }, "downloads": -1, "filename": "tdparser-1.1.1.tar.gz", "has_sig": false, "md5_digest": "ceaea3d8469c09c4defd6abb25808dcc", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 18864, "upload_time": "2012-11-02T14:51:25", "url": "https://files.pythonhosted.org/packages/07/e9/a18d792d39777d9bda59555f2d9ea5e1e9eabc4ff394bbe0526cddaf7835/tdparser-1.1.1.tar.gz" } ], "1.1.2": [ { "comment_text": "", "digests": { "md5": "e269e1181a04314bd73d704700e9d402", "sha256": "8aa8ee1723d02acb109a2801b077c79659610167f88e1c7d962ee6fca6c331ac" }, "downloads": -1, "filename": "tdparser-1.1.2.tar.gz", "has_sig": false, "md5_digest": "e269e1181a04314bd73d704700e9d402", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 19824, "upload_time": "2012-11-02T15:48:22", "url": "https://files.pythonhosted.org/packages/06/1d/a496202d3461ceea8fb2e33a808384dc21cdcfdbe620d936496e92881abc/tdparser-1.1.2.tar.gz" } ], "1.1.3": [ { "comment_text": "", "digests": { "md5": "058a305ed584c12acc6183e01805f1d1", "sha256": "14c91242385a187f8fd22d6956800692080e89ac1c96a12cfe463ad99151d907" }, "downloads": -1, "filename": "tdparser-1.1.3.tar.gz", "has_sig": false, "md5_digest": "058a305ed584c12acc6183e01805f1d1", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 19813, "upload_time": "2012-11-02T16:24:26", "url": "https://files.pythonhosted.org/packages/bf/5b/980d53f7ab8f9b6f49e3528014a7be1a6b62d2e90186daa4dc3897f2815f/tdparser-1.1.3.tar.gz" } ], "1.1.4": [ { "comment_text": "", "digests": { "md5": "6a4b30850a1443753e2f6fb974f72f05", "sha256": "5add525ecdd2731dfedff843cec6e922ccfdab8d386fc9185769ee57c0df5f47" }, "downloads": -1, "filename": "tdparser-1.1.4.tar.gz", "has_sig": false, "md5_digest": "6a4b30850a1443753e2f6fb974f72f05", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 20178, "upload_time": "2013-03-11T20:48:18", "url": "https://files.pythonhosted.org/packages/e9/e5/601d14b7936035baeca18253299b939f0fd8e4559a25980febb429dd7415/tdparser-1.1.4.tar.gz" } ], "1.1.6": [ { "comment_text": "", "digests": { "md5": "4edb068641f58c824fc1a0a233973a5d", "sha256": "d12ff20157f28b0f19113875f6fbff0812900b64028b2361d45d6c8a4cee5af9" }, "downloads": -1, "filename": "tdparser-1.1.6.tar.gz", "has_sig": false, "md5_digest": "4edb068641f58c824fc1a0a233973a5d", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 20395, "upload_time": "2013-09-14T11:55:36", "url": "https://files.pythonhosted.org/packages/92/d8/0142d0866549a2969c7085d45bc27093731f5f0d0f61b24b3d1dd8105b5f/tdparser-1.1.6.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "4edb068641f58c824fc1a0a233973a5d", "sha256": "d12ff20157f28b0f19113875f6fbff0812900b64028b2361d45d6c8a4cee5af9" }, "downloads": -1, "filename": "tdparser-1.1.6.tar.gz", "has_sig": false, "md5_digest": "4edb068641f58c824fc1a0a233973a5d", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 20395, "upload_time": "2013-09-14T11:55:36", "url": "https://files.pythonhosted.org/packages/92/d8/0142d0866549a2969c7085d45bc27093731f5f0d0f61b24b3d1dd8105b5f/tdparser-1.1.6.tar.gz" } ] }