+----------------------------------------------------------------------
+-- This would have been best done through library 'metalua.walk',
+-- but walk depends on match, so we have to break the dependency.
+-- It replaces all instances of `...' in `ast' with `term', unless
+-- it appears in a function.
+----------------------------------------------------------------------
+function replace_dots (ast, term)
+ local function rec (x)
+ if type(x) == 'table' then
+ if x.tag=='Dots' then
+ if term=='ambiguous' then
+ error ("You can't use `...' on the right of a match case when it appears "..
+ "more than once on the left")
+ else
+ x <- term
+ end
+ elseif x.tag=='Function' then return
+ else for y in ivalues (x) do rec (y) end end
+ end
+ end
+ return rec (ast)
+end
+