@@ -76,6 +76,14 @@ end
7676-- @type AstElement
7777-- @local
7878local AstElement = Type:make_subtype 'AstElement'
79+AstElement.__call = function(t, ...)
80+ local success, ret = pcall(t.constructor, ...)
81+ if success then
82+ return setmetatable(ret, t.behavior)
83+ else
84+ error(string.format('Constructor for %s failed: %s\n', t.name, ret))
85+ end
86+end
7987
8088--- Create a new constructor
8189-- @local
@@ -144,7 +152,7 @@ end
144152--- Create a new element given its tag and arguments
145153-- @local
146154function AstElement.new(constr, ...)
147− local element = { t = constr.__type.name }
155+ local element = {}
148156 local content = {...}
149157 -- special case for unary constructors
150158 if #content == 1 then
@@ -167,7 +175,8 @@ end
167175-- @function Pandoc
168176-- @tparam {Block,...} blocks document content
169177-- @tparam[opt] Meta meta document meta data
170−function M.Pandoc(blocks, meta)
178+M.Pandoc = AstElement:make_subtype'Pandoc'
179+function M.Pandoc.constructor (blocks, meta)
171180 meta = meta or {}
172181 return {
173182 ["blocks"] = List:new(blocks),
@@ -187,20 +196,18 @@ M.Doc = M.Pandoc
187196--- `Meta`.
188197-- @function Meta
189198-- @tparam meta table table containing document meta information
190−M.Meta = {}
191−M.Meta.__call = function(t, meta)
192− return setmetatable(meta, t)
193−end
194−setmetatable(M.Meta, M.Meta)
199+M.Meta = AstElement:make_subtype'Meta'
200+M.Meta.constructor = function (meta) return meta end
195201
196202
197203------------------------------------------------------------------------
198204-- MetaValue
199205-- @section MetaValue
200−M.MetaValue = AstElement:make_subtype{}
201−M.MetaValue.__call = function(t, ...)
206+M.MetaValue = AstElement:make_subtype('MetaValue')
207+M.MetaValue.__call = function (t, ...)
202208 return t:new(...)
203209end
210+
204211--- Meta blocks
205212-- @function MetaBlocks
206213-- @tparam {Block,...} blocks blocks
@@ -256,7 +263,7 @@ end
256263
257264--- Block elements
258265-- @type Block
259−M.Block = AstElement:make_subtype{}
266+M.Block = AstElement:make_subtype'Block'
260267M.Block.__call = function (t, ...)
261268 return t:new(...)
262269end
@@ -431,7 +438,7 @@ M.Table = M.Block:create_constructor(
431438
432439--- Inline element class
433440-- @type Inline
434−M.Inline = AstElement:make_subtype{}
441+M.Inline = AstElement:make_subtype'Inline'
435442M.Inline.__call = function (t, ...)
436443 return t:new(...)
437444end
@@ -794,7 +801,7 @@ end
794801setmetatable(M.Attr, M.Attr)
795802
796803-- Citation
797−M.Citation = {}
804+M.Citation = AstElement:make_subtype'Citation'
798805
799806--- Creates a single citation.
800807-- @function Citation
@@ -804,20 +811,16 @@ M.Citation = {}
804811-- @tparam[opt] {Inline,...} suffix citation suffix
805812-- @tparam[opt] int note_num note number
806813-- @tparam[opt] int hash hash number
807−M.Citation.__call = function(t, id, mode, prefix, suffix, note_num, hash)
808− return setmetatable(
809− {
810− id = id,
811− mode = mode,
812− prefix = prefix or {},
813− suffix = suffix or {},
814− note_num = note_num or 0,
815− hash = hash or 0,
816− },
817− t
818− )
814+function M.Citation.constructor (id, mode, prefix, suffix, note_num, hash)
815+ return {
816+ id = id,
817+ mode = mode,
818+ prefix = prefix or {},
819+ suffix = suffix or {},
820+ note_num = note_num or 0,
821+ hash = hash or 0,
822+ }
819823end
820−setmetatable(M.Citation, M.Citation)
821824
822825
823826------------------------------------------------------------------------