spirit: Using actions on rules with more precision

Hello, Is it possible to define a rule: rule<> mystringrule = confix_p('"', *c_escape_ch_p, '"') differently such that I can use std::string value; bool result = parse("\"hello world\"", mystringrule[assign_a(value)]).full; and get the text = "hello world" assigned to value? Currently, I get text = "\"hello world\"" instead. ie with double quotes at either end. Thanks -John

John Ky wrote:
Is it possible to define a rule:
rule<> mystringrule = confix_p('"', *c_escape_ch_p, '"')
differently such that I can use
std::string value;
bool result = parse("\"hello world\"", mystringrule[assign_a(value)]).full;
and get the text = "hello world" assigned to value?
Currently, I get text = "\"hello world\"" instead. ie with double quotes at either end.
I'ld suggest to use closures for that (not tested): struct strlit_closure : public boost::spirit::closure<strlit_closure, std::string> { member1 val; }; typedef rule<strlit_closure> rule_type; rule_type mystringrule = confix_p('"', *c_escape_ch_p[assign_a(mystringrule.val)], '"'); bool result = parse("\"hello world\"", mystringrule[assign_a(value)]).full; This is based on the fact, that the first closure member of a rule is used as the rule's value. HTH Regards Hartmut

Hi Hartmut, Thanks for the help. I got it to work with an extra ::context_t in: typedef rule<strlit_closure::context_t> rule_type; Is that the correct way to do it? Also, I have another problem: The rule: rule_string mystringrule; mystringrule = * ( confix_p('"', (*c_escape_ch_p)[mystringrule.val += construct_<string>(arg1, arg2)], "') ); Will match: "hello""world" as: helloworld But the rule: rule_string mystringrule; mystringrule = * ( confix_p('"', (*c_escape_ch_p)[mystringrule.val += construct_<string>(arg1, arg2)], "') | (anychar_p - '"') /* this line added */ ); matched returning an empty string. -John On 1/24/06, Hartmut Kaiser <hartmut.kaiser@gmail.com> wrote:
John Ky wrote:
Is it possible to define a rule:
rule<> mystringrule = confix_p('"', *c_escape_ch_p, '"')
differently such that I can use
std::string value;
bool result = parse("\"hello world\"", mystringrule[assign_a(value)]).full;
and get the text = "hello world" assigned to value?
Currently, I get text = "\"hello world\"" instead. ie with double quotes at either end.
I'ld suggest to use closures for that (not tested):
struct strlit_closure : public boost::spirit::closure<strlit_closure, std::string> { member1 val; };
typedef rule<strlit_closure> rule_type;
rule_type mystringrule = confix_p('"', *c_escape_ch_p[assign_a(mystringrule.val)], '"');
bool result = parse("\"hello world\"", mystringrule[assign_a(value)]).full;
This is based on the fact, that the first closure member of a rule is used as the rule's value.
HTH Regards Hartmut

Resolved. I have to apologise - I just worked out that I needed to use anychar - blank_p because blank_p was matching elsewhere in the grammar. -John On 1/24/06, John Ky <newhoggy@gmail.com> wrote:
Hi Hartmut,
Thanks for the help.
I got it to work with an extra ::context_t in:
typedef rule<strlit_closure::context_t> rule_type;
Is that the correct way to do it?
Also, I have another problem:
The rule:
rule_string mystringrule; mystringrule = * ( confix_p('"', (*c_escape_ch_p)[mystringrule.val += construct_<string>(arg1, arg2)], "') );
Will match:
"hello""world"
as:
helloworld
But the rule:
rule_string mystringrule; mystringrule = * ( confix_p('"', (*c_escape_ch_p)[mystringrule.val += construct_<string>(arg1, arg2)], "') | (anychar_p - '"') /* this line added */ );
matched returning an empty string.
-John
On 1/24/06, Hartmut Kaiser <hartmut.kaiser@gmail.com> wrote:
John Ky wrote:
Is it possible to define a rule:
rule<> mystringrule = confix_p('"', *c_escape_ch_p, '"')
differently such that I can use
std::string value;
bool result = parse("\"hello world\"", mystringrule[assign_a(value)]).full;
and get the text = "hello world" assigned to value?
Currently, I get text = "\"hello world\"" instead. ie with double quotes at either end.
I'ld suggest to use closures for that (not tested):
struct strlit_closure : public boost::spirit::closure<strlit_closure, std::string> { member1 val; };
typedef rule<strlit_closure> rule_type;
rule_type mystringrule = confix_p('"', *c_escape_ch_p[assign_a(mystringrule.val)], '"');
bool result = parse("\"hello world\"", mystringrule[assign_a(value)]).full;
This is based on the fact, that the first closure member of a rule is used as the rule's value.
HTH Regards Hartmut
participants (2)
-
Hartmut Kaiser
-
John Ky