Multiply entities beyond necessity even more (force better build parallelism)
[hiphop-php.git] / hphp / hack / src / parser / full_fidelity_editable_trivia.ml
blob4a69f4e9bf605b435bee61a557edeee3683fce8a
1 (*
2 * Copyright (c) 2016, Facebook, Inc.
3 * All rights reserved.
5 * This source code is licensed under the MIT license found in the
6 * LICENSE file in the "hack" directory of this source tree.
8 *)
10 (**
11 * An editable trivia contains the text of the trivia; these trivia are not
12 * backed by a source text, like the positioned trivia are.
15 module TriviaKind = Full_fidelity_trivia_kind
16 module PositionedTrivia = Full_fidelity_positioned_trivia
17 module SourceText = Full_fidelity_source_text
19 type t = {
20 kind: TriviaKind.t;
21 text: string;
23 [@@deriving show]
25 let make_ignore_error source_text offset width =
27 kind = TriviaKind.IgnoreError;
28 text = SourceText.sub source_text offset width;
31 let make_extra_token_error source_text offset width =
33 kind = TriviaKind.ExtraTokenError;
34 text = SourceText.sub source_text offset width;
37 let make_fallthrough source_text offset width =
39 kind = TriviaKind.FallThrough;
40 text = SourceText.sub source_text offset width;
43 let make_fix_me source_text offset width =
44 { kind = TriviaKind.FixMe; text = SourceText.sub source_text offset width }
46 let make_whitespace source_text offset width =
48 kind = TriviaKind.WhiteSpace;
49 text = SourceText.sub source_text offset width;
52 let make_eol source_text offset width =
54 kind = TriviaKind.EndOfLine;
55 text = SourceText.sub source_text offset width;
58 let make_single_line_comment source_text offset width =
60 kind = TriviaKind.SingleLineComment;
61 text = SourceText.sub source_text offset width;
64 let make_delimited_comment source_text offset width =
66 kind = TriviaKind.DelimitedComment;
67 text = SourceText.sub source_text offset width;
70 let make_after_halt_compiler source_text offset width =
72 kind = TriviaKind.AfterHaltCompiler;
73 text = SourceText.sub source_text offset width;
76 (* HackFormat is using this to create trivia. It's deeply manipulating strings
77 * so it was easier to create this helper function to avoid ad-hoc
78 * SourceText construction.*)
79 let create_delimited_comment text =
80 { kind = TriviaKind.DelimitedComment; text }
82 let width trivia = String.length trivia.text
84 let kind trivia = trivia.kind
86 let text trivia = trivia.text
88 let text_from_trivia_list trivia_list =
89 (* TODO: Better way to accumulate a string? *)
90 let folder str trivia = str ^ text trivia in
91 List.fold_left folder "" trivia_list
93 let from_positioned source_text positioned_trivia offset =
94 let kind = PositionedTrivia.kind positioned_trivia in
95 let width = PositionedTrivia.width positioned_trivia in
96 let text = SourceText.sub source_text offset width in
97 { kind; text }
99 let from_positioned_list source_text ts offset =
100 let rec aux acc ts offset =
101 match ts with
102 | [] -> acc
103 | h :: t ->
104 let et = from_positioned source_text h offset in
105 aux (et :: acc) t (offset + PositionedTrivia.width h)
107 List.rev (aux [] ts offset)
109 let to_json trivia =
110 Hh_json.(
111 JSON_Object
113 ("kind", JSON_String (TriviaKind.to_string trivia.kind));
114 ("text", JSON_String trivia.text);