예제 #1
0
파일: parse.py 프로젝트: number0/bayeslite
def parse_bql_phrases(scanner):
    semantics = BQLSemantics()
    parser = grammar.Parser(semantics)
    while not semantics.failed:
        token = scanner.read()
        semantics.context.append(token)
        if token[0] == -1:  # error
            semantics.syntax_error(token)
        else:
            if token[0] == 0:  # EOF
                # Implicit ; at EOF.
                parser.feed((grammar.T_SEMI, ''))
            parser.feed(token)
        if semantics.phrase is not None:
            phrase = semantics.phrase
            semantics.phrase = None
            if 0 < len(semantics.errors):
                # Keep parsing in order to detect more errors, but
                # don't yield any broken phrases in case the caller
                # will try to process them before we finish parsing
                # the whole thing.
                continue
            if 0 < scanner.n_numpar:
                n_numpar = scanner.n_numpar
                nampar_map = scanner.nampar_map
                yield ast.Parametrized(phrase, n_numpar, nampar_map)
            else:
                yield phrase
        if token[0] == 0:  # EOF
            break
    if 0 < len(semantics.errors):
        raise BQLParseError(semantics.errors)
    if semantics.failed:
        raise BQLParseError(['parse failed mysteriously!'])
예제 #2
0
def parse(tokenses):
    semantics = CGPM_Semantics()
    parser = grammar.Parser(semantics)
    for token in tokenize(tokenses):
        semantics.context.append(token)
        if len(semantics.context) > 10:
            semantics.context.pop(0)
        parser.feed(token)
    if semantics.errors:
        raise BQLParseError(semantics.errors)
    if semantics.failed:
        raise BQLParseError(['parse failed mysteriously'])
    assert semantics.schema is not None
    return semantics.schema