Commit b7fac4ab authored by POTTIER Francois's avatar POTTIER Francois

Cleanup in calc-inspection. Isolated two more files.

parent e12cc76d
open Parser
open Parser.MenhirInterpreter
(* In order to submit artificial tokens to the parser, we need a function that
converts a terminal symbol to a (dummy) token. Unfortunately, we cannot (in
general) auto-generate this code, because it requires making up semantic
values of arbitrary OCaml types. *)
let terminal2token (type a) (symbol : a terminal) : token =
match symbol with
| T_TIMES ->
TIMES
| T_RPAREN ->
RPAREN
| T_PLUS ->
PLUS
| T_MINUS ->
MINUS
| T_LPAREN ->
LPAREN
| T_INT ->
INT 0
| T_EOL ->
EOL
| T_DIV ->
DIV
| T_error ->
assert false
open Parser.MenhirInterpreter
(* This module offers the functionality required by the functor
[ErrorReporting.Printers.Make]. *)
val terminal2token: _ terminal -> token
open Parser.MenhirInterpreter
(* In order to print syntax error messages and/or debugging information, we
need a symbol printer. *)
let print_symbol symbol : string =
match symbol with
| X (T T_TIMES) ->
"*"
| X (T T_RPAREN) ->
")"
| X (T T_PLUS) ->
"+"
| X (T T_MINUS) ->
"-"
| X (T T_LPAREN) ->
"("
| X (T T_INT) ->
"INT"
| X (N N_expr) ->
"expr"
| X (N N_main) ->
"main"
| X (T T_EOL) ->
"EOL"
| X (T T_DIV) ->
"/"
| X (T T_error) ->
"error"
(* In order to print a view of the stack that includes semantic values,
we need an element printer. (If we don't need this feature, then
[print_symbol] above suffices.) *)
let print_element e : string =
match e with
| Element (s, v, _, _) ->
match incoming_symbol s with
| T T_TIMES ->
"*"
| T T_RPAREN ->
")"
| T T_PLUS ->
"+"
| T T_MINUS ->
"-"
| T T_LPAREN ->
"("
| T T_INT ->
string_of_int v
| N N_expr ->
string_of_int v
| N N_main ->
string_of_int v
| T T_EOL ->
""
| T T_DIV ->
"/"
| T T_error ->
"error"
(* The public functions. *)
let print = prerr_string
let print_symbol s = print (print_symbol s)
let print_element = Some (fun s -> print (print_element s))
open Parser.MenhirInterpreter
(* This module offers the functionality required by the functor
[MenhirLib.Printers.Make]. *)
val print: string -> unit
val print_symbol: xsymbol -> unit
val print_element: (element -> unit) option
open MenhirLib.General
open Parser.MenhirInterpreter
(* --------------------------------------------------------------------------- *)
(* In order to submit artificial tokens to the parser, we need a function that
converts a terminal symbol to a (dummy) token. Unfortunately, we cannot (in
general) auto-generate this code, because it requires making up semantic
values of arbitrary OCaml types. *)
let terminal2token (type a) (symbol : a terminal) : token =
let open Parser in
match symbol with
| T_TIMES ->
TIMES
| T_RPAREN ->
RPAREN
| T_PLUS ->
PLUS
| T_MINUS ->
MINUS
| T_LPAREN ->
LPAREN
| T_INT ->
INT 0
| T_EOL ->
EOL
| T_DIV ->
DIV
| T_error ->
assert false
(* In order to print syntax error messages and/or debugging information, we
need a symbol printer. *)
let print_symbol symbol =
match symbol with
| X (T T_TIMES) ->
"*"
| X (T T_RPAREN) ->
")"
| X (T T_PLUS) ->
"+"
| X (T T_MINUS) ->
"-"
| X (T T_LPAREN) ->
"("
| X (T T_INT) ->
"INT"
| X (N N_expr) ->
"expr"
| X (N N_main) ->
"main"
| X (T T_EOL) ->
"EOL"
| X (T T_DIV) ->
"/"
| X (T T_error) ->
"error"
(* In order to print a view of the stack that includes semantic values,
we need an element printer. (If we don't need this feature, then
[print_symbol] above suffices.) *)
let print_element e : string =
match e with
| Element (s, v, _, _) ->
match incoming_symbol s with
| T T_TIMES ->
"*"
| T T_RPAREN ->
")"
| T T_PLUS ->
"+"
| T T_MINUS ->
"-"
| T T_LPAREN ->
"("
| T T_INT ->
string_of_int v
| N N_expr ->
string_of_int v
| N N_main ->
string_of_int v
| T T_EOL ->
""
| T T_DIV ->
"/"
| T T_error ->
"error"
(* --------------------------------------------------------------------------- *)
(* TEMPORARY comment *)
(* Instantiate [MenhirLib.Printers] for our parser. This requires providing a
few printing functions -- see [CalcPrinters]. *)
module P =
MenhirLib.Printers.Make (Parser.MenhirInterpreter) (struct
let print s = Printf.fprintf stderr "%s" s
let print_symbol s = print (print_symbol s)
let print_element = Some (fun s -> print (print_element s))
end)
MenhirLib.Printers.Make
(Parser.MenhirInterpreter)
(CalcPrinters)
(* Instantiate [MenhirLib.ErrorReporting] for our parser. This requires
providing a few functions -- see [CalcErrorReporting]. *)
module E =
MenhirLib.ErrorReporting.Make(Parser.MenhirInterpreter) (struct
let terminal2token = terminal2token
end)
MenhirLib.ErrorReporting.Make
(Parser.MenhirInterpreter)
(CalcErrorReporting)
let print_explanation explanation =
(* TEMPORARY not satisfactory at all *)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment