Ejemplo n.º 1
0
 def parse(self, expr: str) -> Value:
     input_stream = InputStream(expr)
     lexer = wizardLexer(input_stream)
     stream = CommonTokenStream(lexer)
     parser = wizardParser(stream)
     parser._errHandler = BailErrorStrategy()
     return self.visitExpr(parser.parseWizard().body().expr(0), self.state)
Ejemplo n.º 2
0
def verify_query(what):
    lexer = queryLexer(what)
    stream = CommonTokenStream(lexer)
    parser = queryParser(stream)
    parser.removeErrorListeners()
    parser._errHandler = BailErrorStrategy()
    parser.buildParseTrees = False
    try:
        parser.script()
        return True
    except ParseCancellationException:
        return False
Ejemplo n.º 3
0
class Parser:

    _error_strategies: ClassVar[Dict[str, ErrorStrategy]] = {
        'default': DefaultErrorStrategy(),
        'bail': BailErrorStrategy(),
    }

    def __init__(self, file: str):
        self._parser = self._build_parser(file)
        self._error_listener = self._setup_listener()

    def _setup_listener(self) -> WebIDLErrorListener:
        self._parser.removeErrorListeners()
        self._parser.addErrorListener(error_listener := WebIDLErrorListener())

        return error_listener

    @staticmethod
    def _build_parser(file: str) -> WebIDLParser:
        stream = FileStream(fileName=file, encoding='utf-8')

        functions = (
            WebIDLLexer,
            CommonTokenStream,
            WebIDLParser,
        )

        return cast(
            WebIDLParser,
            reduce(lambda acc, func: func(acc), functions, stream),
        )

    def setup_parser_strategy(self, strategy: str) -> None:
        self._parser._errHandler = self._error_strategies[strategy]

    @_setup_strategy('bail')
    def parse(self) -> ParserRuleContext:
        try:
            return self._parser.webIDL()
        except ParseCancellationException:
            raise IDLParseError

    @_setup_strategy('default')
    def validate(self) -> List[SyntaxErrorInfo]:
        self._parser.webIDL()
        return self._error_listener.errors
Ejemplo n.º 4
0
 def __init__(self, input_stream: TokenStream, output: TextIO = sys.stdout, build_parse_trees=True):
     super().__init__(input_stream, output)
     self.removeErrorListener(ConsoleErrorListener.INSTANCE)
     self._errHandler = BailErrorStrategy()
     self.buildParseTrees = build_parse_trees
Ejemplo n.º 5
0
 def __init__(self, input: abstract.TokenStream) -> None:
     super().__init__(cast(TokenStream, input))
     self.addErrorListener(DiagnosticErrorListener())
     self._errHandler = BailErrorStrategy()
Ejemplo n.º 6
0
def Parser(tokenStream):
    parser = MiniDecafExprParser(tokenStream)
    parser._errHandler = BailErrorStrategy()
    tree = parser.program()  # 取得一棵以 program 为根的 AST
    return tree