1 ----------------------------------------------------------------------
2 -- Metalua: $Id: mlp_stat.lua,v 1.7 2006/11/15 09:07:50 fab13n Exp $
4 -- Summary: metalua parser, statement/block parser. This is part of
5 -- the definition 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 ----------------------------------------------------------------------
18 --------------------------------------------------------------------------------
23 -- * [mlp.for_header()]
25 --------------------------------------------------------------------------------
27 --------------------------------------------------------------------------------
28 -- eta-expansions to break circular dependency
29 --------------------------------------------------------------------------------
30 local expr = function (lx) return mlp.expr (lx) end
31 local func_val = function (lx) return mlp.func_val (lx) end
32 local expr_list = function (lx) return mlp.expr_list(lx) end
34 module ("mlp", package.seeall)
36 --------------------------------------------------------------------------------
37 -- List of all keywords that indicate the end of a statement block. Users are
38 -- likely to extend this list when designing extensions.
39 --------------------------------------------------------------------------------
42 local block_terminators = { "else", "elseif", "end", "until", ")", "}", "]" }
44 -- FIXME: this must be handled from within GG!!!
45 function block_terminators:add(x)
46 if type (x) == "table" then for _, y in ipairs(x) do self:add (y) end
47 else _G.table.insert (self, x) end
50 --------------------------------------------------------------------------------
51 -- list of statements, possibly followed by semicolons
52 --------------------------------------------------------------------------------
54 name = "statements block",
55 terminators = block_terminators,
56 primary = function (lx)
58 if lx:is_keyword (lx:peek(), ";") then lx:next() end
62 --------------------------------------------------------------------------------
63 -- Helper function for "return <expr_list>" parsing.
64 -- Called when parsing return statements
65 --------------------------------------------------------------------------------
66 local return_expr_list_parser = gg.list {
67 expr, separators = ",", terminators = block_terminators }
69 --------------------------------------------------------------------------------
70 -- for header, between [for] and [do] (exclusive).
71 -- Return the `Forxxx{...} AST, without the body element (the last one).
72 --------------------------------------------------------------------------------
73 function for_header (lx)
74 local var = mlp.id (lx)
75 if lx:is_keyword (lx:peek(), "=") then
76 -- Fornum: only 1 variable
78 local e = expr_list (lx)
79 assert (2 <= #e and #e <= 3, "2 or 3 values in a fornum")
80 return { tag="Fornum", var, unpack (e) }
82 -- Forin: there might be several vars
83 local a = lx:is_keyword (lx:next(), ",", "in")
84 if a=="in" then var_list = { var } else
85 -- several vars; first "," skipped, read other vars
87 primary = id, separators = ",", terminators = "in" } (lx)
88 _G.table.insert (var_list, 1, var) -- put back the first variable
89 lx:next() -- skip "in"
91 local e = expr_list (lx)
92 return { tag="Forin", var_list, e }
96 --------------------------------------------------------------------------------
97 -- Function def parser helper: id ( . id ) *
98 --------------------------------------------------------------------------------
99 local function fn_builder (list)
101 for i = 2, #list do r = { tag="Index", r, id2string(list[i]) } end
104 local func_name = gg.list{ id, separators = ".", builder = fn_builder }
106 --------------------------------------------------------------------------------
107 -- Function def parser helper: ( : id )?
108 --------------------------------------------------------------------------------
109 local method_name = gg.onkeyword{ name = "method invocation", ":", id,
110 transformers = { function(x) return x and id2string(x) end } }
112 --------------------------------------------------------------------------------
113 -- Function def builder
114 --------------------------------------------------------------------------------
115 local function funcdef_builder(x)
116 local name, method, func = x[1], x[2], x[3]
118 name = { tag="Index", name, method }
119 _G.table.insert (func[1], 1, {tag="Id", "self"})
121 return { tag="Set", {name}, {func} }
125 --------------------------------------------------------------------------------
126 -- if statement builder
127 --------------------------------------------------------------------------------
128 local function if_builder (x)
129 local cb_pairs, else_block, r = x[1], x[2], {tag="If"}
130 for i=1,#cb_pairs do r[2*i-1]=cb_pairs[i][1]; r[2*i]=cb_pairs[i][2] end
131 if else_block then r[#r+1] = else_block end
135 --------------------------------------------------------------------------------
136 -- produce a list of (expr,block) pairs
137 --------------------------------------------------------------------------------
138 local elseifs_parser = gg.list {
139 gg.sequence { expr, "then", block },
140 separators = "elseif",
141 terminators = { "else", "end" } }
143 --------------------------------------------------------------------------------
144 -- assignments and calls: statements that don't start with a keyword
145 --------------------------------------------------------------------------------
146 local function assign_or_call_stat_parser (lx)
147 local e = expr_list (lx)
148 local a = lx:is_keyword(lx:peek())
149 local op = a and stat.assignments[a]
151 --FIXME: check that [e] is a LHS
153 local v = expr_list (lx)
154 if type(op)=="string" then return { tag=op, e, v }
155 else return op (e, v) end
159 gg.parse_error (lx, "comma is not a valid statement separator") end
160 if e[1].tag ~= "Call" and e[1].tag ~= "Invoke" then
161 gg.parse_error (lx, "This expression is of type '%s'; "..
162 "only function and method calls make valid statements",
163 e[1].tag or "<list>")
169 local local_stat_parser = gg.multisequence{
170 -- local function <name> <func_val>
171 { "function", id, func_val, builder =
172 function(x) return { tag="Localrec", { x[1] }, { x[2] } } end },
173 -- local <id_list> ( = <expr_list> )?
174 default = gg.sequence{ id_list, gg.onkeyword{ "=", expr_list },
175 builder = function(x) return {tag="Local", x[1], x[2] or { } } end } }
177 --------------------------------------------------------------------------------
179 --------------------------------------------------------------------------------
180 stat = gg.multisequence {
182 { "do", block, "end", builder =
183 function (x) return { tag="Do", unpack (x[1]) } end },
184 { "for", for_header, "do", block, "end", builder =
185 function (x) x[1][#x[1]+1] = x[2]; return x[1] end },
186 { "function", func_name, method_name, func_val, builder=funcdef_builder },
187 { "while", expr, "do", block, "end", builder = "While" },
188 { "repeat", block, "until", expr, builder = "Repeat" },
189 { "local", local_stat_parser, builder = fget (1) },
190 { "return", return_expr_list_parser, builder = fget (1, "Return") },
191 { "break", builder = function() return { tag="Break" } end },
192 { "-{", splice_content, "}", builder = fget(1) },
193 { "if", elseifs_parser, gg.onkeyword{ "else", block }, "end",
194 builder = if_builder },
195 default = assign_or_call_stat_parser }
200 function stat.assignments:add(k, v) self[k] = v end