Skip to content
This repository
tag: v1513
Fetching contributors…

Cannot retrieve contributors at this time

file 306 lines (253 sloc) 9.245 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
(*
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/>.
*)
(**
Traversing JsAst
@author Mathieu Barbin
*)

module List = BaseList
module J = JsAst

let foldmapA traA traB acc e =
  match e with
  | J.Je_this _ ->
      acc, e

  | J.Je_ident (_, _) ->
      acc, e

  | J.Je_array (label, list) ->
      let acc, flist = List.fold_left_map_stable traA acc list in
      acc,
      if list == flist then e else
        J.Je_array (label, flist)

  | J.Je_comma (label, list, last) ->
      let acc, flist = List.fold_left_map_stable traA acc list in
      let acc, flast = traA acc last in
      acc,
      if list == flist && last == flast then e else
        J.Je_comma (label, flist, flast)

  | J.Je_object (label, fields) ->
      let fmap acc ((field, b) as c) =
        let acc, fb = traA acc b in
        acc,
        if b == fb then c else
          (field, fb)
      in
      let acc, ffields = List.fold_left_map_stable fmap acc fields in
      acc,
      if fields == ffields then e else
        J.Je_object (label, ffields)

  | J.Je_string (_, _, _) ->
      acc, e

  | J.Je_num (_, _) ->
      acc, e

  | J.Je_null _ ->
      acc, e

  | J.Je_undefined _ ->
      acc, e

  | J.Je_bool (_, _) ->
      acc, e

  | J.Je_regexp _ ->
      acc, e

  | J.Je_function (label, ident, params, body) ->
      let acc, fbody = List.fold_left_map_stable traB acc body in
      acc,
      if body == fbody then e else
        J.Je_function (label, ident, params, fbody)

  | J.Je_dot (label, expr, field) ->
      let acc, fexpr = traA acc expr in
      acc,
      if expr == fexpr then e else
        J.Je_dot (label, fexpr, field)

  | J.Je_unop (label, op, expr) ->
      let acc, fexpr = traA acc expr in
      acc,
      if expr == fexpr then e else
        J.Je_unop (label, op, fexpr)

  | J.Je_binop (label, op, expr1, expr2) ->
      let acc, fexpr1 = traA acc expr1 in
      let acc, fexpr2 = traA acc expr2 in
      acc,
      if expr1 == fexpr1 && expr2 == fexpr2 then e else
        J.Je_binop (label, op, fexpr1, fexpr2)

  | J.Je_cond (label, cond, then_, else_) ->
      let acc, fcond = traA acc cond in
      let acc, fthen_ = traA acc then_ in
      let acc, felse_ = traA acc else_ in
      acc,
      if cond == fcond && then_ == fthen_ && else_ == felse_ then e else
        J.Je_cond (label, fcond, fthen_, felse_)

  | J.Je_call (label, fun_, args, pure) ->
      let acc, ffun_ = traA acc fun_ in
      let acc, fargs = List.fold_left_map_stable traA acc args in
      acc,
      if fun_ == ffun_ && args == fargs then e else
        J.Je_call (label, ffun_, fargs, pure)

  | J.Je_new (label, obj, args) ->
      let acc, fobj = traA acc obj in
      let acc, fargs = List.fold_left_map_stable traA acc args in
      acc,
      if obj == fobj && args == fargs then e else
        J.Je_new (label, fobj, fargs)

  | J.Je_hole (_, _) ->
      acc, e

  | J.Je_runtime _ ->
      acc, e

let foldmapB traB traA acc e =
  match e with
  | J.Js_var (_, _, None) ->
      acc, e
  | J.Js_var (label, ident, Some expr) ->
      let acc, fexpr = traA acc expr in
      acc,
      if expr == fexpr then e else
        J.Js_var (label, ident, Some fexpr)

  | J.Js_function (label, ident, params, body) ->
      let acc, fbody = List.fold_left_map_stable traB acc body in
      acc,
      if body == fbody then e else
        J.Js_function (label, ident, params, fbody)

  | J.Js_return (label, expr) ->
      let acc, fexpr = Option.foldmap_stable traA acc expr in
      acc,
      if expr == fexpr then e else
        J.Js_return (label, fexpr)

  | J.Js_continue (_, _) ->
      acc, e

  | J.Js_break (_, _) ->
      acc, e

  | J.Js_switch (label, expr, cases, default) ->
      let fmap acc ((expr, stat) as c) =
        let acc, fexpr = traA acc expr in
        let acc, fstat = traB acc stat in
        acc,
        if expr == fexpr && stat == fstat then c else
          (fexpr, fstat)
      in
      let acc, fexpr = traA acc expr in
      let acc, fcases = List.fold_left_map_stable fmap acc cases in
      let acc, fdefault = Option.foldmap_stable traB acc default in
      acc,
      if expr == fexpr && cases == fcases && default == fdefault then e else
        J.Js_switch (label, fexpr, fcases, fdefault)

  | J.Js_if (label, cond, then_, else_) ->
      let acc, fcond = traA acc cond in
      let acc, fthen_ = traB acc then_ in
      let acc, felse_ = Option.foldmap_stable traB acc else_ in
      acc,
      if cond == fcond && then_ == fthen_ && else_ == felse_ then e else
        J.Js_if (label, fcond, fthen_, felse_)

  | J.Js_throw (label, expr) ->
      let acc, fexpr = traA acc expr in
      acc,
      if expr == fexpr then e else
        J.Js_throw (label, fexpr)

  | J.Js_expr (label, expr) ->
      let acc, fexpr = traA acc expr in
      acc,
      if expr == fexpr then e else
        J.Js_expr (label, fexpr)

  | J.Js_trycatch (label, body, catches, finally) ->
      let fmap acc ((ident, expr, stat) as t) =
        let acc, fexpr = Option.foldmap_stable traA acc expr in
        let acc, fstat = traB acc stat in
        acc,
        if expr == fexpr && stat = fstat then t else
          (ident, fexpr, fstat)
      in
      let acc, fbody = traB acc body in
      let acc, fcatches = List.fold_left_map_stable fmap acc catches in
      let acc, ffinally = Option.foldmap_stable traB acc finally in
      acc,
      if body == fbody && catches == fcatches && finally == ffinally then e else
        J.Js_trycatch (label, fbody, fcatches, ffinally)

  | J.Js_for (label, init, cond, incr, body) ->
      let acc, finit = Option.foldmap_stable traA acc init in
      let acc, fcond = Option.foldmap_stable traA acc cond in
      let acc, fincr = Option.foldmap_stable traA acc incr in
      let acc, fbody = traB acc body in
      acc,
      if init == finit && cond == fcond && incr == fincr && body == fbody then e else
        J.Js_for (label, finit, fcond, fincr, fbody)

  | J.Js_forin (label, lhs, rhs, body) ->
      let acc, flhs = traA acc lhs in
      let acc, frhs = traA acc rhs in
      let acc, fbody = traB acc body in
      acc,
      if flhs == lhs && frhs == rhs && fbody == body then e else
        J.Js_forin (label, flhs, frhs, fbody)

  | J.Js_dowhile (label, body, cond) ->
      let acc, fbody = traB acc body in
      let acc, fcond = traA acc cond in
      acc,
      if body == fbody && cond == fcond then e else
        J.Js_dowhile (label, fbody, fcond)

  | J.Js_while (label, cond, body) ->
      let acc, fcond = traA acc cond in
      let acc, fbody = traB acc body in
      acc,
      if cond == fcond && body == fbody then e else
        J.Js_while (label, fcond, fbody)

  | J.Js_block (label, body) ->
      let acc, fbody = List.fold_left_map_stable traB acc body in
      acc,
      if body == fbody then e else
        J.Js_block (label, fbody)

  | J.Js_with (label, expr, body) ->
      let acc, fexpr = traA acc expr in
      let acc, fbody = traB acc body in
      acc,
      if expr == fexpr && body == fbody then e else
        J.Js_with (label, fexpr, fbody)

  | J.Js_label (label, string, stmt) ->
      let acc, fstmt = traB acc stmt in
      acc,
      if stmt == fstmt then e else
        J.Js_label (label, string, fstmt)

  | J.Js_comment (_, _, _) ->
      acc, e

module AB : TraverseInterface.AB
  with type 'a tA = JsAst.expr
  constraint 'a = 'b * 'c * 'd
  and type 'a tB = JsAst.statement
  constraint 'a = 'b * 'c * 'd
=
struct
  type 'a tA = JsAst.expr
  constraint 'a = 'b * 'c * 'd

  type 'a tB = JsAst.statement
  constraint 'a = 'b * 'c * 'd

  let foldmapA = foldmapA
  let foldmapB = foldmapB

  let mapA traA traB e = Traverse.Unoptimized.mapAB foldmapA traA traB e
  let mapB traB traA e = Traverse.Unoptimized.mapAB foldmapB traB traA e

  let iterA traA traB e = Traverse.Unoptimized.iterAB foldmapA traA traB e
  let iterB traB traA e = Traverse.Unoptimized.iterAB foldmapB traB traA e

  let foldA traA traB acc e = Traverse.Unoptimized.foldAB foldmapA traA traB acc e
  let foldB traB traA acc e = Traverse.Unoptimized.foldAB foldmapB traB traA acc e
end


module T = Traverse.MakeAB(AB)

module TExpr = T.A
module TStatement = T.B
module Expr = T.AinA
module Statement = T.BinB
module ExprInStatement = T.AinB
module StatementInExpr = T.BinA
module OnlyExpr = T.OnlyA
module OnlyStatement = T.OnlyB

(* Refreshing the annotations of an expression or a statement *)
module Refresh =
struct
  let aux_expr expr = J.JNewAnnot.expr expr (Annot.next ())
  let aux_stm stm = J.JNewAnnot.stm stm (Annot.next ())
  let expr expr = TExpr.map aux_expr aux_stm expr
  let stm stm = TStatement.map aux_stm aux_expr stm
end
Something went wrong with that request. Please try again.