1

I'm working through http://dev.realworldocaml.org/records.html and am having a problem with require

#require "re.posix";;

(* open Base ;;
open Core ;; *)

(* type <record-name> =
  {
    <field>: <type>;
    <field>: <type>;
  } *)

  type service_info =
    {
      service_name : string ;
      port : int ;
      protocol : string ;
    }
;;

(* #require "re.postix";; *)

let service_info_of_string line =
  let matches =
    Re.exec (Re.Posix.compile_pat "([a-zA-Z]+)[\t]+([0-9]+)/([a-zA-Z]+)") line
  in
  {
    service_name = Re.get matches 1;
    port = Int.of_string (Re.get matches 2);
    protocol = Re.get matches 3;
  }
;;

print_endline "foobar"

This is the output from me calling dune build

dune build hello_world.exe 
         ppx hello_world.pp.ml (exit 1)
(cd _build/default && .ppx/jbuild/ppx_jane/ppx.exe -o hello_world.pp.ml --impl hello_world.ml --dump-ast)
File "hello_world.ml", line 385, characters 0-1:
Error: Syntax error

Here is the jbuild

(executable
 ((name hello_world)
 (libraries (core))
 (preprocess (pps (ppx_jane)))))

I found that "#use "topfind" works if I compile with ocamlc. But havent been able to make it work with dune.

Tips appreciated.thanks

glennsl
  • 28,186
  • 12
  • 57
  • 75
LeftN
  • 37
  • 1
  • 5
  • it works on my side, provided that you add "re" in the libraries statement, remove the #require stmt, and uncomment 'open Core'. – Pierre G. Oct 23 '18 at 16:56

1 Answers1

0

The following fixes shall make your example works :

open Core ;; (* in particular, it makes Int visible *)


type service_info =
    {
      service_name : string ;
      port : int ;
      protocol : string ;
    }
;;

(* #require "re.postix";; *)

let service_info_of_string line =
  let matches =
    Re.exec (Re.Posix.compile_pat "([a-zA-Z]+)[\t]+([0-9]+)/([a-zA-Z]+)") line
  in
  {
    service_name = Re.get matches 1;
    port = Int.of_string (Re.get matches 2);
    protocol = Re.get matches 3;
  }
;;

print_endline "foobar"

And jbuild :

(executable
 ((name hello_world)
 (libraries (core re))
 (preprocess (pps (ppx_jane)))))
Pierre G.
  • 4,346
  • 1
  • 12
  • 25