1 --------------------------------------------------------------------------------
2 -- Copyright (c) 2006-2013 Fabien Fleutot and others.
4 -- All rights reserved.
6 -- This program and the accompanying materials are made available
7 -- under the terms of the Eclipse Public License v1.0 which
8 -- accompanies this distribution, and is available at
9 -- http://www.eclipse.org/legal/epl-v10.html
11 -- This program and the accompanying materials are also made available
12 -- under the terms of the MIT public license which accompanies this
13 -- distribution, and is available at http://www.lua.org/license.html
16 -- Fabien Fleutot - API and implementation
18 --------------------------------------------------------------------------------
20 --------------------------------------------------------------------------------
23 -- * [M.table_bracket_field()]
24 -- * [M.table_field()]
25 -- * [M.table_content()]
28 -- KNOWN BUG: doesn't handle final ";" or "," before final "}"
30 --------------------------------------------------------------------------------
32 local gg = require 'metalua.grammar.generator'
37 local _table = gg.future(M.table)
38 local _expr = gg.future(M).expr
40 --------------------------------------------------------------------------------
41 -- `[key] = value` table field definition
42 --------------------------------------------------------------------------------
43 M.table.bracket_pair = gg.sequence{ "[", _expr, "]", "=", _expr, builder = "Pair" }
45 --------------------------------------------------------------------------------
46 -- table element parser: list value, `id = value` pair or `[value] = value` pair.
47 --------------------------------------------------------------------------------
48 function M.table.element (lx)
49 if lx :is_keyword (lx :peek(), "[") then return M.table.bracket_pair(lx) end
51 if not lx :is_keyword (lx :peek(), "=") then return e end
52 lx :next(); -- skip the "="
53 local key = M.id2string(e) -- will fail on non-identifiers
54 local val = M.expr(lx)
55 local r = { tag="Pair", key, val }
56 r.lineinfo = { first = key.lineinfo.first, last = val.lineinfo.last }
60 -----------------------------------------------------------------------------
61 -- table constructor, without enclosing braces; returns a full table object
62 -----------------------------------------------------------------------------
63 M.table.content = gg.list {
64 -- eta expansion to allow patching the element definition
65 primary = _table.element,
66 separators = { ",", ";" },
70 --------------------------------------------------------------------------------
71 -- complete table constructor including [{...}]
72 --------------------------------------------------------------------------------
73 -- TODO beware, stat and expr use only table.content, this can't be patched.
74 M.table.table = gg.sequence{ "{", _table.content, "}", builder = unpack }