grew_loader.ml 5.13 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 21 22
(* ------------------------------------------------------------------------------------------*)
(** 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)
    | 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
23

24
module Loader = struct
bguillaum's avatar
bguillaum committed
25

bguillaum's avatar
bguillaum committed
26 27

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

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

49 50 51 52 53 54 55 56 57
  (* ------------------------------------------------------------------------------------------*)
  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
58
    with Sys_error msg -> Error.parse ~loc:(Loc.file file) "[Grew_loader.domain] %s" msg
59

bguillaum's avatar
bguillaum committed
60
  (* ------------------------------------------------------------------------------------------*)
61 62 63 64
  (**
     [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
65
  *)
bguillaum's avatar
bguillaum committed
66
  let grs main_file =
67 68 69 70
    let grs_wi = parse_file_to_grs_wi main_file in
    let domain = match grs_wi.Ast.domain_wi with
      | Ast.Dom d -> d
      | Ast.Dom_file file -> domain file in
71
    let rec flatten_modules current_file = function
72
      | [] -> []
bguillaum's avatar
bguillaum committed
73 74 75 76 77 78 79 80
      | 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
81
        (flatten_modules sub_file (parse_file_to_module_list sub_file))
bguillaum's avatar
bguillaum committed
82
        @ (flatten_modules current_file tail) in
83
    {
84 85 86
      Ast.domain = domain;
      Ast.modules = flatten_modules main_file grs_wi.Ast.modules_wi;
      Ast.sequences = grs_wi.Ast.sequences_wi;
bguillaum's avatar
bguillaum committed
87
    }
bguillaum's avatar
bguillaum committed
88

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

bguillaum's avatar
bguillaum committed
100 101

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

bguillaum's avatar
bguillaum committed
112
end (* module Loader *)
113 114 115 116 117 118 119 120 121 122 123 124


module Parser = struct
  (* ------------------------------------------------------------------------------------------*)
  let gr gr_string =
    try
      Global.init "from_string";
      let lexbuf = Lexing.from_string gr_string in
      let gr = parse_handle "from_string" (Grew_parser.gr Grew_lexer.global) lexbuf in
      gr
    with Sys_error msg -> Error.parse "[Grew_parser.gr] %s" msg
end