mirror of
https://github.com/yhirose/cpp-peglib.git
synced 2024-12-22 20:05:31 +00:00
Moved 'choice' property to SemanticValues.
This commit is contained in:
parent
8016f4f0bb
commit
d93a007ef0
21
README.md
21
README.md
@ -12,6 +12,8 @@ The PEG syntax is well described on page 2 in the [document](http://pdos.csail.m
|
||||
* `~` (Ignore operator)
|
||||
* `\x??` (Hex number char)
|
||||
|
||||
This library is a [*Packrat*](http://pdos.csail.mit.edu/~baford/packrat/thesis/thesis.pdf) parser which supports the linear-time parsing.
|
||||
|
||||
How to use
|
||||
----------
|
||||
|
||||
@ -48,12 +50,13 @@ int main(void) {
|
||||
[](const SemanticValues& sv) { return sv[0]; } // 2nd choice
|
||||
};
|
||||
|
||||
parser["Multitive"] = {
|
||||
nullptr, // Default action
|
||||
[](const SemanticValues& sv) {
|
||||
return sv[0].val.get<int>() * sv[1].val.get<int>(); // 1st choice
|
||||
},
|
||||
[](const SemanticValues& sv) { return sv[0]; } // 2nd choice
|
||||
parser["Multitive"] = [](const SemanticValues& sv) {
|
||||
switch (sv.choice) {
|
||||
case 0: // 1st choice
|
||||
return sv[0].val.get<int>() * sv[1].val.get<int>();
|
||||
default: // 2nd choice
|
||||
return sv[0].val.get<int>();
|
||||
}
|
||||
};
|
||||
|
||||
/* This action is not necessary.
|
||||
@ -95,8 +98,9 @@ struct SemanticValue {
|
||||
|
||||
struct SemanticValues : protected std::vector<SemanticValue>
|
||||
{
|
||||
const char* s; // Token start
|
||||
size_t l; // Token length
|
||||
const char* s; // Token start
|
||||
size_t l; // Token length
|
||||
size_t choice; // Choice number (0 based index)
|
||||
}
|
||||
```
|
||||
|
||||
@ -292,7 +296,6 @@ Tested Compilers
|
||||
TODO
|
||||
----
|
||||
|
||||
* Linear-time parsing (Packrat parsing)
|
||||
* Optimization of grammars
|
||||
* Unicode support
|
||||
|
||||
|
8
peglib.h
8
peglib.h
@ -186,8 +186,9 @@ struct SemanticValues : protected std::vector<SemanticValue>
|
||||
{
|
||||
const char* s;
|
||||
size_t l;
|
||||
size_t choice;
|
||||
|
||||
SemanticValues() : s(nullptr), l(0) {}
|
||||
SemanticValues() : s(nullptr), l(0), choice(0) {}
|
||||
|
||||
typedef SemanticValue T;
|
||||
using std::vector<T>::iterator;
|
||||
@ -429,7 +430,6 @@ struct Context
|
||||
const char* s;
|
||||
size_t l;
|
||||
|
||||
size_t choice;
|
||||
const char* error_ptr;
|
||||
const char* msg; // TODO: should be `int`.
|
||||
|
||||
@ -604,8 +604,8 @@ public:
|
||||
}
|
||||
sv.s = chldsv.s;
|
||||
sv.l = chldsv.l;
|
||||
sv.choice = id;
|
||||
c.pop();
|
||||
c.choice = id;
|
||||
return len;
|
||||
}
|
||||
id++;
|
||||
@ -1190,7 +1190,7 @@ inline int Holder::parse(const char* s, size_t l, SemanticValues& sv, Context& c
|
||||
if (success(len) && !outer_->ignore) {
|
||||
assert(!outer_->actions.empty());
|
||||
|
||||
auto i = c.choice + 1; // Index 0 is for the default action
|
||||
auto i = chldsv.choice + 1; // Index 0 is for the default action
|
||||
const auto& action = (i < outer_->actions.size() && outer_->actions[i])
|
||||
? outer_->actions[i]
|
||||
: outer_->actions[0];
|
||||
|
@ -252,7 +252,12 @@ TEST_CASE("Simple calculator test", "[general]")
|
||||
};
|
||||
|
||||
parser["Multitive"] = [](const SemanticValues& sv) {
|
||||
return sv.size() == 1 ? sv[0].val.get<int>() : sv[0].val.get<int>() * sv[1].val.get<int>();
|
||||
switch (sv.choice) {
|
||||
case 0: // Action for the first choice
|
||||
return sv[0].val.get<int>() * sv[1].val.get<int>();
|
||||
default: // Action for the second choice
|
||||
return sv[0].val.get<int>();
|
||||
}
|
||||
};
|
||||
|
||||
parser["Number"] = [](const char* s, size_t l) {
|
||||
|
Loading…
Reference in New Issue
Block a user