#!/home/rir/rakudo/parrot_install/bin/perl6
grammar OP {
token TOP {
<op>+
}
token op {
<cop> | <sop> | <bop>
}
token cop { # constrained
<short> <sep> <long> <div> <char>+
}
token sop { # string
<short> <sep> <long> <div>
}
token bop { # bool
<short> <sep> <long>
}
token long {
\w+
}
token short {
\w
}
token char { # XXX Rename flag. Eliminate '_'.
\w
}
rule sep {
':'
}
rule div {
'/'
}
}
my $s = 'a:aaa b:bbb/ c:ccccc/def';
my $match = OP.parse($s);
die "failed" unless $match;
say '$match<op>: ', $match<op>;
say '$match.from: ', $match.from, ' $match.to: ', $match.to;
say '$match.chars: ', $match.chars;
say '$match.orig: ', $match.orig;
say '$match.Str: ', $match.Str;
say '$match.ast: ', $match.ast;
say '$match.caps: is not found';
say '$match.pos: is not found';
my @array = $match;
say '@array = $match; then @array: [';
for @array -> $i { say $i ; }
say "]";
I have a match object that doesn't appear to contain what I'd
like. The examples, I've found, are not working for me; I find
SO5
a bit thick.
Attempting this with a grammar is a learning exercise.
In the above, I'd like to grab ops, know their "type" and insert
into a hash keyed on the shorts.
Is this arranged correctly to avoid a partial sop matching as a
bop?
The input string will be small; should I
walk the string or walk the match object? As an exercise, treating
the problem like a larger language appeals?
How do I traverse $match?
$match is supposed to work just like $/, yes?
I don't care to capture seps or divs but I would
like to abstract the values. Should this be done in the grammar? --by variables?
Be well,
rir -- swimming a sea of sticky voluminous syntax errors all alike
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.