summaryrefslogtreecommitdiffstats
path: root/pidl/lib/Parse/Pidl/Base.pm
blob: 056b7b37d48aa8e45ba47aab95779edace7a8e26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# Superclass for IDL structure generators
# GPL3

package Parse::Pidl::Base;

use strict;
use warnings;

use Parse::Pidl qw(fatal warning error);

use vars qw($VERSION);
$VERSION = '0.01';

sub indent {
	my $self = shift;
	$self->{tabs} .= "\t";
}

sub deindent {
	my $self = shift;
	$self->{tabs} = substr($self->{tabs}, 1);
}

sub pidl {
	my ($self, $txt) = @_;
	if ($txt) {
		if ($txt !~ /^#/) {
			$self->{res} .= $self->{tabs};
		}
		$self->{res} .= $txt;
	}
	$self->{res} .= "\n";
}


sub pidl_hdr {
	my ($self, $txt) = @_;
	$self->{res_hdr} .= "$txt\n";
}


sub pidl_both {
	my ($self, $txt) = @_;
	$self->{res} .= "$txt\n";
	$self->{res_hdr} .= "$txt\n";
}


# When the PIDL_DEVELOPER env flag is set, we overwrite $self->pidl()
# and $self->pidl_hdr() to annotate the output with location
# information.

sub pidl_dev_msg {
	my $self = shift;
	my ($pkg, $file, $line, $sub) = caller(2);
	# minimise the path
	if ($file =~ m{/pidl/(lib/.+|pidl)$}) {
		$file = $1;
	}
	my $state = $self->{dev_state} // ['uninitialised', 0, ''];
	my ($ploc, $pline, $ptabs) = @$state;
	my $loc = "$sub	 $file";

	if ($loc ne $ploc or
	    abs($line - $pline) > 20 or
	    $self->{tabs} ne $ptabs) {
		$self->{dev_state} = [$loc, $line, $self->{tabs}];
		return "  //<PIDL> $loc:$line";
	}
	return '';
}


if ($ENV{PIDL_DEVELOPER}) {
	undef &pidl;
	undef &pidl_hdr;

	*Parse::Pidl::Base::pidl = sub {
		my ($self, $txt) = @_;

		if ($txt) {
			if ($txt !~ /^#/) {
				$self->{res} .= $self->{tabs};
			}
			$self->{res} .= $txt;
		}
		$self->{res} .= $self->pidl_dev_msg;
		$self->{res} .= "\n";
	};

	*Parse::Pidl::Base::pidl_hdr = sub {
		my ($self, $txt) = @_;
		$txt .= $self->pidl_dev_msg;
		$self->{res_hdr} .= "$txt\n";
	}
}


1;