Skip to content

Commit

Permalink
code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
Zheoni committed Nov 19, 2023
1 parent 37493a8 commit 9281714
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub struct Ast<'a> {
///
/// Probably the iterator you want is an instance of [`PullParser`](crate::parser::PullParser).
#[tracing::instrument(level = "debug", skip_all)]
pub fn build_ast<'input>(events: impl Iterator<Item = Event<'input>>) -> PassResult<Ast<'input>> {
pub fn build_ast<'i>(events: impl Iterator<Item = Event<'i>>) -> PassResult<Ast<'i>> {
let mut blocks = Vec::new();
let mut items = Vec::new();
let mut ctx = SourceReport::empty();
Expand Down
8 changes: 4 additions & 4 deletions src/parser/quantity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ pub struct ParsedQuantity<'a> {

/// "parent" block parser. This is just to emit error/warnings and get the text. No tokens will be consumed
/// `tokens` inside '{' '}'. must not be empty
pub(crate) fn parse_quantity<'input>(
bp: &mut BlockParser<'_, 'input>,
pub(crate) fn parse_quantity<'i>(
bp: &mut BlockParser<'_, 'i>,
tokens: &[Token],
) -> ParsedQuantity<'input> {
) -> ParsedQuantity<'i> {
assert!(!tokens.is_empty(), "empty quantity tokens. this is a bug.");

// create an insolated sub-block for the quantity tokens
Expand Down Expand Up @@ -277,7 +277,7 @@ fn numeric_value(tokens: &[Token], bp: &BlockParser) -> Option<Result<Value, Sou
_ => None,
};
if r.is_some() {
return r.map(|r| r.map(|v| Value::Number(Number::Regular(v))));
return r.map(|r| r.map(Value::from));
}

// remove spaces and comments in between other tokens
Expand Down
30 changes: 11 additions & 19 deletions src/parser/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,14 @@ fn comp_body<'t>(bp: &mut BlockParser<'t, '_>) -> Option<Body<'t>> {
let quantity = line.until(|t| t == T!['}'])?;
let close_span_end = line.bump(T!['}']).span.end();
let close_span = Span::new(close_span_start, close_span_end);
if quantity
let quantity_not_empty = quantity
.iter()
.any(|t| !matches!(t.kind, T![ws] | T![block comment]))
{
Some(Body {
name,
close: Some(close_span),
quantity: Some(quantity),
})
} else {
Some(Body {
name,
close: Some(close_span),
quantity: None,
})
}
.any(|t| !matches!(t.kind, T![ws] | T![block comment]));
Some(Body {
name,
close: Some(close_span),
quantity: quantity_not_empty.then_some(quantity),
})
})
.or_else(|| {
bp.with_recover(|bp| {
Expand Down Expand Up @@ -123,7 +115,7 @@ fn modifiers<'t>(bp: &mut BlockParser<'t, '_>) -> &'t [Token] {
&bp.tokens()[start..bp.current]
}

fn note<'input>(bp: &mut BlockParser<'_, 'input>) -> Option<Text<'input>> {
fn note<'i>(bp: &mut BlockParser<'_, 'i>) -> Option<Text<'i>> {
bp.extension(Extensions::COMPONENT_NOTE)
.then(|| {
bp.with_recover(|line| {
Expand Down Expand Up @@ -287,12 +279,12 @@ fn parse_intermediate_ref_data(
Some(Located::new(data, tokens_span(slice)))
}

fn parse_alias<'input>(
fn parse_alias<'i>(
container: &'static str,
bp: &mut BlockParser<'_, 'input>,
bp: &mut BlockParser<'_, 'i>,
tokens: &[Token],
name_offset: usize,
) -> (Text<'input>, Option<Text<'input>>) {
) -> (Text<'i>, Option<Text<'i>>) {
if let Some(alias_sep) = bp
.extension(Extensions::COMPONENT_ALIAS)
.then(|| tokens.iter().position(|t| t.kind == T![|]))
Expand Down
10 changes: 5 additions & 5 deletions src/parser/token_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
pub use crate::lexer::TokenKind;
use crate::{lexer::Cursor, span::Span};

pub struct TokenStream<'input> {
cursor: Cursor<'input>,
pub struct TokenStream<'i> {
cursor: Cursor<'i>,
consumed: usize,
}

impl<'input> TokenStream<'input> {
pub fn new(input: &'input str) -> Self {
impl<'i> TokenStream<'i> {
pub fn new(input: &'i str) -> Self {
Self {
cursor: Cursor::new(input),
consumed: 0,
}
}
}

impl<'input> Iterator for TokenStream<'input> {
impl<'i> Iterator for TokenStream<'i> {
type Item = Token;

fn next(&mut self) -> Option<Self::Item> {
Expand Down

0 comments on commit 9281714

Please sign in to comment.