Skip to content

Commit

Permalink
fixed regex rule in root position
Browse files Browse the repository at this point in the history
  • Loading branch information
eqv committed May 29, 2020
1 parent c007438 commit 50a806b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ ctx.regex("TAG","[a-z]+")
To test your grammars you can use the generator:

```
$ cargo run --bin generator -- -g grammars/test.py -t 100
$ cargo run --bin generator -- -g grammars/grammar_py_exmaple.py -t 100
<document><some_tag foo=bar><other_tag foo=bar><other_tag foo=bar><some_tag foo=bar></some_tag></other_tag><some_tag foo=bar><other_tag foo=bar></other_tag></some_tag><other_tag foo=bar></other_tag><some_tag foo=bar></some_tag></other_tag><other_tag foo=bar></other_tag><some_tag foo=bar></some_tag></some_tag></document>
```

Expand Down
18 changes: 14 additions & 4 deletions grammartec/src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ use pyo3::prelude::{PyObject, PyResult, Python};
use pyo3::types::{PyBytes, PyString, PyTuple};
use pyo3::FromPyObject;
use recursion_info::RecursionInfo;
use rule::{PlainRule, Rule, RuleChild, RuleIDOrCustom, ScriptRule};
use rule::{PlainRule, Rule, RuleChild, RuleIDOrCustom, ScriptRule, RegExpRule};
use rand::thread_rng;
use rand::Rng;

enum UnparseStep<'dat> {
Term(&'dat [u8]),
Expand Down Expand Up @@ -346,9 +348,17 @@ impl Tree {
ctx.get_rule(ruleid).generate(self, &ctx, max_len);
self.sizes[0] = self.rules.len();
}
Rule::RegExp(..) => {
unreachable!();
} //TODO implement me
Rule::RegExp(RegExpRule { hir, .. }) => {
let rid = RuleIDOrCustom::Custom(
ruleid,
regex_mutator::generate(hir, thread_rng().gen::<u64>()),
);
self.truncate();
self.rules.push(rid);
self.sizes.push(0);
self.paren.push(NodeID::from(0));
self.sizes[0] = self.rules.len();
}
}
}

Expand Down
1 change: 1 addition & 0 deletions test_cases/grammar_regex_root.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ctx.regex("START","[a-z]+")

0 comments on commit 50a806b

Please sign in to comment.