Example #1
0
def parse_program(input: str,
                  forbid_rebuild: bool = False,
                  filename: Optional[str] = None) -> Program:
    l = parser.get_lexer()
    p = parser.get_parser(forbid_rebuild=forbid_rebuild)
    prog: Program = p.parse(input=input, lexer=l, filename=filename)
    prog.input = input
    return prog
Example #2
0
def debug_tokens(filename: str) -> None:
    l = parser.get_lexer()

    with open(filename) as f:
        l.input(f.read())

    while True:
        tok = l.token()
        if not tok:
            break  # No more input
        utils.logger.always_print(str(tok))
Example #3
0
    def __init__(self, predicate, debug=0):
        """
        Initializes the Predicate object with the string predicate.

        Arguments:
            predicate : String predicate
            debug : optional, defaults to 0. Controls the debug behavior
             of the underlying parser and lexer.

        Returns:
            Predicate object
        """
        # Validate the predicate
        if not isinstance(predicate, str):
            raise TypeError("Predicate must be a string!")

        # Initialize the literal resolver
        LiteralResolver.__init__(self)

        # Store the predicate
        self.predicate = predicate

        # Setup the lexer
        lexer = get_lexer()
        self.lexer_errors = lexer.errors

        # Setup the parser
        p = get_parser(lexer=lexer, debug=debug)
        self.parser_errors = p.errors

        # Try to get the AST tree
        try:
            self.ast = p.parse(self.predicate, lexer=lexer)
            self.ast_validated = False
            self.ast_valid = False
            self.ast_errors = None
        except Exception, e:
            self.ast = None
            self.ast_validated = True
            self.ast_valid = False
            self.ast_errors = {"errors": [str(e)], "regex": {}}
Example #4
0
    def __init__(self, predicate, debug=0):
        """
        Initializes the Predicate object with the string predicate.

        Arguments:
            predicate : String predicate
            debug : optional, defaults to 0. Controls the debug behavior
             of the underlying parser and lexer.

        Returns:
            Predicate object
        """
        # Validate the predicate
        if not isinstance(predicate, str):
            raise TypeError("Predicate must be a string!")

        # Initialize the literal resolver
        LiteralResolver.__init__(self)

        # Store the predicate
        self.predicate = predicate

        # Setup the lexer
        lexer = get_lexer()
        self.lexer_errors = lexer.errors

        # Setup the parser
        p = get_parser(lexer=lexer, debug=debug)
        self.parser_errors = p.errors

        # Try to get the AST tree
        try:
            self.ast = p.parse(self.predicate, lexer=lexer)
            self.ast_validated = False
            self.ast_valid = False
            self.ast_errors = None
        except Exception, e:
            self.ast = None
            self.ast_validated = True
            self.ast_valid = False
            self.ast_errors = {"errors": [str(e)], "regex": {}}