hl7 parser with fparsec I have a little experience working with .hl7 data (format for transmitting health data), so I decided to try and write an .hl7 parser using fparsec.

A standard .hl7 segment is a single line with:


First 3 chars are the header
Next 5 chars are the separators (typically |^~\&amp;)
Fields / repetitions delimited by pipes


A segment has fields or repetitions (repeating fields).  A field can have components.   A component can have sub-components.

Sample .hl7 message header:


MSH|^~\\&amp;|Location|SendingApp|DateTimeOfMessage



I am looking for feedback on data design (the types) and the functions.

type Subcomponent = {value:string; position:int}
type Component = {subcomponents: Subcomponent list; position:int}
type SingleField = {components: Component list; position:int}
type Field = Repetitions of SingleField list | SingleField of SingleField
type Segment = { name:string; fields:Field list; }
type Hl7Message = { segments:Segment list }

let hl7Seps = "|^~\\&amp;"

let normalChar = noneOf hl7Seps

let unescape c = match c with
                    | 'F' -&gt; '|'
                    | 'R' -&gt; '~'
                    | 'S' -&gt; '^'
                    | 'T' -&gt; '&amp;'
                    | 'E' -&gt; '\\'
                    | c -&gt; c

let escapedChar = attempt (pchar '\\' &gt;&gt;. anyChar |&gt;&gt; unescape .&gt;&gt; skipChar '\\') &lt;|&gt; pchar '\\'

let pHl7Element = manyChars (normalChar &lt;|&gt; escapedChar)

let pcomp = sepBy pHl7Element (pchar '&amp;') |&gt;&gt; (fun vals -&gt; List.mapi (fun i s -&gt; {value = s; position = i}) vals)

let pfield = sepBy pcomp (pchar '^') |&gt;&gt; (fun comps -&gt; List.mapi (fun i c -&gt; {subcomponents = c; position = i}) comps)

let pRepsOrField = sepBy pfield (pchar '~') |&gt;&gt; (fun fields -&gt; match fields.Length with
                                                                | 0 | 1 -&gt; SingleField {components = fields.Item 0; position = 0}
                                                                | _ -&gt; Repetitions (List.mapi (fun i c -&gt; {components = c; position = i}) fields))

let pheader = anyString 3 |&gt;&gt; (fun name -&gt; name)

let pSegment = pipe2 pheader (sepBy pRepsOrField (pchar '|')) (fun name repsOrFields -&gt; {name = name; fields = repsOrFields})

test pSegment "EVN|A&amp;1^B^C|123~456~789"
I am not really familiar with Fparsec, but a few of your functions could be simplified, or written better:

For example : 

let pcomp = sepBy pHl7Element (pchar '&amp;') |&gt;&gt; (fun vals -&gt; List.mapi (fun i s -&gt; {value = s; position = i}) vals)


Using partial application, it could be written as :

let pcomp = sepBy pHl7Element (pchar '&amp;') |&gt;&gt; (List.mapi (fun i s -&gt; { value = s; position = i }))


And the one after it: 

// Before
let pfield = sepBy pcomp (pchar '^') |&gt;&gt; (fun comps -&gt; List.mapi (fun i c -&gt; {subcomponents = c; position = i}) comps)
// After
let pfield = sepBy pcomp (pchar '^') |&gt;&gt; (List.mapi (fun i c -&gt; { subcomponents = c; position = i }))


Also, (fun x -&gt; x) could be simply replaced by id. 

let pheader = anyString 3 |&gt;&gt; id


On other matters, there is a potential bug in your code : in your pRepsOrField function, you're matching field.Length with 0 then extracting the first item, which will crash your code. Using the Length property to find out the length is .. unusual. You don't even need it. Pattern matching with lists based on length in F# is usually done this way:

match fields with
| [] -&gt; // do something
| [ c ] -&gt;  SingleField { components = c; position = 0 }
| _ -&gt; Repetitions(List.mapi (fun i c -&gt; { components = c; position = i }) fields))


Now you don't have to use extra methods/properties to get the length and and the first item. It is all right there ...