Exemple #1
0
 def __init__(self, src, filename,  
              output=None, debug=False, 
              parse_debug=0, code_debug=False):
     """
     """
     self.code = src
     self.filename = filename
     self.parse_dbg_lvl = 5 if parse_debug else 0
     self.output = output
     self.debug = debug
     self.generator = JsonCCGen()
     self.generator.create(filename, output)
Exemple #2
0
class JsonCC(object):
    """
    Main Jsoncc object 
    """
    def __init__(self, src, filename,  
                 output=None, debug=False, 
                 parse_debug=0, code_debug=False):
        """
        """
        self.code = src
        self.filename = filename
        self.parse_dbg_lvl = 5 if parse_debug else 0
        self.output = output
        self.debug = debug
        self.generator = JsonCCGen()
        self.generator.create(filename, output)


    def parse(self):
        """ Run the parser and get the AST"""
        if self.debug:
            print(self.code)

        logging.debug("Running the parser ...")
        parser = c_parser.CParser(lex_optimize=True, 
                                  lextab='lextab', 
                                  yacc_optimize=True, 
                                  yacctab='yacctab', 
                                  yacc_debug=False)

        self.ast = parser.parse(self.code, filename=self.filename, 
                                debuglevel=self.parse_dbg_lvl)
        if self.ast is None:
            logging.error("Parsing failed ...")
            raise RuntimeError("Parsing failed ...")

        if debug:
            self.ast.show()

    def gen(self):
        """ Generate the code!"""
        self.parse()
        logging.debug("Generate the C code")
        jv = JsonVisitor(self.generator)
        jv.visit(self.ast)
        self.generator.done()

        if self.debug:
            print(self.generator.genH())
            print(self.generator.genC())

        if self.output is not None:
            logging.debug("Writing the generated functions to %s.n and %s.c" %(self.output, self.output))
            hname = ".".join((self.output, "h"))
            cname = ".".join((self.output, "c"))
            with open(cname, "w") as f:
                f.write(self.generator.genC())
            logging.debug("Writing the generated function declarations to %s" % (hname))
            with open(hname, "w") as f:
                f.write(self.generator.genH())
        else:
            sys.stdout.write("/* function bodies */\n")
            sys.stdout.write(self.generator.genC())
            sys.stdout.write("/* function declarations */\n")
            sys.stdout.write(self.generator.genH())