]> git.lizzy.rs Git - metalua.git/blobdiff - src/compiler/gg.lua
more permissive multisequence:add() definition in gg
[metalua.git] / src / compiler / gg.lua
index 33c258bda99345b0265bc9113b6afc8a48abc0ec..d237d28f1939f4fecc66df3ae42e707a5b02e479 100644 (file)
@@ -75,7 +75,7 @@ end
 
 -------------------------------------------------------------------------------
 -- Return true iff [x] is a parser.
--- If it's a gg-generated parser, reutrn the name of its kind.
+-- If it's a gg-generated parser, return the name of its kind.
 -------------------------------------------------------------------------------
 function is_parser (x)
    return type(x)=="function" or getmetatable(x)==parser_metatable and x.kind
@@ -259,12 +259,17 @@ function multisequence (p)
    function p:add (s)
       -- compile if necessary:
       if not is_parser(s) then sequence(s) end
-      if type(s[1]) ~= "string" then 
-         error "Invalid sequence for multiseq"
-      elseif self.sequences[s[1]] then 
+      if is_parser(s) ~= 'sequence' or type(s[1]) ~= "string" then 
+         if self.default then -- two defaults
+            error ("In a multisequence parser, all but one sequences "..
+                   "must start with a keyword")
+         else self.default = s end -- first default
+      elseif self.sequences[s[1]] then -- duplicate keyword
          eprintf (" *** Warning: keyword %q overloaded in multisequence ***", s[1])
+         self.sequences[s[1]] = s
+      else -- newly caught keyword
+         self.sequences[s[1]] = s
       end
-      self.sequences[s[1]] = s
    end -- </multisequence.add>
 
    -------------------------------------------------------------------
@@ -667,6 +672,8 @@ function onkeyword (p)
       if type(x)=="string" then table.insert (p.keywords, x)
       else assert (not p.primary and is_parser (x)); p.primary = x end
    end
+   if not next (p.keywords) then 
+      eprintf("Warning, no keyword to trigger gg.onkeyword") end
    assert (p.primary, 'no primary parser in gg.onkeyword')
    return p
 end --</onkeyword>