2020-08-18 13:50:26 +03:00
|
|
|
// Copyright (C) 2019-2020 Aleo Systems Inc.
|
|
|
|
// This file is part of the Leo library.
|
|
|
|
|
|
|
|
// The Leo library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// The Leo library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2021-01-26 19:21:05 +03:00
|
|
|
use crate::{Circuit, DeprecatedError, Function, FunctionInput, Identifier, ImportStatement, TestFunction};
|
2020-10-31 03:17:17 +03:00
|
|
|
use leo_grammar::{
|
2020-08-16 05:20:41 +03:00
|
|
|
annotations::{Annotation, AnnotationArguments, AnnotationName},
|
|
|
|
definitions::{AnnotatedDefinition, Definition},
|
|
|
|
};
|
|
|
|
|
2021-01-26 19:21:05 +03:00
|
|
|
use std::convert::TryFrom;
|
|
|
|
|
2020-12-07 20:05:55 +03:00
|
|
|
use indexmap::IndexMap;
|
2020-08-16 05:20:41 +03:00
|
|
|
|
|
|
|
pub fn load_annotation(
|
|
|
|
annotated_definition: AnnotatedDefinition,
|
2020-10-27 01:51:46 +03:00
|
|
|
_imports: &mut Vec<ImportStatement>,
|
2020-12-07 20:05:55 +03:00
|
|
|
_circuits: &mut IndexMap<Identifier, Circuit>,
|
|
|
|
_functions: &mut IndexMap<Identifier, Function>,
|
|
|
|
tests: &mut IndexMap<Identifier, TestFunction>,
|
2020-10-02 06:17:47 +03:00
|
|
|
_expected: &mut Vec<FunctionInput>,
|
2021-01-26 19:21:05 +03:00
|
|
|
) -> Result<(), DeprecatedError> {
|
2020-08-16 05:20:41 +03:00
|
|
|
let ast_annotation = annotated_definition.annotation;
|
|
|
|
let ast_definition = *annotated_definition.definition;
|
|
|
|
|
|
|
|
match ast_definition {
|
2021-01-26 19:21:05 +03:00
|
|
|
Definition::Import(_) => {
|
|
|
|
unimplemented!("annotated imports are not supported yet");
|
|
|
|
}
|
|
|
|
Definition::Circuit(_) => {
|
|
|
|
unimplemented!("annotated circuits are not supported yet");
|
|
|
|
}
|
2021-01-21 17:25:36 +03:00
|
|
|
Definition::Function(function) => match ast_annotation.name {
|
2021-01-26 19:21:05 +03:00
|
|
|
// If it's deprecated for more than one type of syntax,
|
|
|
|
// we could just call it before the match on ast_definition.
|
|
|
|
AnnotationName::Context(_) => Err(DeprecatedError::try_from(ast_annotation.name).unwrap()),
|
2021-01-22 01:02:27 +03:00
|
|
|
AnnotationName::Test(_) => {
|
2021-01-21 17:25:36 +03:00
|
|
|
let ident = Identifier::from(function.identifier.clone());
|
2021-01-22 01:05:16 +03:00
|
|
|
_functions.remove(&ident);
|
2021-01-21 17:25:36 +03:00
|
|
|
|
|
|
|
let test_function = leo_grammar::functions::TestFunction::from(function);
|
|
|
|
let test = TestFunction::from(test_function);
|
|
|
|
tests.insert(ident, test.clone());
|
|
|
|
|
|
|
|
load_annotated_test(test, ast_annotation, tests);
|
2021-01-26 19:21:05 +03:00
|
|
|
Ok(())
|
2021-01-21 17:25:36 +03:00
|
|
|
}
|
|
|
|
},
|
2021-01-26 19:21:05 +03:00
|
|
|
Definition::Deprecated(_) => Ok(()),
|
|
|
|
Definition::Annotated(_) => {
|
|
|
|
unimplemented!("nested annotations are not supported yet");
|
|
|
|
}
|
2020-08-16 05:20:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-07 20:05:55 +03:00
|
|
|
pub fn load_annotated_test(test: TestFunction, annotation: Annotation, tests: &mut IndexMap<Identifier, TestFunction>) {
|
2020-08-16 05:20:41 +03:00
|
|
|
let name = annotation.name;
|
|
|
|
let ast_arguments = annotation.arguments;
|
|
|
|
|
|
|
|
match name {
|
2021-01-22 01:02:27 +03:00
|
|
|
AnnotationName::Test(_) if ast_arguments.is_some() => {
|
2021-01-21 20:28:52 +03:00
|
|
|
load_annotated_test_context(test, ast_arguments.unwrap(), tests)
|
|
|
|
}
|
|
|
|
_ => (),
|
2020-08-16 05:20:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn load_annotated_test_context(
|
|
|
|
mut test: TestFunction,
|
|
|
|
ast_arguments: AnnotationArguments,
|
2020-12-07 20:05:55 +03:00
|
|
|
tests: &mut IndexMap<Identifier, TestFunction>,
|
2020-08-16 05:20:41 +03:00
|
|
|
) {
|
|
|
|
let arguments = ast_arguments.arguments;
|
|
|
|
|
|
|
|
if arguments.len() != 1 {
|
|
|
|
panic!("text context annotation must have one argument identifier")
|
|
|
|
}
|
|
|
|
|
|
|
|
let ast_input_file = arguments[0].to_owned();
|
|
|
|
let input_file = Identifier::from(ast_input_file);
|
|
|
|
|
|
|
|
test.input_file = Some(input_file);
|
|
|
|
|
|
|
|
tests.insert(test.function.identifier.clone(), test);
|
|
|
|
}
|