chore: impl Deref for Input
This commit is contained in:
parent
27de7ee68c
commit
7d4b176975
18 changed files with 53 additions and 101 deletions
|
|
@ -4,7 +4,7 @@ use nom::{
|
|||
character::complete::{alpha1, space0, space1},
|
||||
combinator::{cond, opt},
|
||||
sequence::{separated_pair, tuple},
|
||||
IResult, InputLength, InputTake,
|
||||
IResult, InputTake,
|
||||
};
|
||||
|
||||
use super::{
|
||||
|
|
@ -32,7 +32,7 @@ fn block_node_base(input: Input) -> IResult<Input, GreenElement, ()> {
|
|||
_ => SPECIAL_BLOCK,
|
||||
};
|
||||
|
||||
for (input, contents) in line_starts_iter(input.as_str()).map(|i| input.take_split(i)) {
|
||||
for (input, contents) in line_starts_iter(&input).map(|i| input.take_split(i)) {
|
||||
if let Ok((input, block_end)) = block_end_node(input, name) {
|
||||
let (input, post_blank) = blank_lines(input)?;
|
||||
|
||||
|
|
@ -60,7 +60,7 @@ fn block_begin_node(input: Input) -> IResult<Input, (GreenElement, &str), ()> {
|
|||
b.text(begin);
|
||||
b.text(name);
|
||||
|
||||
if name.s.eq_ignore_ascii_case("SRC") {
|
||||
if name.eq_ignore_ascii_case("SRC") {
|
||||
let (input, language) = opt(tuple((
|
||||
space1,
|
||||
take_while1(|c: char| c != ' ' && c != '\t' && c != '\n' && c != '\r'),
|
||||
|
|
@ -84,7 +84,7 @@ fn block_begin_node(input: Input) -> IResult<Input, (GreenElement, &str), ()> {
|
|||
b.ws(ws2);
|
||||
b.nl(nl);
|
||||
Ok((input, (b.finish(BLOCK_BEGIN), name.as_str())))
|
||||
} else if name.s.eq_ignore_ascii_case("EXPORT") {
|
||||
} else if name.eq_ignore_ascii_case("EXPORT") {
|
||||
let (input, ty) = opt(tuple((
|
||||
space1,
|
||||
take_while1(|c: char| c != ' ' && c != '\t' && c != '\n' && c != '\r'),
|
||||
|
|
@ -114,7 +114,7 @@ fn source_block_switches(input: Input) -> IResult<Input, Input, ()> {
|
|||
|
||||
while !i.is_empty() {
|
||||
match tuple::<_, _, (), _>((
|
||||
cond(i.input_len() != input.input_len(), space1),
|
||||
cond(i.len() != input.len(), space1),
|
||||
alt((
|
||||
separated_pair(
|
||||
alt((tag("-l"), tag("-n"))),
|
||||
|
|
@ -131,7 +131,7 @@ fn source_block_switches(input: Input) -> IResult<Input, Input, ()> {
|
|||
}
|
||||
}
|
||||
|
||||
let len = input.input_len() - i.input_len();
|
||||
let len = input.len() - i.len();
|
||||
|
||||
if len == 0 {
|
||||
Err(nom::Err::Error(()))
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
use memchr::{memchr2, memchr2_iter, Memchr2};
|
||||
use nom::{bytes::complete::tag, AsBytes, IResult, InputTake, Slice};
|
||||
use nom::{bytes::complete::tag, IResult, InputTake, Slice};
|
||||
use rowan::{GreenNode, GreenToken, Language, NodeOrToken};
|
||||
use std::iter::once;
|
||||
|
||||
|
|
@ -78,7 +78,7 @@ macro_rules! lossless_parser {
|
|||
let (i, o) = $parser($input)?;
|
||||
tracing::trace!(consumed = o.to_string());
|
||||
debug_assert_eq!(
|
||||
&i_.as_str()[0..(i_.s.len() - i.s.len())],
|
||||
&i_.as_str()[0..(i_.len() - i.len())],
|
||||
&o.to_string(),
|
||||
stringify!("parser must be lossless")
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use nom::{AsBytes, IResult, InputTake};
|
||||
use nom::{IResult, InputTake};
|
||||
|
||||
use super::{
|
||||
combinator::{blank_lines, line_ends_iter, node, GreenElement},
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use nom::{combinator::opt, IResult, InputLength};
|
||||
use nom::{combinator::opt, IResult};
|
||||
|
||||
use super::{
|
||||
combinator::{blank_lines, node, GreenElement},
|
||||
|
|
@ -31,12 +31,7 @@ fn document_node_base(input: Input) -> IResult<Input, GreenElement, ()> {
|
|||
let mut i = input;
|
||||
while !i.is_empty() {
|
||||
let (input, headline) = headline_node(i)?;
|
||||
debug_assert!(
|
||||
i.input_len() > input.input_len(),
|
||||
"{} > {}",
|
||||
i.input_len(),
|
||||
input.input_len(),
|
||||
);
|
||||
debug_assert!(i.len() > input.len(), "{} > {}", i.len(), input.len(),);
|
||||
i = input;
|
||||
children.push(headline);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
use std::iter::once;
|
||||
|
||||
use memchr::memchr2_iter;
|
||||
use nom::{AsBytes, IResult, InputLength, InputTake};
|
||||
use nom::{IResult, InputTake};
|
||||
|
||||
use super::{
|
||||
block::block_node,
|
||||
|
|
@ -44,12 +44,7 @@ pub fn element_nodes(input: Input) -> Result<Vec<GreenElement>, nom::Err<()>> {
|
|||
nodes.extend(paragraph_nodes(head)?);
|
||||
}
|
||||
nodes.push(element);
|
||||
debug_assert!(
|
||||
input.input_len() < i.input_len(),
|
||||
"{} < {}",
|
||||
input.input_len(),
|
||||
i.input_len()
|
||||
);
|
||||
debug_assert!(input.len() < i.len(), "{} < {}", input.len(), i.len());
|
||||
i = input;
|
||||
continue 'l;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
use bytecount::count;
|
||||
use memchr::memchr_iter;
|
||||
use nom::{combinator::map, AsBytes, IResult, Slice};
|
||||
use nom::{combinator::map, IResult, Slice};
|
||||
|
||||
use super::{
|
||||
combinator::{node, token, GreenElement},
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use nom::{AsBytes, IResult, InputTake};
|
||||
use nom::{IResult, InputTake};
|
||||
|
||||
use super::{
|
||||
combinator::{blank_lines, line_ends_iter, node, GreenElement},
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ use nom::{
|
|||
bytes::complete::{tag, take_while},
|
||||
combinator::opt,
|
||||
sequence::tuple,
|
||||
AsBytes, Err, IResult, InputTake,
|
||||
Err, IResult, InputTake,
|
||||
};
|
||||
|
||||
use super::{
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ use nom::{
|
|||
character::complete::{anychar, space0},
|
||||
combinator::{map, opt},
|
||||
sequence::tuple,
|
||||
AsBytes, IResult, InputLength, InputTake, Slice,
|
||||
IResult, InputTake, Slice,
|
||||
};
|
||||
|
||||
use super::{
|
||||
|
|
@ -82,7 +82,7 @@ fn headline_node_base(input: Input) -> IResult<Input, GreenElement, ()> {
|
|||
b.push_opt(section);
|
||||
|
||||
let mut i = input;
|
||||
let current_level = stars.input_len();
|
||||
let current_level = stars.len();
|
||||
while !i.is_empty() {
|
||||
let next_level = i.bytes().take_while(|&c| c == b'*').count();
|
||||
|
||||
|
|
@ -92,12 +92,7 @@ fn headline_node_base(input: Input) -> IResult<Input, GreenElement, ()> {
|
|||
|
||||
let (input, headline) = headline_node(i)?;
|
||||
b.push(headline);
|
||||
debug_assert!(
|
||||
i.input_len() > input.input_len(),
|
||||
"{} > {}",
|
||||
i.input_len(),
|
||||
input.input_len()
|
||||
);
|
||||
debug_assert!(i.len() > input.len(), "{} > {}", i.len(), input.len());
|
||||
i = input;
|
||||
}
|
||||
|
||||
|
|
@ -122,7 +117,7 @@ fn section_text(input: Input) -> IResult<Input, Input, ()> {
|
|||
}
|
||||
}
|
||||
|
||||
Ok(input.take_split(input.input_len()))
|
||||
Ok(input.take_split(input.len()))
|
||||
}
|
||||
|
||||
#[tracing::instrument(level = "debug", skip(input), fields(input = input.s))]
|
||||
|
|
@ -151,7 +146,7 @@ fn headline_tags_node(input: Input) -> IResult<Input, GreenElement, ()> {
|
|||
|
||||
// we're going to skip to first colon, so we start from the
|
||||
// second last character
|
||||
let mut i = input.input_len() - 1;
|
||||
let mut i = input.len() - 1;
|
||||
let mut can_not_be_ws = true;
|
||||
let mut children = vec![token(COLON, ":")];
|
||||
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
use nom::{
|
||||
error::{ErrorKind, ParseError},
|
||||
AsBytes, Compare, CompareResult, Err, FindSubstring, IResult, InputIter, InputLength,
|
||||
InputTake, InputTakeAtPosition, Needed, Offset, Slice,
|
||||
Compare, CompareResult, Err, FindSubstring, IResult, InputIter, InputLength, InputTake,
|
||||
InputTakeAtPosition, Needed, Offset, Slice,
|
||||
};
|
||||
use std::{
|
||||
ops::{Range, RangeFrom, RangeFull, RangeTo},
|
||||
str::{Bytes, CharIndices, Chars},
|
||||
ops::{Deref, Range, RangeFrom, RangeFull, RangeTo},
|
||||
str::{CharIndices, Chars},
|
||||
};
|
||||
|
||||
use super::{
|
||||
|
|
@ -34,11 +34,6 @@ impl<'a> Input<'a> {
|
|||
self.s
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.s.is_empty()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn token(&self, kind: SyntaxKind) -> GreenElement {
|
||||
token(kind, self.s)
|
||||
|
|
@ -58,10 +53,14 @@ impl<'a> Input<'a> {
|
|||
pub fn nl_token(&self) -> GreenElement {
|
||||
token(SyntaxKind::NEW_LINE, self.s)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Deref for Input<'a> {
|
||||
type Target = str;
|
||||
|
||||
#[inline]
|
||||
pub fn bytes(&self) -> Bytes {
|
||||
self.s.bytes()
|
||||
fn deref(&self) -> &'a str {
|
||||
self.s
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -74,13 +73,6 @@ impl<'a> From<(&'a str, &'a ParseConfig)> for Input<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a> AsBytes for Input<'a> {
|
||||
#[inline]
|
||||
fn as_bytes(&self) -> &[u8] {
|
||||
self.s.as_bytes()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Slice<Range<usize>> for Input<'a> {
|
||||
fn slice(&self, range: Range<usize>) -> Self {
|
||||
self.of(self.s.slice(range))
|
||||
|
|
@ -126,7 +118,7 @@ impl<'a, 'b> Compare<&'b str> for Input<'a> {
|
|||
impl<'a> InputLength for Input<'a> {
|
||||
#[inline]
|
||||
fn input_len(&self) -> usize {
|
||||
self.s.len()
|
||||
self.len()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ use nom::{
|
|||
character::complete::space0,
|
||||
combinator::{recognize, verify},
|
||||
sequence::tuple,
|
||||
IResult, InputLength, InputTake,
|
||||
IResult, InputTake,
|
||||
};
|
||||
|
||||
use super::{
|
||||
|
|
@ -59,12 +59,7 @@ pub fn affiliated_keyword_nodes(input: Input) -> IResult<Input, Vec<GreenElement
|
|||
break;
|
||||
}
|
||||
|
||||
debug_assert!(
|
||||
i.input_len() > input_.input_len(),
|
||||
"{} > {}",
|
||||
i.input_len(),
|
||||
input_.input_len()
|
||||
);
|
||||
debug_assert!(i.len() > input_.len(), "{} > {}", i.len(), input_.len());
|
||||
i = input_;
|
||||
children.push(node(SyntaxKind::AFFILIATED_KEYWORD, nodes));
|
||||
}
|
||||
|
|
@ -85,12 +80,7 @@ pub fn tblfm_keyword_nodes(input: Input) -> IResult<Input, Vec<GreenElement>, ()
|
|||
break;
|
||||
}
|
||||
|
||||
debug_assert!(
|
||||
i.input_len() > input.input_len(),
|
||||
"{} > {}",
|
||||
i.input_len(),
|
||||
input.input_len()
|
||||
);
|
||||
debug_assert!(i.len() > input.len(), "{} > {}", i.len(), input.len());
|
||||
i = input;
|
||||
children.push(node(SyntaxKind::KEYWORD, nodes));
|
||||
}
|
||||
|
|
@ -134,9 +124,9 @@ fn key(input: Input) -> IResult<Input, (Input, Option<(Input, Input, Input)>, In
|
|||
take_till(|c: char| c.is_ascii_whitespace() || c == ':'),
|
||||
take_while1(|c: char| c == ':'),
|
||||
))),
|
||||
|i: &Input| i.input_len() >= 2,
|
||||
|i: &Input| i.len() >= 2,
|
||||
)(input)?;
|
||||
let (colon, key) = output.take_split(output.input_len() - 1);
|
||||
let (colon, key) = output.take_split(output.len() - 1);
|
||||
Ok((input, (key, None, colon)))
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ use nom::{
|
|||
bytes::complete::{take_until1, take_while1},
|
||||
character::complete::alpha1,
|
||||
sequence::tuple,
|
||||
AsBytes, IResult, InputTake,
|
||||
IResult, InputTake,
|
||||
};
|
||||
|
||||
use crate::SyntaxKind;
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ use nom::{
|
|||
character::complete::{alphanumeric1, digit1, space0, space1},
|
||||
combinator::{cond, map, opt, recognize, verify},
|
||||
sequence::{preceded, tuple},
|
||||
AsBytes, IResult, InputLength, InputTake,
|
||||
IResult, InputTake,
|
||||
};
|
||||
|
||||
use super::{
|
||||
|
|
@ -39,7 +39,7 @@ fn list_node_base(input: Input) -> IResult<Input, GreenElement, ()> {
|
|||
while !ends_with_empty_blank_lines && !input.is_empty() {
|
||||
let (input_, indent) = space0(input)?;
|
||||
|
||||
if indent.input_len() != first_indent.input_len() {
|
||||
if indent.len() != first_indent.len() {
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
@ -50,10 +50,10 @@ fn list_node_base(input: Input) -> IResult<Input, GreenElement, ()> {
|
|||
|
||||
children.push(list_item);
|
||||
debug_assert!(
|
||||
input.input_len() > input_.input_len(),
|
||||
input.len() > input_.len(),
|
||||
"{} > {}",
|
||||
input.input_len(),
|
||||
input_.input_len(),
|
||||
input.len(),
|
||||
input_.len(),
|
||||
);
|
||||
input = input_;
|
||||
|
||||
|
|
@ -111,7 +111,7 @@ fn list_item_node<'a>(
|
|||
let (input, checkbox) = opt(list_item_checkbox)(input)?;
|
||||
let (input, tag) = cond(!is_ordered, opt(list_item_tag))(input)?;
|
||||
let (input, (ends_with_empty_blank_lines, content)) =
|
||||
list_item_content_node(input, indent.input_len())?;
|
||||
list_item_content_node(input, indent.len())?;
|
||||
let (input, post_blank) = cond(!ends_with_empty_blank_lines, blank_lines)(input)?;
|
||||
|
||||
let mut children = vec![
|
||||
|
|
@ -240,12 +240,7 @@ fn list_item_content_node(input: Input, indent: usize) -> IResult<Input, (bool,
|
|||
children.extend(paragraph_nodes(head)?);
|
||||
}
|
||||
children.push(element);
|
||||
debug_assert!(
|
||||
input.input_len() < i.input_len(),
|
||||
"{} < {}",
|
||||
input.input_len(),
|
||||
i.input_len()
|
||||
);
|
||||
debug_assert!(input.len() < i.len(), "{} < {}", input.len(), i.len());
|
||||
i = input;
|
||||
skip_one = false;
|
||||
continue 'l;
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ use nom::{
|
|||
bytes::complete::{take_until, take_while1},
|
||||
combinator::{map, opt, verify},
|
||||
sequence::tuple,
|
||||
AsBytes, IResult,
|
||||
IResult,
|
||||
};
|
||||
|
||||
use super::{
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use nom::{AsBytes, IResult, InputLength, InputTake};
|
||||
use nom::{IResult, InputTake};
|
||||
|
||||
use super::{
|
||||
combinator::GreenElement,
|
||||
|
|
@ -84,7 +84,7 @@ impl<'a> Iterator for ObjectPositions<'a> {
|
|||
type Item = (Input<'a>, Input<'a>);
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.input.input_len() < 2 || self.pos >= self.input.input_len() {
|
||||
if self.input.len() < 2 || self.pos >= self.input.len() {
|
||||
return None;
|
||||
}
|
||||
|
||||
|
|
@ -236,12 +236,7 @@ where
|
|||
nodes.push(head.text_token())
|
||||
}
|
||||
nodes.push(pre);
|
||||
debug_assert!(
|
||||
input.input_len() < i.input_len(),
|
||||
"{} < {}",
|
||||
input.input_len(),
|
||||
i.input_len()
|
||||
);
|
||||
debug_assert!(input.len() < i.len(), "{} < {}", input.len(), i.len());
|
||||
i = input;
|
||||
continue 'l;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use nom::{IResult, InputLength, InputTake};
|
||||
use nom::{IResult, InputTake};
|
||||
|
||||
use super::{
|
||||
combinator::{blank_lines, line_ends_iter, node, GreenElement},
|
||||
|
|
@ -20,12 +20,7 @@ pub fn paragraph_nodes(input: Input) -> Result<Vec<GreenElement>, nom::Err<()>>
|
|||
while !i.is_empty() {
|
||||
let (input, node) = paragraph_node(i)?;
|
||||
children.push(node);
|
||||
debug_assert!(
|
||||
i.input_len() > input.input_len(),
|
||||
"{} > {}",
|
||||
i.input_len(),
|
||||
input.input_len()
|
||||
);
|
||||
debug_assert!(i.len() > input.len(), "{} > {}", i.len(), input.len());
|
||||
i = input;
|
||||
}
|
||||
Ok(children)
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ use nom::{
|
|||
branch::alt,
|
||||
bytes::complete::{tag, take_while1},
|
||||
combinator::opt,
|
||||
AsBytes, IResult, InputTake,
|
||||
IResult, InputTake,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ use nom::{
|
|||
character::complete::{multispace0, space0},
|
||||
combinator::iterator,
|
||||
sequence::tuple,
|
||||
AsBytes, Err, IResult, InputTake, Slice,
|
||||
Err, IResult, InputTake, Slice,
|
||||
};
|
||||
|
||||
use super::{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue