Repository: casey/just
File: src/parser.rs (Rust)

Code before:
              items.push(Item::Assignment(self.parse_assignment(false)?));
            } else {
              let doc = pop_doc_comment(&mut items, eol_since_last_comment);
              items.push(Item::Recipe(self.parse_recipe(
                doc,
                false,
                BTreeSet::new(),
              )?));
            }
          }
        }
      } else if self.accepted(At)? {
        let doc = pop_doc_comment(&mut items, eol_since_last_comment);
        items.push(Item::Recipe(self.parse_recipe(
          doc,
          true,
          BTreeSet::new(),
        )?));
      } else if let Some(attributes) = self.parse_attributes()? {
        let next_keyword = Keyword::from_lexeme(self.next()?.lexeme());
        match next_keyword {
          Some(Keyword::Alias) if self.next_are(&[Identifier, Identifier, ColonEquals]) => {
            items.push(Item::Alias(self.parse_alias(attributes)?));
          }
          Some(Keyword::Mod)
            if self.next_are(&[Identifier, Identifier, Comment])
              || self.next_are(&[Identifier, Identifier, Eof])
              || self.next_are(&[Identifier, Identifier, Eol])
              || self.next_are(&[Identifier, Identifier, Identifier, StringToken])
              || self.next_are(&[Identifier, Identifier, StringToken])
              || self.next_are(&[Identifier, QuestionMark]) =>
          {
            let doc = pop_doc_comment(&mut items, eol_since_last_comment);
            items.push(self.parse_module(attributes, doc)?);
          }
          _ => {
            let quiet = self.accepted(At)?;
            let doc = pop_doc_comment(&mut items, eol_since_last_comment);
            items.push(Item::Recipe(self.parse_recipe(doc, quiet, attributes)?));
          }
        }
      } else {
        return Err(self.unexpected_token()?);
      }
    }

    if self.next_token == self.tokens.len() {
      Ok(Ast {
        warnings: Vec::new(),
        items,
      })

Review comment (refactor):
This condition is so heinous that we should deduplicate it. Maybe `Parser::next_is_module`?

Code after:
            let name = self.parse_name()?;

            let relative = if self.next_is(StringToken) || self.next_are(&[Identifier, StringToken])
            {
              Some(self.parse_string_literal()?)
            } else {
              None
            };

            items.push(Item::Module {
              attributes: take_attributes(),
              absolute: None,
              doc,
              name,
              optional,
              relative,
            });
          }
          Some(Keyword::Set)
            if self.next_are(&[Identifier, Identifier, ColonEquals])
              || self.next_are(&[Identifier, Identifier, Comment, Eof])
              || self.next_are(&[Identifier, Identifier, Comment, Eol])
              || self.next_are(&[Identifier, Identifier, Eof])
              || self.next_are(&[Identifier, Identifier, Eol]) =>
          {
            items.push(Item::Set(self.parse_set()?));
          }
          _ => {
            if self.next_are(&[Identifier, ColonEquals]) {
              items.push(Item::Assignment(self.parse_assignment(false)?));
            } else {
              let doc = pop_doc_comment(&mut items, eol_since_last_comment);
              items.push(Item::Recipe(self.parse_recipe(
                doc,
                false,
                take_attributes(),
              )?));
            }
          }
        }
      } else if self.accepted(At)? {
        let doc = pop_doc_comment(&mut items, eol_since_last_comment);
        items.push(Item::Recipe(self.parse_recipe(
          doc,
          true,
          take_attributes(),
        )?));
      } else {
        return Err(self.unexpected_token()?);
      }