Package: parse-2d
parse_2dTools for parsing two-dimensional programming languages.ExampleSuppose we want to parse a diagram representing a path, with>,v,<, and^each being single steps.>v  >>
 v  ^
 >>>^One way of tokenizing this is to interpret each of these steps as a token, with a value representing its direction.fromparse_2dimportDiagram,TinyTokenizer,tokenizediagram=Diagram.from_string(">v  >>\nv  ^\n>>>^")tokenizers=[TinyTokenizer(">",0),TinyTokenizer("v",1),TinyTokenizer("<",2),TinyTokenizer("^",3),]fortokenintokenize(diagram,tokenizers):print(token)EachTokenhas a region and a value. The region is what area it covers in the original diagram, while the value can be any Python object representing what you've tokenized.Alternatively, you can extract the path as a single token, using theWireTokenizer, or as a directed path, by subclassingWireTokenizer.A more completesampleis also provided, to demonstrate the use of these tools, by parsing theCircuit Diagramlanguage.ReferenceDiagramADiagramis an infinite two-dimensional grid of "symbols", with a distinguished "whitespace" symbol.Diagrams may be instantiated with a list of lists and the whitespace symbol, or by thefrom_stringmethod.Manual instantiation>>>diagram=Diagram([[1,2],[3]],0)>>>diagram[(0,1)]3>>>diagram[(1,1)]0>>>diagram[(-30,17)]0from_string>>>diagram=Diagram.from_string("ab\nc")>>>diagram[(0,1)]'c'>>>diagram[(1,1)]' 'RegionARegionis an area on a diagram. CustomRegions may be made by inheriting fromRegion. The followingRegions are provided by default:TinyRegion(location)ARegionconsisting of a single point. Has thelocationproperty to provide that point.RectRegion(top_left, bottom_right)A rectangularRegion, aligned with the axes, consisting of the points bounded bytop_leftandbottom_right, including the top and left edges, and excluding the bottom and right edges (analogously torange).SparseRegion(contents)ARegionconsisting of a collection of disparate points. Has thecontentsproperty to provide thatfrozensetof points.TokenATokenconsists of aregioncovered, and avaluethat the token represents.TokenizerATokenizeris an object for extracting tokens from diagrams. CustomTokenizerclasses may be made by inheriting fromTokenizer, and overriding thestarts_onandextract_tokenmethods. See theTokenizerdocstring for more details.TinyTokenizer(symbol, value)Tokenizer for tokens represented by a single symbol.Extracts a token of valuetoken_valuefor everysymbolin the diagram.TemplateTokenizer(template, token_value)Tokenizer for tokens represented by a fixed template of symbols.Thetemplateis either a mapping of relative locations to symbols, or aDiagram.Extracts a token of valuetoken_valuefor every non-overlapping translation of the template found in the parent Diagram.WireTokenizer(segment_connections)Tokenizer for wire tokens, represented by a path through a diagram.A wire consists of multiple symbol "segments", each of which has a fixed collection of directions it can connect to.Thesegment_connectionsis a mapping from segment symbols to a collection of that segment's available connections.Extracts a wire token representing the available connections to that wire.This class assumes that segments connect all possible incoming directions to all possible outgoing directions. Child classes may override this behavior by overriding theconnectionsmethod. See theWireTokenizerdocstring for more details.BoxTokenizer(edge_symbols, contents_tokenizer)Tokenizer for tokens represented by a box of edge symbols.edge_tokensis a mapping from a side of the box, to the collection of symbols that may be used for that edge.contents_tokenizeris a function to determine the value of the extracted token, and is passed the entire box (including the edge) as its only parameter.tokenize(diagram, tokenizers)Yields the non-overlapping tokens found in thediagramby the list oftokenizers.