magicant / yash-rs

Reimplementation of yash, an extended POSIX shell
64 stars 3 forks source link

yash-syntax succeeds in parsing cmd string to List, but fails to parse it to Command or CompoundCommand #387

Closed zjp-CN closed 1 month ago

zjp-CN commented 1 month ago
let cmd = r#"cargo fmt; echo 1"#;
let ast: Command = cmd.parse().unwrap();
called `Result::unwrap()` on an `Err` value: Some(Error { 
  cause: Syntax(RedundantToken), 
  location: Location { code: Code { value: RefCell { value: "cargo fmt; echo 1" }, start_line_number: 1, source: Unknown }, range: 9..10 }
})

If it's parsed to CompoundCommand, then the error is called `Result::unwrap()` on an `Err` value: None.

I thought the cmd string could be parsed as CompoundCommand::Grouping variant to be CompoundCommand and Command.

magicant commented 1 month ago

To parse as a Grouping, you need to enclose the commands in braces:

let cmd = r#"{ cargo fmt; echo 1; }"#;
zjp-CN commented 1 month ago

Thanks! It works well.

Though I made a mistake after seeing your revised cmd string 🥲

let cmd = r#"{cargo fmt; echo 1; }"#; // mistake on left brace without a whitespace
let ast: CompoundCommand = cmd.parse().unwrap(); // error: called `Result::unwrap()` on an `Err` value: None