Verified Commit 3c9e0e3b authored by Alberto Miranda's avatar Alberto Miranda ♨️
Browse files

Add initial support for multiple transformers

parent ed972bb2
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
from .rpcc import Rpcc
+3 −9
Original line number Diff line number Diff line
@@ -2,11 +2,10 @@ import sys

import lark.exceptions

from rpcc import Rpcc
from rpcc.arguments import parse_args
from rpcc.exceptions import RpccError
from rpcc.console import console
from rpcc.parser import Parser
from rpcc.transformers.cxx import Transformer as CxxTransformer


def main(args=None):
@@ -16,20 +15,15 @@ def main(args=None):
        args = sys.argv[1:]

    args = parse_args(args)

    console.configure(args.color_diagnostics)

    rpcc = Rpcc(args.rpc_proto_file, args.output_lang)
    try:
        parser = Parser()
        ast = parser.parse(args.rpc_proto_file)
        rpcc.transform()
    except RpccError as e:
        console.eprint(e)
        return 1

    try:
        ast = CxxTransformer(parser).transform(ast)
    except lark.exceptions.VisitError as e:
        console.eprint(e.orig_exc)
    return 0


+13 −1
Original line number Diff line number Diff line
@@ -22,7 +22,7 @@ class PrintVersion(argparse.Action):
        sys.exit(0)


def parse_args(args):
def parse_args(args) -> argparse.Namespace:
    parser = argparse.ArgumentParser(prog="rpcc",
                                     description="Parse RPC_PROTO_FILE and generate C++ output based on the options "
                                                 "given.")
@@ -33,6 +33,18 @@ def parse_args(args):
        help="The file containing the specifications for the RPCs."
    )

    parser.add_argument(
        "--std",
        choices=["c++17"],
        dest="output_lang",
        metavar="LANG",
        default="c++17",
        help="Determine the output language. Possible values for LANG are:\n"
             "\n"
             "c++17\n"
             "    2017 ISO C++ standard."
    )

    parser.add_argument(
        "--color-diagnostics",
        choices=["never", "always", "auto"],
+13 −1
Original line number Diff line number Diff line
from typing import Collection

from rpcc.meta import FileLocation


class ConfigurationError(ValueError):
    pass


def assert_config(value, options: Collection, msg="Got %r, expected one of %s"):
    if value not in options:
        raise ConfigurationError(msg % (value, options))


class RpccError(SyntaxError):
    label = "syntax error"

@@ -24,7 +35,8 @@ class UnexpectedToken(RpccError):
    def _format_expected(expected, terminals):
        d = {t.name: t for t in terminals}

        expected = [str(d[t_name].pattern).replace('\\', "").replace('\'', '') if t_name in d else t_name for t_name in expected]
        expected = [str(d[t_name].pattern).replace('\\', "").replace('\'', '') if t_name in d else t_name for t_name in
                    expected]

        if len(expected) > 1:
            return "Expected one of:\n\t* %s\n" % '\n\t* '.join(expected)
+35 −0
Original line number Diff line number Diff line
import string
from typing import List

import lark


class Argument:
    """A remote procedure argument.
@@ -12,6 +15,10 @@ class Argument:
        >>> Argument("foobar", "int")
        Argument(...)
    """

    argname: str
    typename: str

    def __init__(self, argname: str, typename: str) -> None:
        self.argname = argname
        self.typename = typename
@@ -31,6 +38,7 @@ class ReturnVariable:
        >>> ReturnVariable("barbaz", "int")
        ReturnVariable(...)
    """

    def __init__(self, varname: str, typename: str) -> None:
        self.varname = varname
        self.typename = typename
@@ -51,10 +59,37 @@ class RemoteProcedure:
        >>> RemoteProcedure("send_message", [Argument("message", "string")], ReturnVariable("retval", "uint32"))
        RemoteProcedure(...)
    """

    def __init__(self, name: str, args: List[Argument], retval: ReturnVariable) -> None:
        self.id = 42
        self.name = name
        self.args = args
        self.retval = retval

    def __repr__(self) -> str:
        return f"RemoteProcedure(name='{self.name}', args={self.args}, retval={self.retval})"


class IR:
    """The Intermediate Representation for our source-to-source compiler.

    Parameters:
        remote_procedures: a list of the remote procedures defined in the iput file.

    Example:
        >>> IR([
        >>>    RemoteProcedure("send_message", [Argument("message", "string")], ReturnVariable("retval", "uint32"))
        >>>    RemoteProcedure("shutdown", [], ReturnVariable("retval", "uint32"))
        >>> ]
        IR(...)
    """

    remote_procedures: List[RemoteProcedure]
    tree = lark.Tree

    def __init__(self, remote_procedures: List[RemoteProcedure]):
        self.remote_procedures = remote_procedures

    def __iter__(self):
        for rpc in self.remote_procedures:
            yield rpc
Loading