package comby-kernel
A match engine for structural code search and replace that supports ~every language
Install
Dune Dependency
Authors
Maintainers
Sources
comby-kernel.1.7.0.tar.gz
md5=ee6556d8bd9b25ed0445ebe23862e48a
sha512=e6386c8ce5ef14bbcab2b0ead5b1edc39375438f56330d5f02e81e467afe6623a7e299f97f26008d77bbc62850c6dc63a7cbe5b81671b5183ff3adeee5946bb3
doc/src/comby-kernel.matchers/template.ml.html
Source file template.ml
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
open Vangstrom open Core_kernel open Match open Types.Template let debug = match Sys.getenv "DEBUG_COMBY" with | exception Not_found -> false | _ -> true module Make (Metasyntax : Types.Metasyntax.S) (External : Types.External.S) : Types.Template.S = struct let up_to p = many1 (not_followed_by p *> any_char) let optional d = Option.value d ~default:"" let character () = choice @@ List.map ~f:char (String.to_list Metasyntax.identifier) let identifier () = many1 @@ character () >>| String.of_char_list let regex_expression suffix = lift String.concat (many1 @@ fix (fun expr -> choice [ lift (fun x -> Format.sprintf "[%s]" @@ String.concat x) (char '[' *> many1 expr <* char ']') ; lift (fun c -> Format.sprintf {|\%c|} c) (char '\\' *> any_char) ; lift String.of_char_list (up_to (string suffix)) ]) ) let regex_body separator suffix = both (option "" (identifier ())) (char separator *> regex_expression suffix) (** Parsers for Matching. Different from rewrite templates which can have :[x].attribute *) module Matching = struct (** Folds left to respect order of definitions in custom metasyntax for matching, where we attempt to parse in order. Note this is significant if a syntax like $X~regex should be tried before shortcircuiting on $X, in which case it should be defined _after_ the $X syntax (most general should be first). *) let hole_parsers = (* hole parsers for match templates only *) List.fold ~init:[] Metasyntax.syntax ~f:(fun acc v -> let result = match v with | Hole (sort, Delimited (left, right)) -> sort, lift3 (fun _left v _right -> v) (string (optional left)) (identifier ()) (string (optional right)) | Hole (sort, Reserved_identifiers l) -> sort, choice (List.map l ~f:string) | Regex (left, separator, right) -> Regex, (* matcher wants <identifier><sep><expr> and splits it later. Fix this later to give v and pattern only *) lift3 (fun _left (v, expr) _right -> Format.sprintf "%s%c%s" v separator expr) (string left) (regex_body separator right) (string right) in result::acc) end let attribute_to_kind = function | "value" -> Value | "length" -> Length | "lines" -> Lines | "offset" | "offset.start" -> OffsetStart | "offset.end" -> OffsetEnd | "line" | "line.start" -> LineStart | "line.end" -> LineEnd | "column" | "column.start" -> ColumnStart | "column.end" -> ColumnEnd | "file" | "file.path" -> FilePath | "file.name" -> FileName | "file.directory" -> FileDirectory | "lowercase" -> Lowercase | "UPPERCASE" -> Uppercase | "Capitalize" -> Capitalize | "uncapitalize" -> Uncapitalize | "UpperCamelCase" -> UpperCamelCase | "lowerCamelCase" -> LowerCamelCase | "UPPER_SNAKE_CASE" -> UpperSnakeCase | "lower_snake_case" -> LowerSnakeCase | "lsif.hover" -> External "lsif.hover" | s -> failwith @@ Format.sprintf "invalid attribute %S" s let attribute_access () = char '.' *> choice [ string "value" ; string "length" ; string "lines" ; string "offset.start" ; string "offset.end" ; string "offset" ; string "line.start" ; string "line.end" ; string "line" ; string "column.start" ; string "column.end" ; string "column" ; string "file.path" ; string "file.name" ; string "file.directory" ; string "file" ; string "lowercase" ; string "UPPERCASE" ; string "Capitalize" ; string "uncapitalize" ; string "UpperCamelCase" ; string "lowerCamelCase" ; string "UPPER_SNAKE_CASE" ; string "lower_snake_case" ; string "lsif.hover" ] <* not_followed_by (Omega_parser_helper.alphanum) (** Folds left to respect order of definitions in custom metasyntax for matching, where we attempt to parse in order. Note this is significant if a syntax like $X~regex should be tried before shortcircuiting on $X, in which case it should be defined _after_ the $X syntax (most general should be first). *) let rewrite_hole_parsers = List.fold ~init:[] Metasyntax.syntax ~f:(fun acc v -> let result = match v with | Hole (_, Delimited (left, right)) -> lift4 (fun left v right kind -> let dot_attribute = if String.(kind = "value") then "" else "."^kind in Format.sprintf "%s%s%s%s" left v right dot_attribute, v, kind) (string (optional left)) (identifier ()) (string (optional right)) (option "value" (attribute_access ())) | Hole (_, Reserved_identifiers l) -> lift2 (fun v kind -> let dot_attribute = if String.(kind = "value") then "" else "."^kind in Format.sprintf "%s%s" v dot_attribute, v, kind) (choice (List.map l ~f:string)) (option "value" (attribute_access ())) | Regex (left, separator, right) -> lift4 (fun left (v, expr) right kind -> let dot_attribute = if String.(kind = "value") then "" else "."^kind in Format.sprintf "%s%s%c%s%s%s" left v separator expr right dot_attribute, v, kind) (string left) (regex_body separator right) (string right) (option "value" (attribute_access ())) in result::acc) let parse_template = let hole = choice rewrite_hole_parsers in many @@ choice [ (pos >>= fun offset -> hole >>| fun (pattern, variable, kind) -> Hole { pattern; variable; offset; kind = attribute_to_kind kind }) ; (up_to (choice rewrite_hole_parsers) >>| fun c -> Constant (String.of_char_list c)) ] let parse template = match parse_string ~consume:All parse_template template with | Ok result -> result | Error e -> failwith ("No rewrite template parse: "^e) let variables template = parse template |> List.filter_map ~f:(function | Hole { pattern; variable; offset; kind } -> Some { pattern; variable; offset; kind } | _ -> None) let to_string template = let buf = Buffer.create 10 in List.iter template ~f:(function | Constant c -> Buffer.add_string buf c | Hole { pattern; _ } -> Buffer.add_string buf pattern); Buffer.contents buf let camel_to_snake s = let rec aux i = function | [] -> [] | ('A'..'Z' as c)::tl when i = 0 -> (Char.lowercase c)::aux (i+1) tl | ('A'..'Z' as c)::tl when i <> 0 -> '_'::(Char.lowercase c)::aux (i+1) tl | c::tl -> c::aux (i+1) tl in aux 0 (String.to_list s) |> String.of_char_list let substitute_kind ?filepath { variable; kind; _ } env = let open Option in let length_to_string n = Format.sprintf "%d" (String.length n) in match kind with | Value -> Environment.lookup env variable | Length -> Environment.lookup env variable >>| length_to_string | Lines -> Environment.lookup env variable >>| String.count ~f:(Char.(=) '\n') >>| (fun v -> if v = 0 then 1 else v) >>| Int.to_string | OffsetStart -> Environment.lookup_range env variable >>| fun { match_start = { offset; _ }; _ } -> Int.to_string offset | OffsetEnd -> Environment.lookup_range env variable >>| fun { match_end = { offset; _ }; _ } -> Int.to_string offset | LineStart -> filepath >>= fun filepath -> Environment.lookup_range env variable >>| fun { match_start = { offset; _ }; _ } -> let source = In_channel.read_all filepath in (* Inefficient. *) let index = Match.Offset.index ~source in let line, _ = Match.Offset.convert_fast ~offset index in Int.to_string line | LineEnd -> filepath >>= fun filepath -> Environment.lookup_range env variable >>| fun { match_end = { offset; _ }; _ } -> let source = In_channel.read_all filepath in (* Inefficient. *) let index = Match.Offset.index ~source in let line, _ = Match.Offset.convert_fast ~offset index in Int.to_string line | ColumnStart -> filepath >>= fun filepath -> Environment.lookup_range env variable >>| fun { match_start = { offset; _ }; _ } -> let source = In_channel.read_all filepath in (* Inefficient. *) let index = Match.Offset.index ~source in let _, column = Match.Offset.convert_fast ~offset index in Int.to_string column | ColumnEnd -> filepath >>= fun filepath -> Environment.lookup_range env variable >>| fun { match_end = { offset; _ }; _ } -> let source = In_channel.read_all filepath in (* Inefficient. *) let index = Match.Offset.index ~source in let _, column = Match.Offset.convert_fast ~offset index in Int.to_string column | FilePath -> filepath | FileName -> filepath >>| Filename.basename | FileDirectory -> filepath >>| Filename.dirname | Lowercase -> Environment.lookup env variable >>| String.lowercase | Uppercase -> Environment.lookup env variable >>| String.uppercase | Capitalize -> Environment.lookup env variable >>| String.capitalize | Uncapitalize -> Environment.lookup env variable >>| String.uncapitalize | UpperCamelCase -> Environment.lookup env variable >>| String.split ~on:'_' >>| List.map ~f:String.capitalize >>| String.concat >>| String.capitalize | LowerCamelCase -> Environment.lookup env variable >>| String.split ~on:'_' >>| List.map ~f:String.capitalize >>| String.concat >>| String.uncapitalize | UpperSnakeCase -> Environment.lookup env variable >>| camel_to_snake >>| String.uppercase | LowerSnakeCase -> Environment.lookup env variable >>| camel_to_snake >>| String.lowercase | External "lsif.hover" -> filepath >>= fun filepath -> if debug then Format.printf "File for lsif.hover lookup: %s@." filepath; Environment.lookup env variable >>= fun value -> Environment.lookup_range env variable >>= fun { match_start = { offset; _ }; _ } -> let source = In_channel.read_all filepath in (* Inefficient. *) if debug then Format.printf "Read filepath, source len is %d@." @@ String.length source; let index = Match.Offset.index ~source in let line, column = Match.Offset.convert_fast ~offset index in let line, column = line - 1, column - 1 + String.length value - 1 in if debug then Format.printf "Var offset:%d line:%d col:%d @." offset line column; External.handler ~name:"lsif.hover" ~filepath ~line ~column | External _ -> assert false let substitute ?filepath template environment = let replacement_content, environment', _ = List.fold template ~init:([], Environment.create (), 0) ~f:(fun (result, env, pos) -> function | Constant c -> c::result, env, pos + String.length c | Hole ({ variable; pattern; _ } as h) -> match substitute_kind ?filepath h environment with | None -> pattern::result, env, pos + String.length variable | Some value -> let advance = pos + String.length value in let range = Range. { match_start = Location.{ default with offset = pos } ; match_end = Location.{ default with offset = advance } } in (* FIXME: here we should probably use pattern, or hole. We don't want to substitute var x for length value if it's used as :[x] vs :[x].length in the same rewrite template. This will only affect the replacement values, which won't clobber the actual result. *) let env = Environment.add ~range env variable value in value::result, env, advance) in String.concat (List.rev replacement_content), environment' (** Currently dead code. Alternative to substitute that searches for hole patterns and uses substr_replace_all. Don't know if it's faster, need to test. Also appears to have a minor offset issue. *) let substitute' template environment = let vars = List.filter_map template ~f:(function | Hole { pattern; variable; offset; kind } -> Some { pattern; variable; offset; kind } | _ -> None) in let template_string = to_string template in let replacement_content, environment = List.fold vars ~init:(template_string, Environment.create ()) ~f:(fun (template, env) { variable; pattern; _ } -> match Environment.lookup environment variable with | None -> template, env | Some value -> match String.substr_index template_string ~pattern with | None -> template, env | Some offset -> let range = Range. { match_start = Location.{ default with offset } ; match_end = Location.{ default with offset = offset + String.length value } } in let env = Environment.add ~range env variable value in String.substr_replace_all template ~pattern ~with_:value, env) in replacement_content, environment end
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>