Changeset 44660


Ignore:
Timestamp:
Apr 21, 2008, 12:29:36 AM (10 years ago)
Author:
Hartmut Kaiser
Message:

Spirit.Lex. Updated Lexer quickstart docs with new action prototypes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libs/spirit/doc/lex/lexer_quickstart2.qbk

    r44360 r44660  
    5959the lexical analyzer. This is very similar to __flex__, where the action code
    6060associated with a token definition gets executed after the recognition of a
    61 matching input sequence. The code above uses functors constructed using
    62 __phoenix2__, but it is possible to insert any C++ functor as long as it
    63 exposes the interface:
     61matching input sequence. The code above uses function objects constructed using
     62__phoenix2__, but it is possible to insert any C++ function or function object
     63as long as it exposes the interface:
    6464
    65     void f (Range r, std::size_t id, Context& ctx, bool& matched);
     65    void f (Range r, Idtype id, bool& matched, Context& ctx);
    6666
    6767[variablelist where:
     
    7272                             defining the type of the `lexertl_lexer<...>`
    7373                             (its first template parameter).]]
    74     [[`std::size_t id`]     [This is the token id for the matched token.]]
     74    [[`Idtype id`]          [This is the token id of the type `std::size_t`
     75                             for the matched token.]]
     76    [[`bool& matched`]      [This boolean value is pre/initialized to `true`.
     77                             If the functor sets it to `false` the lexer
     78                             stops calling any semantic actions attached to
     79                             this token and behaves as if the token have not
     80                             been matched in the first place.]]
    7581    [[`Context& ctx`]       [This is a reference to a lexer specific,
    7682                             unspecified type, providing the context for the
     
    7985                             lexer state control from inside a semantic
    8086                             action.]]
    81     [[`bool& matched`]      [This boolean value is pre/initialized to `true`.
    82                              If the functor sets it to `false` the lexer
    83                              stops calling any semantic actions attached to
    84                              this token and behaves as if the token have not
    85                              been matched in the first place.]]
    8687]
    8788
    88 Even if it is possible to write your own functor implementations, the preferred
    89 way of defining lexer semantic actions is to use __phoenix2__. In this case you
    90 can access the three parameters described in the table above by using the
    91 predefined __phoenix2__ placeholders: `_1` for the iterator range, `_2` for the
    92 token id, `_3` for the reference to the lexer state, and `_4` for the reference
    93 to the boolean value signaling the outcome of the semantic action.
     89When using a C++ function as the semantic action the following prototypes are
     90allowed as well:
    9491
    95 [important  All placeholders (`_1`, `_2`, etc.) used in /lexer/ semantic
    96             actions in conjunction with functors created based on __phoenix2__
    97             need to be imported from the `namespace boost::phoenix::arg_names`
    98             (and *not* `namespace boost::spirit::arg_names`, which is
    99             different from using placeholders in __qi__ or __karma__).
    100             Using the wrong placeholders leads to subtle compilation errors
    101             which are difficult to backtrack to their cause.
    102 ]
     92    void f (Range r, Idtype id, bool& matched);
     93    void f (Range r, Idtype id);
     94    void f (Range r);
    10395
     96Even if it is possible to write your own function object implementations (i.e.
     97using Boost.Lambda or Boost.Bind), the preferred way of defining lexer semantic
     98actions is to use __phoenix2__. In this case you can access the four parameters
     99described in the table above by using the predefined __spirit__ placeholders:
     100`_1` for the iterator range, `_2` for the token id, `_3` for the reference
     101to the boolean value signaling the outcome of the semantic action, and `_4` for
     102the reference to the internal lexer context.
    104103
    105104[heading Associating Token Definitions with the Lexer]
Note: See TracChangeset for help on using the changeset viewer.