From 7d4b176975447ddd396a6a7820a34c40902af50c Mon Sep 17 00:00:00 2001 From: PoiScript Date: Wed, 13 Dec 2023 01:32:53 +0800 Subject: [PATCH] chore: impl Deref for Input --- src/syntax/block.rs | 12 ++++++------ src/syntax/combinator.rs | 4 ++-- src/syntax/comment.rs | 2 +- src/syntax/document.rs | 9 ++------- src/syntax/element.rs | 9 ++------- src/syntax/emphasis.rs | 2 +- src/syntax/fixed_width.rs | 2 +- src/syntax/fn_ref.rs | 2 +- src/syntax/headline.rs | 15 +++++---------- src/syntax/input.rs | 30 +++++++++++------------------ src/syntax/keyword.rs | 20 +++++-------------- src/syntax/latex_fragment.rs | 2 +- src/syntax/list.rs | 19 +++++++----------- src/syntax/macros.rs | 2 +- src/syntax/object.rs | 11 +++-------- src/syntax/paragraph.rs | 9 ++------- src/syntax/subscript_superscript.rs | 2 +- src/syntax/table.rs | 2 +- 18 files changed, 53 insertions(+), 101 deletions(-) diff --git a/src/syntax/block.rs b/src/syntax/block.rs index cbeb264..e9c93c0 100644 --- a/src/syntax/block.rs +++ b/src/syntax/block.rs @@ -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 { _ => 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 { 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 { 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 { 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 { } } - let len = input.input_len() - i.input_len(); + let len = input.len() - i.len(); if len == 0 { Err(nom::Err::Error(())) diff --git a/src/syntax/combinator.rs b/src/syntax/combinator.rs index dc86e51..f7c03e2 100644 --- a/src/syntax/combinator.rs +++ b/src/syntax/combinator.rs @@ -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") ); diff --git a/src/syntax/comment.rs b/src/syntax/comment.rs index 6ccb55e..f77f6b7 100644 --- a/src/syntax/comment.rs +++ b/src/syntax/comment.rs @@ -1,4 +1,4 @@ -use nom::{AsBytes, IResult, InputTake}; +use nom::{IResult, InputTake}; use super::{ combinator::{blank_lines, line_ends_iter, node, GreenElement}, diff --git a/src/syntax/document.rs b/src/syntax/document.rs index 4c07d57..5a526bd 100644 --- a/src/syntax/document.rs +++ b/src/syntax/document.rs @@ -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 { 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); } diff --git a/src/syntax/element.rs b/src/syntax/element.rs index f590622..d0476c5 100644 --- a/src/syntax/element.rs +++ b/src/syntax/element.rs @@ -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, 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; } diff --git a/src/syntax/emphasis.rs b/src/syntax/emphasis.rs index c2654ae..f2b1fb5 100644 --- a/src/syntax/emphasis.rs +++ b/src/syntax/emphasis.rs @@ -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}, diff --git a/src/syntax/fixed_width.rs b/src/syntax/fixed_width.rs index 32875f9..9e9cf2d 100644 --- a/src/syntax/fixed_width.rs +++ b/src/syntax/fixed_width.rs @@ -1,4 +1,4 @@ -use nom::{AsBytes, IResult, InputTake}; +use nom::{IResult, InputTake}; use super::{ combinator::{blank_lines, line_ends_iter, node, GreenElement}, diff --git a/src/syntax/fn_ref.rs b/src/syntax/fn_ref.rs index e24d207..8fd349d 100644 --- a/src/syntax/fn_ref.rs +++ b/src/syntax/fn_ref.rs @@ -3,7 +3,7 @@ use nom::{ bytes::complete::{tag, take_while}, combinator::opt, sequence::tuple, - AsBytes, Err, IResult, InputTake, + Err, IResult, InputTake, }; use super::{ diff --git a/src/syntax/headline.rs b/src/syntax/headline.rs index d4ff787..340223f 100644 --- a/src/syntax/headline.rs +++ b/src/syntax/headline.rs @@ -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 { 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 { 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 { } } - 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 { // 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, ":")]; diff --git a/src/syntax/input.rs b/src/syntax/input.rs index fe2054f..307948c 100644 --- a/src/syntax/input.rs +++ b/src/syntax/input.rs @@ -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> for Input<'a> { fn slice(&self, range: Range) -> 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() } } diff --git a/src/syntax/keyword.rs b/src/syntax/keyword.rs index ecc3af0..f53170a 100644 --- a/src/syntax/keyword.rs +++ b/src/syntax/keyword.rs @@ -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_.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, () 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, 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))) } diff --git a/src/syntax/latex_fragment.rs b/src/syntax/latex_fragment.rs index 1c32732..55520d7 100644 --- a/src/syntax/latex_fragment.rs +++ b/src/syntax/latex_fragment.rs @@ -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; diff --git a/src/syntax/list.rs b/src/syntax/list.rs index 501c812..ab525eb 100644 --- a/src/syntax/list.rs +++ b/src/syntax/list.rs @@ -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 { 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 { 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 Iterator for ObjectPositions<'a> { type Item = (Input<'a>, Input<'a>); fn next(&mut self) -> Option { - 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; } diff --git a/src/syntax/paragraph.rs b/src/syntax/paragraph.rs index c80c1c8..f98c22e 100644 --- a/src/syntax/paragraph.rs +++ b/src/syntax/paragraph.rs @@ -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, 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) diff --git a/src/syntax/subscript_superscript.rs b/src/syntax/subscript_superscript.rs index c75ee07..17f544e 100644 --- a/src/syntax/subscript_superscript.rs +++ b/src/syntax/subscript_superscript.rs @@ -3,7 +3,7 @@ use nom::{ branch::alt, bytes::complete::{tag, take_while1}, combinator::opt, - AsBytes, IResult, InputTake, + IResult, InputTake, }; use crate::{ diff --git a/src/syntax/table.rs b/src/syntax/table.rs index 81b3c9b..2a2ab8a 100644 --- a/src/syntax/table.rs +++ b/src/syntax/table.rs @@ -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::{