Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nameconflict #125

Merged
merged 6 commits into from
Apr 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 3 additions & 12 deletions deps/SnoopCompile/precompile/precompile_AcuteML.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,9 @@ function _precompile_()
precompile(Tuple{Core.kwftype(typeof(AcuteML.render2file)),NamedTuple{(:id, :age, :field, :GPA, :courses),Tuple{Int64,Int64,String,Float64,Array{String,1}}},typeof(render2file),String,Bool})
precompile(Tuple{typeof(AcuteML.aml_create),Expr,Array{Union{Expr, Symbol},1},Array{Any,1},Array{Union{Expr, Symbol, Type},1},Array{Union{Expr, Symbol},1},Array{Union{Missing, String},1},Array{Union{Missing, Function, Symbol},1},Array{Union{Missing, Type},1},String,Type{T} where T,Array{Union{Missing, Function, Symbol},0},Bool,Array{Union{Nothing, Expr},1},Array{Union{Nothing, Expr},1},Array{Union{Nothing, Expr},1},Symbol})
precompile(Tuple{typeof(AcuteML.aml_parse),Expr})
precompile(Tuple{typeof(AcuteML.get_arg_xmlcreator),Nothing,Bool,Expr,Symbol,String,Type{T} where T,Missing,QuoteNode,Expr})
precompile(Tuple{typeof(AcuteML.get_arg_xmlcreator),Nothing,Bool,Symbol,Symbol,String,Type{T} where T,Missing,QuoteNode,Expr})
precompile(Tuple{typeof(AcuteML.get_arg_xmlcreator),Nothing,Bool,Symbol,Symbol,String,Type{T} where T,Symbol,QuoteNode,Expr})
precompile(Tuple{typeof(AcuteML.get_arg_xmlcreator),Nothing,Bool,Type{T} where T,Symbol,String,Type{T} where T,Missing,QuoteNode,Expr})
precompile(Tuple{typeof(AcuteML.get_arg_xmlextractor),Nothing,Bool,Expr,Symbol,String,Type{T} where T,Missing,QuoteNode,Expr})
precompile(Tuple{typeof(AcuteML.get_arg_xmlextractor),Nothing,Bool,Symbol,Symbol,String,Type{T} where T,Missing,QuoteNode,Expr})
precompile(Tuple{typeof(AcuteML.get_arg_xmlextractor),Nothing,Bool,Symbol,Symbol,String,Type{T} where T,Symbol,QuoteNode,Expr})
precompile(Tuple{typeof(AcuteML.get_arg_xmlextractor),Nothing,Bool,Type{T} where T,Symbol,String,Type{T} where T,Missing,QuoteNode,Expr})
precompile(Tuple{typeof(AcuteML.get_arg_xmludpater),Nothing,Bool,Expr,Symbol,String,Type{T} where T,Missing,QuoteNode,Expr})
precompile(Tuple{typeof(AcuteML.get_arg_xmludpater),Nothing,Bool,Symbol,Symbol,String,Type{T} where T,Missing,QuoteNode,Expr})
precompile(Tuple{typeof(AcuteML.get_arg_xmludpater),Nothing,Bool,Symbol,Symbol,String,Type{T} where T,Symbol,QuoteNode,Expr})
precompile(Tuple{typeof(AcuteML.get_arg_xmludpater),Nothing,Bool,Type{T} where T,Symbol,String,Type{T} where T,Missing,QuoteNode,Expr})
precompile(Tuple{typeof(AcuteML.get_arg_xmlcreator),Nothing,Bool,Expr,Expr,String,Type{T} where T,Expr,QuoteNode,Expr})
precompile(Tuple{typeof(AcuteML.get_arg_xmlextractor),Nothing,Bool,Expr,Expr,String,Type{T} where T,Expr,QuoteNode,Expr})
precompile(Tuple{typeof(AcuteML.get_arg_xmludpater),Nothing,Bool,Expr,Expr,String,Type{T} where T,Expr,QuoteNode,Expr})
precompile(Tuple{typeof(AcuteML.multiString),Array{String,1}})
precompile(Tuple{typeof(AcuteML.multiString),Float64})
precompile(Tuple{typeof(AcuteML.multiString),Int64})
Expand Down
15 changes: 9 additions & 6 deletions src/@aml/@aml_create.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function aml_create(expr::Expr, args_param, args_defaultvalue, args_type, args_v
##########################
has_arg_xmlchecker = !ismissing(argfunction)

inps = (has_arg_xmlchecker, argtype, argvar, argname, argliteraltype, argfunction, argsym, argvarcall)
inps = (has_arg_xmlchecker, esc(argtype), esc(argvar), argname, argliteraltype, esc(argfunction), argsym, argvarcall)

args_xmlcreator[iArg]=get_arg_xmlcreator(argcustomcreator, inps...)

Expand Down Expand Up @@ -98,18 +98,21 @@ function aml_create(expr::Expr, args_param, args_defaultvalue, args_type, args_v

struct_definition =:($expr)

struct_xmlchecker = get_struct_xmlchecker(struct_function, args_var)
# args_var is used here to support all the inputs (even non-aml)
esc_args_var = esc.(args_var)

struct_xmlchecker = get_struct_xmlchecker(struct_function, esc_args_var)

# Creator
struct_xmlcreator = get_struct_xmlcreator(S, amlargs_param, struct_xmlchecker, node_initializer, args_xmlcreator, args_var, custom_creator_end)
struct_xmlcreator = get_struct_xmlcreator(S, amlargs_param, struct_xmlchecker, node_initializer, args_xmlcreator, esc_args_var, custom_creator_end)
# Extractor
struct_xmlextractor = get_struct_xmlextractor(S, args_xmlextractor, struct_xmlchecker, args_var, custom_extractor_end)
struct_xmlextractor = get_struct_xmlextractor(S, args_xmlextractor, struct_xmlchecker, esc_args_var, custom_extractor_end)

if iscurly
# Creator
struct_xmlcreator_curly = get_struct_xmlcreator(SQ, P, amlargs_param, struct_xmlchecker, node_initializer, args_xmlcreator, args_var, custom_creator_end)
struct_xmlcreator_curly = get_struct_xmlcreator(SQ, P, amlargs_param, struct_xmlchecker, node_initializer, args_xmlcreator, esc_args_var, custom_creator_end)
# Extractor
struct_xmlextractor_curly = get_struct_xmlextractor(SQ, P, args_xmlextractor, struct_xmlchecker, args_var, custom_extractor_end)
struct_xmlextractor_curly = get_struct_xmlextractor(SQ, P, args_xmlextractor, struct_xmlchecker, esc_args_var, custom_extractor_end)
else
struct_xmlcreator_curly = nothing
struct_xmlextractor_curly = nothing
Expand Down
20 changes: 9 additions & 11 deletions src/@aml/@aml_create/get_arg_xml_.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
"""
Get a argument creator expression
"""
function get_arg_xmlcreator(argcustomcreator, has_arg_xmlchecker::Bool, argtype, argvar, argname, argliteraltype, argfunction, argsym, argvarcall)
esc_argvar = esc(argvar)
function get_arg_xmlcreator(argcustomcreator, has_arg_xmlchecker::Bool, argtype, esc_argvar, argname, argliteraltype, esc_argfunction, argsym, argvarcall)

if !has_arg_xmlchecker
arg_creator = quote
Expand All @@ -15,7 +14,7 @@ function get_arg_xmlcreator(argcustomcreator, has_arg_xmlchecker::Bool, argtype,
else
arg_creator=quote
$(esc(argcustomcreator))
if isnothing($esc_argvar) || ($(esc(argfunction)))($esc_argvar)
if isnothing($esc_argvar) || ($esc_argfunction)($esc_argvar)
addelm!(aml, $argname, $esc_argvar, $argliteraltype)
else
error("$($argname) doesn't meet criteria function")
Expand All @@ -28,21 +27,20 @@ end
"""
Get a argument extractor expression
"""
function get_arg_xmlextractor(argcustomextractor, has_arg_xmlchecker::Bool, argtype, argvar, argname, argliteraltype, argfunction, argsym, argvarcall)
esc_argvar = esc(argvar)
function get_arg_xmlextractor(argcustomextractor, has_arg_xmlchecker::Bool, esc_argtype, esc_argvar, argname, argliteraltype, esc_argfunction, argsym, argvarcall)

if !has_arg_xmlchecker
arg_extractor=quote
$(esc(argcustomextractor))
$esc_argvar = findcontent($(esc(argtype)), $argname, aml, $argliteraltype)
$esc_argvar = findcontent($esc_argtype, $argname, aml, $argliteraltype)
end
else
arg_extractor=quote
$(esc(argcustomextractor))

$esc_argvar = findcontent($(esc(argtype)), $argname, aml, $argliteraltype)
$esc_argvar = findcontent($esc_argtype, $argname, aml, $argliteraltype)

if !isnothing($esc_argvar) && !(($(esc(argfunction)))($esc_argvar))
if !isnothing($esc_argvar) && !(($esc_argfunction)($esc_argvar))
error("$($argname) doesn't meet criteria function")
end
end
Expand All @@ -53,7 +51,7 @@ end
"""
Get a argument updater expression
"""
function get_arg_xmludpater(argcustomupdater, has_arg_xmlchecker::Bool, argtype, argvar, argname, argliteraltype, argfunction, argsym, argvarcall)
function get_arg_xmludpater(argcustomupdater, has_arg_xmlchecker::Bool, esc_argtype, esc_argvar, argname, argliteraltype, esc_argfunction, argsym, argvarcall)

if !has_arg_xmlchecker
arg_updater = quote
Expand All @@ -68,7 +66,7 @@ function get_arg_xmludpater(argcustomupdater, has_arg_xmlchecker::Bool, argtype,
$(esc(argcustomupdater))

if name == $argsym
if isnothing($(argvarcall)) || ($(esc(argfunction)))($(argvarcall))
if isnothing($(argvarcall)) || ($esc_argfunction)($(argvarcall))
updatecontent!(value, $argname, str.aml, $argliteraltype)
else
error("$($argname) doesn't meet criteria function")
Expand Down Expand Up @@ -105,7 +103,7 @@ function parse_argtype(argtype)
# elseif isa(argtype, Symbol) ||
# (isa(argtype, Expr) && argtype.args[1] == :Union) ||
# (isa(argtype, Expr) && argtype.args[1] == :UN) ||
# !(argtype <: AbstractVector)
# !(esc_argtype <: AbstractVector)
else
arg_parsedtype = Any
end
Expand Down
22 changes: 12 additions & 10 deletions src/@aml/@aml_create/get_struct_xml_.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
"""
check function for the aml struct
"""
function get_struct_xmlchecker(struct_function, args_var)
function get_struct_xmlchecker(struct_function, esc_args_var)
# aml Function
if !ismissing(struct_function[1])
F = struct_function[1]
struct_xmlchecker = quote
if !( ($(esc(F)))($(esc.(args_var)...)) )
if !( ($(esc(F)))($(esc_args_var...)) )
error("struct criteria function ($($(esc(F)))) isn't meet")
end
end
Expand All @@ -18,49 +18,51 @@ function get_struct_xmlchecker(struct_function, args_var)
return struct_xmlchecker
end
################################################################
function get_struct_xmlcreator(T, args_param, struct_xmlchecker, node_initializer, args_xmlcreator, args_var, custom_creator_end)
function get_struct_xmlcreator(T, args_param, struct_xmlchecker, node_initializer, args_xmlcreator, esc_args_var, custom_creator_end)
struct_xmlcreator = quote
function ($(esc(T)))(; $(esc.(args_param)...))
$struct_xmlchecker
$node_initializer
$(args_xmlcreator...)
$(esc(custom_creator_end))
return ($(esc(T)))($(esc.(args_var)...), aml)
return ($(esc(T)))($(esc_args_var...), aml)
end
end
return struct_xmlcreator
end
function get_struct_xmlcreator(SQ, P, args_param, struct_xmlchecker, node_initializer, args_xmlcreator, args_var, custom_creator_end)
function get_struct_xmlcreator(SQ, P, args_param, struct_xmlchecker, node_initializer, args_xmlcreator, esc_args_var, custom_creator_end)
struct_xmlcreator_curly = quote
function ($(esc(SQ)))(; $(esc.(args_param)...)) where {$(esc.(P)...)}
$struct_xmlchecker
$node_initializer
$(args_xmlcreator...)
$(esc(custom_creator_end))
return ($(esc(SQ)))($(esc.(args_var)...), aml)
return ($(esc(SQ)))($(esc_args_var...), aml)
end
end
return struct_xmlcreator_curly
end
################################################################
function get_struct_xmlextractor(T, args_xmlextractor, struct_xmlchecker, args_var, custom_extractor_end)
function get_struct_xmlextractor(T, args_xmlextractor, struct_xmlchecker, esc_args_var, custom_extractor_end)

struct_xmlextractor = quote
function ($(esc(T)))(aml::Union{Document, Node})
$(args_xmlextractor...)
$struct_xmlchecker
$(esc(custom_extractor_end))
return ($(esc(T)))($(esc.(args_var)...), aml)
return ($(esc(T)))($(esc_args_var...), aml)
end
end
return struct_xmlextractor
end
function get_struct_xmlextractor(SQ, P, args_xmlextractor, struct_xmlchecker, args_var, custom_extractor_end)
function get_struct_xmlextractor(SQ, P, args_xmlextractor, struct_xmlchecker, esc_args_var, custom_extractor_end)

struct_xmlextractor_curly = quote
function ($(esc(SQ)))(aml::Union{Document, Node}) where {$(esc.(P)...)}
$(args_xmlextractor...)
$struct_xmlchecker
$(esc(custom_extractor_end))
return ($(esc(SQ)))($(esc.(args_var)...), aml)
return ($(esc(SQ)))($(esc_args_var...), aml)
end
end
return struct_xmlextractor_curly
Expand Down
42 changes: 40 additions & 2 deletions src/AcuteML.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,14 @@ export Node, Document
@aml

# Type Definition
Use `@aml` macro to define a Julia type, and then the package automatically creates a xml or html associated with the defined type.
Use `@aml` macro to define a Julia type, and then the package automatically creates a xml or html associated with the defined type. The general syntax would look like this:
```julia
@aml mutable struct mybody "body"
myh1, "h1"
p::Vector{String}, "~"
end
```
Now, we go into the details:

### Document Definition
* Use doc literal before the root name to define a HTML or XML document. For HTML documents root should always be "html".
Expand All @@ -40,7 +47,7 @@ end
```

### Nodes (Elements) Definition
* Specify the html/xml struct name as a string after the struct name after a space
* Specify the html/xml name of struct as a string after the struct name (after a space)
```julia
@aml mutable struct Person "person"
# add fields (elements) here
Expand All @@ -63,6 +70,29 @@ field, "study-field"
age::UInt, "~"
```

!!! warning
The field names of a struct should not be the same as other defined types. This error happens when you use the same name of a type for a field name. For example, the follwing is an **error**:
```julia
@aml struct person "~"
name, "~"
end
@aml struct myxml doc"~"
person::person, "~"
end
```
Another example of this error:
```julia
@aml struct myxml doc"~"
Int, "myint"
end
```
However, you can choose any xml/html name. The xml/html name of the fields isn't related to the types defined in Julia. So the following is a **valid** syntax:
```julia
@aml struct myxml doc"~"
myint, "Int"
end
```

### Attributes
* If the value is going to be an attribute put `att` before its name
```julia
Expand Down Expand Up @@ -378,6 +408,14 @@ macro aml(expr)
# args_param.args # empty
expr.args[3], args_param, args_defaultvalue, args_type, args_var, args_name, args_function, args_literaltype, struct_name, struct_nodetype, struct_function, is_struct_mutable, args_custom_creator, args_custom_extractor, args_custom_updater, T = aml_parse(expr)

# check if the field name is a defined type
for arg_var in args_var
if isdefined(__module__, arg_var) && isa(getfield(__module__, arg_var), Type)
f, l = __source__.file, __source__.line
@error "Change the field name `$arg_var` in struct `$T` to something else. The name conflicts with an already defined type name. \n Happens at $f:$l"
end
end

out = aml_create(expr, args_param, args_defaultvalue, args_type, args_var, args_name, args_function, args_literaltype, struct_name, struct_nodetype, struct_function, is_struct_mutable, args_custom_creator, args_custom_extractor, args_custom_updater, T)

# elseif expr isa Expr && expr.head == :tuple
Expand Down