Skip to content
This repository
tag: v331
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 61 lines (53 sloc) 2.27 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
(*
Copyright © 2011 MLstate

This file is part of OPA.

OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.

OPA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
more details.

You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*)
(*
* This file is a standalone parser for the opa syntax
* It is meant for debugging/profiling the parser
*)

let show = ref false
let show_pos = ref false
let stop = ref false
let files = ref []
let () =
  Arg.parse
    ["--show", Arg.Set show, "show the parse tree"
    ;"--pos", Arg.Set show_pos, "show the positions when printing"
    ;"--stop", Arg.Set stop, "stop at the first file containing a parse error"]
    (fun s -> files := s :: !files)
    (Printf.sprintf "Usage: %s [options] files" Sys.argv.(0))

let () = OManager.CompilerAsLib.at_exit { OManager.at_exit = fun _ -> raise Exit }

let () =
  let code = ref 0 in
  let printer = if !show_pos then OpaPrint.string_and_pos else OpaPrint.string in
  let ppenv = Pprocess.fill_with_sysenv Pprocess.empty_env in
  let ppenv = Pprocess.add_env "OPA_VERSION" "S3" ppenv in
  let ppopt = Pprocess.default_options ppenv in
  let process = (Pprocess.process Pplang.opa_description ppopt) in
  List.iter
    (fun filename ->
       match File.content_opt filename with
       | None -> Printf.printf "%s is not a file, ignoring it\n%!" filename
       | Some content ->
           try
             let content = process content in
             let ast = OpaParser.code ~cache:false ~filename content in
             if !show then Format.printf "%s:@\n%a@." filename printer#code ast;
             Printf.printf "Parsed %s successfully\n%!" filename
           with
           | Exit ->
               (* the error message has been printed already *)
               incr code;
               if !stop then exit !code
    ) (List.rev !files);
  exit (min 255 !code)
Something went wrong with that request. Please try again.