Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
POTTIER Francois
menhir
Commits
6d1bd410
Commit
6d1bd410
authored
Mar 23, 2017
by
POTTIER Francois
Browse files
New function [Printer.string_of_expr].
parent
89d6447c
Changes
2
Hide whitespace changes
Inline
Side-by-side
src/printer.ml
View file @
6d1bd410
(* A pretty-printer for [IL]. *)
open
IL
open
Printf
module
Make
(
X
:
sig
module
PreliminaryMake
(
X
:
sig
(* We assume that the following types and functions are given. This
allows us to work both with buffers of type [Buffer.t] and with
output channels of type [out_channel]. *)
type
channel
val
fprintf
:
channel
->
(
'
a
,
channel
,
unit
)
format
->
'
a
val
output_substring
:
channel
->
string
->
int
->
int
->
unit
(* This is the channel that is being written to. *)
val
f
:
out_
channel
val
f
:
channel
(* This controls the way we print OCaml stretches (types and
semantic actions). We either surround them with #line directives
...
...
@@ -20,6 +27,16 @@ module Make (X : sig
val
locate_stretches
:
string
option
end
)
=
struct
open
X
let
output_char
f
c
=
fprintf
f
"%c"
c
let
output_string
f
s
=
fprintf
f
"%s"
s
let
flush
f
=
fprintf
f
"%!"
(* ------------------------------------------------------------------------- *)
(* Dealing with newlines and indentation. *)
...
...
@@ -731,6 +748,36 @@ let expr e =
end
(* ------------------------------------------------------------------------- *)
(* Instantiation with output channels. *)
module
Make
(
X
:
sig
val
f
:
out_channel
val
locate_stretches
:
string
option
end
)
=
struct
include
PreliminaryMake
(
struct
type
channel
=
out_channel
include
X
let
fprintf
=
Printf
.
fprintf
let
output_substring
=
output_substring
end
)
end
(* ------------------------------------------------------------------------- *)
(* Instantiation with buffers. *)
module
MakeBuffered
(
X
:
sig
val
f
:
Buffer
.
t
val
locate_stretches
:
string
option
end
)
=
struct
include
PreliminaryMake
(
struct
type
channel
=
Buffer
.
t
include
X
let
fprintf
=
Printf
.
bprintf
let
output_substring
=
Buffer
.
add_substring
end
)
end
(* ------------------------------------------------------------------------- *)
(* Common instantiations. *)
...
...
@@ -742,3 +789,14 @@ let print_expr f e =
end
)
in
P
.
expr
e
let
string_of_expr
e
=
let
b
=
Buffer
.
create
512
in
let
module
P
=
MakeBuffered
(
struct
let
f
=
b
let
locate_stretches
=
None
end
)
in
P
.
expr
e
;
Buffer
.
contents
b
src/printer.mli
View file @
6d1bd410
...
...
@@ -25,6 +25,8 @@ end) : sig
end
(* Common instantiations. *)
(* Common instantiations. In the following two functions, [locate_stretches]
is [None], so no #line directives are printed. *)
val
print_expr
:
out_channel
->
IL
.
expr
->
unit
(* no #line directives *)
val
print_expr
:
out_channel
->
IL
.
expr
->
unit
val
string_of_expr
:
IL
.
expr
->
string
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment