What area is: "my $ foo" and what is it used for?

With a regular expression, token, or rule, you can define such a variable;

token directive { :my $foo = "in command"; <command> <subject> <value>? } 

The language documentation does not and very little in S05 - Regular rules and regulations to quote:

Any regular expression in a grammar is actually just a kind of method, and you can declare variables in such a routine using a colon followed by any sphere declarator parsed by Perl 6, including my, our, state, and a constant. (Since quasi-declarators, temp, and let are also recognized.) A single statement (up through the end semicolon or end end figure) is parsed like normal Perl 6 code:

 token prove-nondeterministic-parsing { :my $threshold = rand; 'maybe' \s+ <it($threshold)> } 

I get that regular expression in grammars is very similar to methods in classes; I get that you can run the block anywhere in the rule, and if the parsing successfully reaches this point, the block will be executed - but I don’t understand what this thing is for.

Can someone clearly define what their area of ​​action is; explain what it needs and give a typical use case?

+5
source share
1 answer

In what area is :my $foo; ?

:my $foo ...; has the lexical scale of the rule / token / regular expression in which it appears.

(And :my $*foo ...; - pay attention to the additional * denoting the dynamic variable, - has both the lexical and dynamic areas of the rule / token / regular expression in which it appears.)

What is used for

Here's what happens without this construct:

 regex scope-too-small { # Opening `{` opens a regex lexical scope. { my $foo = / bar / } # Block with its own inner lexical scope. $foo # ERROR: Variable '$foo' is not declared } grammar scope-too-large { # Opening `{` opens lexical scope for gramamr. my $foo = / bar / ; regex r1 { ... } # `$foo` is recognized inside `r1`... ... regex r999 { ... } # ...but also inside r999 } 

Thus, the syntax is : ... ; used to get exactly the required area - neither too wide nor too narrow.

Typical Use Cases

This function is commonly used in large or complex grammars to avoid a weak area (which generates errors).

For a suitable example of the exact lexical field of view, you can see the declaration and use of @extra_tweaks in token babble , as defined in the current Rakudo grammar snapshot .nqp source code .

P6 supports action objects . These are classes with methods matching each other with rules in the grammar. Whenever a rule matches, it invokes the appropriate method of action. Dynamic variables ensure that the declaration of variables that are bound to a block (method, rule, etc.) that they are declared in lexically and dynamically, which means that they are also available in the corresponding action method, is exactly correct. For an example, see the @*nibbles declaration in the Rakudo grammar module and its use in the Rakudo action module .

+7
source

Source: https://habr.com/ru/post/1245468/


All Articles