Alternate if syntax support
[hiphop-php.git] / hphp / hack / src / parser / full_fidelity_editable_positioned_original_source_data.ml
blob9adacbab2683907861b3517c2237a73df9a55131
1 (**
2 * Copyright (c) 2017, 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 (**
12 * SourceData represents information with relation to the original SourceText.
15 module SourceText = Full_fidelity_source_text
16 module Syntax = Full_fidelity_positioned_syntax
17 module Token = Full_fidelity_positioned_token
18 module Trivia = Full_fidelity_positioned_trivia
20 (**
21 * Data about the token with respect to the original source text.
23 type t = {
24 source_text: SourceText.t;
25 offset: int; (* Beginning of first trivia *)
26 leading_width: int;
27 width: int; (* Width of actual token, not counting trivia *)
28 trailing_width: int;
29 leading: Trivia.t list;
30 trailing: Trivia.t list;
33 let empty =
35 source_text = SourceText.empty;
36 offset = 0;
37 leading_width = 0;
38 width = 0;
39 trailing_width = 0;
40 leading = [];
41 trailing = [];
44 let from_positioned_token positioned_token =
46 source_text = Token.source_text positioned_token;
47 offset = Token.leading_start_offset positioned_token;
48 leading_width = Token.leading_width positioned_token;
49 width = Token.width positioned_token;
50 trailing_width = Token.trailing_width positioned_token;
51 leading = Token.leading positioned_token;
52 trailing = Token.trailing positioned_token;
55 let from_positioned_syntax syntax =
57 source_text = Syntax.source_text syntax;
58 offset = Syntax.leading_start_offset syntax;
59 leading_width = Syntax.leading_width syntax;
60 width = Syntax.width syntax;
61 trailing_width = Syntax.trailing_width syntax;
62 leading = [];
63 trailing = [];
66 let source_text data =
67 data.source_text
69 let leading_width data =
70 data.leading_width
72 let width data =
73 data.width
75 let trailing_width data =
76 data.trailing_width
78 let leading_start_offset data =
79 data.offset
81 let leading data =
82 data.leading
84 let with_leading leading data =
85 (* TODO: Do we need to update leading_width and offset? *)
86 { data with leading }
88 let trailing data =
89 data.trailing
91 let start_offset data =
92 leading_start_offset data + leading_width data
94 let end_offset data =
95 start_offset data + width data
97 let full_width data =
98 leading_width data + width data + trailing_width data
100 let text data =
101 SourceText.sub (source_text data) (start_offset data) (width data)
104 * Merges two SourceDatas by computing the span between them.
106 * The resulting SourceData will have:
107 * These properties of the beginning SourceData:
108 * - SourceText
109 * - offset
110 * - leading_width
111 * - leading
112 * These properties of the ending SourceData:
113 * - trailing_width
114 * - trailing
116 * The width will be computed as the distance from the beginning SourceData's
117 * start_offset to the ending SourceData's end_offset.
119 let spanning_between b e =
121 source_text = b.source_text;
122 offset = b.offset;
123 leading_width = b.leading_width;
124 width = end_offset e - start_offset b;
125 trailing_width = e.trailing_width;
126 leading = b.leading;
127 trailing = e.trailing;
130 let to_json data =
131 let open Hh_json in
132 JSON_Object [
133 ("offset", int_ data.offset);
134 ("leading_width", int_ data.leading_width);
135 ("width", int_ data.width);
136 ("trailing_width", int_ data.trailing_width);