Skip to content

Commit

Permalink
feat: structure parser
Browse files Browse the repository at this point in the history
  • Loading branch information
therain7 committed Oct 22, 2024
1 parent 62381e8 commit 47f01a0
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
55 changes: 55 additions & 0 deletions lib/parse/str.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
[@@@ocaml.text "/*"]

(** Copyright 2024, Andrei, PavlushaSource *)

(** SPDX-License-Identifier: MIT *)

[@@@ocaml.text "/*"]

(* https://ocaml.org/manual/5.2/modules.html#s:module-expr *)

open! Base
open Angstrom
open LAst

open Common

let plet =
let* rec_flag, bindings = plet Expr.pexpr Pat.ppat in
opt @@ spaced (string "in")
>>= function
| None ->
return (StrLet (rec_flag, bindings))
| Some _ ->
Expr.pexpr >>| fun expr -> StrEval (ExpLet (rec_flag, bindings, expr))

let pty_params_ =
let pmultiple = parens @@ sep_by1 (ws *> char ',') (ws *> pty_var_id) in
let psingle = pty_var_id >>| List.return in
psingle <|> pmultiple <|> return []

let pconstruct_decl_ =
let* id = pconstruct_id in
let* arg =
opt @@ spaced (string "of")
>>= function None -> return None | Some _ -> Ty.pty >>| Option.some
in
return {id; arg}

let pty_decl =
let* params = string "type" *> ws1 *> pty_params_ in
let* id = ws *> pty_con_id in
let* variants =
ws *> string "=" *> ws
*> opt (string "|")
*> sep_by1 (ws *> char '|') (ws *> pconstruct_decl_)
in
return (StrType {id; params; variants})

let peval = Expr.pexpr >>| fun expr -> StrEval expr

let pstr_item = ws *> choice [plet; pty_decl; peval]

let pstr =
let sep = ws *> opt (string ";;") in
sep_by sep pstr_item <* sep
13 changes: 13 additions & 0 deletions lib/parse/str.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[@@@ocaml.text "/*"]

(** Copyright 2024, Andrei, PavlushaSource *)

(** SPDX-License-Identifier: MIT *)

[@@@ocaml.text "/*"]

open! Base
open Angstrom
open LAst

val pstr : structure t

0 comments on commit 47f01a0

Please sign in to comment.