let ( let* ) = Result.bind type parsed_kind = | Datum of Sexp.t | Discard | Eof type parsed = (parsed_kind, Errors.error) result type state = { lexer: Lexer.state; pairs: (char * Location.span) list; } let from : Lexer.state -> state = fun lexer -> { lexer; pairs = [] } (* --- *) let build_span : Lexer.state -> Location.t -> Location.t -> Location.span = fun lexer min max -> let id = Lexer.source_id lexer in { id; min; max } let push_pair : state -> char -> Location.span -> state = fun state delim loc -> { state with pairs = (delim, loc) :: state.pairs } let pop_pair : state -> char -> Location.span -> ((state * Location.span), Errors.error) result = fun state delim end_span -> match state.pairs with | (h, start_span) :: rest when h == delim -> let state = { state with pairs = rest } in let span = build_span state.lexer start_span.min end_span.max in Ok (state, span) | (_, loc) :: _ -> Error (`Invalid_syntax ("unexpected close delimeter for"), loc.min) | _ -> let loc = Lexer.current_cursor state.lexer in Error (`Invalid_syntax ("unexpected close delimeter"), loc) (* --- *) let located : Sexp.sexp -> Location.span -> parsed = fun sexp span -> Ok (Datum (sexp, span)) let located_err : Errors.t -> Location.span -> parsed = fun err span -> Error (err, span.min) let rec peek : state -> (Lexer.token, Errors.error) result = fun state -> match (Lexer.peek state.lexer) with | Ok (Lexer.Comment _, _) -> Lexer.commit state.lexer; peek state | _ as res -> res let rec take : state -> (Lexer.token, Errors.error) result = fun state -> match (Lexer.take state.lexer) with | Ok (Lexer.Comment _, _) -> take state | _ as res -> res (* --- *) let starts_sexp : Lexer.kind -> bool = function | Lexer.Eof -> true | Lexer.Atom _ -> true | Lexer.Keyword _ -> true | Lexer.Verbatim _ -> true | Lexer.Prefix _ -> true | Lexer.Open _ -> true | _ -> false let rec read_sexp : state -> parsed = fun state -> let* (tok, span) = take state in match tok with | Lexer.Eof -> Ok Eof | Lexer.Atom a -> located (Sexp.Atom a) span | Lexer.Keyword k -> read_keyword state k span | Lexer.Verbatim t -> located (Sexp.Verbatim t) span | Lexer.Prefix p -> read_prefix state p span | Lexer.Open d -> read_delimited state d span | _ -> located_err (`Invalid_syntax "expected an sexpr") span (* keywords can start tagged forms *) and read_keyword : state -> string -> Location.span -> parsed = fun state k span -> let* (tok, next) = peek state in if (Location.adjacent span next) && (starts_sexp tok) then let* item = read_sexp state in match item with | Eof -> located (Sexp.Keyword k) span | Discard -> located (Sexp.Keyword k) span | Datum item -> let span = Location.merge span (Sexp.span item) in located (Sexp.Tagged (k, item)) span else located (Sexp.Keyword k) span (* delimited collections list = (x y z . 42) # can be improper, with a single tail element group = { x z y } [1 2] # may not be improper, interpreted with syntax *) and read_delimited : state -> char -> Location.span -> parsed = fun state odelim span -> let state = push_pair state odelim span in if odelim == '(' then read_list state [] else read_group state odelim [] and read_group : state -> char -> Sexp.t list -> parsed = (* may not be improper *) fun state odelim acc -> let* (tok, span) = peek state in match tok with | Lexer.Close od' -> Lexer.commit state.lexer; let* (_, span) = pop_pair state od' span in located (Sexp.Group (odelim, (List.rev acc))) span | _ -> let* item = read_sexp state in match item with | Eof -> located_err (`Invalid_syntax "unterminated group") span | Discard -> read_group state odelim acc | Datum item -> read_group state odelim (item :: acc) and read_list : state -> Sexp.t list -> parsed = (* can be improper *) fun state acc -> let* (tok, span) = peek state in match tok with | Lexer.Dot -> Lexer.commit state.lexer; if (List.is_empty acc) then located_err (`Invalid_syntax "improper list without head") span else read_list_tail state (List.rev acc) | Lexer.Close od' -> Lexer.commit state.lexer; let* (_, span) = pop_pair state od' span in located (Sexp.List ((List.rev acc), None)) span | _ -> let* item = read_sexp state in match item with | Eof -> located_err (`Invalid_syntax "unterminated list") span | Discard -> read_list state acc | Datum item -> read_list state (item :: acc) and read_list_tail : state -> Sexp.t list -> parsed = fun state items -> let* tail = read_group state '(' [] in match tail with | Eof -> located_err (`Invalid_syntax "unterminated improper list") (Lexer.current_span state.lexer) | Discard -> read_list_tail state items | Datum ((Sexp.Group (_, [tail])), span) -> located (Sexp.List (items, Some tail)) span | Datum (_, span) -> located_err (`Invalid_syntax "expected single item improper tail") span (* quotes, quasiy, reserved, form-comment *) and read_prefix : state -> Lexer.prefix -> Location.span -> parsed = fun state prefix span -> let* (_, next) = peek state in (* comment form doesn't need to be adjacent *) if prefix = Lexer.CommentForm || Location.adjacent span next then begin let* item = read_sexp state in match item with | Eof -> located_err (`Invalid_syntax "unterminated prefix") (Lexer.current_span state.lexer) | Discard -> read_prefix state prefix span | Datum item -> ( let span = build_span state.lexer span.min (Sexp.span item).max in match prefix with | Lexer.Quote -> located (Sexp.Quote item) span | Lexer.Quasiquote -> located (Sexp.Quasiquote item) span | Lexer.Unquote -> located (Sexp.Unquote item) span | Lexer.UnquoteSplice -> located (Sexp.UnquoteSplice item) span | Lexer.Reserved -> located (Sexp.Reserved item) span | Lexer.CommentForm -> Ok Discard ) end else located_err (`Invalid_syntax "unterminated prefix") (Lexer.current_span state.lexer) (* --- *) let read_one source = let scanner = Scanner.from source in let lexer = Lexer.from scanner in let state = from lexer in let rec parse _ = let* item = read_sexp state in match item with | Eof -> Ok None | Discard -> parse () | Datum item -> Ok (Some item) in parse () let read_many source = let scanner = Scanner.from source in let lexer = Lexer.from scanner in let state = from lexer in let rec parse acc = let* next = read_sexp state in match next with | Eof -> Ok (List.rev acc) | Discard -> parse acc | Datum next -> parse (next :: acc) in parse [] let parse_string name string = read_many (Source.of_string name string) let parse_channel name channel = read_many (Source.of_channel name channel)