Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!: drop DEFAULT_FILE_NAME #288

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions crates/cli/src/result_formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use marzano_core::api::{
AllDone, AnalysisLog, CreateFile, DoneFile, FileMatchResult, InputFile, Match, MatchResult,
PatternInfo, RemoveFile, Rewrite,
};
use marzano_core::constants::DEFAULT_FILE_NAME;
use marzano_messenger::output_mode::OutputMode;
use std::fmt::Display;
use std::fs::read_to_string;
Expand Down Expand Up @@ -76,7 +75,7 @@ fn print_all_done(item: &AllDone, f: &mut fmt::Formatter<'_>) -> fmt::Result {
}

fn print_error_log(log: &AnalysisLog, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let file_prefix = if log.file.is_empty() || log.file == DEFAULT_FILE_NAME {
let file_prefix = if log.file.is_empty() {
"".to_owned()
} else {
format!("{}: ", log.file)
Expand All @@ -87,8 +86,12 @@ fn print_error_log(log: &AnalysisLog, f: &mut fmt::Formatter<'_>) -> fmt::Result
} else if log.level == 310 {
style(log.message.to_string()).dim().fmt(f)
} else if log.level == 441 {
let title = format!("Log in {}", log.file).bold();
writeln!(f, "{}: {}", title, log.message)?;
if log.file.is_empty() {
writeln!(f, "{}", log.message)?;
} else {
let title = format!("Log in {}", log.file).bold();
writeln!(f, "{}: {}", title, log.message)?;
}
let empty_string = String::new();
let range = match log.range.as_ref() {
Some(range) => indent(
Expand Down Expand Up @@ -120,7 +123,7 @@ fn print_error_log(log: &AnalysisLog, f: &mut fmt::Formatter<'_>) -> fmt::Result
/// Implement some log overrides to make CLI usage more friendly
fn humanize_log(log: &mut AnalysisLog, input_pattern: &str) {
if log.level == 299
&& (log.file.is_empty() || log.file == DEFAULT_FILE_NAME)
&& log.file.is_empty()
&& (input_pattern.ends_with(".yaml") || input_pattern.ends_with(".yml"))
{
log.message = format!(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
source: apps/marzano/cli/tests/apply.rs
source: crates/cli_bin/tests/apply.rs
expression: "String::from_utf8(output.stdout)?"
---
Log in PlaygroundPattern: Warning: sequential matches at the top of the file. If a pattern matched outside of a sequential, but no longer matches, it is likely because naked patterns are automatically wrapped with `contains bubble <pattern>`
Warning: sequential matches at the top of the file. If a pattern matched outside of a sequential, but no longer matches, it is likely because naked patterns are automatically wrapped with `contains bubble <pattern>`
- Range: 47:5 - 47:20
- Source:
engine marzano(0.1)
Expand Down Expand Up @@ -91,4 +91,3 @@ Log in PlaygroundPattern: Warning: sequential matches at the top of the file. If


Processed 4 files and found 4 matches

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ source: crates/cli_bin/tests/plumbing.rs
expression: "String::from_utf8(result.stderr)?"
---
Error: Failed to compile pattern `console.lo: engine_id: Some("marzano(0.1)")
file_name: Some("PlaygroundPattern")
file_name: None
level: Some(299)
message: "Pattern syntax error at 1:1. If you hit this error while running grit apply on a pattern from the Grit standard library, try running grit init. If you are running a custom pattern, check out the docs at https://docs.grit.io/ for help with writing patterns."
position: Some(Position { line: 1, column: 1 })
5 changes: 1 addition & 4 deletions crates/core/src/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,7 @@ mod tests {
"#
.to_string();
let mut libs = BTreeMap::new();
libs.insert(
"async_foo.grit".to_string(),
"llm_chat(messages=[])".to_string(),
);
libs.insert("async_foo.grit".into(), "llm_chat(messages=[])".to_string());
let mut parser = MarzanoGritParser::new().unwrap();
let parsed = parser
.parse_file(&src_code, Some(Path::new("test.grit")))
Expand Down
14 changes: 11 additions & 3 deletions crates/core/src/built_in_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ use im::Vector;
use itertools::Itertools;
use rand::prelude::SliceRandom;
use rand::Rng;
use std::collections::BTreeMap;
use std::{
collections::BTreeMap,
path::{Path, PathBuf},
};

// todo we can probably use a macro to generate a function that takes a vec and
// and calls the input function with the vec args unpacked.
Expand Down Expand Up @@ -178,9 +181,14 @@ fn resolve_path_fn<'a>(
None => return Err(anyhow!("No path argument provided for resolve function")),
};

let resolved_path = resolve(target_path, current_file.into())?;
let resolved_path = match &current_file {
Some(current_file) => resolve(Path::new(target_path.as_ref()), current_file)?,
None => PathBuf::from(target_path.as_ref()),
};

Ok(ResolvedPattern::from_string(resolved_path))
Ok(ResolvedPattern::from_string(
resolved_path.to_string_lossy().to_string(),
))
}

fn capitalize(s: &str) -> String {
Expand Down
2 changes: 0 additions & 2 deletions crates/core/src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
pub use grit_pattern_matcher::constants::DEFAULT_FILE_NAME;

pub const MAX_FILE_SIZE: usize = 1_000_000;
20 changes: 5 additions & 15 deletions crates/core/src/paths.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use anyhow::anyhow;
use anyhow::Result;
use path_absolutize::Absolutize;
use std::borrow::Cow;
use std::path::{Path, PathBuf};

#[cfg(feature = "absolute_filename")]
Expand All @@ -16,23 +15,14 @@ pub(crate) fn absolutize(path: &Path) -> Result<PathBuf> {
Ok(path.to_owned())
}

pub(crate) fn resolve<'a>(target_path: Cow<'a, str>, from_file: Cow<'a, str>) -> Result<String> {
let source_path = Path::new(from_file.as_ref()).parent().ok_or_else(|| {
pub(crate) fn resolve(target_path: &Path, from_file: &Path) -> Result<PathBuf> {
let source_path = from_file.parent().ok_or_else(|| {
anyhow!(
"could not get parent directory of file name {}",
from_file.as_ref()
from_file.display()
)
})?;
let our_path = Path::new(target_path.as_ref());
let absolutized = our_path.absolutize_from(source_path)?;
let absolutized = target_path.absolutize_from(source_path)?;
// path.push(target_path);
Ok(absolutized
.to_str()
.ok_or_else(|| {
anyhow!(
"could not build absolute path from file name {}",
target_path
)
})?
.to_owned())
Ok(absolutized.to_path_buf())
}
3 changes: 2 additions & 1 deletion crates/core/src/pattern_compiler/as_compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use anyhow::{anyhow, Result};
use grit_pattern_matcher::pattern::{Container, Match, Pattern, Predicate, Where};
use grit_util::{traverse, AnalysisLogBuilder, AstNode, Language, Order};
use marzano_util::{cursor_wrapper::CursorWrapper, node_with_source::NodeWithSource};
use std::path::Path;

pub(crate) struct AsCompiler;

Expand Down Expand Up @@ -37,7 +38,7 @@ impl NodeCompiler for AsCompiler {
let range = node.range();
let log = AnalysisLogBuilder::default()
.level(441_u16)
.file(context.compilation.file)
.file(context.compilation.file.map(Path::to_owned))
.source(node.source)
.position(range.start)
.range(range)
Expand Down
15 changes: 6 additions & 9 deletions crates/core/src/pattern_compiler/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ use anyhow::{bail, Result};
use grit_pattern_matcher::pattern::State;
use grit_pattern_matcher::{
constant::Constant,
constants::{
ABSOLUTE_PATH_INDEX, DEFAULT_FILE_NAME, FILENAME_INDEX, MATCH_VAR, NEW_FILES_INDEX,
PROGRAM_INDEX,
},
constants::{ABSOLUTE_PATH_INDEX, FILENAME_INDEX, MATCH_VAR, NEW_FILES_INDEX, PROGRAM_INDEX},
pattern::{
And, BooleanConstant, CallBuiltIn, Container, GritFunctionDefinition, Match, Pattern,
PatternDefinition, Predicate, PredicateDefinition, ResolvedPattern,
Expand All @@ -37,7 +34,7 @@ use marzano_language::{
self, grit_parser::MarzanoGritParser, language::Tree, target_language::TargetLanguage,
};

use std::{collections::BTreeMap, path::Path, vec};
use std::{collections::BTreeMap, vec};

pub type CallbackMatchFn = dyn for<'a> Fn(
&<problem::MarzanoQueryContext as grit_pattern_matcher::context::QueryContext>::ResolvedPattern<'a>,
Expand Down Expand Up @@ -93,7 +90,7 @@ impl PatternBuilder {
let error = ". never matches and should not be used as a pattern. Did you mean to run 'grit apply <pattern> .'?";
bail!(error);
}
let src_tree = grit_parser.parse_file(&src, Some(Path::new(DEFAULT_FILE_NAME)))?;
let src_tree = grit_parser.parse_file(&src, None)?;

let root = src_tree.root_node();
let mut built_ins = BuiltIns::get_built_in_functions();
Expand All @@ -118,7 +115,7 @@ impl PatternBuilder {
} = get_definition_info(&libs, &root, grit_parser)?;

let context = CompilationContext {
file: DEFAULT_FILE_NAME,
file: None,
built_ins: &built_ins,
lang: &lang,
pattern_definition_info: &pattern_definition_indices,
Expand Down Expand Up @@ -204,7 +201,7 @@ impl PatternBuilder {
injected_limit: Option<usize>,
) -> Result<Self> {
let compilation = CompilationContext {
file: DEFAULT_FILE_NAME,
file: None,
built_ins: &self.built_ins,
lang: &self.language,
pattern_definition_info: &self.pattern_definition_indices,
Expand Down Expand Up @@ -266,7 +263,7 @@ impl PatternBuilder {
}));

let compilation = CompilationContext {
file: DEFAULT_FILE_NAME,
file: None,
built_ins: &self.built_ins,
lang: &self.language,
pattern_definition_info: &self.pattern_definition_indices,
Expand Down
32 changes: 14 additions & 18 deletions crates/core/src/pattern_compiler/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,8 @@ use crate::{
problem::{MarzanoQueryContext, Problem},
};
use anyhow::{anyhow, bail, Result};
use grit_pattern_matcher::{
constants::{
DEFAULT_FILE_NAME,
},
pattern::{
GritFunctionDefinition, PatternDefinition, PredicateDefinition,
VariableSourceLocations,
},
use grit_pattern_matcher::pattern::{
GritFunctionDefinition, PatternDefinition, PredicateDefinition, VariableSourceLocations,
};
use grit_util::{traverse, AnalysisLogs, Ast, AstNode, FileRange, Order, Range, VariableMatch};
use itertools::Itertools;
Expand All @@ -41,7 +35,7 @@ use std::{
use tracing::instrument;

pub(crate) struct CompilationContext<'a> {
pub file: &'a str,
pub file: Option<&'a Path>,
pub built_ins: &'a BuiltIns,
pub lang: &'a TargetLanguage,
pub pattern_definition_info: &'a BTreeMap<String, DefinitionInfo>,
Expand Down Expand Up @@ -300,15 +294,18 @@ pub(crate) fn get_definitions(
.sorted_by(|x, y| Ord::cmp(x.1, y.1))
.map(|x| VariableSourceLocations {
name: x.0.clone(),
file: context.file.to_owned(),
file: context.file.map(Path::to_owned),
locations: BTreeSet::new(),
})
.collect(),
);

for (file, pattern) in libs.iter() {
let mut node_context = NodeCompilationContext {
compilation: &CompilationContext { file, ..*context },
compilation: &CompilationContext {
file: Some(Path::new(file)),
..*context
},
// We're not in a local scope yet, so this map is kinda useless.
// It's just there because all node compilers expect one.
vars: &mut BTreeMap::new(),
Expand Down Expand Up @@ -488,7 +485,7 @@ pub(crate) fn filter_libs(
) -> Result<Vec<(String, String)>> {
let node_like = "nodeLike";
let predicate_call = "predicateCall";
let tree = parser.parse_file(src, Some(Path::new(DEFAULT_FILE_NAME)))?;
let tree = parser.parse_file(src, None)?;
let DefsToFilenames {
patterns: pattern_file,
predicates: predicate_file,
Expand All @@ -500,10 +497,9 @@ pub(crate) fn filter_libs(

let mut stack: Vec<Tree> = if will_autowrap {
let before_each_file = "before_each_file()";
let before_tree =
parser.parse_file(before_each_file, Some(Path::new(DEFAULT_FILE_NAME)))?;
let before_tree = parser.parse_file(before_each_file, None)?;
let after_each_file = "after_each_file()";
let after_tree = parser.parse_file(after_each_file, Some(Path::new(DEFAULT_FILE_NAME)))?;
let after_tree = parser.parse_file(after_each_file, None)?;

vec![tree, before_tree, after_tree]
} else {
Expand Down Expand Up @@ -559,7 +555,7 @@ fn find_definition_if_exists(
if let Some(file_name) = files.get(name) {
if !filtered.contains_key(file_name) {
if let Some(file_body) = libs.get(file_name) {
filtered.insert(file_name.to_owned(), file_body.to_owned());
filtered.insert(file_name.clone(), file_body.clone());
let tree = parser.parse_file(file_body, Some(Path::new(file_name)))?;
return Ok(Some(tree));
}
Expand Down Expand Up @@ -590,7 +586,7 @@ pub fn src_to_problem_libs(
injected_limit: Option<usize>,
) -> Result<CompilationResult> {
let mut parser = MarzanoGritParser::new()?;
let src_tree = parser.parse_file(&src, Some(Path::new(DEFAULT_FILE_NAME)))?;
let src_tree = parser.parse_file(&src, None)?;
let lang = TargetLanguage::from_tree(&src_tree).unwrap_or(default_lang);
let builder = PatternBuilder::start(src, libs, lang, name, &mut parser, custom_built_ins)?;
builder.compile(file_ranges, injected_limit)
Expand All @@ -610,7 +606,7 @@ impl VariableLocations {
let mut variables = vec![];
for (i, scope) in self.locations.iter().enumerate() {
for (j, var) in scope.iter().enumerate() {
if var.file == DEFAULT_FILE_NAME {
if var.file.is_none() {
variables.push(VariableMatch {
name: var.name.clone(),
scoped_name: format!("{}_{}_{}", i, j, var.name),
Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/pattern_compiler/not_compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl NodeCompiler for NotCompiler {
}) {
let log = AnalysisLogBuilder::default()
.level(441_u16)
.file(context.compilation.file)
.file(context.compilation.file.map(Into::into))
.source(node.source)
.position(range.start)
.range(range)
Expand Down Expand Up @@ -68,7 +68,7 @@ impl NodeCompiler for PrNotCompiler {
}) {
let log = AnalysisLogBuilder::default()
.level(441_u16)
.file(context.compilation.file)
.file(context.compilation.file.map(Into::into))
.source(node.source)
.position(range.start)
.range(range)
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/pattern_compiler/regex_compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl NodeCompiler for RegexCompiler {
);
let log = AnalysisLogBuilder::default()
.level(441_u16)
.file(context.compilation.file)
.file(context.compilation.file.map(Into::into))
.source(node.source)
.position(range.start)
.range(range)
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/pattern_compiler/rewrite_compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl NodeCompiler for RewriteCompiler {
let range = node.range();
let log = AnalysisLogBuilder::default()
.level(441_u16)
.file(context.compilation.file)
.file(context.compilation.file.map(Into::into))
.source(node.source)
.position(range.start)
.range(range)
Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/pattern_compiler/snippet_compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
use crate::{pattern_compiler::compiler::NodeCompilationContext, split_snippet::split_snippet};
use anyhow::{anyhow, bail, Result};
use grit_pattern_matcher::{
constants::{DEFAULT_FILE_NAME, GLOBAL_VARS_SCOPE_INDEX},
constants::GLOBAL_VARS_SCOPE_INDEX,
pattern::{DynamicPattern, DynamicSnippet, DynamicSnippetPart, Pattern, Variable},
};
use grit_util::{AstNode, Language, Position, Range};
Expand Down Expand Up @@ -118,7 +118,7 @@ pub(crate) fn dynamic_snippet_from_source(
*var,
)));
} else if let Some(var) = context.global_vars.get(var.as_ref()) {
if context.compilation.file == DEFAULT_FILE_NAME {
if context.compilation.file.is_none() {
context.vars_array[GLOBAL_VARS_SCOPE_INDEX][*var]
.locations
.insert(range);
Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/pattern_compiler/step_compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl NodeCompiler for StepCompiler {
let range = node.range();
let log = AnalysisLogBuilder::default()
.level(441_u16)
.file(context.compilation.file)
.file(context.compilation.file.map(Into::into))
.source(node.source)
.position(range.start)
.range(range)
Expand All @@ -100,7 +100,7 @@ impl NodeCompiler for StepCompiler {
let range = node.range();
let log = AnalysisLogBuilder::default()
.level(441_u16)
.file(context.compilation.file)
.file(context.compilation.file.map(Into::into))
.source(node.source)
.position(range.start)
.range(range)
Expand Down
Loading
Loading