front.ml 6.44 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
(******************************************************************************)
(*                                                                            *)
(*                                   Menhir                                   *)
(*                                                                            *)
(*                       François Pottier, Inria Paris                        *)
(*              Yann Régis-Gianas, PPS, Université Paris Diderot              *)
(*                                                                            *)
(*  Copyright Inria. All rights reserved. This file is distributed under the  *)
(*  terms of the GNU General Public License version 2, as described in the    *)
(*  file LICENSE.                                                             *)
(*                                                                            *)
(******************************************************************************)

14
(* The front-end. This module performs a series of toplevel side effects. *)
15

16 17 18 19 20 21 22
(* ------------------------------------------------------------------------- *)

(* Reading a grammar from a file. *)

let load_partial_grammar filename =
  let validExt = if Settings.coq then ".vy" else ".mly" in
  if not (Filename.check_suffix filename validExt) then
23
    Error.error []
24
      "argument file names should end in %s. \"%s\" is not accepted."
25
      validExt filename;
26
  InputFile.new_input_file filename;
27 28 29
  try

    let contents = IO.read_whole_file filename in
30 31 32 33 34 35 36 37
    InputFile.with_file_contents contents (fun () ->
      let open Lexing in
      let lexbuf = Lexing.from_string contents in
      lexbuf.lex_curr_p <- { lexbuf.lex_curr_p with pos_fname = filename };
      (* the grammar: *)
      { (Driver.grammar Lexer.main lexbuf)
        with Syntax.pg_filename = filename }
    )
38 39

  with Sys_error msg ->
40
    Error.error [] "%s" msg
41 42 43 44 45

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

(* Read all of the grammar files that are named on the command line. *)

46
let partial_grammars =
47 48 49 50 51 52 53
  List.map load_partial_grammar Settings.filenames

let () =
  Time.tick "Lexing and parsing"

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

54 55 56 57 58 59 60
(* Eliminate anonymous rules. *)

let partial_grammars =
  List.map Anonymous.transform_partial_grammar partial_grammars

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

61 62
(* If several grammar files were specified, merge them. *)

63
let parameterized_grammar =
64 65 66 67 68 69 70
  PartialGrammar.join_partial_grammars partial_grammars

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

(* Expand away all applications of parameterized nonterminal symbols, so as to
   obtain a grammar without parameterized nonterminal symbols. *)

71
let grammar =
72 73 74 75 76 77
  ParameterizedGrammar.expand parameterized_grammar

let () =
  Time.tick "Joining and expanding"

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

79 80 81 82
(* If [--only-tokens] was specified on the command line, produce
   the definition of the [token] type and stop. *)

let () =
83
  TokenType.produce_tokentypes grammar
84

85 86
(* ------------------------------------------------------------------------- *)

87 88 89 90 91 92 93 94
(* Perform reachability analysis. *)

let grammar =
  Reachability.trim grammar

let () =
  Time.tick "Trimming"

95 96
(* ------------------------------------------------------------------------- *)

97 98
(* If [--depend] or [--raw-depend] was specified on the command line,
   perform dependency analysis and stop. *)
99 100 101 102 103 104 105 106 107

let () =
  match Settings.depend with
  | Settings.OMRaw
  | Settings.OMPostprocess ->
      Infer.depend grammar (* never returns *)
  | Settings.OMNone ->
      ()

108 109 110
(* The purpose of [--depend] and [--raw-depend] is to support [--infer].
   Indeed, [--infer] is implemented by producing a mock [.ml] file (which
   contains just the semantic actions) and invoking [ocamlc]. This requires
111 112 113 114 115
   certain [.cmi] files to exist. So, [--(raw-)depend] is a way for us to
   announce which [.cmi] files we need. It is implemented by producing the
   mock [.ml] file and running [ocamldep] on it. We also produce a mock
   [.mli] file, even though in principle it should be unnecessary -- see
   comment in [nonterminalType.mli]. *)
116

117 118
(* ------------------------------------------------------------------------- *)

119 120 121 122 123 124 125 126
(* If some flags imply that we will NOT produce an OCaml parser, then there
   is no need to perform type inference, so we act as if --infer was absent.
   This saves time and dependency nightmares. *)

let skipping_parser_generation =
  Settings.coq ||
  Settings.compile_errors <> None ||
  Settings.interpret_error ||
127
  Settings.list_errors ||
128 129 130 131
  Settings.compare_errors <> None ||
  Settings.update_errors <> None ||
  Settings.echo_errors <> None ||
  false
132 133 134 135
    (* maybe also: [preprocess_mode <> PMNormal] *)

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

136 137
(* If [--infer] was specified on the command line, perform type inference.
   The OCaml type of every nonterminal is then known. *)
138 139

let grammar =
140
  if Settings.infer && not skipping_parser_generation then
141 142 143 144 145 146
    let grammar = Infer.infer grammar in
    Time.tick "Inferring types for nonterminals";
    grammar
  else
    grammar

147
(* ------------------------------------------------------------------------- *)
148 149 150 151 152 153 154

(* Expand away some of the position keywords. *)

let grammar =
  KeywordExpansion.expand_grammar grammar

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

156 157 158 159 160
(* If [--no-inline] was specified on the command line, skip the
   inlining of non terminal definitions marked with %inline. *)

let grammar =
  if Settings.inline then begin
161
    let grammar, inlined =
162 163
      NonTerminalDefinitionInlining.inline grammar
    in
164
    if not Settings.infer && inlined && not skipping_parser_generation then
165
      Error.warning []
166 167
        "you are using the standard library and/or the %%inline keyword. We\n\
         recommend switching on --infer in order to avoid obscure type error messages.";
168 169 170
    Time.tick "Inlining";
    grammar
  end
171
  else
172 173
    grammar

174 175
(* ------------------------------------------------------------------------- *)

176 177 178 179 180 181 182 183 184 185
(* If [--only-preprocess] or [--only-preprocess-drop] was specified on the
   command line, print the grammar and stop. Otherwise, continue. *)

let () =
  match Settings.preprocess_mode with
  | Settings.PMOnlyPreprocess mode ->
      UnparameterizedPrinter.print mode stdout grammar;
      exit 0
  | Settings.PMNormal ->
      ()