use super::*;
impl<N: Network, Instruction: InstructionTrait<N>, Command: CommandTrait<N>> Parser
for ProgramCore<N, Instruction, Command>
{
#[inline]
fn parse(string: &str) -> ParserResult<Self> {
enum P<N: Network, Instruction: InstructionTrait<N>, Command: CommandTrait<N>> {
M(Mapping<N>),
I(StructType<N>),
R(RecordType<N>),
C(ClosureCore<N, Instruction>),
F(FunctionCore<N, Instruction, Command>),
}
let (string, imports) = many0(Import::parse)(string)?;
let (string, _) = Sanitizer::parse(string)?;
let (string, _) = tag(Self::type_name())(string)?;
let (string, _) = Sanitizer::parse_whitespaces(string)?;
let (string, id) = ProgramID::parse(string)?;
let (string, _) = Sanitizer::parse_whitespaces(string)?;
let (string, _) = tag(";")(string)?;
let (string, components) = many1(alt((
map(Mapping::parse, |mapping| P::<N, Instruction, Command>::M(mapping)),
map(StructType::parse, |struct_| P::<N, Instruction, Command>::I(struct_)),
map(RecordType::parse, |record| P::<N, Instruction, Command>::R(record)),
map(ClosureCore::parse, |closure| P::<N, Instruction, Command>::C(closure)),
map(FunctionCore::parse, |function| P::<N, Instruction, Command>::F(function)),
)))(string)?;
let (string, _) = Sanitizer::parse(string)?;
map_res(take(0usize), move |_| {
let mut program = match ProgramCore::<N, Instruction, Command>::new(id) {
Ok(program) => program,
Err(error) => {
eprintln!("{error}");
return Err(error);
}
};
for component in components.iter() {
let result = match component {
P::M(mapping) => program.add_mapping(mapping.clone()),
P::I(struct_) => program.add_struct(struct_.clone()),
P::R(record) => program.add_record(record.clone()),
P::C(closure) => program.add_closure(closure.clone()),
P::F(function) => program.add_function(function.clone()),
};
match result {
Ok(_) => (),
Err(error) => {
eprintln!("{error}");
return Err(error);
}
}
}
for import in imports.iter() {
match program.add_import(import.clone()) {
Ok(_) => (),
Err(error) => {
eprintln!("{error}");
return Err(error);
}
}
}
Ok::<_, Error>(program)
})(string)
}
}
impl<N: Network, Instruction: InstructionTrait<N>, Command: CommandTrait<N>> FromStr
for ProgramCore<N, Instruction, Command>
{
type Err = Error;
fn from_str(string: &str) -> Result<Self> {
match Self::parse(string) {
Ok((remainder, object)) => {
ensure!(remainder.is_empty(), "Failed to parse string. Remaining invalid string is: \"{remainder}\"");
Ok(object)
}
Err(error) => bail!("Failed to parse string. {error}"),
}
}
}
impl<N: Network, Instruction: InstructionTrait<N>, Command: CommandTrait<N>> Debug
for ProgramCore<N, Instruction, Command>
{
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
Display::fmt(self, f)
}
}
#[allow(clippy::format_push_string)]
impl<N: Network, Instruction: InstructionTrait<N>, Command: CommandTrait<N>> Display
for ProgramCore<N, Instruction, Command>
{
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let mut program = String::new();
if !self.imports.is_empty() {
for import in self.imports.values() {
program.push_str(&format!("{import}\n"));
}
program.push('\n');
}
program += &format!("{} {};\n\n", Self::type_name(), self.id);
for (identifier, definition) in self.identifiers.iter() {
match definition {
ProgramDefinition::Mapping => match self.mappings.get(identifier) {
Some(mapping) => program.push_str(&format!("{mapping}\n\n")),
None => return Err(fmt::Error),
},
ProgramDefinition::Struct => match self.structs.get(identifier) {
Some(struct_) => program.push_str(&format!("{struct_}\n\n")),
None => return Err(fmt::Error),
},
ProgramDefinition::Record => match self.records.get(identifier) {
Some(record) => program.push_str(&format!("{record}\n\n")),
None => return Err(fmt::Error),
},
ProgramDefinition::Closure => match self.closures.get(identifier) {
Some(closure) => program.push_str(&format!("{closure}\n\n")),
None => return Err(fmt::Error),
},
ProgramDefinition::Function => match self.functions.get(identifier) {
Some(function) => program.push_str(&format!("{function}\n\n")),
None => return Err(fmt::Error),
},
}
}
program.pop();
write!(f, "{program}")
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Program;
use console::network::Testnet3;
type CurrentNetwork = Testnet3;
#[test]
fn test_program_parse() -> Result<()> {
let (string, program) = Program::<CurrentNetwork>::parse(
r"
program to_parse.aleo;
struct message:
first as field;
second as field;
function compute:
input r0 as message.private;
add r0.first r0.second into r1;
output r1 as field.private;",
)
.unwrap();
assert!(string.is_empty(), "Parser did not consume all of the string: '{string}'");
assert!(program.contains_struct(&Identifier::from_str("message")?));
assert!(program.contains_function(&Identifier::from_str("compute")?));
Ok(())
}
#[test]
fn test_program_parse_function_zero_inputs() -> Result<()> {
let (string, program) = Program::<CurrentNetwork>::parse(
r"
program to_parse.aleo;
function compute:
add 1u32 2u32 into r0;
output r0 as u32.private;",
)
.unwrap();
assert!(string.is_empty(), "Parser did not consume all of the string: '{string}'");
assert!(program.contains_function(&Identifier::from_str("compute")?));
Ok(())
}
#[test]
fn test_program_display() -> Result<()> {
let expected = r"program to_parse.aleo;
struct message:
first as field;
second as field;
function compute:
input r0 as message.private;
add r0.first r0.second into r1;
output r1 as field.private;
";
let program = Program::<CurrentNetwork>::from_str(expected)?;
assert_eq!(expected, format!("{program}"));
Ok(())
}
}