~ubuntu-branches/ubuntu/vivid/horae/vivid

« back to all changes in this revision

Viewing changes to 0CPAN/Parse-RecDescent-1.94/demo/demo_cpp.pl

  • Committer: Bazaar Package Importer
  • Author(s): Carlo Segre
  • Date: 2006-12-26 11:54:29 UTC
  • Revision ID: james.westby@ubuntu.com-20061226115429-kjuhf6h9w6bohlwj
Tags: upstream-063
ImportĀ upstreamĀ versionĀ 063

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/local/bin/perl -sw
 
2
 
 
3
# RECURSIVE #includes DURING A RECURSIVE DESCENT
 
4
 
 
5
use Parse::RecDescent;
 
6
 
 
7
sub loadfile($)
 
8
{
 
9
        open FILE, $_[0]  or die "Couldn't find included file: $_[0]\n";
 
10
        my $contents = <FILE>;
 
11
        close FILE;
 
12
        return $contents;
 
13
}
 
14
 
 
15
%macro = ();
 
16
 
 
17
sub demacro($)
 
18
{
 
19
        my $text = shift;
 
20
        while (($macro,$defn) = each %macro )
 
21
        {
 
22
                $text =~ s/$macro/$defn/;
 
23
        }
 
24
        return $text;
 
25
}
 
26
 
 
27
$grammar =
 
28
q{
 
29
        file     : line(s)
 
30
 
 
31
        line     : include
 
32
                 | macrodef
 
33
                 | linedir
 
34
                        { $thisline = $item[1]; }
 
35
                 | codeline
 
36
                        { print "found: [$item[1]] at $thisline\n" }
 
37
 
 
38
        include  : '#include' filename
 
39
                        { 
 
40
                          print "pre: [$text] at $thisline\n";
 
41
                          $text = ::loadfile($item[-1]) . $text;
 
42
                          Parse::RecDescent::LineCounter::resync $thisline;
 
43
                          print "post: [$text] at $thisline\n";
 
44
                        }
 
45
 
 
46
        filename : '"' m#[a-z0-9_./-]+#i '"'
 
47
                        { $return = $item[-2] }
 
48
                 | '<' m#[a-z0-9_./-]+#i '>'
 
49
                        { $return = $item[-2] }
 
50
 
 
51
        macrodef : '#define' /[a-z]\w*/i /.*/
 
52
                        { $::macro{$item[-2]} = $item[-1] }
 
53
 
 
54
        linedir:   '#line' /\d+/
 
55
 
 
56
        codeline : /.*\n/
 
57
                        { $return = ::demacro($item[-1]); }     
 
58
 
 
59
};
 
60
 
 
61
$parse = new Parse::RecDescent ($grammar);
 
62
 
 
63
undef $/;
 
64
 
 
65
$reinput = $input = <>;
 
66
 
 
67
$parse->file($input) or die "Bad file! No biscuit!\n";
 
68
 
 
69
$parse->file($reinput) or die "Bad file! No biscuit!\n";