Skip to content

Commit

Permalink
fix: Styling fix
Browse files Browse the repository at this point in the history
  • Loading branch information
dhedey committed Jan 9, 2025
1 parent 9c48d1b commit 0eb014f
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 40 deletions.
12 changes: 7 additions & 5 deletions src/commands/concat_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ fn concat_into_literal(
Ok(InterpretedStream::of_literal(literal))
}


fn concat_recursive(arguments: InterpretedStream) -> String {
fn concat_recursive_internal(output: &mut String, arguments: TokenStream) {
for token_tree in arguments {
Expand Down Expand Up @@ -118,9 +117,9 @@ macro_rules! define_concat_command {

impl CommandDefinition for $command {
const COMMAND_NAME: &'static str = $command_name;

const OUTPUT_BEHAVIOUR: CommandOutputBehaviour = CommandOutputBehaviour::SingleToken;

fn parse(mut arguments: InterpreterParseStream) -> Result<Self> {
Ok(Self {
arguments: arguments.parse_all_for_interpretation()?,
Expand All @@ -129,11 +128,14 @@ macro_rules! define_concat_command {
}

impl CommandInvocation for $command {
fn execute(self: Box<Self>, interpreter: &mut Interpreter) -> Result<InterpretedStream> {
fn execute(
self: Box<Self>,
interpreter: &mut Interpreter,
) -> Result<InterpretedStream> {
$output_fn(self.arguments, interpreter, $conversion_fn)
}
}
}
};
}

//=======================================
Expand Down
21 changes: 16 additions & 5 deletions src/commands/control_flow_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,18 @@ impl CommandDefinition for IfCommand {
static ERROR: &str = "Expected [!if! (condition) { true_code }] or [!if! (condition) { true_code } !else! { false_code}]";

let condition = arguments.next_item(ERROR)?;
let true_code = arguments.next_as_kinded_group(Delimiter::Brace, ERROR)?.into_inner_stream();
let true_code = arguments
.next_as_kinded_group(Delimiter::Brace, ERROR)?
.into_inner_stream();
let false_code = if !arguments.is_empty() {
arguments.next_as_punct_matching('!', ERROR)?;
arguments.next_as_ident_matching("else", ERROR)?;
arguments.next_as_punct_matching('!', ERROR)?;
Some(arguments.next_as_kinded_group(Delimiter::Brace, ERROR)?.into_inner_stream())
Some(
arguments
.next_as_kinded_group(Delimiter::Brace, ERROR)?
.into_inner_stream(),
)
} else {
None
};
Expand Down Expand Up @@ -69,7 +75,9 @@ impl CommandDefinition for WhileCommand {
static ERROR: &str = "Expected [!while! (condition) { code }]";

let condition = arguments.next_item(ERROR)?;
let loop_code = arguments.next_as_kinded_group(Delimiter::Brace, ERROR)?.into_inner_stream();
let loop_code = arguments
.next_as_kinded_group(Delimiter::Brace, ERROR)?
.into_inner_stream();
arguments.assert_end(ERROR)?;

Ok(Self {
Expand All @@ -84,7 +92,8 @@ impl CommandInvocation for WhileCommand {
let mut output = InterpretedStream::new();
let mut iteration_count = 0;
loop {
let evaluated_condition = self.condition
let evaluated_condition = self
.condition
.clone()
.interpret_as_expression(interpreter)?
.evaluate()?
Expand All @@ -99,7 +108,9 @@ impl CommandInvocation for WhileCommand {
interpreter
.config()
.check_iteration_count(&self.condition, iteration_count)?;
self.loop_code.clone().interpret_as_tokens_into(interpreter, &mut output)?;
self.loop_code
.clone()
.interpret_as_tokens_into(interpreter, &mut output)?;
}

Ok(output)
Expand Down
2 changes: 1 addition & 1 deletion src/expressions/expression_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::*;
///
/// Ideally we'd parse expressions at parse time, but that requires writing a custom parser for
/// a subset of the rust expression tree...
///
///
/// Instead, to be lazy for now, we interpret the stream at intepretation time to substitute
/// in variables and commands, and then parse the resulting expression with syn.
#[derive(Clone)]
Expand Down
10 changes: 6 additions & 4 deletions src/interpretation/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl Command {
let ident = tokens.next_as_ident("").ok()?;
Some((ident, tokens))
}

fn extract_command_data(
command_ident: &Ident,
parse_stream: &mut InterpreterParseStream,
Expand All @@ -115,14 +115,14 @@ impl Command {
parse_stream.next_as_punct_matching('!', "").ok()?;
Some(command_kind)
}

// Attempt to match `[!ident`, if that doesn't match, we assume it's not a command invocation,
// so return `Ok(None)`
let (command_ident, mut parse_stream) = match matches_command_start(group) {
Some(command_start) => command_start,
None => return Ok(None),
};

// We have now checked enough that we're confident the user is pretty intentionally using
// the call convention. Any issues we hit from this point will be a helpful compiler error.
match extract_command_data(&command_ident, &mut parse_stream) {
Expand Down Expand Up @@ -154,7 +154,9 @@ impl Command {
CommandOutputBehaviour::GroupedStream => {
output.push_new_group(substitution, Delimiter::None, self.source_group_span_range);
}
CommandOutputBehaviour::EmptyStream | CommandOutputBehaviour::SingleToken | CommandOutputBehaviour::AppendStream => {
CommandOutputBehaviour::EmptyStream
| CommandOutputBehaviour::SingleToken
| CommandOutputBehaviour::AppendStream => {
output.extend(substitution);
}
}
Expand Down
28 changes: 17 additions & 11 deletions src/interpretation/interpretation_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,26 @@ pub(crate) struct InterpretationStream {
}

impl InterpretationStream {
pub(crate) fn parse_from_token_stream(token_stream: TokenStream, span_range: SpanRange) -> Result<Self> {
pub(crate) fn parse_from_token_stream(
token_stream: TokenStream,
span_range: SpanRange,
) -> Result<Self> {
InterpreterParseStream::new(token_stream, span_range).parse_all_for_interpretation()
}

pub(crate) fn parse(parse_stream: &mut InterpreterParseStream, span_range: SpanRange) -> Result<Self> {
pub(crate) fn parse(
parse_stream: &mut InterpreterParseStream,
span_range: SpanRange,
) -> Result<Self> {
let mut items = Vec::new();
while let Some(next_item) = NextItem::parse(parse_stream)? {
items.push(next_item);
}
Ok(Self {
items,
span_range,
})
Ok(Self { items, span_range })
}
}

impl<'a> Interpret for InterpretationStream {
impl Interpret for InterpretationStream {
fn interpret_as_tokens_into(
self,
interpreter: &mut Interpreter,
Expand Down Expand Up @@ -69,8 +72,9 @@ pub(crate) struct InterpretationGroup {

impl InterpretationGroup {
pub(super) fn parse(source_group: Group) -> Result<Self> {
let interpretation_stream = InterpreterParseStream::new(source_group.stream(), source_group.span_range())
.parse_all_for_interpretation()?;
let interpretation_stream =
InterpreterParseStream::new(source_group.stream(), source_group.span_range())
.parse_all_for_interpretation()?;
Ok(Self {
source_group,
interpretation_stream,
Expand All @@ -93,7 +97,8 @@ impl Interpret for InterpretationGroup {
output: &mut InterpretedStream,
) -> Result<()> {
output.push_new_group(
self.interpretation_stream.interpret_as_tokens(interpreter)?,
self.interpretation_stream
.interpret_as_tokens(interpreter)?,
self.source_group.delimiter(),
self.source_group.span_range(),
);
Expand All @@ -106,7 +111,8 @@ impl Interpret for InterpretationGroup {
expression_stream: &mut ExpressionStream,
) -> Result<()> {
expression_stream.push_expression_group(
self.interpretation_stream.interpret_as_expression(interpreter)?,
self.interpretation_stream
.interpret_as_expression(interpreter)?,
self.source_group.delimiter(),
self.source_group.span_range(),
);
Expand Down
6 changes: 5 additions & 1 deletion src/interpretation/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ impl Default for InterpreterConfig {
}

impl InterpreterConfig {
pub(crate) fn check_iteration_count(&self, span_source: &impl HasSpanRange, count: usize) -> Result<()> {
pub(crate) fn check_iteration_count(
&self,
span_source: &impl HasSpanRange,
count: usize,
) -> Result<()> {
if let Some(limit) = self.iteration_limit {
if count > limit {
return span_source.err(format!("Iteration limit of {} exceeded", limit));
Expand Down
31 changes: 21 additions & 10 deletions src/interpretation/interpreter_parse_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,17 @@ impl InterpreterParseStream {
Some(next_item) => {
self.latest_item_span_range = next_item.span_range();
Some(next_item)
},
}
None => None,
})
}

pub(crate) fn next_item(&mut self, error_message: &'static str) -> Result<NextItem> {
match self.next_item_or_end()? {
Some(item) => Ok(item),
None => self.latest_item_span_range.err(format!("Unexpected end: {error_message}")),
None => self
.latest_item_span_range
.err(format!("Unexpected end: {error_message}")),
}
}

Expand All @@ -86,9 +88,13 @@ impl InterpreterParseStream {
}
}

pub(crate) fn next_as_ident_matching(&mut self, ident_name: &str, error_message: &'static str) -> Result<Ident> {
pub(crate) fn next_as_ident_matching(
&mut self,
ident_name: &str,
error_message: &'static str,
) -> Result<Ident> {
match self.next_item(error_message)? {
NextItem::Ident(ident) if &ident.to_string() == ident_name => Ok(ident),
NextItem::Ident(ident) if ident.to_string() == ident_name => Ok(ident),
other => other.err(error_message),
}
}
Expand All @@ -100,24 +106,29 @@ impl InterpreterParseStream {
}
}

pub(crate) fn next_as_punct_matching(&mut self, char: char, error_message: &'static str) -> Result<Punct> {
pub(crate) fn next_as_punct_matching(
&mut self,
char: char,
error_message: &'static str,
) -> Result<Punct> {
match self.next_item(error_message)? {
NextItem::Punct(punct) if punct.as_char() == char => Ok(punct),
other => other.err(error_message),
}
}

pub(crate) fn next_as_kinded_group(&mut self, delimiter: Delimiter, error_message: &'static str) -> Result<InterpretationGroup> {
pub(crate) fn next_as_kinded_group(
&mut self,
delimiter: Delimiter,
error_message: &'static str,
) -> Result<InterpretationGroup> {
match self.next_item(error_message)? {
NextItem::Group(group) if group.delimiter() == delimiter => Ok(group),
other => other.err(error_message),
}
}

pub(crate) fn next_as_variable(
&mut self,
error_message: &'static str,
) -> Result<Variable> {
pub(crate) fn next_as_variable(&mut self, error_message: &'static str) -> Result<Variable> {
match self.next_item(error_message)? {
NextItem::Variable(variable_substitution) => Ok(variable_substitution),
other => other.err(error_message),
Expand Down
2 changes: 1 addition & 1 deletion src/interpretation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ mod next_item;
mod variable;

pub(crate) use command::*;
pub(crate) use interpretation_stream::*;
pub(crate) use interpreted_stream::*;
pub(crate) use interpreter::*;
pub(crate) use interpretation_stream::*;
pub(crate) use interpreter_parse_stream::*;
pub(crate) use next_item::*;
pub(crate) use variable::*;
3 changes: 2 additions & 1 deletion src/interpretation/next_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ impl NextItem {
}
}
TokenTree::Punct(punct) => {
if let Some(variable) = Variable::parse_consuming_only_if_match(&punct, parse_stream)
if let Some(variable) =
Variable::parse_consuming_only_if_match(&punct, parse_stream)
{
NextItem::Variable(variable)
} else {
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,8 @@ pub fn preinterpret(token_stream: proc_macro::TokenStream) -> proc_macro::TokenS

fn preinterpret_internal(input: TokenStream) -> Result<TokenStream> {
let mut interpreter = Interpreter::new();
let interpretation_stream = InterpretationStream::parse_from_token_stream(input, Span::call_site().span_range())?;
let interpretation_stream =
InterpretationStream::parse_from_token_stream(input, Span::call_site().span_range())?;
let interpreted_stream = interpretation_stream.interpret_as_tokens(&mut interpreter)?;
Ok(interpreted_stream.into_token_stream())
}
Expand Down

0 comments on commit 0eb014f

Please sign in to comment.