grew_loader.ml 6.16 KB
Newer Older
bguillaum's avatar
bguillaum committed
1 2 3 4 5 6 7 8 9 10
(**********************************************************************************)
(*    Libcaml-grew - a Graph Rewriting library dedicated to NLP applications      *)
(*                                                                                *)
(*    Copyright 2011-2013 Inria, Université de Lorraine                           *)
(*                                                                                *)
(*    Webpage: http://grew.loria.fr                                               *)
(*    License: CeCILL (see LICENSE folder or "http://www.cecill.info")            *)
(*    Authors: see AUTHORS file                                                   *)
(**********************************************************************************)

bguillaum's avatar
bguillaum committed
11
open Grew_base
bguillaum's avatar
bguillaum committed
12
open Grew_ast
pj2m's avatar
pj2m committed
13

14 15 16 17 18 19 20
(* ------------------------------------------------------------------------------------------*)
(** general function to handle parse errors *)
let parse_handle file fct lexbuf =
  let get_loc () = Loc.file_line file !Global.current_line in
  try fct lexbuf with
    | Grew_lexer.Error msg -> Error.parse ~loc:(get_loc ()) "Lexing error: %s" msg
    | Grew_parser.Error -> Error.parse ~loc:(get_loc ()) "Syntax error: %s" (Lexing.lexeme lexbuf)
bguillaum's avatar
bguillaum committed
21 22
    | Error.Build (msg, None) -> Error.parse ~loc:(get_loc ()) "Syntax error: %s" msg
    | Error.Build (msg, Some loc) -> Error.parse ~loc "Syntax error: %s" msg
23 24
    | Failure msg -> Error.parse ~loc:(get_loc ()) "Failure: %s" msg
    | err -> Error.bug ~loc:(get_loc ()) "Unexpected error: %s" (Printexc.to_string err)
pj2m's avatar
pj2m committed
25

26
module Loader = struct
bguillaum's avatar
bguillaum committed
27

bguillaum's avatar
bguillaum committed
28 29

  (* ------------------------------------------------------------------------------------------*)
30
  let parse_file_to_grs_wi file =
31
    try
bguillaum's avatar
bguillaum committed
32
      Global.init file;
33
      let in_ch = open_in file in
bguillaum's avatar
bguillaum committed
34
      let lexbuf = Lexing.from_channel in_ch in
35
      let grs = parse_handle file (Grew_parser.grs_wi Grew_lexer.global) lexbuf in
bguillaum's avatar
bguillaum committed
36 37
      close_in in_ch;
      grs
38
    with Sys_error msg -> Error.parse ~loc:(Loc.file file) "[Grew_loader.parse_file_to_grs_wi] %s" msg
pj2m's avatar
pj2m committed
39

bguillaum's avatar
bguillaum committed
40
  (* ------------------------------------------------------------------------------------------*)
41
  let parse_file_to_module_list file =
42
    try
bguillaum's avatar
bguillaum committed
43
      Global.init file;
44
      let in_ch = open_in file in
bguillaum's avatar
bguillaum committed
45
      let lexbuf = Lexing.from_channel in_ch in
bguillaum's avatar
bguillaum committed
46
      let module_list = parse_handle file (Grew_parser.included Grew_lexer.global) lexbuf in
bguillaum's avatar
bguillaum committed
47 48
      close_in in_ch;
      module_list
49
    with Sys_error msg -> Error.parse ~loc:(Loc.file file) "[Grew_loader.parse_file_to_module_list] %s" msg
pj2m's avatar
pj2m committed
50

51 52 53 54 55 56 57 58 59
  (* ------------------------------------------------------------------------------------------*)
  let domain file =
    try
      Global.init file;
      let in_ch = open_in file in
      let lexbuf = Lexing.from_channel in_ch in
      let gr = parse_handle file (Grew_parser.domain Grew_lexer.global) lexbuf in
      close_in in_ch;
      gr
60
    with Sys_error msg -> Error.parse ~loc:(Loc.file file) "[Grew_loader.domain] %s" msg
61

bguillaum's avatar
bguillaum committed
62
  (* ------------------------------------------------------------------------------------------*)
63 64 65 66
  (**
     [parse_string file] where [file] is a file following the grew syntax
     @param file the file to parse
     @return a syntactic tree of the parsed file
bguillaum's avatar
bguillaum committed
67
  *)
bguillaum's avatar
bguillaum committed
68
  let grs main_file =
69 70
    let grs_wi = parse_file_to_grs_wi main_file in
    let domain = match grs_wi.Ast.domain_wi with
bguillaum's avatar
bguillaum committed
71 72 73
      | None -> None
      | Some (Ast.Dom d) -> Some d
      | Some (Ast.Dom_file file) -> Some (domain file) in
74
    let rec flatten_modules current_file = function
75
      | [] -> []
bguillaum's avatar
bguillaum committed
76 77 78 79 80 81 82 83
      | Ast.Modul m :: tail ->
        {m with Ast.mod_dir = Filename.dirname current_file}
        :: (flatten_modules current_file tail)
      | Ast.Includ (inc_file,loc) :: tail ->
        let sub_file =
          if Filename.is_relative inc_file
          then Filename.concat (Filename.dirname current_file) inc_file
          else inc_file in
84
        (flatten_modules sub_file (parse_file_to_module_list sub_file))
bguillaum's avatar
bguillaum committed
85
        @ (flatten_modules current_file tail) in
86
    {
87 88
      Ast.domain = domain;
      Ast.modules = flatten_modules main_file grs_wi.Ast.modules_wi;
bguillaum's avatar
bguillaum committed
89
      Ast.strategies = grs_wi.Ast.strategies_wi;
bguillaum's avatar
bguillaum committed
90
    }
bguillaum's avatar
bguillaum committed
91

bguillaum's avatar
bguillaum committed
92
  (* ------------------------------------------------------------------------------------------*)
bguillaum's avatar
bguillaum committed
93
  let gr file =
94
    try
bguillaum's avatar
bguillaum committed
95
      Global.init file;
96
      let in_ch = open_in file in
bguillaum's avatar
bguillaum committed
97
      let lexbuf = Lexing.from_channel in_ch in
bguillaum's avatar
bguillaum committed
98
      let gr = parse_handle file (Grew_parser.gr Grew_lexer.global) lexbuf in
bguillaum's avatar
bguillaum committed
99 100
      close_in in_ch;
      gr
101 102
    with Sys_error msg -> Error.parse ~loc:(Loc.file file) "[Grew_loader.gr] %s" msg

bguillaum's avatar
bguillaum committed
103 104

  (* ------------------------------------------------------------------------------------------*)
bguillaum's avatar
bguillaum committed
105
  let pattern file =
bguillaum's avatar
bguillaum committed
106
    try
bguillaum's avatar
bguillaum committed
107
      Global.init file;
bguillaum's avatar
bguillaum committed
108 109
      let in_ch = open_in file in
      let lexbuf = Lexing.from_channel in_ch in
bguillaum's avatar
bguillaum committed
110
      let gr = parse_handle file (Grew_parser.pattern Grew_lexer.global) lexbuf in
bguillaum's avatar
bguillaum committed
111 112
      close_in in_ch;
      gr
113
    with Sys_error msg -> Error.parse ~loc:(Loc.file file) "[Grew_loader.pattern] %s" msg
bguillaum's avatar
bguillaum committed
114

115 116 117 118 119 120 121 122 123 124 125
  (* ------------------------------------------------------------------------------------------*)
  let constituent file =
    try
      Global.init file;
      let in_ch = open_in file in
      let lexbuf = Lexing.from_channel in_ch in
      let graph = parse_handle file (Grew_parser.constituent Grew_lexer.const) lexbuf in
      close_in in_ch;
      graph
    with Sys_error msg -> Error.parse ~loc:(Loc.file file) "[Grew_loader.constituent] %s" msg

bguillaum's avatar
bguillaum committed
126
end (* module Loader *)
127 128 129 130 131 132 133 134


module Parser = struct
  (* ------------------------------------------------------------------------------------------*)
  let gr gr_string =
    try
      Global.init "from_string";
      let lexbuf = Lexing.from_string gr_string in
135
      let gr = parse_handle "Not a file" (Grew_parser.gr Grew_lexer.global) lexbuf in
136 137
      gr
    with Sys_error msg -> Error.parse "[Grew_parser.gr] %s" msg
138 139 140 141 142 143 144 145 146 147

  (* ------------------------------------------------------------------------------------------*)
  let constituent s =
    try
      Global.init "Not a file";
      let lexbuf = Lexing.from_string s in
      let graph = parse_handle "Not a file" (Grew_parser.constituent Grew_lexer.const) lexbuf in
      graph
    with Sys_error msg -> Error.parse "[Grew_parser.constituent] %s" msg

148
end