Ticket #2511: employee.patch

File employee.patch, 3.1 KB (added by Charles Brockman <cmbrockman@…>, 14 years ago)
  • libs/spirit/doc/qi_and_karma/employee.qbk

     
    1515demonstrates some features of Spirit2 that makes this easy. In the process,
    1616you'll learn about:
    1717
    18 * More about attributes
     18* More attributes
    1919* Auto rules
    2020* Some more built-in parsers
    2121* Directives
     
    2626
    2727[tutorial_employee_struct]
    2828
    29 Then, we need to tell __fusion__ about our employee struct to make it a first-
    30 class fusion citizen. If you don't know fusion yet, it is a __boost__ library
     29Then, we need to tell __fusion__ about our employee struct to make it a
     30first-class fusion citizen. If you don't know fusion yet, it is a __boost__ library
    3131for working with heterogenous collections of data, commonly referred to as
    3232tuples. Spirit uses fusion extensively as part of its infrastructure.
    3333
     
    6161
    6262    employee_parser() : employee_parser::base_type(start)
    6363
    64 Initializes the base class.
     64Initialize the base class.
    6565
    6666    rule<Iterator, std::string(), space_type> quoted_string;
    6767    rule<Iterator, employee(), space_type> start;
    6868
    69 Declares two rules: `quoted_string` and `start`. `start` has the same template
     69Declare two rules: `quoted_string` and `start`. `start` has the same template
    7070parameters as the grammar itself. `quoted_string` has a `std::string` attribute.
    7171
    7272[heading Lexeme]
     
    8787
    8888    a - b
    8989
    90 parses `a` but not `b`. Its attribute is just `A`, the attribute of `a`. `b`'s
     90parses `a` but not `b`. Its attribute is just `A`; the attribute of `a`. `b`'s
    9191attribute is ignored. Hence, the attribute of:
    9292
    9393    char_ - '"'
     
    9898
    9999    +a
    100100
    101 is the close kin of the kleene star we got so used to in our tutorial. Like it's
    102 kin, the kleene star, its attribute is a `std::vector<A>` where `A` is the
     101is the close kin of the Kleene star we got so used to in our tutorial. Like its
     102kin, the Kleene star, its attribute is a `std::vector<A>` where `A` is the
    103103attribute of `a`. So, putting all these together, the attribute of
    104104
    105105    +(char_ - '"')
     
    143143
    144144    fusion::vector<std::vector<char> >
    145145
    146 But wait, there's one more collapsing rule: If after the attribute is a single
    147 element `fusion::vector`, The element is stripped naked from its container. So,
    148 to make a long story short, the attribute of the expression:
     146But wait, there's one more collapsing rule: If the attribute is followed by a
     147single element `fusion::vector`, the element is stripped naked from its container.
     148To make a long story short, the attribute of the expression:
    149149
    150150    '"' >> +(char_ - '"') >> '"'
    151151
     
    159159
    160160    r = p[_val = _1];
    161161
    162 If you have a rule definition like above where the attribute of the RHS (right
    163 hand side) of the rule is compatibe with the attribute of the LHS (left hand
    164 side), then you can rewrite it as:
     162If you have a rule definition such as the above, where the attribute of the RHS
     163(right hand side) of the rule is compatible with the attribute of the LHS (left
     164hand side), you can rewrite it as:
    165165
    166166    r %= p;
    167167