-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f042cb0
commit 120c14c
Showing
8 changed files
with
238 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use std::path::{Path, PathBuf}; | ||
|
||
use scout_lexer::TokenKind; | ||
use scout_parser::ast::{ExprKind, Identifier}; | ||
|
||
use crate::EvalError; | ||
|
||
#[derive(Debug)] | ||
pub struct ResolvedMod { | ||
pub ident: Identifier, | ||
pub filepath: String, | ||
} | ||
|
||
pub fn resolve_module(module: &ExprKind) -> Result<ResolvedMod, EvalError> { | ||
let ident = match module { | ||
ExprKind::Ident(ident) => Ok(ident.clone()), | ||
ExprKind::Infix(_, _, rhs) => match rhs.as_ref() { | ||
ExprKind::Ident(ident) => Ok(ident.clone()), | ||
_ => Err(EvalError::InvalidImport), | ||
}, | ||
_ => Err(EvalError::InvalidImport), | ||
}?; | ||
let buf = resolve_module_file(module)?; | ||
let filepath = convert_path_buf(buf)?; | ||
Ok(ResolvedMod { filepath, ident }) | ||
} | ||
|
||
fn resolve_std_file(ident: &Identifier) -> Result<PathBuf, EvalError> { | ||
if *ident == Identifier::new("std".into()) { | ||
let path = Path::new("scout-lib").to_owned(); | ||
Ok(path) | ||
} else { | ||
Ok(Path::new(&ident.name).to_owned()) | ||
} | ||
} | ||
|
||
fn convert_path_buf(buf: PathBuf) -> Result<String, EvalError> { | ||
let res = buf.to_str().ok_or(EvalError::InvalidImport)?.to_owned(); | ||
Ok(res) | ||
} | ||
|
||
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) | ||
} | ||
(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), | ||
}, | ||
_ => Err(EvalError::InvalidImport), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// links returns a list of nodes | ||
// that are anchor elements. | ||
// | ||
// Optionally provide a scope to limit | ||
// the search space. | ||
def links(scope = null) do | ||
if scope do | ||
return $$(scope)"a[href]" |> href() | ||
end | ||
|
||
$$"a[href]" |> href() | ||
end |
Oops, something went wrong.