How can I use the perl6 regex metasyntax, ?
In perl6 grammars, as explained here (note, the design documents are not guaranteed to be up-to-date as the implementation is finished), if an opening angle bracket is followed by an identifier then the construct is a call to a subrule, method or function.
If the character following the identifier is an opening paren, then it's a call to a method or function eg: <foo('bar')>. As explained further down the page, if the first char after the identifier is a space, then the rest of the string up to the closing angle will be interpreted as a regex argument to the method - to quote:
<foo bar>
is more or less equivalent to
<foo(/bar/)>
What's the proper way to use this feature? In my case, I'm parsing line oriented data and I'm trying to declare a rule that will instigate a seperate search on the current line being parsed:
#!/usr/bin/env perl6
# use Grammar::Tracer ;
grammar G {
my $SOLpos = -1 ; # Start-of-line pos
regex TOP { <line>+ }
method SOLscan($regex) {
# Start a new cursor
my $cur = self."!cursor_start_cur"() ;
# Set pos and from to start of the current line
$cur.from($SOLpos) ;
$cur.pos($SOLpos) ;
# Run the given regex on the cursor
$cur = $regex($cur) ;
# If pos is >= 0, we found what we were looking for
if $cur.pos >= 0 {
$cur."!cursor_pass"(self.pos, 'SOLscan')
}
self
}
token line {
{ $SOLpos = self.pos ; say '$SOLpos = ' ~ $SOLpos }
[
|| <word> <ws> 'two' { say 'matched two' } <SOLscan w+> <ws> <word>
|| <word>+ %% <ws> { say 'matched words' }
]
n
}
token word { S+ }
token ws { h+ }
}
my $mo = G.subparse: q:to/END/ ;
hello world
one two three
END
As it is, this code produces:
$ ./h.pl
$SOLpos = 0
matched words
$SOLpos = 12
matched two
Too many positionals passed; expected 1 argument but got 2
in method SOLscan at ./h.pl line 14
in regex line at ./h.pl line 32
in regex TOP at ./h.pl line 7
in block <unit> at ./h.pl line 41
$
Line 14 is $cur.from($SOLpos). If commented out, line 15 produces the same error. It appears as though .pos and .from are read only... (maybe :-)
Any ideas what the proper incantation is?
Note, any proposed solution can be a long way from what I've done here - all I'm really wanting to do is understand how the mechanism is supposed to be used.
perl6 regexp-grammars
|
show 2 more comments
In perl6 grammars, as explained here (note, the design documents are not guaranteed to be up-to-date as the implementation is finished), if an opening angle bracket is followed by an identifier then the construct is a call to a subrule, method or function.
If the character following the identifier is an opening paren, then it's a call to a method or function eg: <foo('bar')>. As explained further down the page, if the first char after the identifier is a space, then the rest of the string up to the closing angle will be interpreted as a regex argument to the method - to quote:
<foo bar>
is more or less equivalent to
<foo(/bar/)>
What's the proper way to use this feature? In my case, I'm parsing line oriented data and I'm trying to declare a rule that will instigate a seperate search on the current line being parsed:
#!/usr/bin/env perl6
# use Grammar::Tracer ;
grammar G {
my $SOLpos = -1 ; # Start-of-line pos
regex TOP { <line>+ }
method SOLscan($regex) {
# Start a new cursor
my $cur = self."!cursor_start_cur"() ;
# Set pos and from to start of the current line
$cur.from($SOLpos) ;
$cur.pos($SOLpos) ;
# Run the given regex on the cursor
$cur = $regex($cur) ;
# If pos is >= 0, we found what we were looking for
if $cur.pos >= 0 {
$cur."!cursor_pass"(self.pos, 'SOLscan')
}
self
}
token line {
{ $SOLpos = self.pos ; say '$SOLpos = ' ~ $SOLpos }
[
|| <word> <ws> 'two' { say 'matched two' } <SOLscan w+> <ws> <word>
|| <word>+ %% <ws> { say 'matched words' }
]
n
}
token word { S+ }
token ws { h+ }
}
my $mo = G.subparse: q:to/END/ ;
hello world
one two three
END
As it is, this code produces:
$ ./h.pl
$SOLpos = 0
matched words
$SOLpos = 12
matched two
Too many positionals passed; expected 1 argument but got 2
in method SOLscan at ./h.pl line 14
in regex line at ./h.pl line 32
in regex TOP at ./h.pl line 7
in block <unit> at ./h.pl line 41
$
Line 14 is $cur.from($SOLpos). If commented out, line 15 produces the same error. It appears as though .pos and .from are read only... (maybe :-)
Any ideas what the proper incantation is?
Note, any proposed solution can be a long way from what I've done here - all I'm really wanting to do is understand how the mechanism is supposed to be used.
perl6 regexp-grammars
Out of curiosity, are the methodscursor_start_cur()andcursor_pass()documented anywhere? I could not find any in class Cursor
– Håkon Hægland
May 7 '18 at 9:37
2
Not formerly, as they're considered "internals." It's a little out of date, but edument's Rakudo internals course (edumentab.github.io/rakudo-and-nqp-internals-course/…) has some information half way down "day 2". Also, you can read the source (Luke) here: github.com/perl6/nqp/blob/master/src/QRegex/Cursor.nqp
– Marty
May 7 '18 at 10:03
1
@Marty if they are considered internals, shouldn't there be a way of defining that function without using them?
– jjmerelo
May 7 '18 at 10:13
1
You can advance the cursor by return aMatchobject from the regex. But I can't get it to work in this case. The regex that's passed as an argument also comes out asForeignCode, not asRegex, which makes me suspect that the feature might not work all that well in Rakudo at all.
– moritz
May 7 '18 at 15:14
1
moritz - my guess is that compiled regex (QAST::Regex nodes) are seen asForeignCodeas far as the main slang is concerned.
– Marty
May 7 '18 at 21:49
|
show 2 more comments
In perl6 grammars, as explained here (note, the design documents are not guaranteed to be up-to-date as the implementation is finished), if an opening angle bracket is followed by an identifier then the construct is a call to a subrule, method or function.
If the character following the identifier is an opening paren, then it's a call to a method or function eg: <foo('bar')>. As explained further down the page, if the first char after the identifier is a space, then the rest of the string up to the closing angle will be interpreted as a regex argument to the method - to quote:
<foo bar>
is more or less equivalent to
<foo(/bar/)>
What's the proper way to use this feature? In my case, I'm parsing line oriented data and I'm trying to declare a rule that will instigate a seperate search on the current line being parsed:
#!/usr/bin/env perl6
# use Grammar::Tracer ;
grammar G {
my $SOLpos = -1 ; # Start-of-line pos
regex TOP { <line>+ }
method SOLscan($regex) {
# Start a new cursor
my $cur = self."!cursor_start_cur"() ;
# Set pos and from to start of the current line
$cur.from($SOLpos) ;
$cur.pos($SOLpos) ;
# Run the given regex on the cursor
$cur = $regex($cur) ;
# If pos is >= 0, we found what we were looking for
if $cur.pos >= 0 {
$cur."!cursor_pass"(self.pos, 'SOLscan')
}
self
}
token line {
{ $SOLpos = self.pos ; say '$SOLpos = ' ~ $SOLpos }
[
|| <word> <ws> 'two' { say 'matched two' } <SOLscan w+> <ws> <word>
|| <word>+ %% <ws> { say 'matched words' }
]
n
}
token word { S+ }
token ws { h+ }
}
my $mo = G.subparse: q:to/END/ ;
hello world
one two three
END
As it is, this code produces:
$ ./h.pl
$SOLpos = 0
matched words
$SOLpos = 12
matched two
Too many positionals passed; expected 1 argument but got 2
in method SOLscan at ./h.pl line 14
in regex line at ./h.pl line 32
in regex TOP at ./h.pl line 7
in block <unit> at ./h.pl line 41
$
Line 14 is $cur.from($SOLpos). If commented out, line 15 produces the same error. It appears as though .pos and .from are read only... (maybe :-)
Any ideas what the proper incantation is?
Note, any proposed solution can be a long way from what I've done here - all I'm really wanting to do is understand how the mechanism is supposed to be used.
perl6 regexp-grammars
In perl6 grammars, as explained here (note, the design documents are not guaranteed to be up-to-date as the implementation is finished), if an opening angle bracket is followed by an identifier then the construct is a call to a subrule, method or function.
If the character following the identifier is an opening paren, then it's a call to a method or function eg: <foo('bar')>. As explained further down the page, if the first char after the identifier is a space, then the rest of the string up to the closing angle will be interpreted as a regex argument to the method - to quote:
<foo bar>
is more or less equivalent to
<foo(/bar/)>
What's the proper way to use this feature? In my case, I'm parsing line oriented data and I'm trying to declare a rule that will instigate a seperate search on the current line being parsed:
#!/usr/bin/env perl6
# use Grammar::Tracer ;
grammar G {
my $SOLpos = -1 ; # Start-of-line pos
regex TOP { <line>+ }
method SOLscan($regex) {
# Start a new cursor
my $cur = self."!cursor_start_cur"() ;
# Set pos and from to start of the current line
$cur.from($SOLpos) ;
$cur.pos($SOLpos) ;
# Run the given regex on the cursor
$cur = $regex($cur) ;
# If pos is >= 0, we found what we were looking for
if $cur.pos >= 0 {
$cur."!cursor_pass"(self.pos, 'SOLscan')
}
self
}
token line {
{ $SOLpos = self.pos ; say '$SOLpos = ' ~ $SOLpos }
[
|| <word> <ws> 'two' { say 'matched two' } <SOLscan w+> <ws> <word>
|| <word>+ %% <ws> { say 'matched words' }
]
n
}
token word { S+ }
token ws { h+ }
}
my $mo = G.subparse: q:to/END/ ;
hello world
one two three
END
As it is, this code produces:
$ ./h.pl
$SOLpos = 0
matched words
$SOLpos = 12
matched two
Too many positionals passed; expected 1 argument but got 2
in method SOLscan at ./h.pl line 14
in regex line at ./h.pl line 32
in regex TOP at ./h.pl line 7
in block <unit> at ./h.pl line 41
$
Line 14 is $cur.from($SOLpos). If commented out, line 15 produces the same error. It appears as though .pos and .from are read only... (maybe :-)
Any ideas what the proper incantation is?
Note, any proposed solution can be a long way from what I've done here - all I'm really wanting to do is understand how the mechanism is supposed to be used.
perl6 regexp-grammars
perl6 regexp-grammars
asked May 7 '18 at 8:57
MartyMarty
2,335715
2,335715
Out of curiosity, are the methodscursor_start_cur()andcursor_pass()documented anywhere? I could not find any in class Cursor
– Håkon Hægland
May 7 '18 at 9:37
2
Not formerly, as they're considered "internals." It's a little out of date, but edument's Rakudo internals course (edumentab.github.io/rakudo-and-nqp-internals-course/…) has some information half way down "day 2". Also, you can read the source (Luke) here: github.com/perl6/nqp/blob/master/src/QRegex/Cursor.nqp
– Marty
May 7 '18 at 10:03
1
@Marty if they are considered internals, shouldn't there be a way of defining that function without using them?
– jjmerelo
May 7 '18 at 10:13
1
You can advance the cursor by return aMatchobject from the regex. But I can't get it to work in this case. The regex that's passed as an argument also comes out asForeignCode, not asRegex, which makes me suspect that the feature might not work all that well in Rakudo at all.
– moritz
May 7 '18 at 15:14
1
moritz - my guess is that compiled regex (QAST::Regex nodes) are seen asForeignCodeas far as the main slang is concerned.
– Marty
May 7 '18 at 21:49
|
show 2 more comments
Out of curiosity, are the methodscursor_start_cur()andcursor_pass()documented anywhere? I could not find any in class Cursor
– Håkon Hægland
May 7 '18 at 9:37
2
Not formerly, as they're considered "internals." It's a little out of date, but edument's Rakudo internals course (edumentab.github.io/rakudo-and-nqp-internals-course/…) has some information half way down "day 2". Also, you can read the source (Luke) here: github.com/perl6/nqp/blob/master/src/QRegex/Cursor.nqp
– Marty
May 7 '18 at 10:03
1
@Marty if they are considered internals, shouldn't there be a way of defining that function without using them?
– jjmerelo
May 7 '18 at 10:13
1
You can advance the cursor by return aMatchobject from the regex. But I can't get it to work in this case. The regex that's passed as an argument also comes out asForeignCode, not asRegex, which makes me suspect that the feature might not work all that well in Rakudo at all.
– moritz
May 7 '18 at 15:14
1
moritz - my guess is that compiled regex (QAST::Regex nodes) are seen asForeignCodeas far as the main slang is concerned.
– Marty
May 7 '18 at 21:49
Out of curiosity, are the methods
cursor_start_cur() and cursor_pass() documented anywhere? I could not find any in class Cursor– Håkon Hægland
May 7 '18 at 9:37
Out of curiosity, are the methods
cursor_start_cur() and cursor_pass() documented anywhere? I could not find any in class Cursor– Håkon Hægland
May 7 '18 at 9:37
2
2
Not formerly, as they're considered "internals." It's a little out of date, but edument's Rakudo internals course (edumentab.github.io/rakudo-and-nqp-internals-course/…) has some information half way down "day 2". Also, you can read the source (Luke) here: github.com/perl6/nqp/blob/master/src/QRegex/Cursor.nqp
– Marty
May 7 '18 at 10:03
Not formerly, as they're considered "internals." It's a little out of date, but edument's Rakudo internals course (edumentab.github.io/rakudo-and-nqp-internals-course/…) has some information half way down "day 2". Also, you can read the source (Luke) here: github.com/perl6/nqp/blob/master/src/QRegex/Cursor.nqp
– Marty
May 7 '18 at 10:03
1
1
@Marty if they are considered internals, shouldn't there be a way of defining that function without using them?
– jjmerelo
May 7 '18 at 10:13
@Marty if they are considered internals, shouldn't there be a way of defining that function without using them?
– jjmerelo
May 7 '18 at 10:13
1
1
You can advance the cursor by return a
Match object from the regex. But I can't get it to work in this case. The regex that's passed as an argument also comes out as ForeignCode, not as Regex, which makes me suspect that the feature might not work all that well in Rakudo at all.– moritz
May 7 '18 at 15:14
You can advance the cursor by return a
Match object from the regex. But I can't get it to work in this case. The regex that's passed as an argument also comes out as ForeignCode, not as Regex, which makes me suspect that the feature might not work all that well in Rakudo at all.– moritz
May 7 '18 at 15:14
1
1
moritz - my guess is that compiled regex (QAST::Regex nodes) are seen as
ForeignCode as far as the main slang is concerned.– Marty
May 7 '18 at 21:49
moritz - my guess is that compiled regex (QAST::Regex nodes) are seen as
ForeignCode as far as the main slang is concerned.– Marty
May 7 '18 at 21:49
|
show 2 more comments
1 Answer
1
active
oldest
votes
It does not seem to be in the corresponding directory in roast, so that would make it a "Not Yet Implemented" feature, I'm afraid.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50210456%2fhow-can-i-use-the-perl6-regex-metasyntax-foo-regex%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
It does not seem to be in the corresponding directory in roast, so that would make it a "Not Yet Implemented" feature, I'm afraid.
add a comment |
It does not seem to be in the corresponding directory in roast, so that would make it a "Not Yet Implemented" feature, I'm afraid.
add a comment |
It does not seem to be in the corresponding directory in roast, so that would make it a "Not Yet Implemented" feature, I'm afraid.
It does not seem to be in the corresponding directory in roast, so that would make it a "Not Yet Implemented" feature, I'm afraid.
answered Dec 31 '18 at 16:27
jjmerelojjmerelo
5,72531747
5,72531747
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50210456%2fhow-can-i-use-the-perl6-regex-metasyntax-foo-regex%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Out of curiosity, are the methods
cursor_start_cur()andcursor_pass()documented anywhere? I could not find any in class Cursor– Håkon Hægland
May 7 '18 at 9:37
2
Not formerly, as they're considered "internals." It's a little out of date, but edument's Rakudo internals course (edumentab.github.io/rakudo-and-nqp-internals-course/…) has some information half way down "day 2". Also, you can read the source (Luke) here: github.com/perl6/nqp/blob/master/src/QRegex/Cursor.nqp
– Marty
May 7 '18 at 10:03
1
@Marty if they are considered internals, shouldn't there be a way of defining that function without using them?
– jjmerelo
May 7 '18 at 10:13
1
You can advance the cursor by return a
Matchobject from the regex. But I can't get it to work in this case. The regex that's passed as an argument also comes out asForeignCode, not asRegex, which makes me suspect that the feature might not work all that well in Rakudo at all.– moritz
May 7 '18 at 15:14
1
moritz - my guess is that compiled regex (QAST::Regex nodes) are seen as
ForeignCodeas far as the main slang is concerned.– Marty
May 7 '18 at 21:49