Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext
Parser Directive Inhibiting Skipping (lexeme[])
Description

The lexeme directive makes its subject a primitive. In a logical point of view, lexemes (and primitives) are minimal atomic units (e.g. words, numbers, identifiers, etc). These are the things that you'd normally put in the lexer (hinting at the term "lexeme"), but in a lexer-less world, you put these in a lexeme. Seeing its subject as a primitive, the lexeme directive does an initial pre-skip (as all primitives do) and turns off white space skipping.

At the phrase level, the parser ignores white spaces, possibly including comments. Use lexeme in situations where you want to work at the character level instead of the phrase level. Parsers can be made to work at the character level by enclosing the pertinent parts inside the lexeme directive. For example, here's a rule that parses integers:

integer = lexeme[ -(lit('+') | '-') >> +digit ];

The lexeme directive instructs its subject parser to work on the character level. Without it, the integer rule would have allowed erroneous embedded white spaces in inputs such as "1 2 345" which will be parsed as "12345".

[Note] Note

Keep in mind that lexeme[] pre-skips spaces. If this is not desired, use the no_skip directive instead.

Header
// forwards to <boost/spirit/home/qi/directive/lexeme.hpp>
#include <boost/spirit/include/qi_lexeme.hpp>

Also, see Include Structure.

Namespace

Name

boost::spirit::lexeme // alias: boost::spirit::qi::lexeme

Model of

UnaryParser

Notation

a

A Parser.

Expression Semantics

Semantics of an expression is defined only where it differs from, or is not defined in UnaryParser.

Expression

Semantics

lexeme[a]

Pre-skip and turn off white space skipping for the subject parser, a (and all its children).

Attributes

See Compound Attribute Notation.

Expression

Attribute

lexeme[a]

a: A --> lexeme[a]: A
a: Unused --> lexeme[a]: Unused

Complexity

The complexity is defined by the complexity of the subject parser, a

Example
[Note] Note

The test harness for the example(s) below is presented in the Basics Examples section.

Some using declarations:

using boost::spirit::qi::lexeme;
using boost::spirit::qi::lit;
using boost::spirit::ascii::digit;

Simple usage of lexeme[]:

The use of lexeme here will prevent skipping in between the digits and the sign making inputs such as "1 2 345" erroneous.

test_phrase_parser("12345", lexeme[ -(lit('+') | '-') >> +digit ]);


PrevUpHomeNext