Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pathlib
- import tkinter.filedialog
- import unittest
- from parsy import regex,string,whitespace,forward_declaration,generate
- # grinch: graph+ EOF; -
- #
- # graph: 'graph' name constants? (node ';')*? 'end'; -
- # constants: '~[' attr+ ']'; -
- # node: ID node_attrs=attrs? (rel (node | id_group) rel_attrs? )*; -
- # rel_attrs: '{' attr+ '}'; -
- # rel: '-' | '>'; -
- # id_group: '(' node+ ')'; -
- # attrs: '[' attr+ ']'; -
- # attr: name '=' (STRING | VAR); +
- # name: ID ('.' ID)*; + T
- #
- # ID: ('a'..'z' | 'A'..'Z' | '\u0100'..'\u017E' | '\u0400'..'\u04FF' | '0'..'9')+; + T
- # STRING: '"' ('\\"' | ~["])* '"'; + T
- # VAR: '$' ID; + T
- # COMMENT: '--' ~[\n]* -> skip; +
- # COMMENT_MULTILINE: '--[[' .*? ']]' -> skip; +
- class TestParser(unittest.TestCase):
- class Comment:
- def __init__(self,comment: str):
- self.comment=comment
- def __str__(self):
- return self.comment
- class MultilineComment:
- def __init__(self,comment: str):
- self.comment=comment
- def __str__(self):
- return self.comment
- def test_t_id(self):
- self.assertEqual(t_id.parse("aboba"),"aboba")
- self.assertEqual(t_id.parse("123"),"123")
- self.assertEqual(t_id.parse("абоба"),"абоба")
- def test_name(self):
- self.assertEqual(nt_name.parse("aboba.aboba"),"aboba.aboba")
- self.assertEqual(nt_name.parse("123.f31афы"),"123.f31афы")
- self.assertEqual(nt_name.parse("абоба.aboba"),"абоба.aboba")
- def test_t_string(self):
- self.assertEqual(t_string.parse('"ab\\"o\\"ba"'),'"ab\"o\"ba"')
- self.assertEqual(t_string.parse('"абоба\\""'),'"абоба\""')
- self.assertEqual(t_string.parse('"аб\\"о\\"\\"\\"\\"ба"'),'"аб\"о\"\"\"\"ба"')
- def test_t_var(self):
- self.assertEqual(t_var.parse("$aboba"),"$aboba")
- self.assertEqual(t_var.parse("$123"),"$123")
- self.assertEqual(t_var.parse("$абоба"),"$абоба")
- def test_t_comment(self):
- self.assertEqual(t_comment.parse("--this is a comment"),"this is a comment")
- self.assertEqual(t_comment.parse("--this is a comment --this is too"),"this is a comment --this is too")
- t_comment=regex(r"--[^\n]*")
- t_comment_multiline=regex(r"--\[\[[^]]*]]")
- def ws(x): return (whitespace|t_comment|t_comment_multiline).many()>>x
- def wstring(s): return ws(string(s))
- def wregex(s): return ws(regex(s))
- t_id=wregex(r"[a-zA-Z\u0100-\u017E\u0400-\u04FF0-9]+")
- t_string_char=string('\\"').result('"')|regex(r'[^"]')
- t_string=string('"')+t_string_char.many().concat()+string('"')
- t_var=string("$")+t_id
- nt_name=ws(t_id+((string(".")+t_id).many().concat()))
- nt_attr=nt_name+wstring("=")+ws(t_string|t_var)
- nt_attr_plus=nt_attr.at_least(1)
- nt_attrs=wstring("[")>>nt_attr_plus<<wstring("]")
- nt_node=forward_declaration()
- nt_id_group=wstring("(")>>nt_node.at_least(1)<<wstring(")")
- nt_rel=wstring("-")|wstring(">")
- nt_rel_attrs=wstring("{")>>nt_attr_plus<<wstring("}")
- nt_node.become(
- t_id+nt_attrs.optional()+(nt_rel+(nt_node|nt_id_group)+nt_rel_attrs.optional()).optional()
- )
- nt_constants=wstring("~[")>>nt_attr_plus<<wstring("]")
- nt_decl=ws(nt_node)+ws(string(";"))
- nt_graph=wstring("graph")+nt_name+nt_constants.optional()+nt_decl.many()+wstring("end")
- @generate
- def get_graph():
- yield wstring("graph")
- graph_name=yield nt_name
- graph_constants=yield nt_constants.optional([])
- graph_nodes=yield nt_decl.many()
- yield wstring("end")
- return graph_name,graph_constants,graph_nodes
- # if __name__=='__main__':
- # unittest.main()
- #
- #
- # file_name=tkinter.filedialog.askopenfilename()
- # file=pathlib.Path(file_name).read_text()
- # print(file)
- # print(nt_graph.parse(file))
- # print(nt_constants.parse('~[defname="aboba" aboba="aboba"]'))
- print(get_graph.parse('graph test.g.a ~[defname="aboba" aboba="aboba"] 0>8; 0>18; end'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement