Example #1
0
    def run_block_test(self, block):
        try:
            lang = block.lang
            code = [block.code]

            if lang.endswith('-repl'):
                lang = lang.rpartition('-')[0]
                code = self.extract_snippets_from_repl(block.code)

            for snippet in code:
                if lang == 'edgeql':
                    ql_parser.parse_block(snippet)
                elif lang == 'sdl':
                    # Strip all the "using extension ..." and comment
                    # lines as they interfere with our module
                    # detection.
                    sdl = re.sub(r'(using\s+extension\s+\w+;)|(#.*?\n)', '',
                                 snippet).strip()

                    # the snippet itself may either contain a module
                    # block or have a fully-qualified top-level name
                    if not sdl or re.match(
                            r'''(?xm)
                                (\bmodule\s+\w+\s*{) |
                                (^.*
                                    (type|annotation|link|property|constraint)
                                    \s+(\w+::\w+)\s+
                                    ({|extending)
                                )
                            ''', sdl):
                        ql_parser.parse_sdl(snippet)
                    else:
                        ql_parser.parse_sdl(f'module default {{ {snippet} }}')
                elif lang == 'edgeql-result':
                    # REPL results
                    pass
                elif lang == 'pseudo-eql':
                    # Skip "pseudo-eql" language as we don't have a
                    # parser for it.
                    pass
                elif lang == 'graphql':
                    graphql_parser.parse(snippet)
                elif lang == 'graphql-schema':
                    # The graphql-schema can be highlighted using graphql
                    # lexer, but it does not have a dedicated parser.
                    pass
                elif lang == 'json':
                    json.loads(snippet)
                elif lang in {
                        'bash', 'powershell', 'c', 'javascript', 'python'
                }:
                    pass
                else:
                    raise LookupError(f'unknown code-lang {lang}')
        except Exception as ex:
            raise AssertionError(
                f'unable to parse {block.lang} code block in '
                f'{block.filename}, around line {block.lineno}') from ex
Example #2
0
    def load_schema(cls, source):
        decls = [('test', qlparser.parse_sdl(source))]

        schema = _load_std_schema()
        return s_ddl.apply_sdl(decls,
                               target_schema=schema,
                               current_schema=schema)
Example #3
0
def parse_module_declarations(schema, declarations):
    """Create a schema and populate it with provided declarations."""
    loader = DeclarationLoader(schema)

    for module_name, declaration in declarations:
        decl_ast = ql_parser.parse_sdl(declaration)
        schema = loader.load_module(module_name, decl_ast)

    return schema
Example #4
0
 def load_schema(cls,
                 source: str,
                 modname: str = 'test') -> s_schema.Schema:
     sdl_schema = qlparser.parse_sdl(f'module {modname} {{ {source} }}')
     schema = _load_std_schema()
     return s_ddl.apply_sdl(
         sdl_schema,
         base_schema=schema,
         current_schema=schema,
     )
Example #5
0
    def load_schema(cls, source: str, modname: str='test') -> s_schema.Schema:
        target = qlparser.parse_sdl(f'module {modname} {{ {source} }}')
        decls = [
            # The target is a Schema with a single module block. We
            # want to extract the declarations from it.
            (modname, target.declarations[0].declarations)
        ]

        schema = _load_std_schema()
        return s_ddl.apply_sdl(
            decls, target_schema=schema, current_schema=schema)
Example #6
0
 def load_schema(
         cls, source: str, modname: Optional[str]=None) -> s_schema.Schema:
     if not modname:
         modname = cls.DEFAULT_MODULE
     sdl_schema = qlparser.parse_sdl(f'module {modname} {{ {source} }}')
     schema = _load_std_schema()
     return s_ddl.apply_sdl(
         sdl_schema,
         base_schema=schema,
         current_schema=schema,
     )
    def run_block_test(self, block):
        try:
            lang = block.lang
            code = [block.code]

            if lang.endswith('-repl'):
                lang = lang.rpartition('-')[0]
                code = self.extract_snippets_from_repl(block.code)

            for snippet in code:
                if lang == 'edgeql':
                    ql_parser.parse_block(snippet)
                elif lang == 'sdl':
                    ql_parser.parse_sdl(snippet)
                elif lang == 'edgeql-result':
                    # REPL results
                    pass
                elif lang == 'pseudo-eql':
                    # Skip "pseudo-eql" language as we don't have a
                    # parser for it.
                    pass
                elif lang == 'graphql':
                    graphql_parser.parse(snippet)
                elif lang == 'graphql-schema':
                    # The graphql-schema can be highlighted using graphql
                    # lexer, but it does not have a dedicated parser.
                    pass
                elif lang == 'json':
                    json.loads(snippet)
                elif lang == 'bash':
                    pass
                else:
                    raise LookupError(f'unknown code-lang {lang}')
        except Exception as ex:
            raise AssertionError(
                f'unable to parse {block.lang} code block in '
                f'{block.filename}, around line {block.lineno}') from ex
Example #8
0
def helpful_parsing(source: str) -> Optional[Any]:
    """helpful_parsing(source: str) -> Optional[Any]:
    
    When EdgeDB 1-alpha was released to the public,
    error reporting was not as sophisticated as it
    is now, but nevertheless I am adding this functionality
    to edgemorph because I wanted it back then
    and did not have it.

    This function helps you debug your EdgeDB SDL module
    files without needing to connect to the database.
    By accessing `col`, `position`, and `line` on the `err`
    argument, we can format a new error message
    to indicate where the EdgeDB syntax error is occuring
    with a style similar to the one found on the database CLI.
    """
    try:
        syntax_lex = qlparser.parse_sdl(source)
        return syntax_lex
    except EdgeQLSyntaxError as err:
        print("\n" + red("ERROR") + ": Syntax correction(s) needed here\n")
        formatted_err = SDLSource(source, err)
        print(str(formatted_err))
        sys.exit(1)
Example #9
0
    def run_block_test(self, block):
        try:
            lang = block.lang

            if lang.endswith('-repl'):
                lang = lang.rpartition('-')[0]
                code = self.extract_snippets_from_repl(block.code)
            elif lang.endswith('-diff'):
                # In the diff block we need to truncate "-"/"+" at the
                # beginning of each line. We will make two copies of
                # the code as the before and after version. Both will
                # be validated.
                before = []
                after = []
                for line in block.code.split('\n'):

                    if line == "":
                        continue

                    first = line.strip()[0]
                    if first == '-':
                        before.append(line[1:])
                    elif first == '+':
                        after.append(line[1:])
                    else:
                        before.append(line[1:])
                        after.append(line[1:])

                code = ['\n'.join(before), '\n'.join(after)]
                # truncate the "-diff" from the language
                lang = lang[:-5]
            else:
                code = [block.code]

            for snippet in code:
                if lang == 'edgeql':
                    ql_parser.parse_block(snippet)
                elif lang == 'sdl':
                    # Strip all the "using extension ..." and comment
                    # lines as they interfere with our module
                    # detection.
                    sdl = re.sub(r'(using\s+extension\s+\w+;)|(#.*?\n)', '',
                                 snippet).strip()

                    # the snippet itself may either contain a module
                    # block or have a fully-qualified top-level name
                    if not sdl or re.match(
                            r'''(?xm)
                                (\bmodule\s+\w+\s*{) |
                                (^.*
                                    (type|annotation|link|property|constraint)
                                    \s+(\w+::\w+)\s+
                                    ({|extending)
                                )
                            ''', sdl):
                        ql_parser.parse_sdl(snippet)
                    else:
                        ql_parser.parse_sdl(f'module default {{ {snippet} }}')
                elif lang == 'edgeql-result':
                    # REPL results
                    pass
                elif lang == 'pseudo-eql':
                    # Skip "pseudo-eql" language as we don't have a
                    # parser for it.
                    pass
                elif lang == 'graphql':
                    graphql_parser.parse(snippet)
                elif lang == 'graphql-schema':
                    # The graphql-schema can be highlighted using graphql
                    # lexer, but it does not have a dedicated parser.
                    pass
                elif lang == 'json':
                    json.loads(snippet)
                elif lang in {
                        'bash', 'powershell', 'shell', 'c', 'javascript',
                        'python', 'typescript', 'go', 'yaml'
                }:
                    pass
                elif lang[-5:] == '-diff':
                    pass
                else:
                    raise LookupError(f'unknown code-lang {lang}')
        except Exception as ex:
            raise AssertionError(
                f'unable to parse {block.lang} code block in '
                f'{block.filename}, around line {block.lineno}') from ex