|
NAMEMakefile::DOM - Simple DOM parser for MakefilesVERSIONThis document describes Makefile::DOM 0.008 released on 18 November 2014.DESCRIPTIONThis libary can serve as an advanced lexer for (GNU) makefiles. It parses makefiles as "documents" and the parsing is lossless. The results are data structures similar to DOM trees. The DOM trees hold every single bit of the information in the original input files, including white spaces, blank lines and makefile comments. That means it's possible to reproduce the original makefiles from the DOM trees. In addition, each node of the DOM trees is modifiable and so is the whole tree, just like the PPI module used for Perl source parsing and the HTML::TreeBuilder module used for parsing HTML source.If you're looking for a true GNU make parser that generates an AST, please see Makefile::Parser::GmakeDB instead. The interface of "Makefile::DOM" mimics the API design of PPI. In fact, I've directly stolen the source code and POD documentation of PPI::Node, PPI::Element, and PPI::Dumper, with the full permission from the author of PPI, Adam Kennedy. "Makefile::DOM" tries to be independent of specific makefile's syntax. The same set of DOM node types is supposed to get shared by different makefile DOM generators. For example, MDOM::Document::Gmake parses GNU makefiles and returns an instance of MDOM::Document, i.e., the root of the DOM tree while the NMAKE makefile lexer in the future, "MDOM::Document::Nmake", also returns instances of the MDOM::Document class. Later, I'll also consider adding support for dmake and bsdmake. Structure of the DOMMakefile DOM (MDOM) is a structured set of a series of data types. They provide a flexible document model conformed to the makefile syntax. Below is a complete list of the 19 MDOM classes in the current implementation where the indentation indicates the class inheritance relationships.MDOM::Element MDOM::Node MDOM::Unknown MDOM::Assignment MDOM::Command MDOM::Directive MDOM::Document MDOM::Document::Gmake MDOM::Rule MDOM::Rule::Simple MDOM::Rule::StaticPattern MDOM::Token MDOM::Token::Bare MDOM::Token::Comment MDOM::Token::Continuation MDOM::Token::Interpolation MDOM::Token::Modifier MDOM::Token::Separator MDOM::Token::Whitespace It's not hard to see that all of the MDOM classes inherit from the MDOM::Element class. MDOM::Token and MDOM::Node are its direct children. The former represents a string token which is atomic from the perspective of the lexer while the latter represents a structured node, which usually has one or more children, and serves as the container for other DOM::Element objects. Next we'll show a few examples to demonstrate how to map DOM trees to particular makefiles.
It can be observed from the examples above that the MDOM representation for the makefile's lexical elements is rather loose. It only provides very limited structural representation instead of making a bad guess. OPERATIONS FOR MDOM TREESGenerating an MDOM tree from a GNU makefile only requires two lines of Perl code:use MDOM::Document::Gmake; my $dom = MDOM::Document::Gmake->new('Makefile'); If the makefile source code being parsed is already stored in a Perl variable, say, $var, then we can construct an MDOM via the following code: my $dom = MDOM::Document::Gmake->new(\$var); Now $dom becomes the reference to the root of the MDOM tree and its type is now MDOM::Document::Gmake, which is also an instance of the MDOM::Node class. Just as mentioned above, "MDOM::Node" is the container for other MDOM::Element instances. So we can retrieve some element node's value via its "child" method: $node = $dom->child(3); # or $node = $dom->elements(0); And we may also use the "elements" method to obtain the values of all the nodes: @elems = $dom->elements; For every MDOM node, its corresponding makefile source can be generated by invoking its "content" method. BUGS AND TODOThe current implementation of the MDOM::Document::Gmake lexer is based on a hand-written state machie. Although the efficiency of the engine is not bad, the code is rather complicated and messy, which hurts both extensibility and maintanabilty. So it's expected to rewrite the parser using some grammatical tools like the Perl 6 regex engine Pugs::Compiler::Rule or a yacc-style one like Parse::Yapp.SOURCE REPOSITORYYou can always get the latest source code of this module from its GitHub repository:<http://github.com/agentzh/makefile-dom-pm> If you want a commit bit, please let me know. AUTHORYichun "agentzh" Zhang (章亦春) <agentzh@gmail.com>COPYRIGHTCopyright 2006-2014 by Yichun "agentzh" Zhang (章亦春).This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. SEE ALSOMDOM::Document, MDOM::Document::Gmake, PPI, Makefile::Parser::GmakeDB, makesimple.
Visit the GSP FreeBSD Man Page Interface. |