Hi,
I may be missing something obvious here, but I have a question about code reuse within a language grammar. I have a language (Stata) which permits constructs of this form
<prefix cmd> [options]: <cmd>
Moreover, prefixes can be combined. If we call
<prefix cmd> [options]:
the prefix, then we may have
<prefix> [[<prefix>] ...] <cmd>
I'd like to match the prefix (and scope it as meta.prefix), and then match the <cmd> when it follows the prefix. However, the <cmd> can also appear in other contexts too (where it should also be matched). As I'm thinking about this now, it seems that it would be ideal if I could define the prefix (which itself requires a rather complicated regex) in one place, and then refer to that in other places, such as where a specific command that can follow a prefix is defined. I am familiar with the language repository and have used it for recursive constructs and to embed one grammar within another, but can't figure out how it might be used here (if at all).
Should I be thinking about this a different way?
Thanks,
-- Phil
On Apr 19, 2013, at 0:28, Phil Schumm pschumm@uchicago.edu wrote:
[…] As I'm thinking about this now, it seems that it would be ideal if I could define the prefix (which itself requires a rather complicated regex) in one place, and then refer to that in other places, such as where a specific command that can follow a prefix is defined. I am familiar with the language repository and have used it for recursive constructs and to embed one grammar within another, but can't figure out how it might be used here (if at all).
I’m having a hard time following the requirements here.
You have two patterns, ‘prefix’ and ‘cmd’.
Can these be made into two rules that are placed in the repository and included where needed?
I have a feeling that the issue is the ordering, e.g. sometimes you want to force ‘cmd’ to be anchored to a previous ‘prefix’ match, and othertimes you do not?!?
There is some support for anchoring mathces in 2.0 by using \G in the pattern. This’ll anchor the match to the end of the previous match, a simple example:
{ begin = 'st'; end = '$'; patterns = ( { match = '\Gart'; name = 'meta.innert'; } ); };
This will match: ‘start' and ‘st art’, but only in the first case, will ‘art’ be scoped as ‘meta.inner’.
On Apr 21, 2013, at 4:03 AM, Allan Odgaard wrote:
You have two patterns, ‘prefix’ and ‘cmd’.
Can these be made into two rules that are placed in the repository and included where needed?
Yes, that's what I'd like to do -- I just can't figure out how to put them together.
I have a feeling that the issue is the ordering, e.g. sometimes you want to force ‘cmd’ to be anchored to a previous ‘prefix’ match, and othertimes you do not?!?
Exactly.
There is some support for anchoring mathces in 2.0 by using \G in the pattern. This’ll anchor the match to the end of the previous match, a simple example:
{ begin = 'st'; end = '$'; patterns = ( { match = '\Gart'; name = 'meta.innert'; } ); };
This will match: ‘start' and ‘st art’, but only in the first case, will ‘art’ be scoped as ‘meta.inner’.
Thanks -- knowing about \G is great. Now in your example, what if 'art' were in the repository? Could I do the following:
{ begin = 'st'; end = '$'; patterns = ( { include = '#art'; } ); }
where in the repository I have
art = { name = 'meta.innert'; match = '\Gart'; }
Finally, suppose 'art' were also valid at the beginning of a line? It seems like if I wanted to reuse the definition (including the scope), I would add:
{ begin = '^'; end = '$'; patterns = ( { include = '#art'; } ); }
Does that make sense? Moreover, since this would then match every line in the file, would there be a substantial speed hit?
-- Phil
On Apr 21, 2013, at 23:14, Phil Schumm pschumm@uchicago.edu wrote:
[…] Now in your example, what if 'art' were in the repository? Could I do the following:
{ begin = 'st'; end = '$'; patterns = ( { include = '#art'; } ); }
where in the repository I have
art = { name = 'meta.innert'; match = '\Gart'; }
Yes, that is functionally equivalent.
Finally, suppose 'art' were also valid at the beginning of a line? It seems like if I wanted to reuse the definition (including the scope), I would add:
{ begin = '^'; end = '$'; patterns = ( { include = '#art'; } ); }
Does that make sense? […]
An alternative would be to anchor the match to either BOL or end of last match, example:
art = { name = 'meta.innert'; match = '(^|\G)art'; }