cancelling a match programmatically

40 views
Skip to first unread message

Brannon King

unread,
Sep 20, 2013, 11:23:08 AM9/20/13
to eto-...@googlegroups.com
In considering the question of how to do numerical range checking, it seems that I should be able to do something like this:
 
var number = new NumberParser();
number.PreMatch += n => { double d; return double.TryParse(n.StringValue, out d) && d < 2000.0; };
 
However, I don't see any way to return whether or not that match was successful. It might also be useful to set an error message in that scenario.

Curtis Wensley

unread,
Sep 20, 2013, 11:33:56 AM9/20/13
to eto-...@googlegroups.com
The way to do this would be to subclass NumericParser and override InnerParse() to do what you want.. e.g.

public class MyNumeric : NumberParser
{
public int MinValue { get; set; }

public int MaxValue { get; set; }

protected override ParseMatch InnerParse(ParseArgs args)
{
var pos = args.Scanner.Position;
var match = base.InnerParse(args);
if (match.Success)
{
var str = args.Scanner.SubString(match.Index, match.Length);
int val;
if (int.TryParse(str, out val))
{
if (val >= MinValue && val <= MaxValue)
{
return match;
}
}
}
args.Scanner.SetPosition(pos);
return args.NoMatch;
}
}


PreMatch/Match events are called after the parsing has completed..  it is an alternative to navigating the match tree manually.

Brannon King

unread,
Sep 24, 2013, 11:34:12 AM9/24/13
to eto-...@googlegroups.com
Overriding InnerParse works well for doing a custom match. How do I add a custom error message to go with it?

Curtis Wensley

unread,
Sep 24, 2013, 11:37:07 AM9/24/13
to eto-...@googlegroups.com
Override Parser.GetErrorMessage()
Reply all
Reply to author
Forward
0 new messages