our
$VERSION
=
'0.1'
;
sub
parse {
my
$self
=
shift
;
my
$string_or_ref
=
shift
;
my
$gram
=
shift
||
$self
->{
default
};
my
(
$no_error_bit
,
$no_modify
) =
@_
;
unless
(
ref
(
$string_or_ref
) eq
'ARRAY'
) {
$string_or_ref
= [
$string_or_ref
]; }
$self
->flush;
unless
(
$gram
eq
$self
->{
last
}) {
$self
->{
last
} =
$gram
;
$self
->prepare_gram;
}
foreach
my
$string
(@{
$string_or_ref
}) {
$self
->parse_string(
$string
); }
if
(
$self
->{collection}{
$self
->{
last
}}{pre_rules_code}) {
eval
(
$self
->{collection}{
$self
->{
last
}}{pre_rules_code}); };
$self
->parse_rules(
$self
->{collection}{
$self
->{
last
}}{default_color},
$no_error_bit
,
$no_modify
);
if
(
$self
->{collection}{
$self
->{
last
}}{post_rules_code}) {
eval
(
$self
->{collection}{
$self
->{
last
}}{post_rules_code}); };
return
$self
->stringify;
}
sub
stringify {
my
$self
=
shift
;
my
$string
=
''
;
foreach
my
$block
(@{
$self
->{tree}}) {
if
( (
$block
->[2]) && (
grep
{
$block
->[2] =~ /^
$_
$/i} @{
$self
->{ansi_colors}}) ) {
$string
.= color(
lc
(
$block
->[2])).
$block
->[0].color(
'reset'
);
unless
((
$block
->[1] eq
'END'
) || (
$block
->[1] eq
'BROKEN'
)) {
$string
.=
$block
->[1];
}
}
else
{
$string
.=
$block
->[0];
unless
((
$block
->[1] eq
'END'
) || (
$block
->[1] eq
'BROKEN'
)) {
$string
.=
$block
->[1];
}
}
}
return
$string
;
}
sub
gen_rules {
my
$self
=
shift
;
if
(
$self
->{collection}{
$self
->{
last
}}{colors}) {
foreach
my
$color
(
keys
%{
$self
->{collection}{
$self
->{
last
}}{colors}}) {
my
$exp
=
'('
.
join
(
'|'
, @{
$self
->{collection}{
$self
->{
last
}}{colors}{
$color
}}).
')'
;
unshift
@{
$self
->{collection}{
$self
->{
last
}}{rules}}, [
'^'
.
$exp
.
'$'
,
$color
];
}
}
}
sub
prepare_gram {
my
$self
=
shift
;
if
(
@_
) {
$self
->{ansi_colors} =
shift
; }
unless
(
$self
->{collection}{
$self
->{
last
}}{expr}) {
$self
->gen_expr; }
unless
(
$self
->{collection}{
$self
->{
last
}}{made_rules}) {
$self
->gen_rules;
$self
->{collection}{
$self
->{
last
}}{made_rules} = 1;
}
}
1;