Skip to content

Instantly share code, notes, and snippets.

@arodland
Created August 24, 2009 03:15
Show Gist options
  • Save arodland/173629 to your computer and use it in GitHub Desktop.
Save arodland/173629 to your computer and use it in GitHub Desktop.
use Regexp::Grammars;
my $grammar = qr#
<[definition]> ** <separator> <.separator>?
<rule: separator> ; | \n
<rule: definition> <key> (?: : <value>)?
<rule: key> <MATCH=string>
<rule: value> <MATCH=string>
<rule: string> <MATCH=bareword> | <MATCH=quoted>
<rule: bareword> \w+
<rule: quoted> " <[quotedpart]>+ " (?{ $MATCH = join "", @{ $MATCH{quotedpart} } })
<rule: quotedpart> \\ <MATCH=(.)> | [^"\\]+
#xms;
my $data = 'depth:50; offset:0; content:"perl\;6"; nocase;';
if ($data =~ $grammar) {
for my $def (@{ $/{definition} }) {
if (exists $def->{value}) {
print qq("$def->{key}" = "$def->{value}"\n);
} else {
print qq("$def->{key}"\n);
}
}
} else {
print "Failed!\n";
}
/(?msx-i:(?{; *Regexp::Grammars::LOGFILE
= Regexp::Grammars::_open_log('>>','-'); })((?{; @! = () if !pos;
local @Regexp::Grammars::RESULT_STACK
= (@Regexp::Grammars::RESULT_STACK, {});
local $Regexp::Grammars::DEBUG = 'off' })
(?:(?{;
local @Regexp::Grammars::RESULT_STACK = (@Regexp::Grammars::RESULT_STACK, {});
})((?&definition))(?{;
local @Regexp::Grammars::RESULT_STACK = (
@Regexp::Grammars::RESULT_STACK[0..@Regexp::Grammars::RESULT_STACK-3],
Regexp::Grammars::_pop_current_result_frame_with_list(
\@Regexp::Grammars::RESULT_STACK, 'definition', $^N
),
);
}))(?:(?&ws)(?{;@Regexp::Grammars::RESULT_STACK = @Regexp::Grammars::RESULT_STACK;})(?:(?{;
local @Regexp::Grammars::RESULT_STACK = (@Regexp::Grammars::RESULT_STACK, {});
})((?&separator))(?{;
local @Regexp::Grammars::RESULT_STACK = (
@Regexp::Grammars::RESULT_STACK[0..@Regexp::Grammars::RESULT_STACK-3],
Regexp::Grammars::_pop_current_result_frame(
\@Regexp::Grammars::RESULT_STACK, 'separator', $^N
),
);
}))(?&ws)(?:(?{;
local @Regexp::Grammars::RESULT_STACK = (@Regexp::Grammars::RESULT_STACK, {});
})((?&definition))(?{;
local @Regexp::Grammars::RESULT_STACK = (
@Regexp::Grammars::RESULT_STACK[0..@Regexp::Grammars::RESULT_STACK-3],
Regexp::Grammars::_pop_current_result_frame_with_list(
\@Regexp::Grammars::RESULT_STACK, 'definition', $^N
),
);
})))* (?:(?{;
local @Regexp::Grammars::RESULT_STACK = (@Regexp::Grammars::RESULT_STACK, {});
})((?&separator))(?{;
local @Regexp::Grammars::RESULT_STACK = (
@Regexp::Grammars::RESULT_STACK[0..@Regexp::Grammars::RESULT_STACK-2]
);
}))?
(?(DEFINE)
(?<separator> (?{
local @Regexp::Grammars::RESULT_STACK = @Regexp::Grammars::RESULT_STACK;
$Regexp::Grammars::RESULT_STACK[-1]{'!'}=$#{!};})
(?: ; |(?&ws)\n
)
(?{;$#{!}=delete $Regexp::Grammars::RESULT_STACK[-1]{'!'};})
)
)
(?(DEFINE)
(?<definition> (?{
local @Regexp::Grammars::RESULT_STACK = @Regexp::Grammars::RESULT_STACK;
$Regexp::Grammars::RESULT_STACK[-1]{'!'}=$#{!};})
(?: (?:(?{;
local @Regexp::Grammars::RESULT_STACK = (@Regexp::Grammars::RESULT_STACK, {});
})((?&key))(?{;
local @Regexp::Grammars::RESULT_STACK = (
@Regexp::Grammars::RESULT_STACK[0..@Regexp::Grammars::RESULT_STACK-3],
Regexp::Grammars::_pop_current_result_frame(
\@Regexp::Grammars::RESULT_STACK, 'key', $^N
),
);
}))(?&ws)(?:(?&ws):(?&ws)(?:(?{;
local @Regexp::Grammars::RESULT_STACK = (@Regexp::Grammars::RESULT_STACK, {});
})((?&value))(?{;
local @Regexp::Grammars::RESULT_STACK = (
@Regexp::Grammars::RESULT_STACK[0..@Regexp::Grammars::RESULT_STACK-3],
Regexp::Grammars::_pop_current_result_frame(
\@Regexp::Grammars::RESULT_STACK, 'value', $^N
),
);
})))?
)
(?{;$#{!}=delete $Regexp::Grammars::RESULT_STACK[-1]{'!'};})
)
)
(?(DEFINE)
(?<key> (?{
local @Regexp::Grammars::RESULT_STACK = @Regexp::Grammars::RESULT_STACK;
$Regexp::Grammars::RESULT_STACK[-1]{'!'}=$#{!};})
(?: (?:(?{;
local @Regexp::Grammars::RESULT_STACK = (@Regexp::Grammars::RESULT_STACK, {});
})((?&string))(?{;
local @Regexp::Grammars::RESULT_STACK = (
@Regexp::Grammars::RESULT_STACK[0..@Regexp::Grammars::RESULT_STACK-3],
Regexp::Grammars::_pop_current_result_frame(
\@Regexp::Grammars::RESULT_STACK, '=', $^N
),
);
}))
)
(?{;$#{!}=delete $Regexp::Grammars::RESULT_STACK[-1]{'!'};})
)
)
(?(DEFINE)
(?<value> (?{
local @Regexp::Grammars::RESULT_STACK = @Regexp::Grammars::RESULT_STACK;
$Regexp::Grammars::RESULT_STACK[-1]{'!'}=$#{!};})
(?: (?:(?{;
local @Regexp::Grammars::RESULT_STACK = (@Regexp::Grammars::RESULT_STACK, {});
})((?&string))(?{;
local @Regexp::Grammars::RESULT_STACK = (
@Regexp::Grammars::RESULT_STACK[0..@Regexp::Grammars::RESULT_STACK-3],
Regexp::Grammars::_pop_current_result_frame(
\@Regexp::Grammars::RESULT_STACK, '=', $^N
),
);
}))
)
(?{;$#{!}=delete $Regexp::Grammars::RESULT_STACK[-1]{'!'};})
)
)
(?(DEFINE)
(?<string> (?{
local @Regexp::Grammars::RESULT_STACK = @Regexp::Grammars::RESULT_STACK;
$Regexp::Grammars::RESULT_STACK[-1]{'!'}=$#{!};})
(?: (?:(?{;
local @Regexp::Grammars::RESULT_STACK = (@Regexp::Grammars::RESULT_STACK, {});
})((?&bareword))(?{;
local @Regexp::Grammars::RESULT_STACK = (
@Regexp::Grammars::RESULT_STACK[0..@Regexp::Grammars::RESULT_STACK-3],
Regexp::Grammars::_pop_current_result_frame(
\@Regexp::Grammars::RESULT_STACK, '=', $^N
),
);
})) |(?&ws)(?:(?{;
local @Regexp::Grammars::RESULT_STACK = (@Regexp::Grammars::RESULT_STACK, {});
})((?&quoted))(?{;
local @Regexp::Grammars::RESULT_STACK = (
@Regexp::Grammars::RESULT_STACK[0..@Regexp::Grammars::RESULT_STACK-3],
Regexp::Grammars::_pop_current_result_frame(
\@Regexp::Grammars::RESULT_STACK, '=', $^N
),
);
}))
)
(?{;$#{!}=delete $Regexp::Grammars::RESULT_STACK[-1]{'!'};})
)
)
(?(DEFINE)
(?<bareword> (?{
local @Regexp::Grammars::RESULT_STACK = @Regexp::Grammars::RESULT_STACK;
$Regexp::Grammars::RESULT_STACK[-1]{'!'}=$#{!};})
(?: \w+
)
(?{;$#{!}=delete $Regexp::Grammars::RESULT_STACK[-1]{'!'};})
)
)
(?(DEFINE)
(?<quoted> (?{
local @Regexp::Grammars::RESULT_STACK = @Regexp::Grammars::RESULT_STACK;
$Regexp::Grammars::RESULT_STACK[-1]{'!'}=$#{!};})
(?: "(?&ws)(?:(?{;
local @Regexp::Grammars::RESULT_STACK = (@Regexp::Grammars::RESULT_STACK, {});
})((?&quotedpart))(?{;
local @Regexp::Grammars::RESULT_STACK = (
@Regexp::Grammars::RESULT_STACK[0..@Regexp::Grammars::RESULT_STACK-3],
Regexp::Grammars::_pop_current_result_frame_with_list(
\@Regexp::Grammars::RESULT_STACK, 'quotedpart', $^N
),
);
}))+(?&ws)" (?{
local @Regexp::Grammars::RESULT_STACK = @Regexp::Grammars::RESULT_STACK;
$Regexp::Grammars::RESULT_STACK[-1]{q{=}} = join "", @{ $Regexp::Grammars::RESULT_STACK[-1]{quotedpart} } })
)
(?{;$#{!}=delete $Regexp::Grammars::RESULT_STACK[-1]{'!'};})
)
)
(?(DEFINE)
(?<quotedpart> (?{
local @Regexp::Grammars::RESULT_STACK = @Regexp::Grammars::RESULT_STACK;
$Regexp::Grammars::RESULT_STACK[-1]{'!'}=$#{!};})
(?: \\ (?{;local @Regexp::Grammars::RESULT_STACK
= @Regexp::Grammars::RESULT_STACK;})(?&ws)(?:((.))(?{;local @Regexp::Grammars::RESULT_STACK = (
@Regexp::Grammars::RESULT_STACK[0..@Regexp::Grammars::RESULT_STACK-2],
Regexp::Grammars::_extend_current_result_frame_with_scalar(
\@Regexp::Grammars::RESULT_STACK, '=', $^N
),
);})) |(?&ws)[^"\\]+
)
(?{;$#{!}=delete $Regexp::Grammars::RESULT_STACK[-1]{'!'};})
)
)
)(?{; $Regexp::Grammars::RESULT_STACK[-1]{""} //= $^N;
local $Regexp::Grammars::match_frame = pop @Regexp::Grammars::RESULT_STACK;
delete @{$Regexp::Grammars::match_frame}{
grep {substr($_,0,1) eq '_'} keys %{$Regexp::Grammars::match_frame}
};
if (@Regexp::Grammars::RESULT_STACK) {
$Regexp::Grammars::RESULT_STACK[-1]{'(?R)'} = $Regexp::Grammars::match_frame;
}
*/ = $Regexp::Grammars::match_frame;
})(?(DEFINE)
(?<ws>(?:\s*))
(?<hk>(?:\S+))
)
)/
"depth" = "50"
"offset" = "0"
"content" = "perl;6"
"nocase"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment