interpret.ml 13.3 KB
Newer Older
1 2 3 4 5 6 7
(* This module is in charge of handling the [--interpret] option,
   if it is present. *)

module I = Invariant (* artificial dependency; ensures that [Invariant] runs first *)

(* --------------------------------------------------------------------------- *)

8 9 10 11 12 13 14 15 16 17 18
(* The following definitions are in sync with [SentenceParser]. *)

open Grammar
type terminals = Terminal.t list
type sentence = Nonterminal.t option * terminals
type located_sentence = Positions.positions * sentence
type message = string

(* A run is a series of sentences together with an error message. *)

type run = located_sentence list * message
19

20 21
(* A targeted sentence is a located sentence together with the state
   into which it leads. *)
22

23 24 25 26 27 28 29 30
type targeted_sentence = located_sentence * Lr1.node

(* A targeted run is a series of targeted sentences together with an error
   message. *)

type targeted_run = targeted_sentence list * message

(* --------------------------------------------------------------------------- *)
POTTIER Francois's avatar
POTTIER Francois committed
31

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
(* Debugging.

let print_sentence (nto, terminals) : string =
  let b = Buffer.create 128 in
  Option.iter (fun nt ->
    Printf.bprintf b "%s: " (Nonterminal.print true nt)
  ) nto;
  List.iter (fun t ->
    Printf.bprintf b "%s " (Terminal.print t)
  ) terminals;
  Printf.bprintf b "\n";
  Buffer.contents b

let print_sentence sentence : unit =
  print_string (print_sentence sentence)

let print_located_sentence (_, sentence) : unit =
  print_sentence sentence

*)

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
(* --------------------------------------------------------------------------- *)

(* [stream] turns a finite list of terminals into a stream of terminals. *)

exception EndOfStream

let stream (toks : Terminal.t list) : unit -> Terminal.t * Lexing.position * Lexing.position =
  let toks = ref toks in
  fun () ->

    let tok =
      match !toks with
      | tok :: more ->

	  (* Take a token off the list, and return it. *)

	  toks := more;
	  tok

      | [] ->

	  (* The finite list has been exhausted. Here, two plausible behaviors
	     come to mind.

	     The first behavior consists in raising an exception. In that case,
	     we are creating a finite stream, and it is up to the parser to not
	     read past its end.

	     The second behavior consists in returning a designated token. In
	     that case, we are creating an infinite, eventually constant,
	     stream.

	     The choice between these two behaviors is somewhat arbitrary;
	     furthermore, in the second case, the choice of the designated
	     token is arbitrary as well. Here, we adopt the second behavior if
	     and only if the grammar has an EOF token, and we use EOF as the
	     designated token. Again, this is arbitrary, and could be changed
	     in the future. *)

	  match Terminal.eof with
	  | Some eof ->
	      eof
	  | None ->
	      raise EndOfStream

    in

    (* For now, return dummy positions. *)

    tok, Lexing.dummy_pos, Lexing.dummy_pos

(* --------------------------------------------------------------------------- *)

106 107 108 109 110 111 112
(* [start sentence] returns the start symbol that we should use to interpret
   the sentence [sentence]. *)

(* If a start symbol was explicitly provided as part of the sentence, we use
   it. Otherwise, we use the grammar's unique start symbol, if there is
   one. *)

113
let start poss ((nto, _) : sentence) : Nonterminal.t =
114 115 116 117 118 119
  match nto with
  | Some nt ->
      nt
  | None ->
      match ProductionMap.is_singleton Lr1.entry with
      | None ->
120
          Error.error poss
121 122 123 124 125 126 127 128 129
            "Because the grammar has multiple start symbols, each of the\n\
             sentences provided on the standard input channel must be of the\n\
             form: <start symbol>: <token>*"
      | Some (prod, _) ->
          match Production.classify prod with
          | Some nt ->
              nt
          | None ->
              assert false
130

131
(* --------------------------------------------------------------------------- *)
132

133
(* [interpret] interprets a sentence. *)
134

135
let interpret ((_, toks) as sentence) : unit =
136

137
  let nt = start [] sentence in
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180

  (* Run the reference interpreter. This can produce a concrete syntax tree
     ([Some cst]), fail with a parser error ([None]), or fail with a lexer error
     ([EndOfStream]). *)

  (* In either case, we produce just one line of output, so it should be clear
     to the user which outcomes correspond to which sentences (should multiple
     sentences be supplied). *)

  begin try
    match
      MenhirLib.Convert.Simplified.traditional2revised
	(ReferenceInterpreter.interpret Settings.trace nt)
	(stream toks)
    with

    | Some cst ->

	(* Success. *)

	Printf.printf "ACCEPT";
	if Settings.interpret_show_cst then begin
	  print_newline();
	  Cst.show stdout cst
	end

    | None ->

	(* Parser failure. *)

	Printf.printf "REJECT"

  with EndOfStream ->

    (* Lexer failure. *)
    
    Printf.printf "OVERSHOOT"

  end;
  print_newline()

(* --------------------------------------------------------------------------- *)

181 182
(* [interpret_error_aux] interprets a sentence, expecting it to end in an
   error. Failure or success is reported via two continuations. *)
183

184 185
let interpret_error_aux poss ((_, terminals) as sentence) fail succeed =
  let nt = start poss sentence in
186
  let open ReferenceInterpreter in
187
  match check_error_path nt terminals with
188
  | OInputReadPastEnd ->
189
      fail "No syntax error occurs."
190
  | OInputNotFullyConsumed ->
191
      fail "A syntax error occurs before the last token is reached."
192
  | OUnexpectedAccept ->
193
      fail "No syntax error occurs; in fact, this input is accepted."
194
  | OK state ->
195
      succeed state
196

197
(* --------------------------------------------------------------------------- *)
198

199 200 201 202 203 204 205 206 207
(* [interpret_error] interprets a sentence, expecting it to end in an error.
   Failure or success is reported on the standard output channel. This is
   used by [--interpret-error]. *)

let fail msg =
  Printf.printf "BAD\n# %s\n%!" msg;
  exit 1

let succeed s =
208
  let s = Lr1.number s in
209 210 211 212 213 214 215 216 217 218
  Printf.printf
    "OK %d\n# This sentence ends with a syntax error in state %d.\n%!"
    s s;
  exit 0

let interpret_error sentence =
  interpret_error_aux [] sentence fail succeed

(* --------------------------------------------------------------------------- *)

219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
(* [target_sentence] interprets a (located) sentence, expecting it to end in
   an error, computes the state in which the error is obtained, and constructs
   a targeted sentence. *)

let fail poss msg =
  Error.signal poss (Printf.sprintf
    "This sentence does not end with a syntax error, as desired.\n%s"
    msg
  );
  [] (* dummy result *)

let target_sentence : located_sentence -> targeted_sentence list =
  fun (poss, sentence) ->
    interpret_error_aux poss sentence
      (fail poss)
      (fun s -> [ (poss, sentence), s ])

let target_run : run -> targeted_run =
  fun (sentences, message) ->
    List.flatten (List.map target_sentence sentences), message

let target_runs : run list -> targeted_run list =
  fun runs ->
    let runs = List.map target_run runs in
    if Error.errors() then exit 1;
    runs
245

246
(* --------------------------------------------------------------------------- *)
247

248 249
(* [setup()] returns a function [read] which reads one sentence from the
   standard input channel. *)
250

251
let setup () : unit -> sentence option =
252

253 254 255
  let open Lexing in
  let lexbuf = from_channel stdin in
  lexbuf.lex_curr_p <- { lexbuf.lex_curr_p with pos_fname = "(stdin)" };
256

257 258
  let read () =
    try
259
      SentenceParser.optional_sentence SentenceLexer.lex lexbuf
260 261 262
    with Parsing.Parse_error ->
      Error.error (Positions.lexbuf lexbuf) "Ill-formed input sentence."
  in
263

264 265 266 267 268 269
  read

(* --------------------------------------------------------------------------- *)

(* If [--interpret] is set, interpret the sentences found on the standard
   input channel, then stop, without generating a parser. *)
270

271 272 273 274 275 276 277
(* We read a series of sentences from the standard input channel. To allow
   interactive use, we interpret each sentence as soon as it is read. *)

let () =
  if Settings.interpret then
    let read = setup() in
    while true do
278 279
      match read() with
      | None ->
280
  	  exit 0
281
      | Some sentence ->
282 283
	  interpret sentence
    done
284

285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
(* --------------------------------------------------------------------------- *)

(* If [--interpret-error] is set, interpret one sentence found on the standard
   input channel, then stop, without generating a parser. *)

(* We read just one sentence, confirm that this sentence ends in an error, and
   (if that is the case) display the number of the state that is reached. *)

let () =
  if Settings.interpret_error then
    let read = setup() in
    match read() with
    | None ->
      exit 1 (* abnormal: no input *)
    | Some sentence ->
        interpret_error sentence (* never returns *)
301

302 303
(* --------------------------------------------------------------------------- *)

304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
(* Reading a [.messages] file. *)

let read_messages filename : run list =
  (* Read and segment the file. *)
  let segments : (string * Lexing.lexbuf) list = Segment.segment filename in
  (* Process the segments, two by two. We expect one segment to contain
     a non-empty series of sentences, and the next segment to contain
     free-form text. *)
  let rec loop accu segments =
    match segments with
    | [] ->
        List.rev accu
    | (_, lexbuf) :: [] ->
        (* Oops, we are desynchronized. *)
        Error.signal
          (Positions.one (Lexing.lexeme_end_p lexbuf))
          "Syntax error: missing a final message. I may be desynchronized.";
        List.rev accu
    | (_, lexbuf) :: (text, _) :: segments ->
        (* Read a non-empty series of located sentences. *)
        match SentenceParser.entry SentenceLexer.lex lexbuf with
        | exception Parsing.Parse_error ->
            (* Report an error. *)
            Error.signal
              (Positions.one (Lexing.lexeme_start_p lexbuf))
              "Syntax error: ill-formed sentence.";
            (* Continue anyway. *)
            loop accu segments
        | sentences ->
            loop ((sentences, text) :: accu) segments
  in
  let runs = loop [] segments in
  if Error.errors() then exit 1;
  (* Although we try to report several errors, [SentenceLexer.lex] may
     abort the whole process after just one error. This could be improved. *)
  runs

(* --------------------------------------------------------------------------- *)

343 344 345 346 347
(* If [--compile-errors <filename>] is set, compile the error message
   descriptions found in file [filename] down to OCaml code, then stop. *)

let () =
  Settings.compile_errors |> Option.iter (fun filename ->
348

349 350
    (* Read the file. *)
    let runs = read_messages filename in
351

352 353
    (* Convert every sentence to a state number. We signal an error if a
       sentence does not end in an error, as expected. *)
354
    let runs = target_runs runs in
355 356 357

    (* Build a mapping of states to located sentences. This allows us to
       detect if two sentences lead to the same state. *)
POTTIER Francois's avatar
POTTIER Francois committed
358
    let (_ : located_sentence Lr1.NodeMap.t) =
359 360 361 362 363 364 365 366 367 368 369 370
      List.fold_left (fun mapping (sentences_and_states, _message) ->
        List.fold_left (fun mapping (sentence2, s) ->
          match Lr1.NodeMap.find s mapping with
          | sentence1 ->
              Error.signal (fst sentence1 @ fst sentence2)
                (Printf.sprintf
                   "Redundancy: these sentences both cause an error in state %d."
                   (Lr1.number s));
              mapping
          | exception Not_found ->
              Lr1.NodeMap.add s sentence2 mapping
        ) mapping sentences_and_states
371
      ) Lr1.NodeMap.empty runs
372 373 374
    in
    if Error.errors() then exit 1;

POTTIER Francois's avatar
POTTIER Francois committed
375 376 377 378 379 380 381
    (* In principle, we would like to check whether this set of sentences
       is complete (i.e., covers all states where an error can arise), but
       this is costly -- it requires running [LRijkstra]. Instead, we will
       probably offer a separate facility for comparing two [.messages]
       files, one of which can be produced via [--list-errors]. This will
       ensure completeness. *)

382 383 384 385
    (* Now, compile this information down to OCaml code. We wish to
       produce a function that maps a state number to a message. By
       convention, we call this function [message]. *)

POTTIER Francois's avatar
POTTIER Francois committed
386 387
    let name = "message" in

388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
    let open IL in
    let open CodeBits in
    let default = {
      branchpat  = PWildcard;
      branchbody = eraisenotfound
        (* The default branch raises an exception, which can be caught by
           the user, who can then produce a generic error message. *)
    } in
    let branches =
      List.fold_left (fun branches (sentences_and_states, message) ->
        (* Create an or-pattern for these states. *)
        let states = List.map (fun (_, s) ->
          pint (Lr1.number s)
        ) sentences_and_states in
        (* Map all these states to this message. *)
        { branchpat = POr states;
          branchbody = EStringConst message } :: branches
405
      ) [ default ] runs
406 407 408
    in
    let messagedef = {
      valpublic = true;
POTTIER Francois's avatar
POTTIER Francois committed
409
      valpat = PVar name;
410 411
      valval = EFun ([ PVar "s" ], EMatch (EVar "s", branches))
    } in
POTTIER Francois's avatar
POTTIER Francois committed
412 413 414 415 416 417 418
    let program = [
      SIComment (Printf.sprintf
                   "This file was auto-generated based on \"%s\"." filename);
      SIComment (Printf.sprintf
                   "Please note that the function [%s] can raise [Not_found]." name);
      SIValDefs (false, [ messagedef ])
    ] in
419 420 421 422 423 424 425 426 427

    (* Write this program to the standard output channel. *)

    let module P = Printer.Make (struct
      let f = stdout
      let locate_stretches = None
    end) in
    P.program program;

428 429 430
    exit 0
  )