Alternate if syntax support
[hiphop-php.git] / hphp / hack / src / parser / syntaxTransforms.ml
blobf97aeb2678cb128fe9501b03b0bc5d3667787ec1
1 (**
2 * Copyright (c) 2018, Facebook, Inc.
3 * All rights reserved.
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the "hack" directory of this source tree. An additional grant
7 * of patent rights can be found in the PATENTS file in the same directory.
9 *)
11 module ES = Full_fidelity_editable_syntax
12 module PS = Full_fidelity_positioned_syntax
13 module ET = ES.Token
14 module PT = PS.Token
15 module PositionedSyntaxTree = Full_fidelity_syntax_tree.WithSyntax(PS)
17 let editable_from_positioned tree =
18 let rec aux text positioned_node offset =
19 match PS.syntax positioned_node with
20 | PS.Token token ->
21 let source_text = PT.source_text token in
22 let width = PT.width token in
23 let leading =
24 Full_fidelity_editable_trivia.from_positioned_list
25 source_text
26 (PT.leading token)
27 offset
29 let trailing =
30 Full_fidelity_editable_trivia.from_positioned_list
31 source_text
32 (PT.trailing token)
33 (offset + (PT.leading_width token) + width)
35 let editable_token =
36 ET.make
37 (PT.kind token)
38 source_text
39 (PT.leading_start_offset token)
40 width
41 leading
42 trailing
44 let syntax = ES.Token editable_token in
45 ES.make syntax ES.Value.NoValue
46 | _ ->
47 let folder (acc, offset) child =
48 let new_child = aux text child offset in
49 let w = PS.full_width child in
50 (new_child :: acc, offset + w) in
51 let kind = PS.kind positioned_node in
52 let positioneds = PS.children positioned_node in
53 let (editables, _) = List.fold_left folder ([], offset) positioneds in
54 let editables = List.rev editables in
55 let syntax = ES.syntax_from_children kind editables in
56 ES.make syntax ES.Value.NoValue
58 aux (PositionedSyntaxTree.text tree) (PositionedSyntaxTree.root tree) 0