Beispiel #1
0
def validate(sgrammar, expression = None, inputfile = None, outputformat = None, **kwargs):
    """Read input file contents, creates grammar and transform objects, create connections, 
    and afterwards reads required input/launch main loop"""
    from pydsl.Memory.Loader import load_parser
    parser = load_parser(sgrammar, "descent")
    if expression:
        resulttrees = parser.get_trees(expression, True)
    elif inputfile:
        with open(inputfile, "rb") as f:
            resulttrees = parser.get_trees(f.read(), True)
    else:
        raise Exception("No input method")
    jsonlist = []
    for index, posttree in enumerate(resulttrees):
        if outputformat == "str":
            print("Tree: " + str(index) + "\n")
        if outputformat == "str":
            if posttree.valid:
                print("Result OK")
            else:
                print("Errors:")
                print(print_errors(posttree))
        elif outputformat == "json":
            jsonlist.append(errors_to_list(posttree))
    if outputformat == "json":
        import json
        print(json.dumps(jsonlist))
    return True
Beispiel #2
0
    def __init__(self, bnf, parser="auto"):
        LOG.debug("SymbolGrammarTools.__init__: Begin")
        GrammarTools.__init__(self, bnf)
        parser = bnf.options.get("parser", parser)
        from pydsl.Memory.Loader import load_parser

        self.__parser = load_parser(bnf, parser)
Beispiel #3
0
def function(inputdic, inputgt, outputgt):
    grammarname =  inputdic['grammar']
    from pydsl.Memory.Loader import load_parser
    grammar = load_parser(grammarname)
    result = grammar.get_trees(inputdic['string'])
    #print(result)
    #print(result[0])
    result = result[0]
    return {"output":tree_to_dot(result)}
Beispiel #4
0
 def testBasic(self):
     from pydsl.Memory.Loader import load_parser
     parser = load_parser('Date', 'descent')
     self.assertFalse(parser.get_trees("11/11/ab", True)[0].valid)
     self.assertTrue(parser.get_trees("11/11/2011", True)[0].valid)