1 ----------------------------------------------------------------------
2 -- Metalua: $Id: mlp_table.lua,v 1.5 2006/11/10 02:11:17 fab13n Exp $
4 -- Summary: metalua parser, table constructor parser. This is part
5 -- of thedefinition of module [mlp].
7 ----------------------------------------------------------------------
9 -- Copyright (c) 2006, Fabien Fleutot <metalua@gmail.com>.
11 -- This software is released under the MIT Licence, see licence.txt
14 ----------------------------------------------------------------------
16 -- $Log: mlp_table.lua,v $
17 -- Revision 1.5 2006/11/10 02:11:17 fab13n
18 -- compiler faithfulness to 5.1 improved
20 -- mlp.expr refactored
22 -- Revision 1.4 2006/11/09 09:39:57 fab13n
25 -- Revision 1.3 2006/11/07 04:38:00 fab13n
26 -- first bootstrapping version.
28 -- Revision 1.2 2006/11/05 15:08:34 fab13n
29 -- updated code generation, to be compliant with 5.1
31 ----------------------------------------------------------------------
33 --------------------------------------------------------------------------------
36 -- * [mlp.table_field()]
37 -- * [mlp.table_content()]
40 -- KNOWN BUG: doesn't handle final ";" or "," before final "}"
42 --------------------------------------------------------------------------------
48 module ("mlp", package.seeall)
50 --------------------------------------------------------------------------------
51 -- eta expansion to break circular dependencies:
52 --------------------------------------------------------------------------------
53 local function _expr (lx) return expr(lx) end
55 --------------------------------------------------------------------------------
56 -- [[key] = value] table field definition
57 --------------------------------------------------------------------------------
58 local bracket_field = gg.sequence{ "[", _expr, "]", "=", _expr, builder = "Pair" }
60 --------------------------------------------------------------------------------
61 -- [id = value] or [value] table field definition;
62 -- [[key]=val] are delegated to [bracket_field()]
63 --------------------------------------------------------------------------------
64 function table_field (lx)
65 if lx:is_keyword (lx:peek(), "[") then return bracket_field (lx) end
67 if lx:is_keyword (lx:peek(), "=") then
68 lx:next(); -- skip the "="
69 local key = id2string(e)
71 local r = { tag="Pair", key, val }
72 r.lineinfo = { first = key.lineinfo.first, last = val.lineinfo.last }
77 local function _table_field(lx) return table_field(lx) end
79 --------------------------------------------------------------------------------
80 -- table constructor, without enclosing braces; returns a full table object
81 --------------------------------------------------------------------------------
82 table_content = gg.list { _table_field,
83 separators = { ",", ";" }, terminators = "}", builder = "Table" }
85 local function _table_content(lx) return table_content(lx) end
87 --------------------------------------------------------------------------------
88 -- complete table constructor including [{...}]
89 --------------------------------------------------------------------------------
90 table = gg.sequence{ "{", _table_content, "}", builder = fget(1) }