| Home | Libraries | People | FAQ | More

#### Attributes of Primitive Components

Parsers and generators in Spirit are fully attributed. Spirit.Qi parsers always expose an attribute specific to their type. This is called synthesized attribute as it is returned from a successful match representing the matched input sequence. For instance, numeric parsers, such as `int_` or `double_`, return the `int` or `double` value converted from the matched input sequence. Other primitive parser components have other intuitive attribute types, such as for instance `int_` which has `int`, or `ascii::char_` which has `char`. For primitive parsers apply the normal C++ convertibility rules: you can use any C++ type to receive the parsed value as long as the attribute type of the parser is convertible to the type provided. The following example shows how a synthesized parser attribute (the `int` value) is extracted by calling the API function `qi::parse`:

    int value = 0;
    std::string str("123");
    std::string::iterator strbegin = str.begin();
    qi::parse(strbegin, str.end(), int_, value);   // value == 123

The attribute type of a generator defines what data types this generator is able to consume in order to produce its output. Spirit.Karma generators always expect an attribute specific to their type. This is called consumed attribute and is expected to be passed to the generator. The consumed attribute is most of the time the value the generator is designed to emit output for. For primitive generators the normal C++ convertibility rules apply. Any data type convertible to the attribute type of a primitive generator can be used to provide the data to generate. We present a similar example as above, this time the consumed attribute of the `int_` generator (the `int` value) is passed to the API function `karma::generate`:

    int value = 123;
    std::string str;
    std::back_insert_iterator<std::string> out(str);
    karma::generate(out, int_, value);                // str == "123"

Other primitive generator components have other intuitive attribute types, very similar to the corresponding parser components. For instance, the `ascii::char_` generator has `char` as consumed attribute. For a full list of available parser and generator primitives and their attribute types please see the sections Qi Parsers and Karma Generators.