コード例 #1
0
def do_import(
    subject: import_fn.ImportTokens, cache: Dict[import_fn.ImportTokens,
                                                 import_fn.ModuleInfo]
) -> import_fn.ModuleInfo:
    """Imports the module identified (globally) by 'subject'.

  Resolves against an existing import in 'cache' if it is present.

  Args:
    subject: Tokens that globally uniquely identify the module to import; e.g.
      something built-in like ('std',) for the standard library or something
      fully qualified like ('xls', 'lib', 'math').
    cache: Cache that we resolve against so we don't waste resources
      re-importing things in the import DAG.

  Returns:
    The imported module information.
  """
    assert subject
    if subject in cache:
        return cache[subject]

    if subject in [('std', ), ('float32', ), ('bfloat16', )]:
        path = 'xls/dslx/stdlib/{}.x'.format(subject[0])
    else:
        path = os.path.join(*subject) + '.x'

    f_import = functools.partial(do_import, cache=cache)
    fully_qualified_name = '.'.join(subject)

    if os.path.exists(path):
        with open(path, mode='rb') as f:
            contents = f.read().decode('utf-8')
    elif os.path.exists(os.path.join(os.path.pardir, path)):
        # Genrules in-house execute inside a subdirectory, so we also search
        # starting from the parent directory for now.
        #
        # An alternative would be to explicitly note the DSLX_PATH when invoking the
        # tool in this special genrule context, but since we expect module paths to
        # be fully qualified at the moment, we opt for this kluge.
        path = os.path.join(os.path.pardir, path)
        with open(path, mode='rb') as f:
            contents = f.read().decode('utf-8')
    else:
        contents = runfiles.get_contents_as_text(path)
        path = runfiles.get_path(path)

    logging.vlog(3, 'Parsing and typechecking %r: start', fully_qualified_name)
    m, node_to_type = parse_and_typecheck.parse_text(contents,
                                                     fully_qualified_name,
                                                     f_import=f_import,
                                                     filename=path,
                                                     print_on_error=True)
    logging.vlog(3, 'Parsing and typechecking %r: done', fully_qualified_name)

    assert node_to_type is not None
    cache[subject] = (m, node_to_type)
    return m, node_to_type
コード例 #2
0
 def _get_module(
         self, program: Text) -> Tuple[ast.Module, type_info_mod.TypeInfo]:
     filename = '/fake/test_program.x'
     with fakefs_test_util.scoped_fakefs(filename, program):
         m, type_info = parse_and_typecheck.parse_text(program,
                                                       'test_program',
                                                       print_on_error=True,
                                                       f_import=None,
                                                       filename=filename)
         return m, type_info
コード例 #3
0
 def _get_module(self,
                 program: Text) -> Tuple[ast.Module, deduce.NodeToType]:
     filename = '/fake/test_program.x'
     with fakefs_util.scoped_fakefs(filename, program):
         m, node_to_type = parse_and_typecheck.parse_text(
             program,
             'test_program',
             print_on_error=True,
             f_import=None,
             filename=filename)
         return m, node_to_type
コード例 #4
0
 def _get_module(self,
                 program: Text) -> Tuple[ast.Module, type_info_mod.TypeInfo]:
   filename = '/fake/test_program.x'
   with fakefs_test_util.scoped_fakefs(filename, program):
     m, type_info = parse_and_typecheck.parse_text(
         program,
         'test_program',
         print_on_error=True,
         import_cache=ImportCache(),
         additional_search_paths=(),
         filename=filename)
     return m, type_info
コード例 #5
0
ファイル: ir_converter_helpers.py プロジェクト: hixio-mh/xls
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)
コード例 #6
0
ファイル: parser_main.py プロジェクト: zeta1999/xls
def main(argv):
    if len(argv) > 2:
        raise app.UsageError('Too many command-line arguments.')

    path = argv[1]
    with open(path) as f:
        text = f.read()

    name = os.path.basename(path)
    name, _ = os.path.splitext(name)

    import_cache = {}
    f_import = functools.partial(import_routines.do_import, cache=import_cache)

    module = parse_and_typecheck.parse_text(text,
                                            name,
                                            filename=path,
                                            print_on_error=True,
                                            f_import=f_import)
    pprint.pprint(module)
コード例 #7
0
ファイル: parser_main.py プロジェクト: masc-ucsc/xls
def main(argv):
    init_xls.init_xls(argv)
    if len(argv) > 2:
        raise app.UsageError('Too many command-line arguments.')

    path = argv[1]
    with open(path) as f:
        text = f.read()

    name = os.path.basename(path)
    name, _ = os.path.splitext(name)

    importer = import_helpers.Importer()

    module = parse_and_typecheck.parse_text(
        text,
        name,
        filename=path,
        print_on_error=True,
        import_cache=importer.cache,
        additional_search_paths=importer.additional_search_paths)
    pprint.pprint(module)