Moved 'choice' property to SemanticValues.

pull/3/head
yhirose 9 years ago
parent 8016f4f0bb
commit d93a007ef0
  1. 21
      README.md
  2. 8
      peglib.h
  3. 7
      test/test.cc

@ -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

@ -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…
Cancel
Save