alex/rply — dự án mã nguồn mở — đang có 391 sao GitHub. An attempt to port David Beazley's PLY to RPython, and give it a cooler API.
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
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/>_
alex/rply có 391 sao GitHub — tải lại trang để xem số mới nhất, hoặc xem trực tiếp github.com/alex/rply. TopGit phản chiếu số sao của GitHub nhưng không cam kết đến từng phút.
alex/rply có phải mã nguồn mở không?
Có — alex/rply phát hành theo license BSD-3-Clause, nghĩa là mã nguồn mở để đọc, fork và (tùy license) tái sử dụng. Mã: github.com/alex/rply.
alex/rply còn đang phát triển không?
Commit gần nhất trên alex/rply là 3.6 năm trước (theo timestamp GitHub). Repo có 62 fork — một chỉ báo về mức độ quan tâm của cộng đồng.
alex/rply là gì?
alex/rply (alex/rply) là dự án Python trên GitHub. Theo mô tả gốc: An attempt to port David Beazley's PLY to RPython, and give it a cooler API.
Đọc thêm về alex/rply ở đâu?
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/alex/rply là nguồn chính thức.
Đọc đầy đủ README ở tab phía trên.
Muốn nghe thêm một ý kiến về rply?
Hỏi một AI đọc được trang này — một cú bấm là có ngay nhận định về rply.