# Path: pydsl/encoding.py

# 
# Path: pydsl/lex.py
# def lexer_factory(alphabet, base, force_lexer = None):
#     if force_lexer is None:
#         if alphabet == ascii_encoding:
#             force_lexer = "lexer"
#         elif isinstance(alphabet, Choice) and alphabet.alphabet == base:
#             force_lexer = "brute_force"
#         else:
#             force_lexer = "general"
#         
#     if force_lexer == "lexer":
#         return ChoiceLexer(alphabet)
#     elif force_lexer == "brute_force":
#         return ChoiceBruteForceLexer(alphabet)
#     elif force_lexer == "general":
#         return GeneralLexer(alphabet, base)
#     else:
#         raise ValueError
# 
# Path: pydsl/parser/LL.py
# class LL1RecursiveDescentParser(TopDownParser):
#     def get_trees(self, data, showerrors = False): # -> list:
#         """ returns a list of trees with valid guesses """
#         if showerrors:
#             raise NotImplementedError("This parser doesn't implement errors")
#         self.data = data
#         self.index = 0
#         try:
#             return [self.__aux_parser(self._productionset.initialsymbol)]
#         except (IndexError, ParseError):
#             return []
# 
#     def __aux_parser(self, symbol):
#         from pydsl.grammar.symbol import TerminalSymbol
#         if isinstance(symbol, TerminalSymbol):
#             LOG.debug("matching symbol %s, data:%s, index:%s" % (symbol,self.data,self.index ))
#             result= self.match(symbol)
#             LOG.debug("symbol matched %s" % result)
#             return result
#         productions = self._productionset.getProductionsBySide(symbol)
#         valid_firsts = []
#         for production in productions:
#             first_of_production = self._productionset.first_lookup(production.rightside[0])
#             if check(first_of_production, [self.current]):
#                 valid_firsts.append(production)
#         if len(valid_firsts) != 1:
#             raise ParseError("Expected only one valid production, found %s" % len(valid_firsts), 0)
#         childlist = [self.__aux_parser(x) for x in valid_firsts[0].rightside]
#         left = childlist[0].left
#         right = childlist[-1].right
#         content = [x.content for x in childlist]
#         return ParseTree(left, right, symbol, content, childlist=childlist)
# 
# 
#     def consume(self):
#         self.index +=1
#         if self.index > len(self.data):
#             raise IndexError("Attempted to consume index %s of data %s" % (self.index, self.data))
# 
#     @property
#     def current(self):
#         return self.data[self.index]
# 
#     def match(self, symbol):
#         if symbol.check([self.current]):
#             current = self.current
#             self.consume()
#             return ParseTree(self.index-1, self.index, symbol, current)
#         raise Exception("Not matched")


import unittest
from pydsl.encoding import ascii_encoding
from pydsl.lex import lexer_factory
from pydsl.parser.LL import LL1RecursiveDescentParser
        from pydsl.file.BNF import strlist_to_production_set
        from pydsl.parser.backtracing import BacktracingErrorRecursiveDescentParser
        from pydsl.grammar.symbol import NonTerminalSymbol
        from pydsl.file.BNF import strlist_to_production_set
        from pydsl.grammar import RegularExpression
            from pydsl.grammar.symbol import NonTerminalSymbol
        from pydsl.grammar.PEG import Choice
        from pydsl.grammar.definition import String, RegularExpression
        from pydsl.encoding import ascii_encoding
        from pydsl.lex import lex
                return to_number(tree.childlist[0].symbol.gd) + to_number(tree.childlist[2].symbol.gd)
            raise Exception
            
        result = parse_tree_walker(parse_tree[0])
        self.assertEqual(result, 3)


    def test_calculator_simple(self):
        grammar_def = [
                "S ::= E",
                "E ::= number operator number",
                "number := Word,integer,max",
                "operator := String,+",
                ]
        repository = {'integer':RegularExpression("^[0123456789]*$")}
        production_set = strlist_to_production_set(grammar_def, repository)
        rdp = LL1RecursiveDescentParser(production_set)
        parse_tree = rdp("1+2")

        def parse_tree_walker(tree):
            if tree.symbol == NonTerminalSymbol("S"):
                return parse_tree_walker(tree.childlist[0])
            if tree.symbol == NonTerminalSymbol("E"):
                return int(str(tree.childlist[0].content)) + int(str(tree.childlist[2].content))
            else:
                raise Exception
            
        result = parse_tree_walker(parse_tree[0])
        self.assertEqual(result, 3)
        math_alphabet = Choice([RegularExpression("^[0123456789]*$"),Choice([String('+')])])

# Path: pydsl/encoding.py

# 
# Path: pydsl/lex.py
# def lexer_factory(alphabet, base, force_lexer = None):
#     if force_lexer is None:
#         if alphabet == ascii_encoding:
#             force_lexer = "lexer"
#         elif isinstance(alphabet, Choice) and alphabet.alphabet == base:
#             force_lexer = "brute_force"
#         else:
#             force_lexer = "general"
#         
#     if force_lexer == "lexer":
#         return ChoiceLexer(alphabet)
#     elif force_lexer == "brute_force":
#         return ChoiceBruteForceLexer(alphabet)
#     elif force_lexer == "general":
#         return GeneralLexer(alphabet, base)
#     else:
#         raise ValueError
# 
# Path: pydsl/parser/LL.py
# class LL1RecursiveDescentParser(TopDownParser):
#     def get_trees(self, data, showerrors = False): # -> list:
#         """ returns a list of trees with valid guesses """
#         if showerrors:
#             raise NotImplementedError("This parser doesn't implement errors")
#         self.data = data
#         self.index = 0
#         try:
#             return [self.__aux_parser(self._productionset.initialsymbol)]
#         except (IndexError, ParseError):
#             return []
# 
#     def __aux_parser(self, symbol):
#         from pydsl.grammar.symbol import TerminalSymbol
#         if isinstance(symbol, TerminalSymbol):
#             LOG.debug("matching symbol %s, data:%s, index:%s" % (symbol,self.data,self.index ))
#             result= self.match(symbol)
#             LOG.debug("symbol matched %s" % result)
#             return result
#         productions = self._productionset.getProductionsBySide(symbol)
#         valid_firsts = []
#         for production in productions:
#             first_of_production = self._productionset.first_lookup(production.rightside[0])
#             if check(first_of_production, [self.current]):
#                 valid_firsts.append(production)
#         if len(valid_firsts) != 1:
#             raise ParseError("Expected only one valid production, found %s" % len(valid_firsts), 0)
#         childlist = [self.__aux_parser(x) for x in valid_firsts[0].rightside]
#         left = childlist[0].left
#         right = childlist[-1].right
#         content = [x.content for x in childlist]
#         return ParseTree(left, right, symbol, content, childlist=childlist)
# 
# 
#     def consume(self):
#         self.index +=1
#         if self.index > len(self.data):
#             raise IndexError("Attempted to consume index %s of data %s" % (self.index, self.data))
# 
#     @property
#     def current(self):
#         return self.data[self.index]
# 
#     def match(self, symbol):
#         if symbol.check([self.current]):
#             current = self.current
#             self.consume()
#             return ParseTree(self.index-1, self.index, symbol, current)
#         raise Exception("Not matched")


# Path: tests/functional/test_Case.py
import unittest
from pydsl.encoding import ascii_encoding
from pydsl.lex import lexer_factory
from pydsl.parser.LL import LL1RecursiveDescentParser
        from pydsl.file.BNF import strlist_to_production_set
        from pydsl.parser.backtracing import BacktracingErrorRecursiveDescentParser
        from pydsl.grammar.symbol import NonTerminalSymbol
        from pydsl.file.BNF import strlist_to_production_set
        from pydsl.grammar import RegularExpression
            from pydsl.grammar.symbol import NonTerminalSymbol
        from pydsl.grammar.PEG import Choice
        from pydsl.grammar.definition import String, RegularExpression
        from pydsl.encoding import ascii_encoding
        from pydsl.lex import lex

                return to_number(tree.childlist[0].symbol.gd) + to_number(tree.childlist[2].symbol.gd)
            raise Exception
            
        result = parse_tree_walker(parse_tree[0])
        self.assertEqual(result, 3)


    def test_calculator_simple(self):
        grammar_def = [
                "S ::= E",
                "E ::= number operator number",
                "number := Word,integer,max",
                "operator := String,+",
                ]
        repository = {'integer':RegularExpression("^[0123456789]*$")}
        production_set = strlist_to_production_set(grammar_def, repository)
        rdp = LL1RecursiveDescentParser(production_set)
        parse_tree = rdp("1+2")

        def parse_tree_walker(tree):
            if tree.symbol == NonTerminalSymbol("S"):
                return parse_tree_walker(tree.childlist[0])
            if tree.symbol == NonTerminalSymbol("E"):
                return int(str(tree.childlist[0].content)) + int(str(tree.childlist[2].content))
            else:
                raise Exception
            
        result = parse_tree_walker(parse_tree[0])
        self.assertEqual(result, 3)
        math_alphabet = Choice([RegularExpression("^[0123456789]*$"),Choice([String('+')])])

        tokens = [x for x in lex(math_alphabet, ascii_encoding, "11+2")]