Exporting of dynamically scoped variables?











up vote
5
down vote

favorite
1












Basically, the question is more about grammars but I think that it could be more of a interesting exercise on dynamic variables.



I have a grammar role with a prototyped token (the example is simplified to demonstrate the idea):



proto token foo {*}
token foo:sym<a> {
:my $*delimiter = q<">;
" ~ " <value>
}
token foo:sym<b> {
:my $*delimiter = q<'>;
' ~ ' <value>
}
token value {
.+? <?before $($*delimeter) || $($*custom-delimiter)>
}


When the role is consumed by a grammar I want the $*custom-delimiter to be set by the grammar. Of course, I can declare it everywhere where <foo> is needed. But sometimes it is ok to have it pre-initialized with a universal default. Something like:



{ $*custom-delimiter //= $default-delimiter }


in the value token would work. But external pre-declaration would still be needed.



I hoped that:



our $*custom-delimiter is export = $default-delimiter;


in the scope of module where the role is declared would work. But apparently it doesn't. So, the question is: are there any elegant solutions to this?



Actually, I also hope that the solution would allow to move declaration of $*delimiter in foo outside of the token definitions too.



As a side note: my first thought was about adding a parameter to the token. But having absolutely identical signatures for each variant is looking terrible too:



token foo:sym<a> ( $*custom-delimiter = $default-delimiter ) {
}
token foo:sym<b> ( $*custom-delimiter = $default-delimiter ) {
}
token foo:sym<c> ( $*custom-delimiter = $default-delimiter ) {
}


Another approach is to have something like:



token pre-foo ( $*custom-delimiter = $default-delimiter ) {
<foo>
}


In this case an additional method would be required in actions class to propagate $/<foo>.ast one level up.










share|improve this question


















  • 1




    I'm too tired to be able to understand your question tonight. But you know roles can be parameterized, right? eg role foo[$bar = 99] { method baz { say $bar } }; foo[42].baz; # 42. Is that relevant?
    – raiph
    Nov 16 at 2:45












  • Never needed it, so – it's a good reminder! But – no, unfortunately. Token foo is used by the same grammar in different contexts where different delimiters are used.
    – Vadim Belman
    Nov 16 at 2:49















up vote
5
down vote

favorite
1












Basically, the question is more about grammars but I think that it could be more of a interesting exercise on dynamic variables.



I have a grammar role with a prototyped token (the example is simplified to demonstrate the idea):



proto token foo {*}
token foo:sym<a> {
:my $*delimiter = q<">;
" ~ " <value>
}
token foo:sym<b> {
:my $*delimiter = q<'>;
' ~ ' <value>
}
token value {
.+? <?before $($*delimeter) || $($*custom-delimiter)>
}


When the role is consumed by a grammar I want the $*custom-delimiter to be set by the grammar. Of course, I can declare it everywhere where <foo> is needed. But sometimes it is ok to have it pre-initialized with a universal default. Something like:



{ $*custom-delimiter //= $default-delimiter }


in the value token would work. But external pre-declaration would still be needed.



I hoped that:



our $*custom-delimiter is export = $default-delimiter;


in the scope of module where the role is declared would work. But apparently it doesn't. So, the question is: are there any elegant solutions to this?



Actually, I also hope that the solution would allow to move declaration of $*delimiter in foo outside of the token definitions too.



As a side note: my first thought was about adding a parameter to the token. But having absolutely identical signatures for each variant is looking terrible too:



token foo:sym<a> ( $*custom-delimiter = $default-delimiter ) {
}
token foo:sym<b> ( $*custom-delimiter = $default-delimiter ) {
}
token foo:sym<c> ( $*custom-delimiter = $default-delimiter ) {
}


Another approach is to have something like:



token pre-foo ( $*custom-delimiter = $default-delimiter ) {
<foo>
}


In this case an additional method would be required in actions class to propagate $/<foo>.ast one level up.










share|improve this question


















  • 1




    I'm too tired to be able to understand your question tonight. But you know roles can be parameterized, right? eg role foo[$bar = 99] { method baz { say $bar } }; foo[42].baz; # 42. Is that relevant?
    – raiph
    Nov 16 at 2:45












  • Never needed it, so – it's a good reminder! But – no, unfortunately. Token foo is used by the same grammar in different contexts where different delimiters are used.
    – Vadim Belman
    Nov 16 at 2:49













up vote
5
down vote

favorite
1









up vote
5
down vote

favorite
1






1





Basically, the question is more about grammars but I think that it could be more of a interesting exercise on dynamic variables.



I have a grammar role with a prototyped token (the example is simplified to demonstrate the idea):



proto token foo {*}
token foo:sym<a> {
:my $*delimiter = q<">;
" ~ " <value>
}
token foo:sym<b> {
:my $*delimiter = q<'>;
' ~ ' <value>
}
token value {
.+? <?before $($*delimeter) || $($*custom-delimiter)>
}


When the role is consumed by a grammar I want the $*custom-delimiter to be set by the grammar. Of course, I can declare it everywhere where <foo> is needed. But sometimes it is ok to have it pre-initialized with a universal default. Something like:



{ $*custom-delimiter //= $default-delimiter }


in the value token would work. But external pre-declaration would still be needed.



I hoped that:



our $*custom-delimiter is export = $default-delimiter;


in the scope of module where the role is declared would work. But apparently it doesn't. So, the question is: are there any elegant solutions to this?



Actually, I also hope that the solution would allow to move declaration of $*delimiter in foo outside of the token definitions too.



As a side note: my first thought was about adding a parameter to the token. But having absolutely identical signatures for each variant is looking terrible too:



token foo:sym<a> ( $*custom-delimiter = $default-delimiter ) {
}
token foo:sym<b> ( $*custom-delimiter = $default-delimiter ) {
}
token foo:sym<c> ( $*custom-delimiter = $default-delimiter ) {
}


Another approach is to have something like:



token pre-foo ( $*custom-delimiter = $default-delimiter ) {
<foo>
}


In this case an additional method would be required in actions class to propagate $/<foo>.ast one level up.










share|improve this question













Basically, the question is more about grammars but I think that it could be more of a interesting exercise on dynamic variables.



I have a grammar role with a prototyped token (the example is simplified to demonstrate the idea):



proto token foo {*}
token foo:sym<a> {
:my $*delimiter = q<">;
" ~ " <value>
}
token foo:sym<b> {
:my $*delimiter = q<'>;
' ~ ' <value>
}
token value {
.+? <?before $($*delimeter) || $($*custom-delimiter)>
}


When the role is consumed by a grammar I want the $*custom-delimiter to be set by the grammar. Of course, I can declare it everywhere where <foo> is needed. But sometimes it is ok to have it pre-initialized with a universal default. Something like:



{ $*custom-delimiter //= $default-delimiter }


in the value token would work. But external pre-declaration would still be needed.



I hoped that:



our $*custom-delimiter is export = $default-delimiter;


in the scope of module where the role is declared would work. But apparently it doesn't. So, the question is: are there any elegant solutions to this?



Actually, I also hope that the solution would allow to move declaration of $*delimiter in foo outside of the token definitions too.



As a side note: my first thought was about adding a parameter to the token. But having absolutely identical signatures for each variant is looking terrible too:



token foo:sym<a> ( $*custom-delimiter = $default-delimiter ) {
}
token foo:sym<b> ( $*custom-delimiter = $default-delimiter ) {
}
token foo:sym<c> ( $*custom-delimiter = $default-delimiter ) {
}


Another approach is to have something like:



token pre-foo ( $*custom-delimiter = $default-delimiter ) {
<foo>
}


In this case an additional method would be required in actions class to propagate $/<foo>.ast one level up.







perl6






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 at 19:22









Vadim Belman

40612




40612








  • 1




    I'm too tired to be able to understand your question tonight. But you know roles can be parameterized, right? eg role foo[$bar = 99] { method baz { say $bar } }; foo[42].baz; # 42. Is that relevant?
    – raiph
    Nov 16 at 2:45












  • Never needed it, so – it's a good reminder! But – no, unfortunately. Token foo is used by the same grammar in different contexts where different delimiters are used.
    – Vadim Belman
    Nov 16 at 2:49














  • 1




    I'm too tired to be able to understand your question tonight. But you know roles can be parameterized, right? eg role foo[$bar = 99] { method baz { say $bar } }; foo[42].baz; # 42. Is that relevant?
    – raiph
    Nov 16 at 2:45












  • Never needed it, so – it's a good reminder! But – no, unfortunately. Token foo is used by the same grammar in different contexts where different delimiters are used.
    – Vadim Belman
    Nov 16 at 2:49








1




1




I'm too tired to be able to understand your question tonight. But you know roles can be parameterized, right? eg role foo[$bar = 99] { method baz { say $bar } }; foo[42].baz; # 42. Is that relevant?
– raiph
Nov 16 at 2:45






I'm too tired to be able to understand your question tonight. But you know roles can be parameterized, right? eg role foo[$bar = 99] { method baz { say $bar } }; foo[42].baz; # 42. Is that relevant?
– raiph
Nov 16 at 2:45














Never needed it, so – it's a good reminder! But – no, unfortunately. Token foo is used by the same grammar in different contexts where different delimiters are used.
– Vadim Belman
Nov 16 at 2:49




Never needed it, so – it's a good reminder! But – no, unfortunately. Token foo is used by the same grammar in different contexts where different delimiters are used.
– Vadim Belman
Nov 16 at 2:49

















active

oldest

votes











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',
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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53326588%2fexporting-of-dynamically-scoped-variables%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53326588%2fexporting-of-dynamically-scoped-variables%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents