Skip to content

Commit

Permalink
json repr
Browse files Browse the repository at this point in the history
  • Loading branch information
maxmindlin committed Aug 9, 2024
1 parent 116eb42 commit 9563519
Show file tree
Hide file tree
Showing 11 changed files with 452 additions and 50 deletions.
69 changes: 46 additions & 23 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = ["scout-interpreter", "scout-lexer", "scout-parser", "scout-worker"]
members = ["scout-interpreter", "scout-json", "scout-lexer", "scout-parser", "scout-worker"]

# Config for 'cargo dist'
[workspace.metadata.dist]
Expand Down
1 change: 1 addition & 0 deletions scout-interpreter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ keywords = [

[dependencies]
scout-parser = { version = "0.6.0", path = "../scout-parser/" }
scout-json = { version = "0.1", path = "../scout-json" }
fantoccini = "0.19.3"
futures = "0.3.30"
serde = { version = "1.0", features = ["derive"] }
Expand Down
12 changes: 12 additions & 0 deletions scout-interpreter/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ pub enum BuiltinKind {
SetCookies,
ToJson,
HttpRequest,
SetViewport,
}

impl BuiltinKind {
Expand Down Expand Up @@ -77,6 +78,7 @@ impl BuiltinKind {
"setCookies" => Some(SetCookies),
"toJson" => Some(ToJson),
"httpRequest" => Some(HttpRequest),
"setViewport" => Some(SetViewport),
_ => None,
}
}
Expand Down Expand Up @@ -149,6 +151,16 @@ impl BuiltinKind {
_ => Err(EvalError::InvalidFnParams),
}
}
SetViewport => {
assert_param_len!(args, 2);
match (&*args[0], &*args[1]) {
(Object::Number(w), Object::Number(h)) => {
crawler.set_window_size(*w as u32, *h as u32).await?;
Ok(Arc::new(Object::Null))
}
_ => Err(EvalError::InvalidFnParams),
}
}
ToJson => {
assert_param_len!(args, 1);
let json = args[0].to_json().await;
Expand Down
6 changes: 3 additions & 3 deletions scout-interpreter/src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ fn eval_statement<'a>(
StmtKind::Assign(lhs, expr) => {
let val = eval_expression(expr, crawler, env.clone(), results.clone()).await?;
match lhs {
ExprKind::Infix(lhs, TokenKind::LBracket, rhs) => {
ExprKind::Infix(lhs, t, rhs) if t.kind == TokenKind::LBracket => {
let r_obj =
eval_expression(rhs, crawler, env.clone(), results.clone()).await?;
let l_obj =
Expand Down Expand Up @@ -788,7 +788,7 @@ fn eval_expression<'a>(
let l_obj = eval_expression(lhs, crawler, env.clone(), results.clone()).await?;
let res = eval_infix(
l_obj.clone(),
op,
&op.kind,
rhs,
crawler,
env.clone(),
Expand All @@ -810,7 +810,7 @@ fn eval_expression<'a>(
}
ExprKind::Prefix(rhs, op) => {
let r_obj = eval_expression(rhs, crawler, env.clone(), results.clone()).await?;
let res = eval_prefix(r_obj, op).await?;
let res = eval_prefix(r_obj, &op.kind).await?;
Ok(res)
}
}
Expand Down
26 changes: 15 additions & 11 deletions scout-interpreter/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,22 @@ fn convert_path_buf(buf: PathBuf) -> Result<String, EvalError> {
fn resolve_module_file(module: &ExprKind) -> Result<PathBuf, EvalError> {
match module {
ExprKind::Ident(ident) => resolve_std_file(ident),
ExprKind::Infix(lhs, TokenKind::DbColon, rhs) => match (lhs.as_ref(), rhs.as_ref()) {
(ExprKind::Ident(base), ExprKind::Ident(file)) => {
let buf = resolve_std_file(base)?.join(&file.name);
Ok(buf)
ExprKind::Infix(lhs, t, rhs) if t.kind == TokenKind::DbColon => {
match (lhs.as_ref(), rhs.as_ref()) {
(ExprKind::Ident(base), ExprKind::Ident(file)) => {
let buf = resolve_std_file(base)?.join(&file.name);
Ok(buf)
}
(l @ ExprKind::Infix(_, t, _), ExprKind::Ident(file))
if t.kind == TokenKind::DbColon =>
{
let base = resolve_module_file(l)?;
let buf = base.join(&file.name);
Ok(buf)
}
_ => Err(EvalError::InvalidImport(ImportError::UnknownModule)),
}
(l @ ExprKind::Infix(_, TokenKind::DbColon, _), ExprKind::Ident(file)) => {
let base = resolve_module_file(l)?;
let buf = base.join(&file.name);
Ok(buf)
}
_ => Err(EvalError::InvalidImport(ImportError::UnknownModule)),
},
}
_ => Err(EvalError::InvalidImport(ImportError::UnknownModule)),
}
}
16 changes: 16 additions & 0 deletions scout-interpreter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::{
use env::EnvPointer;
use eval::{eval, EvalError, ScrapeResultsPtr};
use object::Object;
use scout_json::ScoutJSON;
use scout_lexer::Lexer;
use scout_parser::{ast::NodeKind, ParseError, Parser};
use serde::Deserialize;
Expand Down Expand Up @@ -47,6 +48,7 @@ impl EnvVars {
pub enum InterpreterError {
EvalError(EvalError),
ParserError(ParseError),
InvalidJson,
}

pub struct GeckDriverProc(Child);
Expand Down Expand Up @@ -88,6 +90,7 @@ impl Interpreter {
_geckodriver_proc: geckodriver_proc,
}
}

pub async fn eval(&self, content: &str) -> Result<Arc<Object>, InterpreterError> {
let lexer = Lexer::new(content);
let mut parser = Parser::new(lexer);
Expand All @@ -103,6 +106,19 @@ impl Interpreter {
}
}

pub async fn eval_json(&self, content: &str) -> Result<Arc<Object>, InterpreterError> {
let ast = serde_json::from_str::<ScoutJSON>(content)
.map_err(|_| InterpreterError::InvalidJson)?
.to_ast();
Ok(eval(
NodeKind::Program(ast),
&self.crawler,
self.env.clone(),
self.results.clone(),
)
.await?)
}

pub fn results(&self) -> ScrapeResultsPtr {
self.results.clone()
}
Expand Down
12 changes: 12 additions & 0 deletions scout-json/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "scout-json"
version = "0.1.0"
edition = "2021"

[dependencies]
scout-parser = { version = "0.6.0", path = "../scout-parser/" }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

[dev-dependencies]
test-case = "3.3.1"
Loading

0 comments on commit 9563519

Please sign in to comment.