Skip to content

Commit

Permalink
clean up more for repl
Browse files Browse the repository at this point in the history
  • Loading branch information
suaviloquence committed Aug 19, 2024
1 parent ef74020 commit 6922488
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 28 deletions.
15 changes: 14 additions & 1 deletion filter-types/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,20 @@ impl<'a, X> Bindings<'a, X> {
)
}

/// Conversts these [`Bindings`] into a [`Value`]. Specifically, it makes
/// Converts a [`Bindings<Data>`] into a [`Bindings<X>`]. This is a lossless operation
/// because a [`Value<Data>`] can always be converted into a [`Value<X>`].
#[must_use]
pub fn from_data(bindings: Bindings<'a, Data>) -> Self {
Self(
bindings
.0
.into_iter()
.map(|(k, v)| (k, Value::from_data(v)))
.collect(),
)
}

/// Converts these [`Bindings`] into a [`Value`]. Specifically, it makes
/// them a key-value `Structure` by taking ownership of the keys and mapping
/// them to the mapped values.
#[must_use]
Expand Down
1 change: 0 additions & 1 deletion src/frontend/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
pub mod ast;
mod parser;
mod repl;
mod scanner;

pub use parser::{ParseError, Parser};
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl<'a> Parser<'a> {
Ok(vec)
}

fn parse_statement(&mut self) -> Result<Statement<'a>> {
pub(crate) fn parse_statement(&mut self) -> Result<Statement<'a>> {
let id = self.try_eat(Token::Id)?.value;
self.try_eat(Token::Colon)?;
let value = self.parse_rvalue()?;
Expand Down
2 changes: 0 additions & 2 deletions src/frontend/repl.rs

This file was deleted.

44 changes: 23 additions & 21 deletions src/interpreter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,15 @@ pub mod filter;
pub use scrapelect_filter_types::{Error, MessageExt, Result, WrapExt};

#[derive(Debug)]
pub struct Interpreter<'ast> {
pub struct Interpreter {
client: reqwest::Client,
ast: &'ast [Statement<'ast>],
}

impl<'ast> Interpreter<'ast> {
impl Interpreter {
#[must_use]
#[inline]
pub fn new(ast: &'ast [Statement<'ast>]) -> Self {
pub fn new() -> Self {

Check failure on line 24 in src/interpreter/mod.rs

View workflow job for this annotation

GitHub Actions / verify formatting and lints

you should consider adding a `Default` implementation for `Interpreter`
Self::with_client(
ast,
reqwest::Client::builder()
.user_agent(concat!(
env!("CARGO_PKG_NAME"),
Expand All @@ -38,14 +36,18 @@ impl<'ast> Interpreter<'ast> {

#[must_use]
#[inline]
pub const fn with_client(ast: &'ast [Statement<'ast>], client: reqwest::Client) -> Self {
Self { ast, client }
pub const fn with_client(client: reqwest::Client) -> Self {
Self { client }
}

#[inline]
pub async fn interpret(&self, root_url: Url) -> Result<Bindings<'ast>> {
pub async fn interpret<'ast>(
&self,
statements: &[Statement<'ast>],
root_url: Url,
) -> Result<Bindings<'ast>> {
let html = self.get_html(&root_url).await?;
self.interpret_block(html.root_element(), self.ast, None, root_url)
self.interpret_block(html.root_element(), statements, None, root_url)
.await
}

Expand All @@ -69,10 +71,10 @@ impl<'ast> Interpreter<'ast> {
Ok(scraper::Html::parse_document(&text))
}

async fn interpret_block(
async fn interpret_block<'ast>(
&self,
element: scraper::ElementRef<'_>,
statements: &'ast [Statement<'ast>],
statements: &[Statement<'ast>],
parent: Option<&ElementContext<'ast, '_>>,
url: Url,
) -> Result<Bindings<'ast>> {
Expand All @@ -85,9 +87,9 @@ impl<'ast> Interpreter<'ast> {
Ok(ctx.bindings.into_data())
}

async fn interpret_statement(
async fn interpret_statement<'ast>(
&self,
statement: &'ast Statement<'ast>,
statement: &Statement<'ast>,
ctx: &mut ElementContext<'ast, '_>,
) -> Result<()> {
let inner = || async move {
Expand All @@ -110,9 +112,9 @@ impl<'ast> Interpreter<'ast> {
})
}

async fn interpret_element(
async fn interpret_element<'ast>(
&self,
element: &'ast Element<'ast>,
element: &Element<'ast>,
ctx: &mut ElementContext<'ast, '_>,
) -> Result<Value> {
let selector_str = &element.selector.to_string();
Expand Down Expand Up @@ -165,10 +167,10 @@ impl<'ast> Interpreter<'ast> {
.wrap_with(|| format!("note: occurred while evaluating element block `{selector_str}`"))
}

fn apply_filters<'ctx>(
fn apply_filters<'a, 'ast: 'a, 'ctx>(
&self,
value: EValue<'ctx>,
mut filters: impl Iterator<Item = &'ast ast::Filter<'ast>>,
mut filters: impl Iterator<Item = &'a ast::Filter<'ast>>,
ctx: &mut ElementContext<'ast, 'ctx>,
) -> Result<EValue<'ctx>> {
filters
Expand Down Expand Up @@ -208,9 +210,9 @@ impl<'ast> Interpreter<'ast> {
.map(EValue::from)
}

fn eval_inline<'ctx>(
fn eval_inline<'ast, 'ctx>(
&self,
inline: &'ast Inline<'ast>,
inline: &Inline<'ast>,
ctx: &mut ElementContext<'ast, 'ctx>,
) -> Result<EValue<'ctx>> {
self.apply_filters(
Expand Down Expand Up @@ -263,7 +265,7 @@ pub async fn interpret_string_harness(
let statements = crate::frontend::Parser::new(program).parse()?;
let html = scraper::Html::parse_document(html);
let statements = Box::leak(Box::new(statements));
let interpreter = Interpreter::new(statements);
let interpreter = Interpreter::new();
interpreter
// TODO: url hack
.interpret_block(
Expand All @@ -290,7 +292,7 @@ mod tests {

let html = scraper::Html::parse_document(&input);

let result = super::Interpreter::new(&ast)
let result = super::Interpreter::new()
.interpret_block(
html.root_element(),
&ast,
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ async fn main() -> anyhow::Result<()> {
.parse()
.with_context(|| format!("parse error in {}:", run_args.file.display()))?;

let interpreter = Interpreter::new(&ast);
let interpreter = Interpreter::new();

let results = interpreter.interpret(run_args.url).await?;
let results = interpreter.interpret(&ast, run_args.url).await?;

println!("{}", serde_json::to_string_pretty(&results)?);
}
Expand Down

0 comments on commit 6922488

Please sign in to comment.