Skip to content

Commit

Permalink
generate default values in xml (closes HaxeFoundation#2677)
Browse files Browse the repository at this point in the history
  • Loading branch information
Simn committed Feb 27, 2014
1 parent 54ebe3c commit eaed45b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
33 changes: 30 additions & 3 deletions genxml.ml
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,37 @@ let gen_meta meta =
) meta in
[node "meta" [] nodes]

let rec gen_type t =
let rec gen_type ?(tfunc=None) t =
match t with
| TMono m -> (match !m with None -> tag "unknown" | Some t -> gen_type t)
| TEnum (e,params) -> gen_type_decl "e" (TEnumDecl e) params
| TInst (c,params) -> gen_type_decl "c" (TClassDecl c) params
| TAbstract (a,params) -> gen_type_decl "x" (TAbstractDecl a) params
| TType (t,params) -> gen_type_decl "t" (TTypeDecl t) params
| TFun (args,r) -> node "f" ["a",String.concat ":" (List.map gen_arg_name args)] (List.map gen_type (List.map (fun (_,opt,t) -> if opt then follow_param t else t) args @ [r]))
| TFun (args,r) ->
let s_const ct = match ct with
| TString s -> Printf.sprintf "'%s'" (Ast.s_escape s)
| _ -> s_const ct
in
let names = String.concat ":" (List.map gen_arg_name args) in
let values = match tfunc with
| None ->
[]
| Some tfunc ->
let has_value = ref false in
let values = List.map (fun (_,cto) -> match cto with
| None ->
""
| Some ct ->
has_value := true;
s_const ct
) tfunc.tf_args in
if !has_value then
["v",String.concat ":" values]
else
[]
in
node "f" (("a",names) :: values) (List.map gen_type (List.map (fun (_,opt,t) -> if opt then follow_param t else t) args @ [r]))
| TAnon a -> node "a" [] (pmap (fun f -> gen_field [] { f with cf_public = false }) a.a_fields)
| TDynamic t2 -> node "d" [] (if t == t2 then [] else [gen_type t2])
| TLazy f -> gen_type (!f())
Expand Down Expand Up @@ -129,7 +152,11 @@ and gen_field att f =
| [] -> []
| nl -> [node "overloads" [] nl]
in
node f.cf_name (if f.cf_public then ("public","1") :: att else att) (gen_type f.cf_type :: gen_meta f.cf_meta @ gen_doc_opt f.cf_doc @ overloads)
let tfunc = match f.cf_expr with
| Some ({eexpr = TFunction tf}) -> Some tf
| _ -> None
in
node f.cf_name (if f.cf_public then ("public","1") :: att else att) (gen_type ~tfunc:tfunc f.cf_type :: gen_meta f.cf_meta @ gen_doc_opt f.cf_doc @ overloads)

let gen_constr e =
let doc = gen_doc_opt e.ef_doc in
Expand Down
5 changes: 3 additions & 2 deletions std/haxe/rtti/CType.hx
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ typedef Path = String

typedef Platforms = List<String>

typedef FunctionArgument = { name : String, opt : Bool, t : CType, ?value:String }
enum CType {
CUnknown;
CEnum( name : Path, params : List<CType> );
CClass( name : Path, params : List<CType> );
CTypedef( name : Path, params : List<CType> );
CFunction( args : List<{ name : String, opt : Bool, t : CType }>, ret : CType );
CFunction( args : List<FunctionArgument>, ret : CType );
CAnonymous( fields : List<ClassField> );
CDynamic( ?t : CType );
CAbstract( name : Path, params : List<CType> );
Expand Down Expand Up @@ -205,7 +206,7 @@ class TypeApi {
case CFunction(args,ret):
switch( t2 ) {
case CFunction(args2,ret2):
return leq(function(a,b) {
return leq(function(a:FunctionArgument,b:FunctionArgument) {
return a.name == b.name && a.opt == b.opt && typeEq(a.t,b.t);
},args,args2) && typeEq(ret,ret2);
default:
Expand Down
3 changes: 3 additions & 0 deletions std/haxe/rtti/XmlParser.hx
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@ class XmlParser {
var args = new List();
var aname = x.att.a.split(":");
var eargs = aname.iterator();
var evalues = x.has.v ? x.att.v.split(":").iterator() : null;
for( e in x.elements ) {
var opt = false;
var a = eargs.next();
Expand All @@ -569,10 +570,12 @@ class XmlParser {
opt = true;
a = a.substr(1);
}
var v = evalues == null ? null : evalues.next();
args.add({
name : a,
opt : opt,
t : xtype(e),
value : v
});
}
var ret = args.last();
Expand Down

0 comments on commit eaed45b

Please sign in to comment.