# Path: pydsl/file/BNF.py
# def load_bnf_file(filepath, repository = None):
#     """Converts a bnf file into a BNFGrammar instance"""
#     linelist = []
#     with open(filepath,'r') as mlfile:
#         for line in mlfile:
#             linelist.append(line)
#     return strlist_to_production_set(linelist, repository)
# 
# Path: pydsl/file/python.py
# def load_python_file(moduleobject):
#     """ Try to create an indexable instance from a module"""
#     if isinstance(moduleobject, str):
#         moduleobject = load_module(moduleobject)
#     if not hasattr(moduleobject, "iclass"):
#         raise KeyError("Element" + str(moduleobject))
#     iclass = getattr(moduleobject, "iclass")
#     mylist = getattr(moduleobject, "__all__", None) or list(filter(lambda x:x[:1] != "_", (dir(moduleobject))))
#     mylist.remove('iclass')
#     resultdic = {}
#     for x in mylist:
#         resultdic[x] = getattr(moduleobject, x)
#     if iclass == "SymbolGrammar":
#         from pydsl.grammar.BNF import BNFGrammar
#         return BNFGrammar(**resultdic)
#     elif iclass == "PLY":
#         from pydsl.grammar.definition import PLYGrammar
#         return PLYGrammar(moduleobject)
#     elif iclass in ["PythonGrammar"]:
#         from pydsl.grammar.definition import PythonGrammar
#         return PythonGrammar(resultdic)
#     elif iclass == "PythonTranslator":
#         return resultdic
#     elif iclass == "parsley":
#         from pydsl.grammar.parsley import ParsleyGrammar
#         return ParsleyGrammar(**resultdic)
#     elif iclass == "pyparsing":
#         return resultdic['root_symbol']
#     else:
#         raise ValueError(str(moduleobject))
# 
# Path: pydsl/grammar/definition.py
# class RegularExpression(Grammar):
#     def __init__(self, regexp, flags = 0):
#         Grammar.__init__(self, None)
#         import re
#         retype = type(re.compile('hello, world'))
#         if isinstance(regexp, retype):
#             self.regexp = regexp
#             self.regexpstr = regexp.pattern
#             self.flags = regexp.flags
#         elif isinstance(regexp, str):
#             self.regexpstr = regexp
#             self.flags = flags
#             self.regexp = re.compile(regexp, flags)
#         else:
#             raise TypeError
# 
#     def __hash__(self):
#         return hash(self.regexpstr)
# 
#     def __eq__(self, other):
#         try:
#             return self.regexpstr == other.regexpstr and self.flags == other.flags
#         except AttributeError:
#             return False
# 
#     def __str__(self):
#         return self.regexpstr
# 
#     @property
#     def first(self):
#         i = 0
#         while True:
#             if self.regexpstr[i] == "^":
#                 i+=1
#                 continue
#             if self.regexpstr[i] == "[":
#                 from .PEG import Choice
#                 return Choice([String(x) for x in self.regexpstr[i+1:self.regexpstr.find("]")]])
#             return String(self.regexpstr[i])
# 
#     def __getattr__(self, attr):
#         return getattr(self.regexp, attr)
# 
#     @property
#     def alphabet(self):
#         from pydsl.encoding import ascii_encoding
#         return ascii_encoding


import unittest
from pydsl.file.BNF import load_bnf_file
from pydsl.file.python import load_python_file
from pydsl.grammar.definition import RegularExpression
#!/usr/bin/python
# -*- coding: utf-8 -*-

#Copyright (C) 2008-2013 Nestor Arocha

"""Test BNF file loading"""


class TestFileLoader(unittest.TestCase):
    """Loading a bnf instance from a .bnf file"""
    def testFileLoader(self):
        repository = {'integer':RegularExpression("^[0123456789]*$"), 

# Path: pydsl/file/BNF.py
# def load_bnf_file(filepath, repository = None):
#     """Converts a bnf file into a BNFGrammar instance"""
#     linelist = []
#     with open(filepath,'r') as mlfile:
#         for line in mlfile:
#             linelist.append(line)
#     return strlist_to_production_set(linelist, repository)
# 
# Path: pydsl/file/python.py
# def load_python_file(moduleobject):
#     """ Try to create an indexable instance from a module"""
#     if isinstance(moduleobject, str):
#         moduleobject = load_module(moduleobject)
#     if not hasattr(moduleobject, "iclass"):
#         raise KeyError("Element" + str(moduleobject))
#     iclass = getattr(moduleobject, "iclass")
#     mylist = getattr(moduleobject, "__all__", None) or list(filter(lambda x:x[:1] != "_", (dir(moduleobject))))
#     mylist.remove('iclass')
#     resultdic = {}
#     for x in mylist:
#         resultdic[x] = getattr(moduleobject, x)
#     if iclass == "SymbolGrammar":
#         from pydsl.grammar.BNF import BNFGrammar
#         return BNFGrammar(**resultdic)
#     elif iclass == "PLY":
#         from pydsl.grammar.definition import PLYGrammar
#         return PLYGrammar(moduleobject)
#     elif iclass in ["PythonGrammar"]:
#         from pydsl.grammar.definition import PythonGrammar
#         return PythonGrammar(resultdic)
#     elif iclass == "PythonTranslator":
#         return resultdic
#     elif iclass == "parsley":
#         from pydsl.grammar.parsley import ParsleyGrammar
#         return ParsleyGrammar(**resultdic)
#     elif iclass == "pyparsing":
#         return resultdic['root_symbol']
#     else:
#         raise ValueError(str(moduleobject))
# 
# Path: pydsl/grammar/definition.py
# class RegularExpression(Grammar):
#     def __init__(self, regexp, flags = 0):
#         Grammar.__init__(self, None)
#         import re
#         retype = type(re.compile('hello, world'))
#         if isinstance(regexp, retype):
#             self.regexp = regexp
#             self.regexpstr = regexp.pattern
#             self.flags = regexp.flags
#         elif isinstance(regexp, str):
#             self.regexpstr = regexp
#             self.flags = flags
#             self.regexp = re.compile(regexp, flags)
#         else:
#             raise TypeError
# 
#     def __hash__(self):
#         return hash(self.regexpstr)
# 
#     def __eq__(self, other):
#         try:
#             return self.regexpstr == other.regexpstr and self.flags == other.flags
#         except AttributeError:
#             return False
# 
#     def __str__(self):
#         return self.regexpstr
# 
#     @property
#     def first(self):
#         i = 0
#         while True:
#             if self.regexpstr[i] == "^":
#                 i+=1
#                 continue
#             if self.regexpstr[i] == "[":
#                 from .PEG import Choice
#                 return Choice([String(x) for x in self.regexpstr[i+1:self.regexpstr.find("]")]])
#             return String(self.regexpstr[i])
# 
#     def __getattr__(self, attr):
#         return getattr(self.regexp, attr)
# 
#     @property
#     def alphabet(self):
#         from pydsl.encoding import ascii_encoding
#         return ascii_encoding


# Path: tests/unit/test_BNFLoad.py
import unittest
from pydsl.file.BNF import load_bnf_file
from pydsl.file.python import load_python_file
from pydsl.grammar.definition import RegularExpression

#!/usr/bin/python
# -*- coding: utf-8 -*-

#Copyright (C) 2008-2013 Nestor Arocha

"""Test BNF file loading"""


class TestFileLoader(unittest.TestCase):
    """Loading a bnf instance from a .bnf file"""
    def testFileLoader(self):
        repository = {'integer':RegularExpression("^[0123456789]*$"), 

                'DayOfMonth':load_python_file('pydsl/contrib/grammar/DayOfMonth.py')}