MultiLanguage/BaseDialect.pm untested

filter: Statementstmt 0.0% 0 / 55 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 19 totaltotal 0.0% 0 / 111 CC 6 SCAR 12.5
CC
6
Cov
0%
left_assoc
6.0
left_assoc_i
6.0
not_expr
6.0
CRAP 3.5
1 package Regexp::MultiLanguage::BaseDialect;
2
3 use Carp;
4 use strict;
5 use warnings;
6
7 =head1 NAME
8
9 Regexp::MultiLanguage::BaseDialect - Takes care of most
10 of the work of writing a dialect for Regexp::MultiLanguage
11
12 =head1 VERSION
13
14 Version 0.03
15
16 =cut
17
18 our $VERSION = 0.03;
19
20 =head1 SYNOPSIS
21
22 Handles interfacing with the Parse::RecDescent grammar to simplify
23 the code that must be written for a dialect of Regexp::MultiLanguage.
24
25 Dialect writers only need write the following functions:
26
27 =over
28
29 =item C<wrap>
30
31 =item C<match_regex>
32
33 =item C<comment_start>
34
35 =item C<make_function>
36
37 =item C<function_call>
38
39 =back
40
41 =head1 TODO
42
43 Better describe the process of building a new dialect.
44
45 =cut
46
47 sub new {
48 my $class = shift;
49 bless { @_ }, $class;
50 }
51
52 # subclasses need to override
53 # wrap, match_regex, comment_start, make_function, function_call
54
55 sub wrap { croak "wrap is abstract" }
56 sub match_regex { croak "regex is abstract" }
57 sub comment_start { croak "comment is abstract" }
58 sub make_function { croak "make_function is abstract" }
59 sub function_call { croak "function_call is abstract" }
60
61 # and now, the meat
62
63 sub regex_file {
64 my ($this,$item) = @_;
65
66 return $this->wrap( $item->{'sequence'} );
67 }
68
69 sub sequence {
70 return join "\n", @{ $_[1]->{'component(s)' } };
71 }
72
73 sub statement {
74 my ($this,$item) = @_;
75
76 $this->make_function( $item->{'identifier'}, $item->{'expr'} );
77 }
78
79 sub comment {
80 my ($this,$item) = @_;
81
82 my $comment = $item->{'__PATTERN1__'};
83 $comment =~ s/^(\/\/|#)//;
84
85 return $this->comment_start . $comment;
86 }
87
88 # expression handling
89
90 sub left_assoc {
91 my $item = shift;
92 my $cur = shift;
93 my $next = shift;
94
95 my $x = $item->{$cur.'_expr_i'} == 0 ? 1 : 0;
96 return ( '(' x ($item->{$cur.'_expr_i'}->[1]+$x)) . $item->{$next.'_expr'} . (' ' x $x) . $item->{$cur.'_expr_i'}->[0] . (')' x $x);
97 }
98
99 sub left_assoc_i {
100 my $item = shift;
101 my $cur = shift;
102 my $next = shift;
103 my $child = shift || $next . '_expr';
104
105 if ( exists $item->{$cur.'_op'} ) {
106 return [' ' . $item->{$cur.'_op'} . ' ' . $item->{$child} . ')' . $item->{$cur.'_expr_i'}->[0], $item->{$cur.'_expr_i'}->[1] + 1 ];
107 } else {
108 return ['',0];
109 }
110 }
111
112 sub or_expr {
113 return left_assoc( $_[1], 'or', 'and' );
114 }
115
116 sub or_expr_i {
117 return left_assoc_i( $_[1], 'or', 'and' );
118 }
119
120 sub and_expr {
121 return left_assoc( $_[1], 'and', 'not' );
122 }
123
124 sub and_expr_i {
125 return left_assoc_i( $_[1], 'and', 'not' );
126 }
127
128 sub not_expr {
129 my ( $this, $item ) = @_;
130
131 return (exists $item->{'__STRING1__'} ? '!' : '') . $item->{'brack_expr'};
132 }
133
134 sub brack_expr {
135 my ( $this, $item ) = @_;
136
137 return '(' . $item->{'expr'} . ')' if exists $item->{'expr'};
138 return $item->{'operand'}
139 }
140
141 sub operand {
142 my ( $this, $item ) = @_;
143
144 if ( exists $item->{'identifier'} ) {
145 return $this->function_call( $item->{'identifier'} );
146
147 } else {
148 return $this->match_regex( $item->{'regex'} );
149 }
150 }
151
152 =head1 SEE ALSO
153
154 L<Regexp::MultiLanguage>
155
156 =head1 AUTHOR
157
158 Robby Walker, robwalker@cpan.org
159
160 =head1 COPYRIGHT & LICENSE
161
162 Copyright 2006 Robby Walker, all rights reserved.
163
164 This program is free software; you can redistribute it and/or modify it
165 under the same terms as Perl itself.
166
167 =cut
168
169 1;