2 * Copyright (c) 2017, Facebook, Inc.
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.
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
21 * Data about the token with respect to the original source text.
24 source_text
: SourceText.t
;
25 offset
: int; (* Beginning of first trivia *)
27 width
: int; (* Width of actual token, not counting trivia *)
29 leading
: Trivia.t list
;
30 trailing
: Trivia.t list
;
35 source_text
= SourceText.empty;
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
;
66 let source_text data
=
69 let leading_width data
=
75 let trailing_width data
=
78 let leading_start_offset data
=
84 let with_leading leading data
=
85 (* TODO: Do we need to update leading_width and offset? *)
91 let start_offset data
=
92 leading_start_offset data
+ leading_width data
95 start_offset data
+ width data
98 leading_width data
+ width data
+ trailing_width 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:
112 * These properties of the ending SourceData:
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;
123 leading_width = b
.leading_width;
124 width = end_offset e
- start_offset b
;
125 trailing_width = e
.trailing_width;
127 trailing = e
.trailing;
133 ("offset", int_ data
.offset
);
134 ("leading_width", int_ data
.leading_width);
135 ("width", int_ data
.width);
136 ("trailing_width", int_ data
.trailing_width);