1
# -*- tab-width: 4 -*- ###############################################
3
# $Id: SimpleParse.pm,v 1.14 2001/11/14 15:03:29 mbox Exp $
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
package LXR::SimpleParse;
21
$CVSID = '$Id: SimpleParse.pm,v 1.14 2001/11/14 15:03:29 mbox Exp $ ';
28
use vars qw(@ISA @EXPORT);
31
@EXPORT = qw(&doparse &untabify &init &nextfrag);
33
my $fileh; # File handle
34
my @frags; # Fragments in queue
35
my @bodyid; # Array of body type ids
36
my @open; # Fragment opening delimiters
37
my @term; # Fragment closing delimiters
38
my $split; # Fragmentation regexp
39
my $open; # Fragment opening regexp
40
my $tabwidth; # Tab width
55
($fileh, $tabhint, @blksep) = @_;
56
$tabwidth = $tabhint || $tabwidth;
58
while (@_ = splice(@blksep,0,3)) {
81
$_[0] =~ s/^(\t+)/(' ' x ($t * length($1)))/ge; # Optimize for common case.
82
$_[0] =~ s/([^\t]*)\t/$1.(' ' x ($t - (length($1) % $t)))/ge;
92
# print "nextfrag called\n";
96
# read one more line if we have processed
97
# all of the previously read line
99
$line = $fileh->getline;
102
$line =~ /^.*-[*]-.*?[ \t;]tab-width:[ \t]*([0-9]+).*-[*]-/) {
106
# &untabify($line, $tabwidth); # We inline this for performance.
108
# Optimize for common case.
110
$line =~ s/^(\t+)/' ' x ($tabwidth * length($1))/ge;
111
$line =~ s/([^\t]*)\t/$1.(' ' x ($tabwidth - (length($1) % $tabwidth)))/ge;
113
# split the line into fragments
114
@frags = split(/($split)/, $line);
120
# skip empty fragments
121
if ($frags[0] eq '') {
125
# check if we are inside a fragment
126
if (defined($frag)) {
127
if (defined($btype)) {
128
my $next = shift(@frags);
130
# Add to the fragment
132
# We are done if this was the terminator
133
last if $next =~ /^$term[$btype]$/;
137
if ($frags[0] =~ /^$open$/) {
138
# print "encountered open token while btype was $btype\n";
141
$frag .= shift(@frags);
145
# print "start of new fragment\n";
146
# Find the blocktype of the current block
147
$frag = shift(@frags);
148
if (defined($frag) && (@_ = $frag =~ /^$open$/)) {
150
# grep in a scalar context returns the number of times
151
# EXPR evaluates to true, which is this case will be
152
# the index of the first defined element in @_.
155
$btype = grep { $i &&= !defined($_) } @_;
156
if(!defined($term[$btype])) {
157
print "fragment without terminator\n";
163
$btype = $bodyid[$btype] if defined($btype);
165
return($btype, $frag);