cmly_write.ml 5.89 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
open BasicSyntax
15 16 17
open Grammar
open Cmly_format

18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
let raw_content stretch =
  stretch.Stretch.stretch_raw_content

let ocamltype (typ : Stretch.ocamltype) : ocamltype =
  match typ with
  | Stretch.Declared stretch ->
      raw_content stretch
  | Stretch.Inferred typ ->
      typ

let ocamltype (typo : Stretch.ocamltype option) : ocamltype option =
  match typo with
  | None ->
      None
  | Some typ ->
      Some (ocamltype typ)

35 36 37 38 39 40 41 42 43
let range (pos : Positions.t) : range =
  {
    r_start = Positions.start_of_position pos;
    r_end   = Positions.end_of_position pos;
  }

let ranges =
  List.map range

44 45 46 47
let attribute (label, payload : Syntax.attribute) : attribute =
  {
    a_label    = Positions.value label;
    a_payload  = raw_content payload;
48
    a_position = range (Positions.position label);
49 50 51 52 53
  }

let attributes : Syntax.attributes -> attributes =
  List.map attribute

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
let terminal (t : Terminal.t) : terminal_def =
  {
    t_kind = (
      if Terminal.equal t Terminal.error then
        `ERROR
      else if
        (match Terminal.eof with
         | None -> false
         | Some eof -> Terminal.equal t eof) then
        `EOF
      else if Terminal.pseudo t then
        `PSEUDO
      else
        `REGULAR
    );
    t_name = Terminal.print t;
70 71
    t_type = ocamltype (Terminal.ocamltype t);
    t_attributes = attributes (Terminal.attributes t);
72 73 74 75 76 77 78 79
  }

let nonterminal (nt : Nonterminal.t) : nonterminal_def =
  let is_start = Nonterminal.is_start nt in
  {
    n_kind = if is_start then `START else `REGULAR;
    n_name = Nonterminal.print false nt;
    n_mangled_name = Nonterminal.print true nt;
80
    n_type = if is_start then None else ocamltype (Nonterminal.ocamltype nt);
81
    n_positions = if is_start then [] else ranges (Nonterminal.positions nt);
82
    n_nullable = Analysis.nullable nt;
83
    n_first = List.map Terminal.t2i (TerminalSet.elements (Analysis.first nt));
84
    n_attributes = if is_start then [] else attributes (Nonterminal.attributes nt);
85 86 87 88 89 90 91 92 93
  }

let symbol (sym : Symbol.t) : symbol =
  match sym with
  | Symbol.N n -> N (Nonterminal.n2i n)
  | Symbol.T t -> T (Terminal.t2i t)

let action (a : Action.t) : action =
  {
94
    a_expr = Printer.string_of_expr (Action.to_il_expr a);
95 96 97 98 99 100 101 102 103 104
    a_keywords = Keyword.KeywordSet.elements (Action.keywords a);
  }

let rhs (prod : Production.index) : producer_def array =
  match Production.classify prod with
  | Some n ->
      [| (N (Nonterminal.n2i n), "", []) |]
  | None ->
      Array.mapi (fun i sym ->
        let id = (Production.identifiers prod).(i) in
105 106
        let attrs = attributes (Production.rhs_attributes prod).(i) in
        symbol sym, id, attrs
107 108 109 110 111 112 113
      ) (Production.rhs prod)

let production (prod : Production.index) : production_def =
  {
    p_kind = if Production.is_start prod then `START else `REGULAR;
    p_lhs = Nonterminal.n2i (Production.nt prod);
    p_rhs = rhs prod;
114
    p_positions = ranges (Production.positions prod);
115 116
    p_action = if Production.is_start prod then None
               else Some (action (Production.action prod));
117
    p_attributes = attributes (Production.lhs_attributes prod);
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
  }

let item (i : Item.t) : production * int =
  let p, i = Item.export i in
  (Production.p2i p, i)

let itemset (is : Item.Set.t) : (production * int) list =
  List.map item (Item.Set.elements is)

let lr0_state (node : Lr0.node) : lr0_state_def =
  {
    lr0_incoming = Option.map symbol (Lr0.incoming_symbol node);
    lr0_items = itemset (Lr0.items node)
  }

let transition (sym, node) : symbol * lr1 =
  (symbol sym, Lr1.number node)

let lr1_state (node : Lr1.node) : lr1_state_def =
  {
    lr1_lr0 = Lr0.core (Lr1.state node);
    lr1_transitions =
      List.map transition (SymbolMap.bindings (Lr1.transitions node));
    lr1_reductions =
      let add t ps rs = (Terminal.t2i t, List.map Production.p2i ps) :: rs in
      TerminalMap.fold_rev add (Lr1.reductions node) []
  }

146 147
let entry_point prod node nt _typ accu : (nonterminal * production * lr1) list =
  (Nonterminal.n2i nt, Production.p2i prod, Lr1.number node) :: accu
148 149 150 151

let encode () : grammar =
  {
    g_basename     = Settings.base;
152 153
    g_preludes     = List.map raw_content Front.grammar.preludes;
    g_postludes    = List.map raw_content Front.grammar.postludes;
154 155 156 157 158
    g_terminals    = Terminal.init terminal;
    g_nonterminals = Nonterminal.init nonterminal;
    g_productions  = Production.init production;
    g_lr0_states   = Array.init Lr0.n lr0_state;
    g_lr1_states   = Array.of_list (Lr1.map lr1_state);
159
    g_entry_points = Lr1.fold_entry entry_point [];
160
    g_attributes   = attributes Analysis.attributes;
161
    g_parameters   = List.map raw_content Front.grammar.parameters;
162 163 164
  }

let write oc t =
165 166 167
  (* .cmly file format: CMLY ++ version string ++ grammar *)
  let magic = "CMLY" ^ Version.version in
  output_string oc magic;
168 169 170
  output_value oc (t : grammar)

let write filename =
171 172 173
  (* Opening in binary mode is required. This is not a text file;
     we write to it using [output_value]. *)
  let oc = open_out_bin filename in
174 175
  write oc (encode());
  close_out oc