# Path: pydsl/translator.py
# def translator_factory(function):
#     from pydsl.grammar.definition import PLYGrammar
#     from pydsl.grammar.parsley import ParsleyGrammar
#     if isinstance(function, PLYGrammar):
#         return PLYTranslator(function)
#     if isinstance(function, ParsleyGrammar):
#         return ParsleyTranslator(function)
#     if isinstance(function, dict):
#         return PythonTranslator(**function)
#     from pyparsing import OneOrMore
#     if isinstance(function, OneOrMore):
#         return PyParsingTranslator(function)
#     if isinstance(function, PythonTranslator):
#         return function
#     raise ValueError(function)
# 
# 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/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))


import unittest
import parsley
import sys
from pydsl.translator import translator_factory
from pydsl.check import checker_factory
from pydsl.file.python import load_python_file
        from pydsl.file.parsley import load_parsley_grammar_file
# pydsl is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
# pydsl is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with pydsl.  If not, see <http://www.gnu.org/licenses/>.


__author__ = "Ptolom"
__copyright__ = "Copyright 2014, Ptolom"
__email__ = "ptolom@hexifact.co.uk"

class TestParsley(unittest.TestCase):
    def testDate(self):
        repository = {'DayOfMonth':load_python_file('pydsl/contrib/grammar/DayOfMonth.py')} #DayOfMonth loaded as checker
        G=load_parsley_grammar_file("pydsl/contrib/grammar/Date.parsley", "expr", repository)
        C=checker_factory(G)
        T=translator_factory(G)
        self.assertTrue(C("2/4/12"))
        self.assertEqual(T("2/4/12"),(2,4,12))
        self.assertRaises(parsley.ParseError,T, "40/4/12")
        
    def testCalculator(self):
        G=load_python_file("pydsl/contrib/translator/calculator.py")

# Path: pydsl/translator.py
# def translator_factory(function):
#     from pydsl.grammar.definition import PLYGrammar
#     from pydsl.grammar.parsley import ParsleyGrammar
#     if isinstance(function, PLYGrammar):
#         return PLYTranslator(function)
#     if isinstance(function, ParsleyGrammar):
#         return ParsleyTranslator(function)
#     if isinstance(function, dict):
#         return PythonTranslator(**function)
#     from pyparsing import OneOrMore
#     if isinstance(function, OneOrMore):
#         return PyParsingTranslator(function)
#     if isinstance(function, PythonTranslator):
#         return function
#     raise ValueError(function)
# 
# 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/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: tests/unit/test_Parsley.py
import unittest
import parsley
import sys
from pydsl.translator import translator_factory
from pydsl.check import checker_factory
from pydsl.file.python import load_python_file
        from pydsl.file.parsley import load_parsley_grammar_file

# pydsl is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
# pydsl is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with pydsl.  If not, see <http://www.gnu.org/licenses/>.


__author__ = "Ptolom"
__copyright__ = "Copyright 2014, Ptolom"
__email__ = "ptolom@hexifact.co.uk"

class TestParsley(unittest.TestCase):
    def testDate(self):
        repository = {'DayOfMonth':load_python_file('pydsl/contrib/grammar/DayOfMonth.py')} #DayOfMonth loaded as checker
        G=load_parsley_grammar_file("pydsl/contrib/grammar/Date.parsley", "expr", repository)
        C=checker_factory(G)
        T=translator_factory(G)
        self.assertTrue(C("2/4/12"))
        self.assertEqual(T("2/4/12"),(2,4,12))
        self.assertRaises(parsley.ParseError,T, "40/4/12")
        
    def testCalculator(self):
        G=load_python_file("pydsl/contrib/translator/calculator.py")

        T=translator_factory(G)