I'm trying to define PHP-style variables in Irony like so:
variable.Rule = "$" + identifier;
Works great, except that you're allowed to put spaces between the $
and the identifier
. I want to prevent that. How?
Do I have to create a new customized terminal? If so, will I still be able to take advantage of the IdentifierTerminal
magic?
Digging around in IdentifierTerminal
I see there's actually a flag for "NameIncludesPrefix", but it's only used in one place. Looks like the prefix is stored in this CompoundTokenDetails
object... which I'm not sure how to use. Edit: Nevermind, this was a dead-end. Those flags are for adding modifiers to how the variable behaves.
This kinda works...
class VariableTerminal : Terminal
{
public VariableTerminal(string name) : base(name)
{
}
public override IList<string> GetFirsts()
{
return new[] { "$" };
}
public override Token TryMatch(ParsingContext context, ISourceStream source)
{
if (source.PreviewChar != '$') return null;
do
{
source.PreviewPosition++;
} while (!source.EOF() && char.IsLetter(source.PreviewChar));
var token = source.CreateToken(OutputTerminal);
return token;
}
}
I'm not really sure what OuputTerminal
is though.. I guess it's some kind of dynamic property based on the current preview position? The way parsing is done in Irony is a little strange I think...
Anyway, the problem with this is what when I use this VariableTerminal
, instead of how I was doing it before with "$" + IdentifierTerminal"
, when there's a syntax error, such as in this code:
p cat
The identifier terminal used to say
Syntax error, expected: { real string $ true false ...
But the variable gives me this error instead:
Invalid character: 'c'
The former error was more useful I think. I don't really understand why it's spitting out a different error...how can I get it to say that instead?
Not sure if this one might help:
http://irony.codeplex.com/discussions/70460
So, sharing it for the 2 lines:
I think you won't be using them in the same way exactly, but maybe something similar.