]> git.lizzy.rs Git - metalua.git/blob - metalua/compiler/parser/annot/generator.lua
Merge branch 'master' of ssh://git.eclipse.org/gitroot/koneki/org.eclipse.koneki...
[metalua.git] / metalua / compiler / parser / annot / generator.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 require 'checks'
21 local gg = require 'metalua.grammar.generator'
22 local M  = { }
23
24 function M.opt(mlc, primary, a_type)
25     checks('table', 'table|function', 'string')
26     return gg.sequence{
27         primary,
28         gg.onkeyword{ "#", function() return assert(mlc.annot[a_type]) end },
29         builder = function(x)
30             local t, annot = unpack(x)
31             return annot and { tag='Annot', t, annot } or t
32         end }
33 end
34
35 -- split a list of "foo" and "`Annot{foo, annot}" into a list of "foo"
36 -- and a list of "annot".
37 -- No annot list is returned if none of the elements were annotated.
38 function M.split(lst)
39     local x, a, some = { }, { }, false
40     for i, p in ipairs(lst) do
41         if p.tag=='Annot' then
42             some, x[i], a[i] = true, unpack(p)
43         else x[i] = p end
44     end
45     if some then return x, a else return lst end
46 end
47
48 return M