Sample Specification Parser

The input specification parser is particular to an application domain. In this section, we look at the parser that is required for our toy object model specification, primarily to review how the AST library is used; the parsing code itself is quite trivial. For more involved parsing tasks, you can use a version of Berkeley yacc, which has been hacked up to output Perl instead of C (available from http://ftp.sterling.com:/local/perl-byacc.tar.Z). I have successfully used this combination to produce IDL parsers for the CORBA specification.

The parser in Example 17.4 allows attributes to have additional annotations like this:

class Foo {
    int  id,  access=readonly, db_col_name=id, index=yes;
};

In the template, these attribute properties can be used just like “standard” properties such as attr_name and attr_type.

Example 17-4. OO_Schema.pm: The Specification Parser

package SchemaParser; use Ast; use Carp; sub parse{ my ($package, $filename) = @_; open (P, $filename) || die "Could not open $filename : $@"; my $root = Ast->new("Root"); eval { while (1) { get_line(); next unless ($line =~ /^\s*class +(\w+)/); $c = Ast->new($1); $c->add_prop("class_name" => $1); $root->add_prop_list("class_list", $c); while (1) { get_line(); last if $line =~ /^\s*}/; if ($line =~ s/^\s*(\w+)\s*(\w+)//) { $a = Ast->new($2); #attribute name $a->add_prop ("attr_name", $2); #attribute type $a->add_prop ("attr_type", $1); #attribute type $c->add_prop_list("attr_list", $a); } $curr_line ...

Get Advanced Perl Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.