Name | rply JSON |
Version |
0.7.8
JSON |
| download |
home_page | |
Summary | A pure Python Lex/Yacc that works with RPython |
upload_time | 2021-01-27 21:14:29 |
maintainer | |
docs_url | None |
author | Alex Gaynor |
requires_python | |
license | BSD 3-Clause License |
keywords |
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
RPLY
====
.. image:: https://secure.travis-ci.org/alex/rply.png
:target: https://travis-ci.org/alex/rply
Welcome to RPLY! A pure Python parser generator, that also works with RPython.
It is a more-or-less direct port of David Beazley's awesome PLY, with a new
public API, and RPython support.
You can find the documentation `online`_.
Basic API:
.. code:: python
from rply import ParserGenerator, LexerGenerator
from rply.token import BaseBox
lg = LexerGenerator()
# Add takes a rule name, and a regular expression that defines the rule.
lg.add("PLUS", r"\+")
lg.add("MINUS", r"-")
lg.add("NUMBER", r"\d+")
lg.ignore(r"\s+")
# This is a list of the token names. precedence is an optional list of
# tuples which specifies order of operation for avoiding ambiguity.
# precedence must be one of "left", "right", "nonassoc".
# cache_id is an optional string which specifies an ID to use for
# caching. It should *always* be safe to use caching,
# RPly will automatically detect when your grammar is
# changed and refresh the cache for you.
pg = ParserGenerator(["NUMBER", "PLUS", "MINUS"],
precedence=[("left", ['PLUS', 'MINUS'])], cache_id="myparser")
@pg.production("main : expr")
def main(p):
# p is a list, of each of the pieces on the right hand side of the
# grammar rule
return p[0]
@pg.production("expr : expr PLUS expr")
@pg.production("expr : expr MINUS expr")
def expr_op(p):
lhs = p[0].getint()
rhs = p[2].getint()
if p[1].gettokentype() == "PLUS":
return BoxInt(lhs + rhs)
elif p[1].gettokentype() == "MINUS":
return BoxInt(lhs - rhs)
else:
raise AssertionError("This is impossible, abort the time machine!")
@pg.production("expr : NUMBER")
def expr_num(p):
return BoxInt(int(p[0].getstr()))
lexer = lg.build()
parser = pg.build()
class BoxInt(BaseBox):
def __init__(self, value):
self.value = value
def getint(self):
return self.value
Then you can do:
.. code:: python
parser.parse(lexer.lex("1 + 3 - 2+12-32"))
You can also substitute your own lexer. A lexer is an object with a ``next()``
method that returns either the next token in sequence, or ``None`` if the token
stream has been exhausted.
Why do we have the boxes?
-------------------------
In RPython, like other statically typed languages, a variable must have a
specific type, we take advantage of polymorphism to keep values in a box so
that everything is statically typed. You can write whatever boxes you need for
your project.
If you don't intend to use your parser from RPython, and just want a cool pure
Python parser you can ignore all the box stuff and just return whatever you
like from each production method.
Error handling
--------------
By default, when a parsing error is encountered, an ``rply.ParsingError`` is
raised, it has a method ``getsourcepos()``, which returns an
``rply.token.SourcePosition`` object.
You may also provide an error handler, which, at the moment, must raise an
exception. It receives the ``Token`` object that the parser errored on.
.. code:: python
pg = ParserGenerator(...)
@pg.error
def error_handler(token):
raise ValueError("Ran into a %s where it wasn't expected" % token.gettokentype())
Python compatibility
--------------------
RPly is tested and known to work under Python 2.7, 3.4+, and PyPy. It is
also valid RPython for PyPy checkouts from ``6c642ae7a0ea`` onwards.
Links
-----
* `Source code and issue tracker <https://github.com/alex/rply/>`_
* `PyPI releases <https://pypi.python.org/pypi/rply>`_
* `Talk at PyCon US 2013: So you want to write an interpreter? <http://pyvideo.org/video/1694/so-you-want-to-write-an-interpreter>`_
.. _`online`: https://rply.readthedocs.io/
Raw data
{
"_id": null,
"home_page": "",
"name": "rply",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "",
"author": "Alex Gaynor",
"author_email": "alex.gaynor@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/29/44/96b3e8e6426b1f21f90d73cff83a6df94ef8a57ce8102db6c582d0cb3b2e/rply-0.7.8.tar.gz",
"platform": "",
"description": "RPLY\n====\n\n.. image:: https://secure.travis-ci.org/alex/rply.png\n :target: https://travis-ci.org/alex/rply\n\nWelcome to RPLY! A pure Python parser generator, that also works with RPython.\nIt is a more-or-less direct port of David Beazley's awesome PLY, with a new\npublic API, and RPython support.\n\nYou can find the documentation `online`_.\n\nBasic API:\n\n.. code:: python\n\n from rply import ParserGenerator, LexerGenerator\n from rply.token import BaseBox\n\n lg = LexerGenerator()\n # Add takes a rule name, and a regular expression that defines the rule.\n lg.add(\"PLUS\", r\"\\+\")\n lg.add(\"MINUS\", r\"-\")\n lg.add(\"NUMBER\", r\"\\d+\")\n\n lg.ignore(r\"\\s+\")\n\n # This is a list of the token names. precedence is an optional list of\n # tuples which specifies order of operation for avoiding ambiguity.\n # precedence must be one of \"left\", \"right\", \"nonassoc\".\n # cache_id is an optional string which specifies an ID to use for\n # caching. It should *always* be safe to use caching,\n # RPly will automatically detect when your grammar is\n # changed and refresh the cache for you.\n pg = ParserGenerator([\"NUMBER\", \"PLUS\", \"MINUS\"],\n precedence=[(\"left\", ['PLUS', 'MINUS'])], cache_id=\"myparser\")\n\n @pg.production(\"main : expr\")\n def main(p):\n # p is a list, of each of the pieces on the right hand side of the\n # grammar rule\n return p[0]\n\n @pg.production(\"expr : expr PLUS expr\")\n @pg.production(\"expr : expr MINUS expr\")\n def expr_op(p):\n lhs = p[0].getint()\n rhs = p[2].getint()\n if p[1].gettokentype() == \"PLUS\":\n return BoxInt(lhs + rhs)\n elif p[1].gettokentype() == \"MINUS\":\n return BoxInt(lhs - rhs)\n else:\n raise AssertionError(\"This is impossible, abort the time machine!\")\n\n @pg.production(\"expr : NUMBER\")\n def expr_num(p):\n return BoxInt(int(p[0].getstr()))\n\n lexer = lg.build()\n parser = pg.build()\n\n class BoxInt(BaseBox):\n def __init__(self, value):\n self.value = value\n\n def getint(self):\n return self.value\n\nThen you can do:\n\n.. code:: python\n\n parser.parse(lexer.lex(\"1 + 3 - 2+12-32\"))\n\nYou can also substitute your own lexer. A lexer is an object with a ``next()``\nmethod that returns either the next token in sequence, or ``None`` if the token\nstream has been exhausted.\n\nWhy do we have the boxes?\n-------------------------\n\nIn RPython, like other statically typed languages, a variable must have a\nspecific type, we take advantage of polymorphism to keep values in a box so\nthat everything is statically typed. You can write whatever boxes you need for\nyour project.\n\nIf you don't intend to use your parser from RPython, and just want a cool pure\nPython parser you can ignore all the box stuff and just return whatever you\nlike from each production method.\n\nError handling\n--------------\n\nBy default, when a parsing error is encountered, an ``rply.ParsingError`` is\nraised, it has a method ``getsourcepos()``, which returns an\n``rply.token.SourcePosition`` object.\n\nYou may also provide an error handler, which, at the moment, must raise an\nexception. It receives the ``Token`` object that the parser errored on.\n\n.. code:: python\n\n pg = ParserGenerator(...)\n\n @pg.error\n def error_handler(token):\n raise ValueError(\"Ran into a %s where it wasn't expected\" % token.gettokentype())\n\nPython compatibility\n--------------------\n\nRPly is tested and known to work under Python 2.7, 3.4+, and PyPy. It is\nalso valid RPython for PyPy checkouts from ``6c642ae7a0ea`` onwards.\n\nLinks\n-----\n\n* `Source code and issue tracker <https://github.com/alex/rply/>`_\n* `PyPI releases <https://pypi.python.org/pypi/rply>`_\n* `Talk at PyCon US 2013: So you want to write an interpreter? <http://pyvideo.org/video/1694/so-you-want-to-write-an-interpreter>`_\n\n.. _`online`: https://rply.readthedocs.io/\n\n\n",
"bugtrack_url": null,
"license": "BSD 3-Clause License",
"summary": "A pure Python Lex/Yacc that works with RPython",
"version": "0.7.8",
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "c07cf66be9e75485ae6901ae77d8bdbc3c0e99ca748ab927b3e18205759bde09",
"md5": "d6f6ea481a37b4c26c017b790b7711a8",
"sha256": "28ffd11d656c48aeb8c508eb382acd6a0bd906662624b34388751732a27807e7"
},
"downloads": -1,
"filename": "rply-0.7.8-py2.py3-none-any.whl",
"has_sig": false,
"md5_digest": "d6f6ea481a37b4c26c017b790b7711a8",
"packagetype": "bdist_wheel",
"python_version": "py2.py3",
"requires_python": null,
"size": 16039,
"upload_time": "2021-01-27T21:14:27",
"upload_time_iso_8601": "2021-01-27T21:14:27.946518Z",
"url": "https://files.pythonhosted.org/packages/c0/7c/f66be9e75485ae6901ae77d8bdbc3c0e99ca748ab927b3e18205759bde09/rply-0.7.8-py2.py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "294496b3e8e6426b1f21f90d73cff83a6df94ef8a57ce8102db6c582d0cb3b2e",
"md5": "200930c68316dff60ff2658c770d9084",
"sha256": "2a808ac25a4580a9991fc304d64434e299a8fc75760574492f242cbb5bb301c9"
},
"downloads": -1,
"filename": "rply-0.7.8.tar.gz",
"has_sig": false,
"md5_digest": "200930c68316dff60ff2658c770d9084",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 15850,
"upload_time": "2021-01-27T21:14:29",
"upload_time_iso_8601": "2021-01-27T21:14:29.594990Z",
"url": "https://files.pythonhosted.org/packages/29/44/96b3e8e6426b1f21f90d73cff83a6df94ef8a57ce8102db6c582d0cb3b2e/rply-0.7.8.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2021-01-27 21:14:29",
"github": false,
"gitlab": false,
"bitbucket": false,
"lcname": "rply"
}