Permalink
Browse files

Store types directly in the full-fidelity typed positioned syntax tre…

…e, rather than JSON

Summary: This allows us to make use of a concrete syntax tree for other purposes than printing out the types as JSON. In particular, I'll use this for an AST-based search service.

Differential Revision: D7543621

fbshipit-source-id: c72b35026c67a0201fc5e9e54e5f4ec8083f0377
  • Loading branch information...
arxanas authored and hhvm-bot committed Apr 16, 2018
1 parent b923f82 commit 4f8461600294c20d40629f10597baf8055c4b208
@@ -76,7 +76,7 @@ module Value = struct
leading_width: int;
width: int; (* Width of node, not counting trivia *)
trailing_width: int;
tys: Hh_json.json list;
tys: Tast_type_collector.collected_type list;
position: Pos.absolute;
}
@@ -92,14 +92,16 @@ module Value = struct
(["position", pos_to_zero_indexed_json value.position
] @ match value.tys with
| [] -> []
| tys -> [("types", JSON_Array tys)])
| tys ->
let json_tys = Tast_type_collector.collected_types_to_json tys in
[("types", JSON_Array json_tys)])
end
module PositionedSyntaxValue = Full_fidelity_positioned_syntax.PositionedSyntaxValue
let positioned_value_to_typed
(position: Pos.absolute)
(types: Hh_json.json list)
(types: Tast_type_collector.collected_type list)
(value: PositionedSyntaxValue.t): Value.t =
{
Value.source_text = value.PositionedSyntaxValue.source_text;
@@ -8,51 +8,60 @@
*)
open Hh_core
open Hh_json
type collected_type = Typing_env.env * Typing_defs.phase_ty
class ['self] type_collector = object (_ : 'self)
inherit [_] Tast_visitor.reduce
method zero = Pos.AbsolutePosMap.empty
method plus = Pos.AbsolutePosMap.union ~combine:(fun _ a b -> Some (a @ b))
method! on_expr_annotation env (p,ty) =
Pos.AbsolutePosMap.singleton (Pos.to_absolute p) [Typing_print.to_json env ty]
Pos.AbsolutePosMap.singleton
(Pos.to_absolute p)
[(env, Typing_defs.LoclTy ty)]
method! on_class_id env (ty,cid) =
match cid with
| Tast.CI ((p,_),_) ->
Pos.AbsolutePosMap.singleton (Pos.to_absolute p) [Typing_print.to_json env ty]
Pos.AbsolutePosMap.singleton
(Pos.to_absolute p)
[(env, Typing_defs.LoclTy ty)]
| _ -> Pos.AbsolutePosMap.empty
method! on_hint (env: Typing_env.env) hint =
let (pos, _) = hint in
let ty = Decl_hint.hint env.Typing_env.decl_env hint in
Pos.AbsolutePosMap.singleton (Pos.to_absolute pos) [Typing_print.to_json env ty]
Pos.AbsolutePosMap.singleton
(Pos.to_absolute pos)
[(env, Typing_defs.DeclTy ty)]
end
let collect_types = new type_collector#go
let types_to_json tast =
let types_list =
tast
|> collect_types
|> Pos.AbsolutePosMap.elements
|> List.map ~f:(fun (pos, tys) ->
JSON_Array [Pos.json pos; JSON_Array tys])
in
JSON_Array types_list
let collected_types_to_json
(collected_types: collected_type list)
: Hh_json.json list =
List.map collected_types ~f:(fun (env, ty) ->
match ty with
| Typing_defs.DeclTy ty -> Typing_print.to_json env ty
| Typing_defs.LoclTy ty -> Typing_print.to_json env ty
)
(*
Ideally this would be just Pos.AbsolutePosMap.get, however the positions
in the Tast are off by 1 from positions in the full fidelity parse trees.
TODO: Fix this when the full fidelity parse tree becomes the parser for type checking.
*)
let get_from_pos_map (position: Pos.absolute) (map: Hh_json.json list Pos.AbsolutePosMap.t) =
let get_from_pos_map
(position: Pos.absolute)
(map: collected_type list Pos.AbsolutePosMap.t) =
let rec aux es =
match es with
| [] -> []
| (pos, tys) :: tl ->
if ((Pos.start_cnum pos) == (Pos.start_cnum position)
&& (Pos.end_cnum pos) == (1 + (Pos.end_cnum position))) then
if ((Pos.start_cnum pos) = (Pos.start_cnum position)
&& (Pos.end_cnum pos) = (1 + (Pos.end_cnum position))) then
tys
else
aux tl
@@ -17,7 +17,7 @@ module TypedTree = Full_fidelity_syntax_tree
let typed_from_positioned
(file: Relative_path.t)
(map: Hh_json.json list Pos.AbsolutePosMap.t)
(map: Tast_type_collector.collected_type list Pos.AbsolutePosMap.t)
(tree: PositionedTree.t): TypedTree.t =
let rec aux (positioned_node: PS.t): TS.t =
let value = PS.value positioned_node in

0 comments on commit 4f84616

Please sign in to comment.