def test(self): raw_code = (" strong param[0] != 'null'", " weak param[1] != 'null'", " strong result != 'null'", " strong 'The bucket is reset' == true", " strong 'The bucket must not be shared' == true", " strong 'parsing is not supported' == false", " strong 'the text to parse is invalid' == false") raw_tree = "root(" \ "strong(!=(param[0],'begin null end'))," \ "weak(!=(param[1],'begin null end'))," \ "strong(!=(result,'begin null end'))," \ "strong(==('begin The bucket is reset end',true))," \ "strong(==('begin The bucket must not be shared end',true))," \ "strong(==('begin parsing is not supported end',false))," \ "strong(==('begin the text to parse is invalid end',false)))" tree = Parser.parse("\n".join(raw_code)) compiler = DfsCompiler() tokens = compiler.accept(tree) tree = Decompiler.dfs(tokens) Validator().accept(tree) filtrator = StringFiltrator() filtrator.accept(tree) Validator().accept(tree) assert str(tree) == raw_tree
def test(self): raw_code = ("result == 'false'", "this is not 'supported'") tree = Parser.parse("\n".join(raw_code)) compiler = DfsCompiler() tokens = compiler.accept(tree) Decompiler.dfs(tokens) Validator().accept(tree)
def test(self): raw_code = (" strong param[0] != 'null'", " weak param[1] != 'null'", " strong result != 'null'", " strong 'The bucket is reset' == true", " strong 'The bucket must not be shared' == true", " strong 'parsing is not supported' == false", " strong 'the text to parse is invalid' == false") raw_tokens = ("root", "strong", "!=", "param[0]", "'null'", "weak", "!=", "param[1]", "'null'", "strong", "!=", "result", "'null'", "strong", "==", "'The bucket is reset'", "true", "strong", "==", "'The bucket must not be shared'", "true", "strong", "==", "'parsing is not supported'", "false", "strong", "==", "'the text to parse is invalid'", "false") raw_tree = "root(" \ "strong(!=(param[0],'null'))," \ "weak(!=(param[1],'null'))," \ "strong(!=(result,'null'))," \ "strong(==('The bucket is reset',true))," \ "strong(==('The bucket must not be shared',true))," \ "strong(==('parsing is not supported',false))," \ "strong(==('the text to parse is invalid',false)))" tree = Parser.parse("\n".join(raw_code)) compiler = DfsCompiler() tokens = compiler.accept(tree) assert len(tokens) == len(raw_tokens) assert all(raw_token == token.name for token, raw_token in zip(tokens, raw_tokens)) assert str(tree) == raw_tree yet_another_tree = Decompiler.dfs(Decompiler.typing(raw_tokens)) Validator().accept(yet_another_tree) assert str(yet_another_tree) == str(tree) assert yet_another_tree == tree
def test(self): raw_code = "strong param[0] == \"Some text\" => param[0] != 'null'" tree = Parser.parse(raw_code) compiler = BfsCompiler() tokens = compiler.accept(tree) reverted_tree = Decompiler.bfs(tokens) Validator().accept(tree) assert reverted_tree == tree
def test(self): raw_code = "strong 'field' is 'supported' strong 'field' is not 'supported'" raw_tree = "root(strong(is('field','supported')),strong(is not('field','supported')))" tree = Parser.parse(raw_code) compiler = DfsCompiler() tokens = compiler.accept(tree) tree = Decompiler.dfs(tokens) Validator().accept(tree) assert str(tree) == raw_tree
def test(self): raw_code = " strong param[0] == 'null' => \"in default zone\"" raw_tree = "root(strong(=>(==(param[0],'null'),\"in default zone\")))" tree = Parser.parse(raw_code) compiler = DfsCompiler() tokens = compiler.accept(tree) tree = Decompiler.dfs(tokens) Validator().accept(tree) assert str(tree) == raw_tree
def test(self): raw_code = "param[0] == false => param[0] != 'null'" raw_tokens = ("root", "strong", "=>", "==", "!=", "param[0]", "false", "param[0]", "'null'") tree = Parser.parse(raw_code) compiler = DfsCompiler() tokens = compiler.accept(tree) tree = Decompiler.dfs(tokens) Validator().accept(tree) compiler = BfsCompiler() tokens = compiler.accept(tree) assert len(tokens) == len(raw_tokens) assert all(token.name == raw_token for token, raw_token in zip(tokens, raw_tokens))
def test(self): raw_code = (" strong param[0] != 'null'", " weak param[1] != 'null'", " strong result != 'null'", " strong 'The bucket is reset' == true", " strong 'The bucket must not be shared' == true", " strong 'parsing is not supported' == false", " strong 'the text to parse is invalid' == false") tree = Parser.parse("\n".join(raw_code)) compiler = DfsCompiler() tokens = compiler.accept(tree) tree = Decompiler.dfs(tokens) Validator().accept(tree) code = Shower().accept(tree) assert "\n".join(code) == "\n".join(string.strip() for string in raw_code)
def test(self): raw_code = ( "param[0] != 'null'", "`param[1] == 'null'", "param[1] == 'null' => 'default zone'", "param[0] != 'null'", "`result != 'null'", "`param[0] == 'negative'", "param[0] != 'null'", "param[1] != 'null'", "param[2] != 'null'", "param[3] == 'null' => param[3] == 'ISOChronology in default zone'", "`param[3] == 'null'", "param[0] is 'valid values defined by the chronology'", "param[1] is 'valid values defined by the chronology'", "param[2] is 'valid values defined by the chronology'", "param[0] is 'the year'", "param[1] is 'the month of the year'", "param[2] is 'the day of the month'", "param[3] is 'the chronology'") tree = Parser.parse("\n".join(raw_code)) compiler = DfsCompiler() tokens = compiler.accept(tree) Decompiler.dfs(tokens) Validator().accept(tree)
def parse(code: str) -> Tree: def log(strings: list): # print("\t{:<15} -> {:s}".format(" ".join(strings), ":".join(str(node) for node in stack))) pass def parse_label(strings: list): log(strings) assert len(strings) == 1 assert len(stack) == 1 name = strings[0] if name == SHORT_WEAK: name = WEAK token = Token(name, LABEL) node = Node(token, stack.pop()) tree.root.children.append(node) def parse_function(strings: list): log(strings) assert len(strings) == 1 assert len(stack) > 0 assert stack[-1].token.name == INVOCATION assert stack[-1].token.type == INVOCATION assert len(stack[-1].children) == 2 assert strings[0] == GET token = Token(GETATTR, OPERATOR) node = Node(token, *stack.pop().children) stack.append(node) def parse_operator(strings: list): log(strings) assert len(strings) >= 1 assert len(stack) > 1 name = " ".join(strings) token = Token(name, OPERATOR) right = stack.pop() left = stack.pop() node = Node(token, left, right) stack.append(node) def parse_marker(strings: list): log(strings) assert len(strings) == 1 name = strings[0] token = Token(name, MARKER) node = Node(token) stack.append(node) def parse_string(strings: list): log(strings) assert len(strings) == 1 name = strings[0] if name[0] not in ('"', "'"): name = "'" + name + "'" token = Token(name, STRING) node = Node(token) stack.append(node) def parse_invocation(strings: list): log(strings) assert len(strings) > 1 num_arguments = min(len(strings) - 2, list(strings).count(",") + 1) assert len(stack) >= num_arguments name = strings[0] + strings[-1] token = Token(name, INVOCATION) node = Node(token, *stack[-num_arguments:]) del stack[-num_arguments:] stack.append(node) try: stack: List[Node] = [] root_token = Token(ROOT, ROOT) root_node = Node(root_token) tree = Tree(root_node) parsers = { LABEL: parse_label, FUNCTION: parse_function, OPERATOR: parse_operator, MARKER: parse_marker, STRING: parse_string, INVOCATION: parse_invocation } parser = build(parsers) parser.parseString(code) assert len(stack) == 0 Validator().accept(tree) return tree except AssertionError: raise ParseException(code, 0, 0, 0) except pyparsing.ParseException as ex: raise ParseException.value_of(code, ex)