I'm trying to use fparsec to parse a simple todo list language (the data from TaskPaper actually) as a simple parser combinator example. But I've run into a bug I can't seem to puzzle out. I'm new to parser combinators and FParsec seems to rely on me knowing Parsec, but I'm finding the parsec documentation inscrutable.
The rules of the task paper language are simple (I'm ignoring @tags for now)
- Projects end with a ':'
- Tasks are start with '-'
- Any other line of text is a plain text note on either the project or task
So the string "Project 1:\nSome note\nProject 2:" should return from parseFile as [ProjectName("Project 1");NoteText("Some note");ProjectName("Project 2")], but instead, I get [ProjectName("Project 1");ProjectName("Some note\nProject 2")]
Below is my parser code.
open FParsec.Primitives
open FParsec.CharParsers
type ProjectAst = ProjectName of string
| TaskText of string
| NoteText of string
let asString (x:char list) :string =
x
|> List.map (fun y -> y.ToString())
|> String.concat ""
let makeNote x = NoteText(asString x)
let parseProject =
parse { let! s = many (noneOf ":\n\r\c")
do! skipChar ':'
return ProjectName( asString s ) }
let parseTask =
parse { do! skipChar '-'
let! s = many (noneOf "\n\r\c")
return TaskText( asString s) }
let parseNote = many (noneOf "\n\r\c") |>> makeNote
let parseLine = parseTask <|> (attempt parseProject) <|> parseNote
let parseFile = sepBy parseLine (many1 whitespace)
Edited
The syntax is taken from Hogbay Software's TaskPaper application TaskPaper website Some examples of the syntax
Project 1: Description of Project One -task for project 1 -another task for project 1 details for another task -final task Go to store: -buy eggs -buy milk