Nick Stinemates
2008-10-23 05:18:39
views:
354answers:
2http://refactormycode.com, and they still don't do F#
Benjol
2008-10-23 05:25:16
+1
A:
I'd use an active pattern to encapsulate the Regex.IsMatch and Regex.Match pairs, like so:
let (|Matches|_|) re s =
let m = Regex(re).Match(s)
if m.Success then
Some(Matches (m.Value))
else
None
Then your nexttoken function can look like:
let nexttoken (st:String) =
match st with
| Matches "^s+" s -> Whitespace(s)
| Matches "^//.*?\r?\n" s -> Comment(s)
...
kvb
2009-03-26 07:28:13
I like this! Don't know how I managed to go so long without discovering active patterns. Thanks, I've learnt something new.
Benjol
2009-03-26 09:41:13
Yeah, looks like Some(m.Value) works fine. If you use total active patterns (which can't fail to match, but can act like discriminated unions), you specify the name of the pattern that matches, so I was doing that here too. It seems like that's not necessary with partial active patterns.
kvb
2009-03-27 04:00:38