问题描述:

So I am playing with lpeg to replace a boost spirit grammar, I must say boost::spirit is far more elegant and natural than lpeg. However it is a bitch to work with due to the constraints of current C++ compiler technology and the issues of TMP in C++. The type mechanism is in this case your enemy rather than your friend. Lpeg on the other hand while ugly and basic results in more productivity.

Anyway, I am digressing, part of my lpeg grammar looks like as follows:

function get_namespace_parser()

local P, R, S, C, V =

lpeg.P, lpeg.R, lpeg.S, lpeg.C, lpeg.V

namespace_parser =

lpeg.P{

"NAMESPACE";

NAMESPACE = V("WS") * P("namespace") * V("SPACE_WS") * V("NAMESPACE_IDENTIFIER")

* V("WS") * V("NAMESPACE_BODY") * V("WS"),

NAMESPACE_IDENTIFIER = V("IDENTIFIER") / print_string ,

NAMESPACE_BODY = "{" * V("WS") *

V("ENTRIES")^0 * V("WS") * "}",

WS = S(" \t\n")^0,

SPACE_WS = P(" ") * V("WS")

}

return namespace_parser

end

This grammar (although incomplete) matches the following namespace foo {}. I'd like to achieve the following semantics (which are common use-cases when using boost spirit).

  1. Create a local variable for the namespace rule.
  2. Add a namespace data structure to this local variable when namespace IDENTIFIER { has been matched.
  3. Pass the newly created namespace data structure to the NAMESPACE_BODY for further construction of the AST... so on and so forth.

I am sure this use-case is achievable. No examples show it. I don't know the language or the library enough to figure out how to do it. Can someone show the syntax for it.

edit : After a few days of trying to dance with lpeg, and getting my feet troden on, I have decided to go back to spirit :D it is clear that lpeg is meant to be weaved with lua functions and that such weaving is very free-form (whereas spirit has clear very well documented semantics). I simply do not have the right mental model of lua yet.

相关阅读:
Top