# Path: pydsl/check.py
# def checker_factory(grammar):
#     from pydsl.grammar.BNF import BNFGrammar
#     from pydsl.grammar.PEG import Sequence, Choice, OneOrMore, ZeroOrMore
#     from pydsl.grammar.definition import PLYGrammar, RegularExpression, String, PythonGrammar, JsonSchema
#     from pydsl.grammar.parsley import ParsleyGrammar
#     if isinstance(grammar, str) and not isinstance(grammar, String):
#         raise TypeError(grammar)
#     if isinstance(grammar, BNFGrammar):
#         return BNFChecker(grammar)
#     elif isinstance(grammar, JsonSchema):
#         return JsonSchemaChecker(grammar)
#     elif isinstance(grammar, RegularExpression):
#         return RegularExpressionChecker(grammar)
#     elif isinstance(grammar, PythonGrammar) or isinstance(grammar, dict) and "matchFun" in grammar:
#         return PythonChecker(grammar)
#     elif isinstance(grammar, PLYGrammar):
#         return PLYChecker(grammar)
#     elif isinstance(grammar, Choice):
#         return ChoiceChecker(grammar)
#     elif isinstance(grammar, ParsleyGrammar):
#         return ParsleyChecker(grammar)
#     elif isinstance(grammar, String):
#         return StringChecker(grammar)
#     elif isinstance(grammar, Sequence):
#         return SequenceChecker(grammar)
#     elif isinstance(grammar, OneOrMore):
#         return OneOrMoreChecker(grammar)
#     elif isinstance(grammar, ZeroOrMore):
#         return ZeroOrMoreChecker(grammar)
#     elif isinstance(grammar, Iterable):
#         return ChoiceChecker(grammar)
#     else:
#         raise ValueError(grammar)
# 
# 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/token.py
# class PositionToken(Token):
#     def __init__(self, content, gd, left=None, right=None):
#         super().__init__(content, gd)
#         self.left = left
#         self.right = right
# 
#     def __eq__(self, other):
#         return self.content == other.content and \
#                self.gd == other.gd and \
#                self.left == other.left and \
#                self.right == other.right
#                
# 
#     def __str__(self):
#         return "".join(str(x) for x in self.content)
# 
# class Token:
#     def __init__(self, content, gd):
#         if not gd:
#             raise ValueError
#         if isinstance(content, str):
#             content = [x for x in content]
#         elif isinstance(content[0], Token):
#             content = [str(x) for x in content]
#         self.content = content
#         self.gd = gd
# 
#     def __eq__(self, other):
#         try:
#             return self.content == other.content and \
#                    self.gd == other.gd
#         except AttributeError:
#             return False
# 
#     def __str__(self):
#         return "".join(str(x) for x in self.content)


import logging
from pydsl.check import checker_factory
from pydsl.lex import lexer_factory
from pydsl.token import PositionToken, Token
    from pydsl.grammar.PEG import Choice
def extract(grammar, inputdata, fixed_start = False, return_first=False):
    """
    Receives a sequence and a grammar, 
    returns a list of PositionTokens with all of the parts of the sequence that 
    are recognized by the grammar
    """
    if not inputdata:
        return []
    checker = checker_factory(grammar)

    totallen = len(inputdata)
    try:
        maxl = grammar.maxsize or totallen
    except NotImplementedError:
        maxl = totallen
    try:
        #minl = grammar.minsize #FIXME: It won't work with incompatible alphabets
        minl = 1
    except NotImplementedError:
        minl = 1
    if fixed_start:
        max_start = 1
    else:
        max_start = totallen
    result = []
    for i in range(max_start):
        for j in range(i+minl, min(i+maxl, totallen) + 1):
            slice = inputdata[i:j]
            check = checker.check(slice)
            if check:

# Path: pydsl/check.py
# def checker_factory(grammar):
#     from pydsl.grammar.BNF import BNFGrammar
#     from pydsl.grammar.PEG import Sequence, Choice, OneOrMore, ZeroOrMore
#     from pydsl.grammar.definition import PLYGrammar, RegularExpression, String, PythonGrammar, JsonSchema
#     from pydsl.grammar.parsley import ParsleyGrammar
#     if isinstance(grammar, str) and not isinstance(grammar, String):
#         raise TypeError(grammar)
#     if isinstance(grammar, BNFGrammar):
#         return BNFChecker(grammar)
#     elif isinstance(grammar, JsonSchema):
#         return JsonSchemaChecker(grammar)
#     elif isinstance(grammar, RegularExpression):
#         return RegularExpressionChecker(grammar)
#     elif isinstance(grammar, PythonGrammar) or isinstance(grammar, dict) and "matchFun" in grammar:
#         return PythonChecker(grammar)
#     elif isinstance(grammar, PLYGrammar):
#         return PLYChecker(grammar)
#     elif isinstance(grammar, Choice):
#         return ChoiceChecker(grammar)
#     elif isinstance(grammar, ParsleyGrammar):
#         return ParsleyChecker(grammar)
#     elif isinstance(grammar, String):
#         return StringChecker(grammar)
#     elif isinstance(grammar, Sequence):
#         return SequenceChecker(grammar)
#     elif isinstance(grammar, OneOrMore):
#         return OneOrMoreChecker(grammar)
#     elif isinstance(grammar, ZeroOrMore):
#         return ZeroOrMoreChecker(grammar)
#     elif isinstance(grammar, Iterable):
#         return ChoiceChecker(grammar)
#     else:
#         raise ValueError(grammar)
# 
# 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/token.py
# class PositionToken(Token):
#     def __init__(self, content, gd, left=None, right=None):
#         super().__init__(content, gd)
#         self.left = left
#         self.right = right
# 
#     def __eq__(self, other):
#         return self.content == other.content and \
#                self.gd == other.gd and \
#                self.left == other.left and \
#                self.right == other.right
#                
# 
#     def __str__(self):
#         return "".join(str(x) for x in self.content)
# 
# class Token:
#     def __init__(self, content, gd):
#         if not gd:
#             raise ValueError
#         if isinstance(content, str):
#             content = [x for x in content]
#         elif isinstance(content[0], Token):
#             content = [str(x) for x in content]
#         self.content = content
#         self.gd = gd
# 
#     def __eq__(self, other):
#         try:
#             return self.content == other.content and \
#                    self.gd == other.gd
#         except AttributeError:
#             return False
# 
#     def __str__(self):
#         return "".join(str(x) for x in self.content)


# Path: pydsl/extract.py
import logging
from pydsl.check import checker_factory
from pydsl.lex import lexer_factory
from pydsl.token import PositionToken, Token
    from pydsl.grammar.PEG import Choice

def extract(grammar, inputdata, fixed_start = False, return_first=False):
    """
    Receives a sequence and a grammar, 
    returns a list of PositionTokens with all of the parts of the sequence that 
    are recognized by the grammar
    """
    if not inputdata:
        return []
    checker = checker_factory(grammar)

    totallen = len(inputdata)
    try:
        maxl = grammar.maxsize or totallen
    except NotImplementedError:
        maxl = totallen
    try:
        #minl = grammar.minsize #FIXME: It won't work with incompatible alphabets
        minl = 1
    except NotImplementedError:
        minl = 1
    if fixed_start:
        max_start = 1
    else:
        max_start = totallen
    result = []
    for i in range(max_start):
        for j in range(i+minl, min(i+maxl, totallen) + 1):
            slice = inputdata[i:j]
            check = checker.check(slice)
            if check:

                this_pt = PositionToken(slice, grammar, i, j)