Last active
December 11, 2015 09:48
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// bool <- "true" / "false" | |
let boolRule = GrammarRule<bool>(Choice [Terminal "true"; Terminal "false"], (fun s _ -> Parsed (s = "true"))) | |
// paren <- "(" expr ")" | |
let parseParen _ = function | |
| Production [TerminalSymbol "("; x; TerminalSymbol ")"] -> x | |
| x -> unexpected x | |
let parenRule = GrammarRule<bool>(Sequence [Terminal "("; NonTerminal "expr"; Terminal ")"], parseParen) | |
// not <- "!" atom | |
let parseNot _ = function | |
| Production [TerminalSymbol "!"; Parsed x] -> Parsed <| not x | |
| x -> unexpected x | |
let notRule = GrammarRule<bool>(Sequence [Terminal "!"; NonTerminal "atom"], parseNot) | |
// atom <- bool / paren / not | |
let atomRule = GrammarRule<bool>(Choice [NonTerminal "bool"; NonTerminal "paren"; NonTerminal "not"], (fun _ x -> x)) | |
// and <- atom ("&" and)? | |
// or <- and ("|" or)? | |
let parseBin _ = function | |
| Production [x; EmptyMatch] -> x | |
| Production [Parsed x; Production [TerminalSymbol "&"; Parsed y]] -> Parsed (x && y) | |
| Production [Parsed x; Production [TerminalSymbol "|"; Parsed y]] -> Parsed (x || y) | |
| x -> unexpected x | |
let andRule = GrammarRule<bool>(Sequence [NonTerminal "atom"; Optional (Sequence [Terminal "&"; NonTerminal "and"])], parseBin) | |
let orRule = GrammarRule<bool>(Sequence [NonTerminal "and"; Optional (Sequence [Terminal "|"; NonTerminal "or"])], parseBin) | |
// expr <- or | |
let exprRule = GrammarRule<bool>(NonTerminal "or", (fun _ x -> x)) | |
// start <- expr <epsilon> | |
let parseStart _ = function | |
| Production [x; EmptyMatch] -> x | |
| x -> unexpected x | |
let startRule = GrammarRule<bool>(Sequence [NonTerminal "expr"; Epsilon], parseStart) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment