Index: cortav.lua ================================================================== --- cortav.lua +++ cortav.lua @@ -118,10 +118,11 @@ new.generation = 1 end end; fns = { fail = function(self, msg, ...) + --error(string.format(msg,...)) ct.exns.tx(msg, self.src.file, self.line or 0, ...):throw() end; insert = function(self, block) block.origin = self:clone() table.insert(self.sec.blocks,block) @@ -161,20 +162,30 @@ if rid then return rid, id, sec end end - for _, i in ipairs(sec.imports) do + if sec.imports.objs[id] then + local ol = sec.imports.objs[id] + return ol.obj, id, ol.sec + end + + for _, i in ipairs(sec.imports.scope) do local rr, ri, rs = checkFromSec(i, doc) if ri then return rr, ri, rs end end if doc.sections[id] then return nil, id, doc.sections[id] end - for _, i in ipairs(doc.globals) do + if doc.globals.objs[id] then + local ol = doc.globals.objs[id] + return ol.obj, id, ol.sec + end + + for _, i in ipairs(doc.globals.scope) do local rr, ri, rs = checkFromSec(i, doc) if ri then return rr, ri, rs end end else local secid, ref = string.match(id, "(.-)%.(.+)") @@ -233,11 +244,11 @@ ct.sec = declare { ident = 'section'; mk = function() return { blocks = {}; refs = {}; - imports = {}; + imports = {scope={}, objs={}}; depth = 0; kind = 'ordinary'; } end; construct = function(self, id, depth) self.id = id @@ -368,11 +379,11 @@ return newdoc, nctx end; }; mk = function(...) return { sections = {}; - globals = {}; + globals = {objs={},scope={}}; secorder = {}; embed = {}; meta = {}; vars = {}; parents = {...}; @@ -961,24 +972,25 @@ kind = "paragraph"; spans = ct.parse_span(l, c); } end) -local insert_section = function(l,c,j) - local depth, id, t = l:match '^([#§]+)([^%s]*)%s*(.-)$' +local function insert_section(skind) return function(l,c,j) + local depth, id, t = l:match '^([#§^]+)([^%s]*)%s*(.-)$' if id and id ~= "" then if c.doc.sections[id] then c:fail('duplicate section name “%s”', id) end else id = nil end local s = c.doc:mksec(id, utf8.len(depth)) s.depth = utf8.len(depth) s.origin = c:clone() - s.blocks={} + s.blocks = {} + if skind then s.kind = skind end - if t and t ~= "" then + if skind ~= "namespace" and t and t ~= "" then local heading = { kind = "label"; spans = ct.parse_span(t,c); origin = s.origin; captions = s; @@ -988,11 +1000,11 @@ s.heading_node = heading end c.sec = s j:hook('section_attach', c, s) -end +end end local dsetmeta = function(w,c,j) local key, val = w(1) c.doc.meta[key] = val j:hook('metadata_set', key, val) @@ -1020,35 +1032,38 @@ desc = dsetmeta; when = dcond; unless = dcond; with = function(w,c) local _,str = w(2) - local aka, name = str:match '^([^=])=(.*)$' - if aka == nil then name=str aka=name end + local aka, name = str:match '^([^=]+)=(.+)$' + if aka == nil then name=str end local o,id,s = c:ref(name) + if o then -- import object - c.sec.import.objs[aka] = o + c.sec.imports.objs[aka or name] = {obj=o, sec=s} else -- import scope - table.insert(c.sec.import.scope, s) + if aka ~= nil then c:fail'alias is meaningless for scope import' end + table.insert(c.sec.imports.scope, s) end end; global = function(w,c) local _,str = w(2) if str ~= nil and str ~= '' then local aka, name = str:match '^([^=])=(.*)$' - if aka == nil then name=str aka=name end + if aka == nil then name=str end local o,id,s = c:ref(name) if o then - c.doc.globals.objs[aka] = name + c.doc.globals.objs[aka or name] = {obj=o, sec=s} else - table.insert(c.doc.globals, s) + if aka ~= nil then c:fail'alias is meaningless for scope import' end + table.insert(c.doc.globals.scope, s) end else - table.insert(c.doc.globals, c.sec) + table.insert(c.doc.globals.scope, c.sec) end end; pragma = function(w,c) end; lang = function(w,c) @@ -1157,12 +1172,13 @@ ct.ctlseqs = { {seq = '.', fn = insert_paragraph}; {seq = '¶', fn = insert_paragraph}; {seq = '❡', fn = insert_paragraph}; - {seq = '#', fn = insert_section}; - {seq = '§', fn = insert_section}; + {seq = '#', fn = insert_section()}; + {seq = '§', fn = insert_section()}; + {seq = '^', fn = insert_section 'namespace'}; {seq = '+', fn = insert_table_row}; {seq = '|', fn = insert_table_row}; {seq = '│', fn = insert_table_row}; {seq = '!', fn = function(l,c,j,d) local last = d[#d]