snarkvm_synthesizer_program/logic/instruction/
parse.rs1use super::*;
17
18impl<N: Network> Parser for Instruction<N> {
19 #[inline]
21 fn parse(string: &str) -> ParserResult<Self> {
22 macro_rules! alt_parser {
28 ($v0:expr) => {{ alt(($v0,)) }};
29 ($v0:expr, $v1:expr) => {{ alt(($v0, $v1,)) }};
30 ($v0:expr, $v1:expr, $v2:expr) => {{ alt(($v0, $v1, $v2,)) }};
31 ($v0:expr, $v1:expr, $v2:expr, $v3:expr) => {{ alt(($v0, $v1, $v2, $v3,)) }};
32 ($v0:expr, $v1:expr, $v2:expr, $v3:expr, $v4:expr) => {{ alt(($v0, $v1, $v2, $v3, $v4,)) }};
33 ($v0:expr, $v1:expr, $v2:expr, $v3:expr, $v4:expr, $v5:expr) => {{ alt(($v0, $v1, $v2, $v3, $v4, $v5,)) }};
34 ($v0:expr, $v1:expr, $v2:expr, $v3:expr, $v4:expr, $v5:expr, $v6:expr) => {{ alt(($v0, $v1, $v2, $v3, $v4, $v5, $v6,)) }};
35 ($v0:expr, $v1:expr, $v2:expr, $v3:expr, $v4:expr, $v5:expr, $v6:expr, $v7:expr) => {{ alt(($v0, $v1, $v2, $v3, $v4, $v5, $v6, $v7,)) }};
36 ($v0:expr, $v1:expr, $v2:expr, $v3:expr, $v4:expr, $v5:expr, $v6:expr, $v7:expr, $v8:expr) => {{ alt(($v0, $v1, $v2, $v3, $v4, $v5, $v6, $v7, $v8,)) }};
37 ($v0:expr, $v1:expr, $v2:expr, $v3:expr, $v4:expr, $v5:expr, $v6:expr, $v7:expr, $v8:expr, $v9:expr) => {{ alt(($v0, $v1, $v2, $v3, $v4, $v5, $v6, $v7, $v8, $v9,)) }};
38 ($v0:expr, $v1:expr, $v2:expr, $v3:expr, $v4:expr, $v5:expr, $v6:expr, $v7:expr, $v8:expr, $v9:expr, $( $variants:expr ),*) => {{ alt((
39 alt_parser!($( $variants ),*), $v0, $v1, $v2, $v3, $v4, $v5, $v6, $v7, $v8, $v9,
40 )) }};
41 }
42
43 macro_rules! instruction_parsers {
50 ($object:expr, |_instruction| $_operation:block, { $( $variant:ident, )+ }) => {{
51 alt_parser!( $( map($variant::parse, Into::into) ),+ )
52 }};
53 }
54
55 let (string, _) = Sanitizer::parse(string)?;
57 let (string, instruction) = crate::instruction!(instruction_parsers!(self, _instruction))(string)?;
59 let (string, _) = Sanitizer::parse_whitespaces(string)?;
61 let (string, _) = tag(";")(string)?;
63
64 Ok((string, instruction))
65 }
66}
67
68impl<N: Network> FromStr for Instruction<N> {
69 type Err = Error;
70
71 #[inline]
73 fn from_str(string: &str) -> Result<Self> {
74 match Self::parse(string) {
75 Ok((remainder, object)) => {
76 ensure!(remainder.is_empty(), "Failed to parse string. Found invalid character in: \"{remainder}\"");
78 Ok(object)
80 }
81 Err(error) => bail!("Failed to parse string. {error}"),
82 }
83 }
84}
85
86#[cfg(test)]
87mod tests {
88 use super::*;
89 use console::network::MainnetV0;
90
91 type CurrentNetwork = MainnetV0;
92
93 #[test]
94 fn test_parse() -> Result<()> {
95 let instruction = "add r0 r1 into r2;";
96 let (remainder, candidate) = Instruction::<CurrentNetwork>::parse(instruction)?;
97 assert_eq!("", remainder);
98 assert_eq!(instruction, candidate.to_string());
99 Ok(())
100 }
101}