Skip to content
Merged
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
1 change: 1 addition & 0 deletions src/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ impl<'run, 'src> Analyzer<'run, 'src> {
)?);
}
}
Item::Newline => {}
Item::Recipe(recipe) => {
if recipe.enabled() {
Self::analyze_recipe(recipe)?;
Expand Down
21 changes: 11 additions & 10 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,19 @@ pub(crate) struct Ast<'src> {

impl ColorDisplay for Ast<'_> {
fn fmt(&self, f: &mut Formatter, color: Color) -> fmt::Result {
let mut iter = self.items.iter().peekable();

while let Some(item) = iter.next() {
writeln!(f, "{}", item.color_display(color))?;
let mut newline = false;
for item in &self.items {
if matches!(item, Item::Newline) {
newline = true;
continue;
}

if let Some(next_item) = iter.peek() {
if matches!(item, Item::Recipe(_))
|| mem::discriminant(item) != mem::discriminant(next_item)
{
writeln!(f)?;
}
if newline {
writeln!(f)?;
newline = false;
}

writeln!(f, "{}", item.color_display(color))?;
}

Ok(())
Expand Down
2 changes: 2 additions & 0 deletions src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub(crate) enum Item<'src> {
private: bool,
relative: Option<StringLiteral<'src>>,
},
Newline,
Recipe(UnresolvedRecipe<'src>),
Set(Set<'src>),
Unexport {
Expand Down Expand Up @@ -85,6 +86,7 @@ impl ColorDisplay for Item<'_> {

Ok(())
}
Self::Newline => Ok(()),
Self::Recipe(recipe) => write!(f, "{}", recipe.color_display(color)),
Self::Set(set) => write!(f, "{set}"),
Self::Unexport { name } => write!(f, "unexport {name}"),
Expand Down
71 changes: 62 additions & 9 deletions src/lexer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use {super::*, CompileErrorKind::*, TokenKind::*};

static DEDENT_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^[ \t\n]*\n[^ \t\n]").unwrap());

/// Just language lexer
///
/// The lexer proceeds character-by-character, as opposed to using regular
Expand Down Expand Up @@ -391,6 +393,12 @@ impl<'src> Lexer<'src> {

match indentation {
Blank => {
if DEDENT_RE.is_match(self.rest()) {
while self.indented() {
self.lex_dedent();
}
}

if !whitespace.is_empty() {
while self.next_is_whitespace() {
self.advance()?;
Expand Down Expand Up @@ -1472,8 +1480,8 @@ mod tests {
Indent:" ",
Text:"a",
Eol,
Eol,
Dedent,
Eol,
Identifier:"b",
Colon,
Eol,
Expand Down Expand Up @@ -1520,8 +1528,8 @@ mod tests {
Indent,
Text:"@mv a b",
Eol,
Eol,
Dedent,
Eol,
Identifier:"a",
Colon,
Eol,
Expand All @@ -1531,8 +1539,8 @@ mod tests {
Whitespace:" ",
Text:"@touch a",
Eol,
Eol,
Dedent,
Eol,
Identifier:"d",
Colon,
Whitespace,
Expand All @@ -1541,8 +1549,8 @@ mod tests {
Indent,
Text:"@rm c",
Eol,
Eol,
Dedent,
Eol,
Identifier:"c",
Colon,
Whitespace,
Expand Down Expand Up @@ -1820,6 +1828,34 @@ mod tests {
)
}

test! {
name: tokenize_continued_indent,
text: "
hello:
foo

bar

baz
",
tokens: (
Identifier:"hello",
Colon,
Eol,
Indent,
Text:"foo",
Eol,
Eol,
Whitespace:" ",
Text:"bar",
Eol,
Dedent,
Eol,
Identifier:"baz",
Eol,
),
}

test! {
name: tokenize_empty_lines,
text: "
Expand Down Expand Up @@ -1856,8 +1892,8 @@ mod tests {
Whitespace:" ",
Text:"dsdf # whatever",
Eol,
Eol,
Dedent,
Eol,
Comment:"# yolo",
Eol,
),
Expand Down Expand Up @@ -1978,8 +2014,8 @@ mod tests {
Whitespace:" ",
Text:"d",
Eol,
Eol,
Dedent,
Eol,
Comment:"# hello",
Eol,
Identifier:"bob",
Expand Down Expand Up @@ -2038,8 +2074,8 @@ mod tests {
Indent,
Text:"@mv a b",
Eol,
Eol,
Dedent,
Eol,
Identifier:"a",
Colon,
Eol,
Expand All @@ -2049,8 +2085,8 @@ mod tests {
Whitespace:" ",
Text:"@touch a",
Eol,
Eol,
Dedent,
Eol,
Identifier:"d",
Colon,
Whitespace,
Expand All @@ -2059,8 +2095,8 @@ mod tests {
Indent,
Text:"@rm c",
Eol,
Eol,
Dedent,
Eol,
Identifier:"c",
Colon,
Whitespace,
Expand Down Expand Up @@ -2227,6 +2263,23 @@ mod tests {
),
}

test! {
name: tokenize_recipe_after_body,
text: "foo:\n echo FOO\n\nfoo:",
tokens: (
Identifier: "foo",
Colon,
Eol,
Indent: " ",
Text: "echo FOO",
Eol,
Dedent,
Eol,
Identifier: "foo",
Colon,
),
}

error! {
name: tokenize_space_then_tab,
input: "a:
Expand Down
9 changes: 8 additions & 1 deletion src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ pub(crate) trait Node<'src> {
impl<'src> Node<'src> for Ast<'src> {
fn tree(&self) -> Tree<'src> {
Tree::atom("justfile")
.extend(self.items.iter().map(Node::tree))
.extend(
self
.items
.iter()
.filter(|item| !matches!(item, Item::Newline))
.map(Node::tree),
)
.extend(self.warnings.iter().map(Node::tree))
}
}
Expand Down Expand Up @@ -52,6 +58,7 @@ impl<'src> Node<'src> for Item<'src> {

tree
}
Self::Newline => unreachable!(),
Self::Function(function) => {
let mut tree = Tree::atom("function");
tree.push_mut(function.name.lexeme());
Expand Down
Loading