anchor.diff:      | None -> None
   
   type DelimType =
  +    | Open
       | SCap of string
       | RCap of string
   
  -type CaptureParams = 
  +//type ExpressionParams =
  +//    {   
  +//        Begin: string option
  +//        Delims: DelimType list
  +//        End: string option
  +//        Func: ExprTypes list -> ExprTypes
  +//    }
  +
  +type SubexpressionType = 
       {
  -        Begin: string
  -        Delims: DelimType list
  -        End: string        
  +        Pattern: DelimType list
           Func: ExprTypes list -> ExprTypes
       }
  -
  -type ExpressionParams =
  -    {
  -        Delims: DelimType list
  -        Func: ExprTypes list -> ExprTypes
  -    }
  -
  +    
   open System.Numerics
   
   let whitespace = [| " "; "\r"; "\n"; "\t"; |]
  
positive.code:         Generator (SubExpression(starte), SubExpression(inc), SubExpression(ende))        
      | list -> failwith (sprintf "Incorrect generator syntax: %A" list)
  
  let expressionTypes =
      [
          { Delims = [];                                    Func = (function | [] -> Unit | exprs -> SubExpression exprs) }
          { Delims = [RCap ","];                            Func = (fun exprs -> Tuple exprs) }
          { Delims = [SCap "fun"; SCap "->"];               Func = generateLambda }
          { Delims = [SCap "=>"];                           Func = generateLambda }
          { Delims = [SCap "if"; SCap "then"; SCap "else"]; Func = generateIfThenElse }
      ]
  
  let captureTypes = 
      [
          { Begin = "{";   Delims = [RCap ".."]; End = "}";  Func = generateNumIterator }
          { Begin = "[";   Delims = [];          End = "]";  Func = (fun exprs -> IndexArgs <| SubExpression exprs) }
          { Begin = "let"; Delims = [SCap "="];  End = "in"; Func = generateBind }
          { Begin = "var"; Delims = [SCap "="];  End = "in"; Func = generateBind }
      ]
      |> List.append (expressionTypes |> List.map (fun et -> { Begin = "("; Delims = et.Delims; End = ")"; Func = et.Func } ))
  
  let (|CaptureDelim|_|) (currentCaptures: CaptureParams list) (text: StringWindow) =
      let matches, delimLen = 
          [ 
              for cc in currentCaptures do
                  match cc.Delims with
                  | (SCap delim) :: dt when text.StartsWith delim -> yield { cc with Delims = dt }, delim
                  | (RCap delim) :: dt when text.StartsWith delim -> yield cc, delim
                  | (RCap _) :: RCap delim :: dt when text.StartsWith delim -> yield { cc with Delims = RCap delim :: dt }, delim
                  | (RCap _) :: SCap delim :: dt when text.StartsWith delim -> yield { cc with Delims = dt }, delim  
                  | _ -> ()          
          ] 
          |> List.allMaxBy (fun (cc, matchedDelim) -> matchedDelim.Length)
          |> (fun (matches, delimlen) -> matches |> List.map (fun (cc, txt) -> cc), delimlen) 
      match matches with
      | [] -> None
      | onecap :: [] -> Some ([onecap], text.Subwindow(delimLen))
      | caps -> Some (caps, text.Subwindow(delimLen))
  
  let (|CaptureEnd|_|) currentCaptures (text: StringWindow) =
      let matches, str = [ for cc in currentCaptures do if text.StartsWith cc.End then yield cc ] |> List.allMaxBy (fun ct -> ct.End.Length) 
      match matches with
      | [] -> None
      | onecap :: [] -> Some ([onecap], text.Subwindow(onecap.End.Length))
      | caps -> Some (caps, text.Subwindow(str))
  
  let (|CaptureBegin|_|) (text: StringWindow) =
      let matches, str = [ for ct in captureTypes do if text.StartsWith ct.Begin then yield ct ] |> List.allMaxBy (fun ct -> ct.Begin.Length)
      match matches with
      | [] -> None
      | onecap :: [] -> Some ([onecap], text.Subwindow(onecap.Begin.Length))
      | caps -> Some (caps, text.Subwindow(str))
  
  
  let parseProgram (startText: string) = 
      let rec parseProgramInner (str: StringWindow) (result: ExprTypes list) (currentCaptures: CaptureParams list) =
          match str with
          | _ when str.Length = 0 -> result, str, currentCaptures
          | CaptureBegin (cParams, crem) -> 
              let subExprs, rem, captures = parseProgramInner crem [] cParams
              let exprConstraint = 
                  match captures with
                  | [] -> failwith "Unexpected end of subexpression"
                  | h :: [] -> h
                  | list -> list |> List.filter (fun cap -> List.isEmpty cap.Delims) |> (function | h :: [] -> h | _ -> failwith "Ambiguous end of subexpression")
              let value = subExprs |> List.rev |> exprConstraint.Func 
              parseProgramInner rem (value :: result) currentCaptures
          | CaptureDelim currentCaptures (cParams, crem) -> 
              let subExprs, rem, captures = parseProgramInner crem [] cParams
              SubExpression (result |> List.rev) :: subExprs, rem, captures 
          | CaptureEnd currentCaptures (cParams, crem) ->  
              match result with
              | [] -> result, crem, cParams
              | results -> [SubExpression (results |> List.rev)], crem, cParams 
          | Skip " " res
          | Skip "\t" res
          | Skip "\r" res
  
negative.code:         | FreeToken ["."; " "; "("; ")"; ","; "."; "]"; "["; "\r"; "\n"; "\t"] (token, rem) ->
              parseProgramInner rem ((Unknown token) :: result) currentCaptures
          | str -> parseProgramInner (str.Subwindow(1)) result currentCaptures
      let _, res, _ = parseProgramInner (StringWindow(startText, 0)) [] [] in res |> List.rev
  