Commits

camlspotter committed daff9e0

planck parser

Comments (0)

Files changed (3)

 
 .PHONY: all clean
 
-OCAMLINCLUDES += ocaml/parsing ocaml/utils
-# OCAMLFIND=$(OCAMLFIND) -toolchain typerex
+PWD=$(dir .)
+OCAMLINCLUDES = $(PWD)/parsing .
+# OCAMLINCLUDES += ocaml/parsing ocaml/utils
 
 OCAMLPACKS[] = sexplib spotlib treeprint planck
 
     ident
     path
 
+    parsing/token
+    parsing/lex
+    parsing/parser
+
     ocaml/utils/config
     ocaml/utils/clflags
     ocaml/utils/misc
     ocaml/parsing/parse
 
     olf
-    ocamlolf
 
     types
     sig

parsing/OMakefile

 
 OCAMLINCLUDES +=
 
-OCAMLFLAGS    +=
+OCAMLFLAGS    += -I ..
 OCAMLCFLAGS   +=
 OCAMLOPTFLAGS += 
 OCAML_LINK_FLAGS += 
 OCAMLDEPFLAGS= -syntax camlp4o -package sexplib.syntax,monad-custom
 OCAMLPPFLAGS=  -syntax camlp4o -package sexplib.syntax,monad-custom
 
-NO_INSTALL=true
-
-FILES0[] =
-   token
-   lex
-   lextest
-
-MyOCamlProgram(lexer_test, $(FILES0))
-
-
-FILES1[] =
-   token
-   lex
-   parser
-   parsetest
-
-MyOCamlProgram(parser_test, $(FILES1))
+# NO_INSTALL=true
+# 
+# FILES0[] =
+#    token
+#    lex
+#    lextest
+# 
+# MyOCamlProgram(lexer_test, $(FILES0))
+# 
+# 
+# FILES1[] =
+#    token
+#    lex
+#    parser
+#    parsetest
+# 
+# MyOCamlProgram(parser_test, $(FILES1))

parsing/parser.ml

 open Spotlib.Spot
 open Planck
-open Sexplib.Conv
 
 module Stream = Stoken.Make(Token)(Position.Region)(Pfile.Parser)
 
   let with_region x = with_region Position.Region.merge x
 end
 
+open Olf
+open Olf.Expr
+
+open Token
 open Parser
-open Token
-
-type constant = 
-  | CInt of int
-  | CString of string
-
-and patt = 
-  | PVar of string
-
-and expr = 
-  | EVar of string
-  | EConst of constant
-  | EApp of expr * expr list
-  | EFun of patt list * expr
-
-and top = 
-  | TopLet of patt * expr
-
-with sexp
-
-let patt = token_option (function LIDENT s -> Some (PVar s) | _ -> None)
 
 let constant = 
   token_option (function 
-    | INT n -> Some (EConst (CInt n))
-    | STRING s -> Some (EConst (CString s))
+    | INT n -> Some (Const (Const.Int n))
+    | STRING s -> Some (Const (Const.String s))
     | _ -> None)
-  
+
+(* CR jfuruse: it is silly to create a function for each ... *)
+let uident : string t = token_option (function UIDENT s -> Some s | _ -> None)
+let lident : string t = token_option (function LIDENT s -> Some s | _ -> None) 
+let in_parens : 'a t -> 'a t = fun x -> surrounded (token LPAREN) (token RPAREN) x
+
+let patt : pattern t = lident >>= fun s -> return & PVar s
+
+let path : Rawname.Path.t t = fun x -> begin 
+  let open Rawname in 
+  perform
+    uids <-- ?** (perform uid <-- uident; token DOT; return uid);
+    lid <-- lident;
+    let rec make_path = function
+      | [] -> assert false
+      | [x] -> Path.Ident x
+      | x::xs -> Path.Dot (make_path xs, x)
+    in
+    return (make_path (lid :: List.rev uids))
+end x
+
+let op = 
+  let open Rawname in
+  tokenp (function 
+    | INFIXOP0 s
+    | INFIXOP1 s
+    | INFIXOP2 s
+    | INFIXOP3 s
+    | INFIXOP4 s
+    | PREFIXOP s -> Some (Path.Ident s)
+    | _ -> None)
+
 let rec simple_expr x = begin
 
   constant 
 
-  <|> token_option (function 
-    | LIDENT s -> Some (EVar s) 
-    | _ -> None)
+  <|> (path >>= fun p -> return & Var p)
 
-  <|> surrounded (token LPAREN) (token RPAREN) expr
+  <|> (in_parens expr)
 
 end x
 
 and application x = begin perform
-  e <-- simple_expr;
-  es <-- ?** simple_expr;
-  return & match es with [] -> e | _ -> EApp (e, es)
+    es <-- ?++ ( (op >>= return & `Op p) <|> (simple_expr >>= return & `Ex e) );
+    return & App es
 end x
 
 and fun_ x = begin perform
   ps <-- ?++ patt;
   token MINUSGREATER;
   e <-- expr;
-  return (EFun (ps, e))
+  return & Abs (ps, e)
 end x
 
 and expr x = begin
 
   fun_
   <|> application
+  <!> simple_expr
 
 end x
   
   p <-- patt;
   token EQUAL;
   e <-- expr;
-  return & TopLet (p,e)
+  return & Top.Let (p,e)
 
 let top = top_let