Skip to content

Commit

Permalink
Update readme.md on the typed terms feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-winter authored Nov 18, 2021
1 parent e3de201 commit 7ebc82a
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ All it needs is a C++17 compiler!
* [Verbose output](#verbose-output)
* [Source tracking](#source-tracking)
* [Buffers](#buffers)
* [Typed terms](#typed-terms)
* [Regular expressions](#regular-expressions)
* [Diagnostics](#diagnostics)

Expand Down Expand Up @@ -881,6 +882,35 @@ iterator& operator ++(); // pre and post incrementation
iterator operator ++(int);
bool operator == (const iterator& other) const; // comparison operator
```
### Typed terms

It is possible to define term value types as custom types, not limited to ```char``` or ```std::string_view```.
It can be achieved using ```typed_term``` class template.

Wrap the usual term definition:

```c++
char_term plus('+');
```
with ```typed_term``` like this:
```c++
// a custom type for the plus term
struct plus_tag{};
typed_term plus(char_term('+'), create<plus_tag>{});
```

The ```create``` is a functor available in the ```ctpg::ftors``` namespace, which simply creates an object of given type using a default constructor of that type.
In fact any callble object can be used instead of ```create```, this is just an example.
The ```plus``` term has a value type identical to the return type of the functor, ```plus_tag``` in this case.

Take a look at the **`typed_terms.cpp`** in the examples, it uses this feature to create a simple calculator, but instead of the
runtime switch statement on the char value like in the **`simple_expr_parser.cpp`**, the functor object has an overload for each arithmetic operator.

>Note: Typed terms cannot use their implicit versions like the basic terms (```char_term```, ```string_term```) in the rules. They have to be
>referrenced by the typed_terms object.
## Regular expressions

Expand Down

0 comments on commit 7ebc82a

Please sign in to comment.