Skip to content
This repository
tag: v962
Fetching contributors…

Cannot retrieve contributors at this time

file 378 lines (334 sloc) 9.057 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
(*
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/>.
*)
(* depends *)
module Arg = BaseArg
module List = BaseList
module String = BaseString

(* alias *)

(* - *)

(*
To extend the type, follow EXTEND-OPT
<!> the type should remain marshalable, becuase it is embeded in plugins.
*)
type optimized_conf = {
  cleanup : bool ;
  localrenaming : bool ;
  (*
EXTEND-OPT
*)
}

(*
<!> Beware, this type is marshaled during the bsl preprocess
*)
type conf =
  | Verbatim
  | Optimized of optimized_conf

let default_opt = {
  cleanup = false ;
  localrenaming = false ;
  (*
EXTEND-OPT
*)
}

let default =
  Optimized default_opt

let get_opt = function
  | Verbatim -> default_opt
  | Optimized opt -> opt

let compare_conf c1 c2 =
  match c1, c2 with
  | Verbatim, Verbatim -> 0
  | Verbatim, _ -> -1
  | _, Verbatim -> 1
  | Optimized {cleanup=c1; localrenaming=l1}, Optimized {cleanup=c2; localrenaming=l2} ->
      match compare c1 c2 with
      | 0 -> compare l1 l2
      | c -> c
      (*
&& EXTEND-OPT
*)

let is_default opt =
  compare_conf default opt = 0

module Private :
sig

  (**
The type parsed for an analysis, corresponding to a property with an argument
Example:
{[
cleanup: false
]}
will be parsed as: ["cleanup", "false"]
*)
  type entry = string * string

  (**
Folding a conf file.
Conf are always folded from the default conf.
May use [OManager] with the given pos in case of error.
*)
  val fold : FilePos.pos -> entry -> conf -> conf

end
  =
struct
 type entry = string * string

 (*
list of handled properties
*)

 let cleanup = "cleanup"
 let localrenaming = "localrenaming"
 let verbatim = "verbatim"

 (*
EXTEND-OPT
*)

 let properties = [
   cleanup ;
   localrenaming ;
   verbatim ;
   (*
EXTEND-OPT
*)
 ]

 (*
list of handled arguments, for each propertie
*)

 let v_true = "true"
 let v_false = "false"

 let bool_arguments = [
   v_true ;
   v_false ;
 ]

 let invalid_argument pos property arguments typo =
   OManager.error (
     "%a@\n"^^
     "Property @{<bright>%s: invalid argument @{<bright>%s@}@\n"^^
     "%a"
   )
     FilePos.pp pos
     property
     typo
     (HintUtils.pp_suggestion arguments) typo

 let get_bool_argument pos property argument =
   if argument = v_true
   then
     true
   else if argument = v_false
   then
     false
   else
     invalid_argument pos property bool_arguments argument

 let fold pos (property, argument) conf =
   Return.set_checkpoint (
     fun label ->

       if property = cleanup
       then (
         let argument = get_bool_argument pos property argument in
         let opt = get_opt conf in
         let opt =
           if opt.localrenaming = argument then opt
           else {
             opt with
               cleanup = argument ;
           } in
         let conf = Optimized opt in
         Return.return label conf
       );

       if property = localrenaming
       then (
         let argument = get_bool_argument pos property argument in
         let opt = get_opt conf in
         let opt =
           if opt.localrenaming = argument then opt
           else {
             opt with
               localrenaming = argument ;
           } in
         let conf = Optimized opt in
         Return.return label conf
       );

       if property = verbatim
       then (
         let argument = get_bool_argument pos property argument in
         let conf =
           if argument
           then
             Verbatim
           else
             (*
keep the previous setting, if it exists one
*)
             Optimized (get_opt conf)
         in
         Return.return label conf
       );

       (*
EXTEND-OPT
*)

       OManager.error (
         "%a@\n"^^
         "Unknown property @{<bright>%s@}@\n"^^
         "%a"
       )
         FilePos.pp pos
         property
         (HintUtils.pp_suggestion properties) property
   )
end

type filename = string

type confs = conf StringMap.t

type t = {
  confs : confs ;
  files : string list ;
  files_set : StringSet.t ;
  (*
cache: same than files, but in a set format
*)
}

let export t = t.confs

let empty files =
  let files_set = StringSet.from_list files in
  let confs =
    let fold file confs =
      StringMap.add file default confs
    in
    StringSet.fold fold files_set StringMap.empty
  in
  {
    files ;
    files_set ;
    confs ;
  }

(*
Parsing the file
A line can be [%s] or property:argument
*)
type parsed_line =
  | Files of StringSet.t
  | Entry of Private.entry

let regexp_file = Str.regexp "\\[\\([^]]*\\)\\]$"
let regexp_property = Str.regexp "\\([^:]*\\):\\([^:]*\\)$"

let parse_line pos files line =
  let line =
    match String.findi '#' line with
    | None -> line
    | Some i -> String.sub line 0 i
  in
  let line = String.trim line in
  if line = ""
  then None
  else Some (
    Return.set_checkpoint (
      fun label ->
        if Str.string_match regexp_file line 0
        then (
          let args = Str.matched_group 1 line in
          let regexps = Arg.split args in
          let fold set str =
            let regexp =
              try
                Str.regexp str
              with
              | Invalid_argument message
              | Failure message
                ->
                  OManager.error (
                    "%a@\n"^^
                    "@[<2>Invalid regexpression:@\n%s@]@\n"
                  )
                    FilePos.pp pos
                    message
            in
            let filter s = Str.string_match regexp (Filename.basename s) 0 in
            let files = List.rev_filter filter files in
            if files = [] then
              OManager.warning ~wclass:WarningClass.bsl_register (
                "%a@\n"^^
                "Regexp <@{<bright>%s@}> does not match any considered js files@\n"
              )
                FilePos.pp pos
                str
            ;
            let add a b = StringSet.add b a in
            List.fold_left add set files
          in
          let files = StringSet.empty in
          let files = List.fold_left fold files regexps in
          let parsed = Files files in
          Return.return label parsed
        );

        if Str.string_match regexp_property line 0
        then (
          let property = Str.matched_group 1 line in
          let property = String.trim property in
          let argument = Str.matched_group 2 line in
          let argument = String.trim argument in
          let parsed = Entry (property, argument) in
          Return.return label parsed
        );

        OManager.error (
          "%a@\n"^^
          "Syntax error.@\n"^^
          "Use one of the following form :@\n"^^
          "@[<2>@\n[file.js]@\n[*]@\n[file1.js, file2.js]@\nproperty : argument@]@\n"
        )
          FilePos.pp pos
    )
  )

(*
Internal accumulator
*)

let fold conffile t =
  let () =
    if not (File.is_regular conffile) then
      OManager.error "I/O error: @{<bright>%S@} -> No such file" conffile
  in
  let () =
    let content =
      match File.content_opt conffile with
      | Some content -> content
      | None ->
          OManager.error "I/O error: cannot read conf file @{<bright>%S@}" conffile
    in
    FilePos.add_file conffile content
  in
  let files = t.files in
  let process_line ((set, confs) as acc) line line_number =
    let pos = FilePos.make_pos_from_line conffile line_number in
    match parse_line pos files line with
    | None -> acc
    | Some parsed -> (
        match parsed with
        | Files set -> (set, confs)
        | Entry entry ->
            let fold file confs =
              let conf =
                try
                  StringMap.find file confs
                with
                | Not_found ->
                    (*
Internal error.
We filter only files already known in the function parse_line,
and the initializer of type [t] build the field [files]
*)
                    assert false
              in
              let conf = Private.fold pos entry conf in
              StringMap.add file conf confs
            in
            let confs = StringSet.fold fold set confs in
            set, confs
      )
  in
  let acc = t.files_set, t.confs in
  let _, confs = File.lines_foldi process_line acc conffile in
  { t with confs }
Something went wrong with that request. Please try again.