def convert_dslx_to_package( text: Text, name: Text, *, import_cache: ImportCache, additional_search_paths: Tuple[str, ...] = (), print_on_error: bool = True, filename: Optional[Text] = None) -> ir_package_mod.Package: """Converts the given DSLX text to an IR package. Args: text: DSLX text. name: Name of the DSLX module / resulting package. import_cache: Cache used for imported modules. additional_search_paths: Additional import module search paths. print_on_error: Whether to print (to stderr) when an error occurs with the DSLX text. filename: Filename to use when displaying error messages, a fake filesystem will be created to hold the DSLX text under this filename. Returns: The parsed and typechecked DSLX code, converted to an XLS IR package. """ fparse_text = parse_and_typecheck.parse_text_fakefs if filename is None else parse_and_typecheck.parse_text filename = filename or '/fake/conversion.x' m, node_to_type = fparse_text( text, name, print_on_error=print_on_error, import_cache=import_cache, additional_search_paths=additional_search_paths, filename=filename) p = ir_converter.convert_module_to_package(m, node_to_type) return p
def convert_dslx_to_package( text: Text, name: Text, f_import, print_on_error: bool = True, filename: Optional[Text] = None) -> ir_package_mod.Package: """Converts the given DSLX text to an IR package. Args: text: DSLX text. name: Name of the DSLX module / resulting package. f_import: Function for resolving import dependencies. print_on_error: Whether to print (to stderr) when an error occurs with the DSLX text. filename: Filename to use when displaying error messages, a fake filesystem will be created to hold the DSLX text under this filename. Returns: The parsed and typechecked DSLX code, converted to an XLS IR package. """ fparse_text = parse_and_typecheck.parse_text_fakefs if filename is None else parse_and_typecheck.parse_text filename = filename or '/fake/conversion.x' m, node_to_type = fparse_text(text, name, print_on_error=print_on_error, f_import=f_import, filename=filename) p = ir_converter.convert_module_to_package(m, node_to_type) return p
def parse_dslx_and_convert_fakefs(name: Text, text: Text, print_on_error: bool, f_import: Optional[ImportFn], filename: Text) -> ir_package.Package: module, node_to_type = parse_and_typecheck.parse_text_fakefs( text, name, print_on_error=print_on_error, filename=filename, f_import=f_import) return convert_module_to_package(module, node_to_type)
def parse_dslx_and_convert(name: Text, text: Text, print_on_error: bool, f_import: Optional[ImportFn], filename: Text) -> ir_package.Package: """Returns the parsed IR package for the given DSLX module text. Args: name: Name of the package being created. text: DSLX text being parsed. print_on_error: Whether to print (to stderr) if there is an error in the DSLX text. f_import: Function used to import dependency modules. filename: Filename that the DSLX text originates from. """ module, node_to_type = parse_and_typecheck.parse_text( text, name, print_on_error=print_on_error, filename=filename, f_import=f_import) return convert_module_to_package(module, node_to_type)
def parse_and_test(program: Text, name: Text, *, filename: Text, raise_on_error: bool = True, test_filter: Optional[Text] = None, trace_all: bool = False, compare_jit: bool = True, seed: Optional[int] = None) -> bool: """Parses program and run all tests contained inside. Args: program: The program text to parse. name: Name for the module. filename: The filename from which "program" text originates. raise_on_error: When true, raises exceptions that happen in tests; otherwise, simply returns a boolean to the caller when all test have run. test_filter: Test filter specification (e.g. as passed from bazel test environment). trace_all: Whether or not to trace all expressions. compare_jit: Whether or not to assert equality between interpreted and JIT'd function return values. seed: Seed for QuickCheck random input stimulus. Returns: Whether or not an error occurred during parsing/testing. Raises: ScanError, ParseError: In case of front-end errors. TypeInferenceError, TypeError: In case of type errors. EvaluateError: In case of a runtime failure. """ did_fail = False test_name = None import_cache = import_routines.ImportCache() f_import = functools.partial(import_routines.do_import, cache=import_cache) type_info = None try: module = Parser(Scanner(filename, program), name).parse_module() type_info = typecheck.check_module(module, f_import) ir_package = (ir_converter.convert_module_to_package( module, type_info, traverse_tests=True) if compare_jit else None) interpreter = Interpreter(module, type_info, f_import, trace_all=trace_all, ir_package=ir_package) for test_name in module.get_test_names(): if not _matches(test_name, test_filter): continue print('[ RUN UNITTEST ]', test_name, file=sys.stderr) interpreter.run_test(test_name) print('[ OK ]', test_name, file=sys.stderr) if ir_package and module.get_quickchecks(): if seed is None: # We want to guarantee non-determinism by default. See # https://abseil.io/docs/cpp/guides/random#stability-of-generated-sequences # for rationale. seed = int(os.getpid() * time.time()) print(f'[ SEED: {seed} ]') for quickcheck in module.get_quickchecks(): test_name = quickcheck.f.name.identifier print('[ RUN QUICKCHECK ]', test_name, file=sys.stderr) interpreter.run_quickcheck(quickcheck, seed=seed) print('[ OK ]', test_name, file=sys.stderr) except PositionalError as e: did_fail = True parser_helpers.pprint_positional_error(e) if test_name: print('[ FAILED ]', test_name, e.__class__.__name__, file=sys.stderr) if raise_on_error: raise finally: if type_info is not None: type_info.clear_type_info_refs_for_gc() return did_fail