2
0
mirror of https://github.com/boostorg/parser.git synced 2026-01-19 04:22:13 +00:00

Add note about how to write a lambda directly in a semantic action. Modify

the claim that directives can be spotted by looking for "[]", by mentioning
the exception of semantic actions.

Partially addresses #93.
This commit is contained in:
Zach Laine
2024-02-11 15:46:14 -06:00
parent e6b59a4784
commit ab20bdd87f

View File

@@ -251,6 +251,18 @@ There are lots of functions like `_attr()` that can be used to access the
state in the parse context. We'll cover more of them later on. _parse_ctx_
defines what exactly the parse context is and how it works.
Note that you can't write an unadorned lambda directly as a semantic action.
Otherwise, the compile will see two `'['` characters and think it's about to
parse an attribute. Parentheses fix this:
p[([](auto & ctx){/*...*/})]
Before you do this, note that the lambdas that you write as semantic actions
are almost always generic (having an `auto & ctx` parameter), and so are very
frequently re-usable. Most semantic action lambdas you write should be
written out-of-line, and given a good name. Even when they are not reused,
named lambdas keep your parsers smaller and easier to read.
[endsect]
[section Parsing to Find Subranges]
@@ -1453,9 +1465,9 @@ could use this parser for the digits at the end:
A directive is an element of your parser that doesn't have any meaning by
itself. Some are second-order parsers that need a first-order parser to do
the actual parsing. Others influence the parse in some way. Lexically, you
can spot a directive by its use of `[]`. Non-directives never use `[]`, and
directives always do.
the actual parsing. Others influence the parse in some way. You can often
spot a directive lexically by its use of `[]`; directives always `[]`.
Non-directives might, but only when attaching a semantic action.
The directives that are second order parsers are technically directives, but
since they are also used to create parsers, it is more useful just to focus on