Parsing with derivatives in F#
MIT License
Fred is an experiment in learning F# through implementing parsing with derivatives.
The initial implementation uses the standard Brzozowski derivative of a regular expression.
Later work will implement Matt Might, David Darais and Daniel Spiewak's work on extending derivative parsing to context free grammars.
At the moment the project is largely feature complete for the regular languages, with a C#-friendly wrapper.
In C#:
// To find all strings "aaa" (or /a{3}/ if you prefer):
RegularParser.Token('a').Count(3)
// /abc(d|e)/
using P = Fred.RegularExpression.RegularParser;
P.Token('a').Then(P.Token('b')).Then(P.Token('c')).Then(P.Token('d').Or(P.Token('e')))
// /(ab)*/
(RegularParser.Token('a').Then(RegularParser.Token('b')).Star()
// /a?/
RegularParser.Token('a').AtMost(1)
In F#:
// To find all strings "aaa" (or /a{3}/ if you prefer):
rep 3 (Char 'a')
// /abc(d|e)/
Cat (all ['a'; 'b'; 'c'], Union (Char 'd', Char 'e'))
// or, because a string is a sequence of char:
Cat (all (List.ofSeq "abc"), Union (Char 'd', Char 'e'))
// /(ab)*/
(all (List.ofSeq "ab")) |> Star
// /a?/
Char 'a' |> opt
In C#:
RegularParser.Num().Count(3,6).Recognise("123456")
In F#:
// In which we not only find stuff, we find stuff in partial input: useful for
// handling slow/buffered input, like reading off a socket.
let ab = all ['a';'b']
let finder = startFind ab
let someProcessedInput = resumableFind finder "abab"
let someMoreProcessing = resumableFind someProcessedInput "abab"
finishFind someMoreProcessing
Copyright (C) 2014 by Frank Shearar
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.