From 4860e2d602ee3431fe08e31acb8901dd0dfd1d10 Mon Sep 17 00:00:00 2001 From: Dan Korostelev Date: Sun, 2 Jul 2017 14:56:30 +0300 Subject: [PATCH] support named types (see https://github.com/HaxeFoundation/haxe/pull/6428) --- src/syntax/ast.ml | 2 ++ src/syntax/astEmitter.ml | 1 + src/syntax/basic/emitter.mli | 1 + src/syntax/grammars/parser.mly | 5 ++++- src/syntax/jsonEmitter.ml | 4 ++++ src/syntax/nullEmitter.ml | 1 + src/syntax/symbolPrinter.ml | 1 + 7 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/syntax/ast.ml b/src/syntax/ast.ml index b5f8a46..e32f7d8 100644 --- a/src/syntax/ast.ml +++ b/src/syntax/ast.ml @@ -46,6 +46,7 @@ and complex_type = | CTParent of type_hint | CTExtend of placed_type_path list * class_field list | CTOptional of type_hint + | CTNamed of placed_name * type_hint and type_hint = complex_type * pos @@ -311,6 +312,7 @@ let s_expr e = | CTParent(t,_) -> "(" ^ s_complex_type tabs t ^ ")" | CTOptional(t,_) -> "?" ^ s_complex_type tabs t | CTExtend (tl, fl) -> "{> " ^ String.concat " >, " (List.map (s_complex_type_path tabs) tl) ^ ", " ^ String.concat ", " (List.map (s_class_field tabs) fl) ^ " }" + | CTNamed ((n,_),(t,_)) -> n ^ " : " ^ s_complex_type tabs t and s_class_field tabs f = match f.cff_doc with | Some s -> "/**\n\t" ^ tabs ^ s ^ "\n**/\n" diff --git a/src/syntax/astEmitter.ml b/src/syntax/astEmitter.ml index aeb78cc..26ba883 100644 --- a/src/syntax/astEmitter.ml +++ b/src/syntax/astEmitter.ml @@ -259,6 +259,7 @@ let emit_complex_type_parent ct p = CTParent ct,p let emit_complex_type_extension paths fields p = CTExtend(paths,fields),p let emit_complex_type_anonymous fields p = CTAnonymous fields,p let emit_complex_type_optional ct p = CTOptional ct,p +let emit_complex_type_named n ct p = CTNamed (n,ct),p let emit_complex_type_function ct1 ct2 p = match fst ct2 with diff --git a/src/syntax/basic/emitter.mli b/src/syntax/basic/emitter.mli index b34ff5d..e35d206 100644 --- a/src/syntax/basic/emitter.mli +++ b/src/syntax/basic/emitter.mli @@ -134,6 +134,7 @@ module type Sig = sig val emit_complex_type_anonymous : t_anonymous_type_fields -> pos -> t_complex_type val emit_complex_type_optional : t_complex_type -> pos -> t_complex_type val emit_complex_type_function : t_complex_type -> t_complex_type -> pos -> t_complex_type + val emit_complex_type_named : string t_pos -> t_complex_type -> pos -> t_complex_type val emit_type_path_parameter_complex_type : t_complex_type -> t_type_path_parameter_kind val emit_type_path_parameter_bracket : t_expr list -> pos -> t_type_path_parameter_kind diff --git a/src/syntax/grammars/parser.mly b/src/syntax/grammars/parser.mly index 9b7bfc5..3a992e4 100644 --- a/src/syntax/grammars/parser.mly +++ b/src/syntax/grammars/parser.mly @@ -349,6 +349,9 @@ complex_type_anonymous: complex_type_optional: | QUESTIONMARK; ct = complex_type; { emit_complex_type_optional ct (mk $startpos $endpos) } +complex_type_named: + | name = pos(dollar_ident); COLON; ct = complex_type { emit_complex_type_named name ct (mk $startpos $endpos) } + complex_type_path: | tp = type_path { emit_complex_type_path tp (mk $startpos $endpos) } @@ -358,7 +361,7 @@ complex_type_function: } complex_type: - | complex_type_parent | complex_type_extension | complex_type_anonymous | complex_type_optional + | complex_type_parent | complex_type_extension | complex_type_anonymous | complex_type_optional | complex_type_named | complex_type_path | complex_type_function { $1 } type_path_parameter_bracket: diff --git a/src/syntax/jsonEmitter.ml b/src/syntax/jsonEmitter.ml index c74dbf0..59ed313 100644 --- a/src/syntax/jsonEmitter.ml +++ b/src/syntax/jsonEmitter.ml @@ -99,6 +99,7 @@ module EnumApi = struct | StructuralExtension | AnonymousStructure | Optional + | Named | TypePath | CTFunction @@ -544,6 +545,9 @@ module JsonEmitter(Api : JsonApi) = struct let emit_complex_type_optional ct p = enum "ComplexType" Optional [tok;ct] + let emit_complex_type_named n ct p = + enum "ComplexType" Named [n;tok;ct] + let emit_complex_type_function ct1 ct2 p = enum "ComplexType" Function [ct1;tok;ct2] (* TODO: Compose these here? *) diff --git a/src/syntax/nullEmitter.ml b/src/syntax/nullEmitter.ml index de2ea4a..a3829e8 100644 --- a/src/syntax/nullEmitter.ml +++ b/src/syntax/nullEmitter.ml @@ -181,6 +181,7 @@ let emit_complex_type_parent ct p = () let emit_complex_type_extension paths fields p = () let emit_complex_type_anonymous fields p = () let emit_complex_type_optional ct p = () +let emit_complex_type_named n ct p = () let emit_complex_type_function ct1 ct2 p = () diff --git a/src/syntax/symbolPrinter.ml b/src/syntax/symbolPrinter.ml index e340c10..b064834 100644 --- a/src/syntax/symbolPrinter.ml +++ b/src/syntax/symbolPrinter.ml @@ -254,6 +254,7 @@ let s_xsymbol x = | N_complex_type_extension -> "extension" | N_complex_type_anonymous -> "anonymous" | N_complex_type_optional -> "optional" + | N_complex_type_named -> "named" | N_complex_type_path -> "path" | N_complex_type_function -> "function" | N_macro_expr_type_hint -> "macro_type_hint"