Commit 49944154 authored by SVN-Git Migration's avatar SVN-Git Migration

Imported Upstream version 0.7.1

parent 0beb2b80
include README.rst
include LICENSE
Metadata-Version: 1.0
Name: rply
Version: 0.7.0
Version: 0.7.1
Summary: A pure Python Lex/Yacc that works with RPython
Home-page: UNKNOWN
Author: Alex Gaynor
......
Metadata-Version: 1.0
Name: rply
Version: 0.7.1
Summary: A pure Python Lex/Yacc that works with RPython
Home-page: UNKNOWN
Author: Alex Gaynor
Author-email: alex.gaynor@gmail.com
License: UNKNOWN
Description: RPLY
====
.. image:: https://secure.travis-ci.org/alex/rply.png
:target: http://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.
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.6, 2.7, 3.1, and 3.2. 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>`_
Platform: UNKNOWN
LICENSE
MANIFEST.in
README.rst
setup.cfg
setup.py
rply/__init__.py
rply/errors.py
rply/grammar.py
rply/lexer.py
rply/lexergenerator.py
rply/parser.py
rply/parsergenerator.py
rply/token.py
rply/utils.py
rply.egg-info/PKG-INFO
rply.egg-info/SOURCES.txt
rply.egg-info/dependency_links.txt
rply.egg-info/top_level.txt
\ No newline at end of file
......@@ -2,6 +2,7 @@ import os
import hashlib
import json
import random
import stat
import string
import sys
import tempfile
......@@ -124,17 +125,23 @@ class ParserGenerator(object):
cache_file = os.path.join(
tempfile.gettempdir(),
"rply-%s-%s-%s.json" % (self.VERSION, self.cache_id, self.compute_grammar_hash(g))
"rply-%s-%s-%s-%s.json" % (self.VERSION, os.getuid(), self.cache_id, self.compute_grammar_hash(g))
)
table = None
if os.path.exists(cache_file):
with open(cache_file) as f:
data = json.load(f)
if self.data_is_valid(g, data):
table = LRTable.from_cache(g, data)
stat_result = os.fstat(f.fileno())
if (
stat_result.st_uid == os.getuid() and
stat.S_IMODE(stat_result.st_mode) == 0o0600
):
if self.data_is_valid(g, data):
table = LRTable.from_cache(g, data)
if table is None:
table = LRTable.from_grammar(g)
with open(cache_file, "w") as f:
fd = os.open(cache_file, os.O_RDWR | os.O_CREAT | os.O_EXCL, 0o0600)
with os.fdopen(fd, "w") as f:
json.dump(self.serialize_table(table), f)
if table.sr_conflicts:
warnings.warn(
......
[wheel]
universal = 1
[egg_info]
tag_build =
tag_date = 0
tag_svn_revision = 0
from distutils.core import setup
from setuptools import setup
with open("README.rst") as f:
......@@ -8,7 +8,7 @@ setup(
name="rply",
description="A pure Python Lex/Yacc that works with RPython",
long_description=readme,
version="0.7.0",
version="0.7.1",
author="Alex Gaynor",
author_email="alex.gaynor@gmail.com",
packages=["rply"],
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment