Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "pasfmt-core"
version = "0.7.0+dev"
edition = "2021"
edition = "2024"

[features]
_lang_types_from_str = ["dep:strum", "dep:strum_macros"]
Expand Down
4 changes: 2 additions & 2 deletions core/benches/benchmark_lexer.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::{ops::Range, time::Duration};

use criterion::{criterion_group, criterion_main, Criterion};
use criterion::{Criterion, criterion_group, criterion_main};
use pasfmt_core::prelude::*;
use rand::{seq::SliceRandom, Rng};
use rand::{Rng, seq::SliceRandom};

fn criterion_benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("lexer");
Expand Down
2 changes: 1 addition & 1 deletion core/datatests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "datatests"
version = "0.0.0"
edition = "2021"
edition = "2024"

[dev-dependencies]
datatest-stable = { workspace = true }
Expand Down
5 changes: 4 additions & 1 deletion core/datatests/tests/suites/logical_line_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,10 @@ impl Display for TestParsingError {
write!(f, "{PREFIX}marker must succeed another token '{input}'")
}
TestParsingError::NoLineTypeDelimiter(input) => {
write!(f, "{PREFIX}no logical line type declaration delimiter '{input}' (expected `number:LogicalLineType`)")
write!(
f,
"{PREFIX}no logical line type declaration delimiter '{input}' (expected `number:LogicalLineType`)"
)
}
TestParsingError::InvalidLogicalLineType(input) => {
write!(f, "{PREFIX}invalid logical line type '{input}'")
Expand Down
10 changes: 5 additions & 5 deletions core/src/defaults/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,10 +534,10 @@ fn get_word_token_type(input: &str) -> RawTokenType {

if input.len() <= MAX_WORD_LENGTH {
let key = hash_keyword(input) as usize;
if let Some(Some((candidate, keyword))) = KEYWORD_LOOKUP_TABLE.get(key) {
if input.eq_ignore_ascii_case(candidate) {
return *keyword;
}
if let Some(Some((candidate, keyword))) = KEYWORD_LOOKUP_TABLE.get(key)
&& input.eq_ignore_ascii_case(candidate)
{
return *keyword;
}
}

Expand Down Expand Up @@ -2673,7 +2673,7 @@ mod tests {
("THIN_SPACE\u{2009}", TT::Identifier),
("ZERO_WIDTH_NBSP\u{FEFF}", TT::Identifier),
// note, does not contain the U+3000 character
( "IDEOGRAPHIC_SPACE", TT::Identifier),
("IDEOGRAPHIC_SPACE", TT::Identifier),
],
)
}
Expand Down
70 changes: 34 additions & 36 deletions core/src/defaults/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,19 +610,19 @@ impl<'a, 'b> InternalDelphiLogicalLineParser<'a, 'b> {
level,
});

if self.context.is_ended.last() == Some(&false) {
if let Some(KK::Else) = self.get_current_keyword_kind() {
let parent = self.get_line_parent_of_current_token();
self.next_token(); // else

trace!("Parse `else` statement");
level = ParserContextLevel::Parent(parent, 1);
self.parse_block(ParserContext {
context_type: ContextType::Statement(StatementKind::Normal),
context_ending_predicate: CEP::Transparent(never_ending),
level,
});
}
if self.context.is_ended.last() == Some(&false)
&& let Some(KK::Else) = self.get_current_keyword_kind()
{
let parent = self.get_line_parent_of_current_token();
self.next_token(); // else

trace!("Parse `else` statement");
level = ParserContextLevel::Parent(parent, 1);
self.parse_block(ParserContext {
context_type: ContextType::Statement(StatementKind::Normal),
context_ending_predicate: CEP::Transparent(never_ending),
level,
});
}

trace!("Finished parsing `if` statement");
Expand Down Expand Up @@ -861,8 +861,8 @@ impl<'a, 'b> InternalDelphiLogicalLineParser<'a, 'b> {
self.context.update_statuses(ending_context);
return;
}
if self.is_at_start_of_line() {
if let Some(line_type) = match context.context_type {
if self.is_at_start_of_line()
&& let Some(line_type) = match context.context_type {
ContextType::Statement(StatementKind::Case) => Some(LLT::CaseArm),
ContextType::LabelBlock
| ContextType::TypeBlock
Expand All @@ -872,9 +872,9 @@ impl<'a, 'b> InternalDelphiLogicalLineParser<'a, 'b> {
Some(LLT::VariantRecordCaseArm)
}
_ => None,
} {
self.set_logical_line_type(line_type);
}
{
self.set_logical_line_type(line_type);
}
}
trace!("parse_statement with {:?}", token_type);
Expand Down Expand Up @@ -1749,10 +1749,9 @@ impl<'a, 'b> InternalDelphiLogicalLineParser<'a, 'b> {
if let Some(TT::IdentifierOrKeyword(
directive @ (KK::Deprecated | KK::Experimental | KK::Platform | KK::Library),
)) = self.tokens.get(token_index).map(RawToken::get_token_type)
&& let Some(token) = self.tokens.get_mut(token_index)
{
if let Some(token) = self.tokens.get_mut(token_index) {
token.set_token_type(TT::Keyword(directive))
}
token.set_token_type(TT::Keyword(directive))
}

line_index -= 1;
Expand Down Expand Up @@ -1847,7 +1846,7 @@ impl<'a, 'b> InternalDelphiLogicalLineParser<'a, 'b> {
}
fn get_current_logical_line_token_types(
&self,
) -> impl DoubleEndedIterator<Item = RawTokenType> + '_ {
) -> impl DoubleEndedIterator<Item = RawTokenType> + use<'_> {
self.get_current_logical_line()
.tokens
.iter()
Expand Down Expand Up @@ -1928,10 +1927,9 @@ impl<'a, 'b> InternalDelphiLogicalLineParser<'a, 'b> {
.checked_sub(1)
.and_then(|prev_pass_index| self.pass_indices.get(prev_pass_index))
.and_then(|&token_index| self.tokens.get_mut(token_index))
&& let TT::IdentifierOrKeyword(keyword_kind) = token.get_token_type()
{
if let TT::IdentifierOrKeyword(keyword_kind) = token.get_token_type() {
token.set_token_type(TT::Keyword(keyword_kind));
}
token.set_token_type(TT::Keyword(keyword_kind));
}
}
fn get_token_type_for_index(&self, index: usize) -> Option<RawTokenType> {
Expand Down Expand Up @@ -2019,17 +2017,17 @@ impl<'a, 'b> InternalDelphiLogicalLineParser<'a, 'b> {
.and_then(Self::get_keyword_kind)
}
fn consolidate_current_ident(&mut self) {
if let Some(token) = self.get_token_mut::<0>() {
if let TT::IdentifierOrKeyword(_) = token.get_token_type() {
token.set_token_type(TT::Identifier);
}
if let Some(token) = self.get_token_mut::<0>()
&& let TT::IdentifierOrKeyword(_) = token.get_token_type()
{
token.set_token_type(TT::Identifier);
}
}
fn consolidate_current_keyword(&mut self) {
if let Some(token) = self.get_token_mut::<0>() {
if let TT::IdentifierOrKeyword(keyword_kind) = token.get_token_type() {
token.set_token_type(TT::Keyword(keyword_kind));
}
if let Some(token) = self.get_token_mut::<0>()
&& let TT::IdentifierOrKeyword(keyword_kind) = token.get_token_type()
{
token.set_token_type(TT::Keyword(keyword_kind));
}
}
fn set_current_token_type(&mut self, token_type: RawTokenType) {
Expand Down Expand Up @@ -2306,10 +2304,10 @@ const PORTABILITY_DIRECTIVES: [KeywordKind; 4] =
[KK::Deprecated, KK::Experimental, KK::Platform, KK::Library];
fn keyword_consolidator(keyword_predicate: impl Fn(KeywordKind) -> bool) -> impl Fn(&mut LLP) {
move |parser| {
if let Some(TT::IdentifierOrKeyword(keyword_kind)) = parser.get_current_token_type() {
if keyword_predicate(keyword_kind) {
parser.consolidate_current_keyword()
}
if let Some(TT::IdentifierOrKeyword(keyword_kind)) = parser.get_current_token_type()
&& keyword_predicate(keyword_kind)
{
parser.consolidate_current_keyword()
}
parser.next_token();
}
Expand Down
34 changes: 16 additions & 18 deletions core/src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -842,10 +842,10 @@ else
impl LogicalLineFormatter for AddSpaceBeforeIdentifier {
fn format(&self, formatted_tokens: &mut FormattedTokens<'_>, input: &LogicalLine) {
for &token in input.get_tokens() {
if formatted_tokens.get_token_type_for_index(token) == Some(TokenType::Identifier) {
if let Some(formatting_data) = formatted_tokens.get_formatting_data_mut(token) {
formatting_data.spaces_before = 1;
}
if formatted_tokens.get_token_type_for_index(token) == Some(TokenType::Identifier)
&& let Some(formatting_data) = formatted_tokens.get_formatting_data_mut(token)
{
formatting_data.spaces_before = 1;
}
}
}
Expand Down Expand Up @@ -965,15 +965,13 @@ else
struct LogicalLinesOnNewLines;
impl LogicalLineFormatter for LogicalLinesOnNewLines {
fn format(&self, formatted_tokens: &mut FormattedTokens<'_>, input: &LogicalLine) {
if let Some(&first_token) = input.get_tokens().first() {
if first_token != 0 && first_token != formatted_tokens.len() - 1 {
if let Some(formatting_data) =
formatted_tokens.get_formatting_data_mut(first_token)
{
formatting_data.spaces_before = 0;
formatting_data.newlines_before = 1;
}
}
if let Some(&first_token) = input.get_tokens().first()
&& first_token != 0
&& first_token != formatted_tokens.len() - 1
&& let Some(formatting_data) = formatted_tokens.get_formatting_data_mut(first_token)
{
formatting_data.spaces_before = 0;
formatting_data.newlines_before = 1;
}
}
}
Expand Down Expand Up @@ -1158,11 +1156,11 @@ else
impl LogicalLineFormatter for RetainSpacesLogcialLinesOnNewLines {
fn format(&self, formatted_tokens: &mut FormattedTokens<'_>, input: &LogicalLine) {
let first_token = *input.get_tokens().first().unwrap();
if first_token != 0 && first_token != formatted_tokens.len() - 1 {
if let Some(formatting_data) = formatted_tokens.get_formatting_data_mut(first_token)
{
formatting_data.newlines_before = 1;
}
if first_token != 0
&& first_token != formatted_tokens.len() - 1
&& let Some(formatting_data) = formatted_tokens.get_formatting_data_mut(first_token)
{
formatting_data.newlines_before = 1;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/lang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ impl<'a> FormattedTokens<'a> {
pub fn tokens_mut(
&mut self,
) -> impl DoubleEndedIterator<Item = (Result<&mut Token<'a>, MutTokenErr>, &mut FormattingData)>
+ ExactSizeIterator {
+ ExactSizeIterator {
self.tokens
.iter_mut()
.zip(&mut self.fmt)
Expand Down
35 changes: 17 additions & 18 deletions core/src/rules/generics_consolidator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,28 +64,27 @@ impl TokenConsolidator for DistinguishGenericTypeParamsConsolidator {
),
) => {}
Some(TokenType::Op(OperatorKind::GreaterThan(_))) => {
if comma_found {
if let Some(
if comma_found
&& let Some(
TokenType::Identifier
| TokenType::Op(OperatorKind::AddressOf)
| TokenType::Keyword(KeywordKind::Not),
) = tokens.get(next_idx + 1).map(TokenData::get_token_type)
{
// cases where it cannot be generics
// Foo(X < Y, U > V)
// Foo(X < Y, U > @V)
// Foo(X < Y, U > not V)

// cases where it is ambiguous but we prefer to treat it as generics
// Foo(X < Y, U > +V)
// Foo(X < Y, U > -V)
// Foo(X < Y, U > (V))
// Foo(X < Y, U > [V])

// cases where it must be generics
// Foo(X < Y, U > /V) (or any other binary operator)
break;
}
{
// cases where it cannot be generics
// Foo(X < Y, U > V)
// Foo(X < Y, U > @V)
// Foo(X < Y, U > not V)

// cases where it is ambiguous but we prefer to treat it as generics
// Foo(X < Y, U > +V)
// Foo(X < Y, U > -V)
// Foo(X < Y, U > (V))
// Foo(X < Y, U > [V])

// cases where it must be generics
// Foo(X < Y, U > /V) (or any other binary operator)
break;
}

let closed_state = state.pop().unwrap();
Expand Down
5 changes: 2 additions & 3 deletions core/src/rules/ignore_asm_instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ mod tests {
fn format(&self, formatted_tokens: &mut FormattedTokens<'_>, _input: &[LogicalLine]) {
for token_index in 1..formatted_tokens.len() {
if let Some(formatting_data) = formatted_tokens.get_formatting_data_mut(token_index)
&& formatting_data.newlines_before == 0
{
if formatting_data.newlines_before == 0 {
formatting_data.spaces_before = 1;
}
formatting_data.spaces_before = 1;
}
}
}
Expand Down
24 changes: 12 additions & 12 deletions core/src/rules/optimising_line_formatter/contexts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ impl<'a> SpecificContextStack<'a> {
/// indices. Typically for modification.
pub(super) fn ctx_iter_indices(
&self,
) -> impl Iterator<Item = (usize, Ref<'a, FormattingContext>)> {
) -> impl Iterator<Item = (usize, Ref<'a, FormattingContext>)> + use<'a> {
self.stack
.into_iter()
.flat_map(|stack| stack.walk_parents())
Expand Down Expand Up @@ -374,11 +374,12 @@ impl<'a> SpecificContextStack<'a> {
filter: impl ContextFilter,
operation: impl Fn(Ref<'_, FormattingContext>, &mut FormattingContextState),
) -> bool {
if let Some((context, data)) = self.get_last_matching_context_mut(node, filter) {
operation(context, data);
true
} else {
false
match self.get_last_matching_context_mut(node, filter) {
Some((context, data)) => {
operation(context, data);
true
}
_ => false,
}
}
fn update_operator_precedences(&self, node: &mut FormattingNode, is_break: bool) {
Expand Down Expand Up @@ -732,12 +733,11 @@ impl<'a> SpecificContextStack<'a> {
data.is_child_broken = true;
data.break_anonymous_routine = Some(true);
});
} else if !child_solutions.is_empty() {
if let Some((_, context)) =
} else if !child_solutions.is_empty()
&& let Some((_, context)) =
self.get_last_matching_context_mut(node, CT::ControlFlowBegin)
{
context.can_break = false;
}
{
context.can_break = false;
}
}
}
Expand Down Expand Up @@ -1282,7 +1282,7 @@ impl<'builder> LineFormattingContextsBuilder<'builder> {
member_access_contexts: NodeRefSet::new(),
}
}
fn type_stack(&self) -> impl Iterator<Item = ContextType> + 'builder {
fn type_stack(&self) -> impl Iterator<Item = ContextType> + use<'builder> {
self.current_context
.walk_parents_data()
.map(|index| index.context_type)
Expand Down
Loading