]> git.lizzy.rs Git - metalua.git/blob - metalua/compiler/parser/table.lua
Merge branch 'master' of ssh://git.eclipse.org/gitroot/koneki/org.eclipse.koneki...
[metalua.git] / metalua / compiler / parser / table.lua
1 --------------------------------------------------------------------------------
2 -- Copyright (c) 2006-2013 Fabien Fleutot and others.
3 --
4 -- All rights reserved.
5 --
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
10 --
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
14 --
15 -- Contributors:
16 --     Fabien Fleutot - API and implementation
17 --
18 --------------------------------------------------------------------------------
19
20 --------------------------------------------------------------------------------
21 --
22 -- Exported API:
23 -- * [M.table_bracket_field()]
24 -- * [M.table_field()]
25 -- * [M.table_content()]
26 -- * [M.table()]
27 --
28 -- KNOWN BUG: doesn't handle final ";" or "," before final "}"
29 --
30 --------------------------------------------------------------------------------
31
32 local gg  = require 'metalua.grammar.generator'
33
34 return function(M)
35
36     M.table = { }
37     local _table = gg.future(M.table)
38     local _expr  = gg.future(M).expr
39
40     --------------------------------------------------------------------------------
41     -- `[key] = value` table field definition
42     --------------------------------------------------------------------------------
43     M.table.bracket_pair = gg.sequence{ "[", _expr, "]", "=", _expr, builder = "Pair" }
44
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
50         local e = M.expr (lx)
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 }
57         return r
58     end
59
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  = { ",", ";" },
67         terminators = "}",
68         builder     = "Table" }
69
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 }
75
76     return M
77 end