Boost logo

Boost :

Subject: Re: [boost] compile time parser generator
From: Ábel Sinkovics (abel_at_[hidden])
Date: 2012-01-09 13:53:42

Hi Dave,

> 1. generate that type based on the /value/ of a function argument (yes,
> you can deduce the length of the string, and thus that type, from the
> /type/ of a string literal argument).

You can pass values constexpr functions operate on to template
metafunctions, but you need to write the conversion. You need to pass
template instances to metafunctions. You can write constexpr functions
that get the properties of the constexpr object as int, char, bool, etc.
values - anything you can use as template arguments. You use pass the
values these functions return when you instantiate your templates. One
example for this is the string in my previous mail: the "properties" of
the string were:
- the length of the string (sizeof(S))
- the charachters of the string (nth(S, n))
I used these expressions when I instantiated the templates MPL provides
and built MPL data-structures from constexpr values. After that the data
can be processed with metaprograms, thus you can generate types.

You can convert data in the other direction as well (but you could do
that in C++98 as well). Having these things, you should be able to
combine constexpr functions and template metafunctions if you implement
the data-conversions.

> 2. Generate any types or choose template specializations based on the
> actual characters used in the string literal. I don't see how you're
> going to generate anything other than a table-based regexp recognizer
> if you can't do that.
We have an implemented and tested a library that can be used to easily
build parsers parsing strings at compile-time (it something like a
compile-time spirit). It uses metaprograms - not constexpr, thus you can
build types as the result of parsing (so you can then specialise
templates based on the content of the string, etc.). It uses constexpr
only to turn a string literal into an MPL string. You can download and
try it out yourself, or you can look at the examples.

Full implementation:
(you can find an (incomplete) wrapper for Xpressive as well)

Further example: typesafe printf - this is implemented as a separate
library, the source code is part of the same github project.
An example program using it:

The typesafe printf is important because it is more than a table-based
regexp recogniser. It builds an MPL list of expected types by parsing
the format string and type-checks the printf arguments using it. Thus,
it demonstrates that you have full access to template metaprogramming
while you're parsing.


Boost list run by bdawes at, gregod at, cpdaniel at, john at