예제 #1
0
def parse(text: str, filename: str = "unknown"):
    tokens = list(run_lexer(filename, text))

    res = _parse(State(), Tokens(tokens))
    if res[0]:
        return res[1]
    msgs = []
    assert res[1]
    maxline = 0
    for each in res[1]:
        i, msg = each
        token = tokens[i]
        lineno = token.lineno
        maxline = max(lineno, maxline)
        colno = token.colno
        msgs.append(f"Line {lineno + 1}, column {colno}, {msg}")

    e = SyntaxError()
    e.lineno = maxline + 1
    e.msg = '\n'.join(msgs)
    e.filename = filename
    off = token.offset
    e.offset = off
    e.text = text[:text.find('\n', off)]
    raise e
예제 #2
0
def parse(text: str, filename: str = "unknown") -> Union[Parsed, Errors]:
    tokens = list(run_lexer(filename, text))
    res = _parse(State(), Tokens(tokens))
    if res[0]:
        return res[1]
    msgs = []
    for each in res[1]:
        i, msg = each
        token = tokens[i]
        lineno = token.lineno
        colno = token.colno
        msgs.append(f"Line {lineno}, column {colno}, {msg}")
    raise SyntaxError(f"Filename {filename}:\n" + "\n".join(msgs))
예제 #3
0
def parse(text: str, filename: str = "unknown") -> AST:
    tokens = list(run_lexer(filename, text))
    res = _parse(State(), Tokens(tokens))
    if res[0]:
        return res[1]

    msgs = []
    token = None
    for each in res[1]:
        i, msg = each
        token = tokens[i]
        lineno = token.lineno
        colno = token.colno
        msgs.append(f"Line {lineno}, column {colno}, {msg}")
    err = ParseError()
    err.msg = f"Filename {filename}:\n" + "\n".join(msgs)
    err.offset = 0 if token is None else token.offset
    raise err
예제 #4
0
def parse(text: str, filename: str = "unknown"):
    p = bootstrap.Parser()

    _parse = mk_parser(empty=bootstrap.empty,
                       cons=bootstrap.cons,
                       singleton=bootstrap.singleton,
                       literal=p.literal,
                       terminal=p.terminal,
                       nonterm=p.nonterm,
                       optional=p.optional,
                       nonseplist=p.list,
                       seplist=p.seplist,
                       alias=p.alias,
                       seq=p.seq,
                       action=p.action,
                       prod=p.prod,
                       symbol=p.symbol,
                       integer=p.integer,
                       call=p.call,
                       mktuple=p.mktuple,
                       alt=p.alt,
                       newscope=p.newscope,
                       ith=p.ith,
                       spelling=p.spelling,
                       mklist=p.mklist,
                       tupletail=p.tupletail,
                       location=p.location)
    tokens = list(run_lexer(filename, text))
    res = _parse(State(), Tokens(tokens))
    if res[0]:
        from io import StringIO
        with StringIO() as o:
            for prod in p.prods:
                o.write(prod)
                o.write(';\n')
            return o.getvalue()
    msgs = []
    for each in res[1]:
        i, msg = each
        token = tokens[i]
        lineno = token.lineno
        colno = token.colno
        msgs.append(f"Line {lineno}, column {colno}, {msg}")
    raise SyntaxError(f"Filename {filename}:\n" + "\n".join(msgs))
예제 #5
0
from rbnf_rts.rts import Tokens, State
from relax import run_lexer, mk_parser

parse = mk_parser()

tokens = list(
    run_lexer(
        "<current file>", """
module F = {
    let x = 1
    let main = fun a b c ->
        match a(b, c) with
        | ^a -> 2
}
"""))
print(parse(State(), Tokens(tokens)))
예제 #6
0
파일: test.py 프로젝트: thautwarm/rbnf-rts
from rbnf_rts.rts import Tokens, State
from rbnf_rts.token import Token
from arith import run_lexer, mk_parser
import operator


def unwrap(x: Token):
    return int(x.value)


ops = {
    '+': operator.add,
    '-': operator.sub,
    '*': operator.mul,
    '/': operator.floordiv,
}


def arith_call(op: Token, lhs, rhs):
    return ops[op.value](lhs, rhs)


scope = dict(arith=arith_call, unwrap=unwrap)

parse = mk_parser(**scope)

tokens = list(run_lexer("<current file>", "1 * -2 + 3 * 4"))
got = parse(State(), Tokens(tokens))
assert got == (True, 10)