Advertisement
Fel1x

Untitled

Nov 8th, 2023
625
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.43 KB | None | 0 0
  1. import pathlib
  2. import tkinter.filedialog
  3. import unittest
  4.  
  5. from parsy import regex,string,whitespace,forward_declaration,generate
  6.  
  7.  
  8. # grinch: graph+ EOF;                                                               -
  9. #
  10. # graph: 'graph' name constants? (node ';')*? 'end';                                -
  11. # constants: '~[' attr+ ']';                                                        -
  12. # node: ID node_attrs=attrs? (rel (node | id_group) rel_attrs? )*;                  -
  13. # rel_attrs: '{' attr+ '}';                                                         -
  14. # rel: '-' | '>';                                                                   -
  15. # id_group: '(' node+ ')';                                                          -
  16. # attrs: '[' attr+ ']';                                                             -
  17. # attr: name '=' (STRING | VAR);                                                    +
  18. # name: ID ('.' ID)*;                                                               + T
  19. #
  20. # ID: ('a'..'z' | 'A'..'Z' | '\u0100'..'\u017E' | '\u0400'..'\u04FF' | '0'..'9')+;  + T
  21. # STRING: '"' ('\\"' | ~["])* '"';                                                  + T
  22. # VAR: '$' ID;                                                                      + T
  23. # COMMENT: '--' ~[\n]* -> skip;                                                     +
  24. # COMMENT_MULTILINE: '--[[' .*? ']]' -> skip;                                       +
  25.  
  26. class TestParser(unittest.TestCase):
  27.     class Comment:
  28.         def __init__(self,comment: str):
  29.             self.comment=comment
  30.  
  31.         def __str__(self):
  32.             return self.comment
  33.  
  34.     class MultilineComment:
  35.         def __init__(self,comment: str):
  36.             self.comment=comment
  37.  
  38.         def __str__(self):
  39.             return self.comment
  40.  
  41.     def test_t_id(self):
  42.         self.assertEqual(t_id.parse("aboba"),"aboba")
  43.         self.assertEqual(t_id.parse("123"),"123")
  44.         self.assertEqual(t_id.parse("абоба"),"абоба")
  45.  
  46.     def test_name(self):
  47.         self.assertEqual(nt_name.parse("aboba.aboba"),"aboba.aboba")
  48.         self.assertEqual(nt_name.parse("123.f31афы"),"123.f31афы")
  49.         self.assertEqual(nt_name.parse("абоба.aboba"),"абоба.aboba")
  50.  
  51.     def test_t_string(self):
  52.         self.assertEqual(t_string.parse('"ab\\"o\\"ba"'),'"ab\"o\"ba"')
  53.         self.assertEqual(t_string.parse('"абоба\\""'),'"абоба\""')
  54.         self.assertEqual(t_string.parse('"аб\\\\"\\"\\"\\"ба"'),'"аб\"о\"\"\"\"ба"')
  55.  
  56.     def test_t_var(self):
  57.         self.assertEqual(t_var.parse("$aboba"),"$aboba")
  58.         self.assertEqual(t_var.parse("$123"),"$123")
  59.         self.assertEqual(t_var.parse("$абоба"),"$абоба")
  60.  
  61.     def test_t_comment(self):
  62.         self.assertEqual(t_comment.parse("--this is a comment"),"this is a comment")
  63.         self.assertEqual(t_comment.parse("--this is a comment --this is too"),"this is a comment --this is too")
  64.  
  65.  
  66. t_comment=regex(r"--[^\n]*")
  67. t_comment_multiline=regex(r"--\[\[[^]]*]]")
  68.  
  69.  
  70. def ws(x): return (whitespace|t_comment|t_comment_multiline).many()>>x
  71.  
  72.  
  73. def wstring(s): return ws(string(s))
  74.  
  75.  
  76. def wregex(s): return ws(regex(s))
  77.  
  78.  
  79. t_id=wregex(r"[a-zA-Z\u0100-\u017E\u0400-\u04FF0-9]+")
  80. t_string_char=string('\\"').result('"')|regex(r'[^"]')
  81. t_string=string('"')+t_string_char.many().concat()+string('"')
  82. t_var=string("$")+t_id
  83.  
  84. nt_name=ws(t_id+((string(".")+t_id).many().concat()))
  85. nt_attr=nt_name+wstring("=")+ws(t_string|t_var)
  86. nt_attr_plus=nt_attr.at_least(1)
  87. nt_attrs=wstring("[")>>nt_attr_plus<<wstring("]")
  88. nt_node=forward_declaration()
  89. nt_id_group=wstring("(")>>nt_node.at_least(1)<<wstring(")")
  90. nt_rel=wstring("-")|wstring(">")
  91. nt_rel_attrs=wstring("{")>>nt_attr_plus<<wstring("}")
  92. nt_node.become(
  93.     t_id+nt_attrs.optional()+(nt_rel+(nt_node|nt_id_group)+nt_rel_attrs.optional()).optional()
  94. )
  95. nt_constants=wstring("~[")>>nt_attr_plus<<wstring("]")
  96. nt_decl=ws(nt_node)+ws(string(";"))
  97. nt_graph=wstring("graph")+nt_name+nt_constants.optional()+nt_decl.many()+wstring("end")
  98.  
  99.  
  100. @generate
  101. def get_graph():
  102.     yield wstring("graph")
  103.     graph_name=yield nt_name
  104.     graph_constants=yield nt_constants.optional([])
  105.     graph_nodes=yield nt_decl.many()
  106.     yield wstring("end")
  107.     return graph_name,graph_constants,graph_nodes
  108.  
  109.  
  110. # if __name__=='__main__':
  111. #   unittest.main()
  112. #
  113. #
  114.  
  115. # file_name=tkinter.filedialog.askopenfilename()
  116. # file=pathlib.Path(file_name).read_text()
  117. # print(file)
  118. # print(nt_graph.parse(file))
  119.  
  120. # print(nt_constants.parse('~[defname="aboba" aboba="aboba"]'))
  121.  
  122. 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