Skip to content

Instantly share code, notes, and snippets.

@PollRobots
Created February 1, 2013 06:49
Show Gist options
  • Select an option

  • Save PollRobots/4689812 to your computer and use it in GitHub Desktop.

Select an option

Save PollRobots/4689812 to your computer and use it in GitHub Desktop.
let parseExpression (grammar:Map<string,GrammarRule<'a>>) start (input:string) =
let rec parse offset = function
| Terminal x ->
let e = offset + x.Length - 1
if e < input.Length && input.[offset..e] = x then (TerminalSymbol x, e + 1) else (Unmatched, offset)
| TerminalOneOf x -> match currentCharacter input offset with
| Some c -> if x.Contains(c) then (TerminalSymbol c, offset + c.Length) else (Unmatched, offset)
| None -> (Unmatched, offset)
| TerminalWildcard -> match currentCharacter input offset with
| Some c -> (TerminalSymbol c, offset + c.Length)
| None -> (Unmatched, offset)
| TerminalUnicode x -> match currentCharacter input offset with
| Some c -> if Char.GetUnicodeCategory(c, 0) = x then (TerminalSymbol c, offset + c.Length) else (Unmatched, offset)
| None -> (Unmatched, offset)
| NonTerminal x ->
let rule = grammar.[x]
// .
// .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment