Ticket #2511: employee.patch
File employee.patch, 3.1 KB (added by , 14 years ago) |
---|
-
libs/spirit/doc/qi_and_karma/employee.qbk
15 15 demonstrates some features of Spirit2 that makes this easy. In the process, 16 16 you'll learn about: 17 17 18 * More a bout attributes18 * More attributes 19 19 * Auto rules 20 20 * Some more built-in parsers 21 21 * Directives … … 26 26 27 27 [tutorial_employee_struct] 28 28 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__ library29 Then, we need to tell __fusion__ about our employee struct to make it a 30 first-class fusion citizen. If you don't know fusion yet, it is a __boost__ library 31 31 for working with heterogenous collections of data, commonly referred to as 32 32 tuples. Spirit uses fusion extensively as part of its infrastructure. 33 33 … … 61 61 62 62 employee_parser() : employee_parser::base_type(start) 63 63 64 Initialize sthe base class.64 Initialize the base class. 65 65 66 66 rule<Iterator, std::string(), space_type> quoted_string; 67 67 rule<Iterator, employee(), space_type> start; 68 68 69 Declare stwo rules: `quoted_string` and `start`. `start` has the same template69 Declare two rules: `quoted_string` and `start`. `start` has the same template 70 70 parameters as the grammar itself. `quoted_string` has a `std::string` attribute. 71 71 72 72 [heading Lexeme] … … 87 87 88 88 a - b 89 89 90 parses `a` but not `b`. Its attribute is just `A` ,the attribute of `a`. `b`'s90 parses `a` but not `b`. Its attribute is just `A`; the attribute of `a`. `b`'s 91 91 attribute is ignored. Hence, the attribute of: 92 92 93 93 char_ - '"' … … 98 98 99 99 +a 100 100 101 is the close kin of the kleene star we got so used to in our tutorial. Like it's102 kin, the kleene star, its attribute is a `std::vector<A>` where `A` is the101 is the close kin of the Kleene star we got so used to in our tutorial. Like its 102 kin, the Kleene star, its attribute is a `std::vector<A>` where `A` is the 103 103 attribute of `a`. So, putting all these together, the attribute of 104 104 105 105 +(char_ - '"') … … 143 143 144 144 fusion::vector<std::vector<char> > 145 145 146 But wait, there's one more collapsing rule: If after the attribute is a single147 element `fusion::vector`, The element is stripped naked from its container. So, 148 to make a long story short, the attribute of the expression:146 But wait, there's one more collapsing rule: If the attribute is followed by a 147 single element `fusion::vector`, the element is stripped naked from its container. 148 To make a long story short, the attribute of the expression: 149 149 150 150 '"' >> +(char_ - '"') >> '"' 151 151 … … 159 159 160 160 r = p[_val = _1]; 161 161 162 If you have a rule definition like above where the attribute of the RHS (right163 hand side) of the rule is compatibe with the attribute of the LHS (left hand 164 side), thenyou can rewrite it as:162 If 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 164 hand side), you can rewrite it as: 165 165 166 166 r %= p; 167 167