lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]




On Tue, Oct 18, 2011 at 12:31 PM, Jaco van der Merwe <jvdmerwe@emss.co.za> wrote:
In Matlab, the transpose of a matrix M is denoted by M'. The MetaLua code should replace this with a method call to the transpose() function.

Your problem is that in the lexer, string lexing has a higher priority than keyword lexing, so the single quote is parsed as a string, and never considered by the symbol-keyword extractor. Moreover, the symbol extractor has to keep the lowest priority, because it always succeeds at finding a symbol (this could be changed, but it never occurred to me that it made sense).

So what you need to do is add an extractor in the lexer, which recognizes the quote, and has higher priority than the short_string_extractor:

$ cat > prime.mlua
-{ block:

   mlp.expr.suffix:add{ "'", prec=100, builder=|a| +{-{a}:transpose()} }

   function lexer.lexer:extract_prime()
       local k = self.src :sub(self.i, self.i)
       if k=="'" then self.i=self.i+1; return "Keyword", "'"
       else return end
   end

   table.insert(lexer.lexer.extractors, 2, "extract_prime")
}

table.print(+{ f'})
^D
$ metalua prime.mlua
`Invoke{ `Id "f", `String "transpose" }
$ _

Beware that with this modification, Metalua won't recognize single-quote strings anymore. This is unavoidable, has sources such as " f' * g' " would become ambiguous, between " f(" * g") " and " f:transpose() * g:transpose() ". There must be clever ways to analyze single quotes in a context-aware way, but I wouldn't recommend that, it would introduce needless headaches and corner-cases for end-users.

For further Metalua enquiries, you can also ask the dedicated mailing list, which recently moved to google groups: http://groups.google.com/group/metalua

-- Fabien.