|
NAMEGraph::Easy::Parser - Parse Graph::Easy from textual descriptionSYNOPSIS# creating a graph from a textual description use Graph::Easy::Parser; my $parser = Graph::Easy::Parser->new(); my $graph = $parser->from_text( '[ Bonn ] => [ Berlin ]'. '[ Berlin ] => [ Rostock ]'. ); print $graph->as_ascii(); print $parser->from_file('mygraph.txt')->as_ascii(); # Also works automatically on graphviz code: print Graph::Easy::Parser->from_file('mygraph.dot')->as_ascii(); DESCRIPTION"Graph::Easy::Parser" lets you parse simple textual descriptions of graphs, and constructs a "Graph::Easy" object from them.The resulting object can than be used to layout and output the graph. InputThe input consists of text describing the graph, encoded in UTF-8.Example: [ Bonn ] --> [ Berlin ] [ Frankfurt ] <=> [ Dresden ] [ Bonn ] --> [ Frankfurt ] [ Bonn ] = > [ Frankfurt ] Graphviz In addition there is a bit of magic that detects graphviz code, so input of the following form will also work: digraph Graph1 { "Bonn" -> "Berlin" } Note that the magic detection only works for named graphs or graph with "digraph" at their start, so the following will not be detected as graphviz code because it looks exactly like valid Graph::Easy code at the start: graph { "Bonn" -> "Berlin" } See Graph::Easy::Parser::Graphviz for more information about parsing graphs in the DOT language. VCG In addition there is a bit of magic that detects VCG code, so input of the following form will also work: graph: { node: { title: Bonn; } node: { title: Berlin; } edge: { sourcename: Bonn; targetname: Berlin; } } See Graph::Easy::Parser::VCG for more information about parsing graphs in the VCG language. Input SyntaxThis is a very brief description of the syntax for the Graph::Easy language, for a full specification, please see Graph::Easy::Manual.
Please see Graph::Easy::Manual for a full description of the syntax rules. OutputThe output will be a Graph::Easy object (unless overridden with "use_class()"), see the documentation for Graph::Easy what you can do with it.EXAMPLESSee Graph::Easy for an extensive list of examples.METHODS"Graph::Easy::Parser" supports the following methods:new()use Graph::Easy::Parser; my $parser = Graph::Easy::Parser->new(); Creates a new parser object. The valid parameters are: debug fatal_errors The first will enable debug output to STDERR: my $parser = Graph::Easy::Parser->new( debug => 1 ); $parser->from_text('[A] -> [ B ]'); Setting "fatal_errors" to 0 will make parsing errors not die, but just set an error string, which can be retrieved with error(). my $parser = Graph::Easy::Parser->new( fatal_errors => 0 ); $parser->from_text(' foo ' ); print $parser->error(); See also catch_messages() for how to catch errors and warnings. reset()$parser->reset(); Reset the status of the parser, clear errors etc. Automatically called when you call any of the "from_XXX()" methods below. use_class()$parser->use_class('node', 'Graph::Easy::MyNode'); Override the class to be used to constructs objects while parsing. The first parameter can be one of the following: node edge graph group The second parameter should be a class that is a subclass of the appropriate base class: package Graph::Easy::MyNode; use base qw/Graph::Easy::Node/; # override here methods for your node class ###################################################### # when overriding nodes, we also need ::Anon package Graph::Easy::MyNode::Anon; use base qw/Graph::Easy::MyNode/; use base qw/Graph::Easy::Node::Anon/; ###################################################### # and :::Empty package Graph::Easy::MyNode::Empty; use base qw/Graph::Easy::MyNode/; ###################################################### package main; use Graph::Easy::Parser; use Graph::Easy; use Graph::Easy::MyNode; use Graph::Easy::MyNode::Anon; use Graph::Easy::MyNode::Empty; my $parser = Graph::Easy::Parser; $parser->use_class('node', 'Graph::Easy::MyNode'); my $graph = $parser->from_text(...); The object $graph will now contain nodes that are of your custom class instead of plain "Graph::Easy::Node". When overriding nodes, you also should provide subclasses for "Graph::Easy::Node::Anon" and "Graph::Easy::Node::Empty", and make these subclasses of your custom node class as shown above. For edges, groups and graphs, you need just one subclass. from_text()my $graph = $parser->from_text( $text ); Create a Graph::Easy object from the textual description in $text. Returns undef for error, you can find out what the error was with error(). This method will reset any previous error, and thus the $parser object can be re-used to parse different texts by just calling "from_text()" multiple times. from_file()my $graph = $parser->from_file( $filename ); my $graph = Graph::Easy::Parser->from_file( $filename ); Creates a Graph::Easy object from the textual description in the file $filename. The second calling style will create a temporary "Graph::Easy::Parser" object, parse the file and return the resulting "Graph::Easy" object. Returns undef for error, you can find out what the error was with error() when using the first calling style. error()my $error = $parser->error(); Returns the last error, or the empty string if no error occurred. If you want to catch warnings from the parser, enable catching of warnings or errors: $parser->catch_messages(1); # Or individually: # $parser->catch_warnings(1); # $parser->catch_errors(1); # something which warns or throws an error: ... if ($parser->error()) { my @errors = $parser->errors(); } if ($parser->warning()) { my @warnings = $parser->warnings(); } See Graph::Easy::Base for more details on error/warning message capture. parse_error()$parser->parse_error( $msg_nr, @params); Sets an error message from a message number and replaces embedded templates like "##param1##" with the passed parameters. _parse_attributes()my $attributes = $parser->_parse_attributes( $txt, $class ); my ($att, $multiples) = $parser->_parse_attributes( $txt, $class ); Internal usage only. Takes a text like this: attribute: value; attribute2 : value2; and returns a hash with the attributes. In list context, also returns the max count of multiple attributes, e.g. 3 when it encounters something like "red|green|blue". When EXPORTExports nothing.SEE ALSOGraph::Easy. Graph::Easy::Parser::Graphviz and Graph::Easy::Parser::VCG.AUTHORCopyright (C) 2004 - 2007 by Tels <http://bloodgate.com>See the LICENSE file for information.
Visit the GSP FreeBSD Man Page Interface. |