Skip to content

Commit

Permalink
Merge 7939bc3 into 9d43463
Browse files Browse the repository at this point in the history
  • Loading branch information
dragostis committed Jul 5, 2016
2 parents 9d43463 + 7939bc3 commit a76942b
Show file tree
Hide file tree
Showing 11 changed files with 280 additions and 102 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -165,4 +165,4 @@
## [v0.0.1](https://github.com/dragostis/pest/tree/v0.0.1) (2016-04-24)


\* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)*
\* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)*
79 changes: 36 additions & 43 deletions src/grammar.rs
Expand Up @@ -15,8 +15,9 @@
/// pest has four special rules:
///
/// * `whitespace` - gets run between rules and sub-rules
/// * `comment` - gets run only between rules
/// * `comment` - gets run between rules and sub-rules
/// * `any` - matches exactly one `char`
/// * `soi` - (start-of-input) matches only when a `Parser` is still at the starting position
/// * `eoi` - (end-of-input) matches only when a `Parser` has reached its
/// [end](trait.Parser#tymethod.end)
///
Expand Down Expand Up @@ -132,8 +133,6 @@ macro_rules! grammar {
// handle precedence climbing
( @conv_prec $pos:ident ($_prec:expr) $_atomic:tt $slf:ident [] [] [] ) => {
{
$slf.input_mut().set_pos($pos);

None
}
};
Expand Down Expand Up @@ -175,36 +174,46 @@ macro_rules! grammar {
{
let mut primary = |slf: &mut Self| {
let pos = slf.input().pos();
let len = slf.queue().len();

grammar!(@skip $atomic slf);
slf.skip();

let result = grammar!(@conv $atomic slf [ $( $primary )* ] [] []);

if !result {
slf.input_mut().set_pos(pos);
slf.queue_mut().truncate(len);
}

result
};
let mut climb = |slf: &mut Self| {
let pos = slf.input().pos();
let len = slf.queue().len();

slf.skip();

let result = grammar!(@conv_prec pos (0u8) $atomic slf [ $( $ts )* ] [] []);

grammar!(@skip $atomic slf);
if result.is_none() {
slf.input_mut().set_pos(pos);
slf.queue_mut().truncate(len);
}

grammar!(@conv_prec pos (0u8) $atomic slf [ $( $ts )* ] [] [])
result
};

let mut pos = $slf.input().pos();
let queue_pos = $slf.queue().len();
let len = $slf.queue().len();

let result = primary($slf);
let result = grammar!(@conv $atomic $slf [ $( $primary )* ] [] []);

if let Some(token) = $slf.queue().get(queue_pos) {
if let Some(token) = $slf.queue().get(len) {
pos = token.start;
}

if result {
$slf.prec_climb(queue_pos, pos, 0, None, &mut primary, &mut climb);
$slf.prec_climb(len, pos, 0, None, &mut primary, &mut climb);
}

result
Expand Down Expand Up @@ -284,17 +293,18 @@ macro_rules! grammar {
{
grammar!(@process false $slf [(( $slf.try(false, |$slf| {
if grammar!(@mtc $slf $a) {
let original = $slf.input().pos();
let original_pos = $slf.input().pos();
let original_len = $slf.queue().len();

$slf.skip_ws();
$slf.skip();

let pos = $slf.input().pos();
let len = $slf.queue().len();

let result = grammar!(@mtc $slf $b);

if $slf.input().pos() == pos && !$slf.eoi_matched() {
$slf.input_mut().set_pos(original);
$slf.input_mut().set_pos(original_pos);
$slf.queue_mut().truncate(original_len);
}

result
Expand Down Expand Up @@ -322,17 +332,20 @@ macro_rules! grammar {
{
grammar!(@process false $slf [(( {
let mut pos = $slf.input().pos();
let mut len = $slf.queue().len();

loop {
if !grammar!(@mtc $slf $a) {
$slf.input_mut().set_pos(pos);
$slf.queue_mut().truncate(len);

break
}

pos = $slf.input().pos();
len = $slf.queue().len();

$slf.skip_ws();
$slf.skip();
}

true
Expand All @@ -357,11 +370,13 @@ macro_rules! grammar {
grammar!(@process false $slf [(( if grammar!(@mtc $slf $a) {
loop {
let pos = $slf.input().pos();
let len = $slf.queue().len();

$slf.skip_ws();
$slf.skip();

if !grammar!(@mtc $slf $a) {
$slf.input_mut().set_pos(pos);
$slf.queue_mut().truncate(len);

break
}
Expand Down Expand Up @@ -418,24 +433,6 @@ macro_rules! grammar {
grammar!(@process $atomic $slf [ $head $( $optail )* ] [ $( $tail )* ])
};

// skip only if not whitespace
( @skip whitespace $_slf:ident ) => ();
( @skip comment $slf:ident ) => ($slf.skip_ws());
( @skip $_name:ident $slf:ident ) => {
{
$slf.skip_com();
$slf.skip_ws();
}
};
// skip if not atomic
( @skip false $slf:ident ) => ();
( @skip true $slf:ident ) => {
{
$slf.skip_com();
$slf.skip_ws();
}
};

// whitespace and comment are always atomic
( @atomic whitespace $_atomic:tt $slf:ident $rules:tt ) => {
grammar!(@conv true $slf $rules [] [])
Expand All @@ -460,9 +457,6 @@ macro_rules! grammar {

if revert || !result {
self.input_mut().set_pos(pos);
}

if !result {
self.queue_mut().truncate(len);
}

Expand Down Expand Up @@ -490,7 +484,9 @@ macro_rules! grammar {
let mut right = self.input().pos();
let queue_pos = self.queue().len();

primary(self);
if !primary(self) {
break
}

if let Some(token) = self.queue().get(queue_pos) {
new_pos = token.start;
Expand Down Expand Up @@ -542,11 +538,10 @@ macro_rules! grammar {
#[inline]
pub fn $name(&mut self) -> bool {
let slf = self;
grammar!(@skip $name slf);

let pos = slf.input().pos();
let len = slf.queue().len();
let tracked_len = slf.tracked_len();
let tracked_len_pos = slf.tracked_len_pos();

let result = grammar!(@atomic $name false slf [ $( $ts )* ]);

Expand All @@ -563,7 +558,7 @@ macro_rules! grammar {
} else {
slf.queue_mut().truncate(len);

if slf.tracked_len() == tracked_len {
if slf.tracked_len_pos() == tracked_len_pos {
slf.track(Rule::$name, pos);
}
}
Expand All @@ -580,7 +575,6 @@ macro_rules! grammar {
#[inline]
pub fn $name(&mut self) -> bool {
let slf = self;
grammar!(@skip $name slf);

let pos = slf.input().pos();
let len = slf.queue().len();
Expand Down Expand Up @@ -625,7 +619,6 @@ macro_rules! grammar {
#[inline]
pub fn $name(&mut self) -> bool {
let slf = self;
grammar!(@skip $name slf);

let result = grammar!(@atomic $name false slf [ $( $ts )* ]);

Expand Down
9 changes: 3 additions & 6 deletions src/parser.rs
Expand Up @@ -40,11 +40,8 @@ pub trait Parser<'a, T: Input<'a>> {
/// Set the current index within the queue. Used in `process!`.
fn set_queue_index(&self, index: usize);

/// Skips white-space.
fn skip_ws(&mut self);

/// Skips comments.
fn skip_com(&mut self);
/// Skips whitespace and comments.
fn skip(&mut self);

/// Returns whether a `Parser` is currently inside an atomic rule.
fn is_atomic(&self) -> bool;
Expand All @@ -56,7 +53,7 @@ pub trait Parser<'a, T: Input<'a>> {
fn track(&mut self, failed: Self::Rule, pos: usize);

/// Returns the length of the tracked `Rule`s.
fn tracked_len(&self) -> usize;
fn tracked_len_pos(&self) -> (usize, usize);

/// Retuns a `Vec` of all expected `Rule`s at the deepest position where the parsing last
/// stopped. It only returns leafs from the rule tree. Used for error reporting.
Expand Down

0 comments on commit a76942b

Please sign in to comment.