Skip to content

Commit 054d667

Browse files
authored
Analyze and Compiler tweaks (#1534)
1 parent a81b094 commit 054d667

7 files changed

+53
-50
lines changed

src/analyzer.rs

+4-41
Original file line numberDiff line numberDiff line change
@@ -41,48 +41,11 @@ impl<'src> Analyzer<'src> {
4141
}
4242
}
4343

44-
let mut settings = Settings::default();
45-
46-
for (_, set) in self.sets {
47-
match set.value {
48-
Setting::AllowDuplicateRecipes(allow_duplicate_recipes) => {
49-
settings.allow_duplicate_recipes = allow_duplicate_recipes;
50-
}
51-
Setting::DotenvLoad(dotenv_load) => {
52-
settings.dotenv_load = Some(dotenv_load);
53-
}
54-
Setting::Export(export) => {
55-
settings.export = export;
56-
}
57-
Setting::Fallback(fallback) => {
58-
settings.fallback = fallback;
59-
}
60-
Setting::IgnoreComments(ignore_comments) => {
61-
settings.ignore_comments = ignore_comments;
62-
}
63-
Setting::PositionalArguments(positional_arguments) => {
64-
settings.positional_arguments = positional_arguments;
65-
}
66-
Setting::Shell(shell) => {
67-
settings.shell = Some(shell);
68-
}
69-
Setting::WindowsPowerShell(windows_powershell) => {
70-
settings.windows_powershell = windows_powershell;
71-
}
72-
Setting::WindowsShell(windows_shell) => {
73-
settings.windows_shell = Some(windows_shell);
74-
}
75-
Setting::Tempdir(tempdir) => {
76-
settings.tempdir = Some(tempdir);
77-
}
78-
}
79-
}
80-
81-
let assignments = self.assignments;
44+
let settings = Settings::from_setting_iter(self.sets.into_iter().map(|(_, set)| set.value));
8245

8346
let mut recipe_table: Table<'src, UnresolvedRecipe<'src>> = Default::default();
8447

85-
AssignmentResolver::resolve_assignments(&assignments)?;
48+
AssignmentResolver::resolve_assignments(&self.assignments)?;
8649

8750
for recipe in recipes {
8851
if let Some(original) = recipe_table.get(recipe.name.lexeme()) {
@@ -96,7 +59,7 @@ impl<'src> Analyzer<'src> {
9659
recipe_table.insert(recipe.clone());
9760
}
9861

99-
let recipes = RecipeResolver::resolve_recipes(recipe_table, &assignments)?;
62+
let recipes = RecipeResolver::resolve_recipes(recipe_table, &self.assignments)?;
10063

10164
let mut aliases = Table::new();
10265
while let Some(alias) = self.aliases.pop() {
@@ -116,7 +79,7 @@ impl<'src> Analyzer<'src> {
11679
}),
11780
}),
11881
aliases,
119-
assignments,
82+
assignments: self.assignments,
12083
recipes,
12184
settings,
12285
})

src/compiler.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ use super::*;
33
pub(crate) struct Compiler;
44

55
impl Compiler {
6-
pub(crate) fn compile(src: &str) -> CompileResult<Justfile> {
6+
pub(crate) fn compile(src: &str) -> CompileResult<(Ast, Justfile)> {
77
let tokens = Lexer::lex(src)?;
8-
98
let ast = Parser::parse(&tokens)?;
9+
let justfile = Analyzer::analyze(&ast)?;
1010

11-
Analyzer::analyze(&ast)
11+
Ok((ast, justfile))
1212
}
1313
}

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub(crate) use {
1818
alias::Alias, analyzer::Analyzer, assignment::Assignment,
1919
assignment_resolver::AssignmentResolver, ast::Ast, attribute::Attribute, binding::Binding,
2020
color::Color, color_display::ColorDisplay, command_ext::CommandExt,
21-
compile_error::CompileError, compile_error_kind::CompileErrorKind,
21+
compile_error::CompileError, compile_error_kind::CompileErrorKind, compiler::Compiler,
2222
conditional_operator::ConditionalOperator, config::Config, config_error::ConfigError,
2323
count::Count, delimiter::Delimiter, dependency::Dependency, dump_format::DumpFormat,
2424
enclosure::Enclosure, error::Error, evaluator::Evaluator, expression::Expression,

src/settings.rs

+41
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,47 @@ pub(crate) struct Settings<'src> {
2020
}
2121

2222
impl<'src> Settings<'src> {
23+
pub(crate) fn from_setting_iter(iter: impl Iterator<Item = Setting<'src>>) -> Self {
24+
let mut settings = Self::default();
25+
26+
for set in iter {
27+
match set {
28+
Setting::AllowDuplicateRecipes(allow_duplicate_recipes) => {
29+
settings.allow_duplicate_recipes = allow_duplicate_recipes;
30+
}
31+
Setting::DotenvLoad(dotenv_load) => {
32+
settings.dotenv_load = Some(dotenv_load);
33+
}
34+
Setting::Export(export) => {
35+
settings.export = export;
36+
}
37+
Setting::Fallback(fallback) => {
38+
settings.fallback = fallback;
39+
}
40+
Setting::IgnoreComments(ignore_comments) => {
41+
settings.ignore_comments = ignore_comments;
42+
}
43+
Setting::PositionalArguments(positional_arguments) => {
44+
settings.positional_arguments = positional_arguments;
45+
}
46+
Setting::Shell(shell) => {
47+
settings.shell = Some(shell);
48+
}
49+
Setting::WindowsPowerShell(windows_powershell) => {
50+
settings.windows_powershell = windows_powershell;
51+
}
52+
Setting::WindowsShell(windows_shell) => {
53+
settings.windows_shell = Some(windows_shell);
54+
}
55+
Setting::Tempdir(tempdir) => {
56+
settings.tempdir = Some(tempdir);
57+
}
58+
}
59+
}
60+
61+
settings
62+
}
63+
2364
pub(crate) fn shell_command(&self, config: &Config) -> Command {
2465
let (command, args) = self.shell(config);
2566

src/subcommand.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,7 @@ impl Subcommand {
179179
) -> Result<(&'src str, Ast<'src>, Justfile<'src>), Error<'src>> {
180180
let src = loader.load(&search.justfile)?;
181181

182-
let tokens = Lexer::lex(src)?;
183-
let ast = Parser::parse(&tokens)?;
184-
let justfile = Analyzer::analyze(&ast)?;
182+
let (ast, justfile) = Compiler::compile(src)?;
185183

186184
if config.verbosity.loud() {
187185
for warning in &justfile.warnings {

src/summary.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub fn summary(path: &Path) -> Result<Result<Summary, String>, io::Error> {
2929
let text = fs::read_to_string(path)?;
3030

3131
match Compiler::compile(&text) {
32-
Ok(justfile) => Ok(Ok(Summary::new(justfile))),
32+
Ok((_, justfile)) => Ok(Ok(Summary::new(justfile))),
3333
Err(compilation_error) => Ok(Err(compilation_error.to_string())),
3434
}
3535
}

src/testing.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use {super::*, crate::compiler::Compiler, pretty_assertions::assert_eq};
22

33
pub(crate) fn compile(text: &str) -> Justfile {
44
match Compiler::compile(text) {
5-
Ok(justfile) => justfile,
5+
Ok((_, justfile)) => justfile,
66
Err(error) => panic!("Expected successful compilation but got error:\n {error}"),
77
}
88
}
@@ -99,6 +99,7 @@ macro_rules! run_error {
9999
if let Subcommand::Run{ overrides, arguments } = &config.subcommand {
100100
match $crate::compiler::Compiler::compile(&$crate::unindent::unindent($src))
101101
.expect("Expected successful compilation")
102+
.1
102103
.run(
103104
&config,
104105
&search,

0 commit comments

Comments
 (0)