deprecate @context annotation, temp fix for main.rs in ast, how should we handle this?

This commit is contained in:
gluaxspeed 2021-01-26 11:21:05 -05:00
parent 91f5c18da8
commit c03451fcd8
9 changed files with 71 additions and 21 deletions

View File

@ -21,7 +21,7 @@ use criterion::{criterion_group, criterion_main, Criterion};
use std::{path::Path, time::Duration};
fn ast(ast: &Grammar) -> Ast {
Ast::new("leo_tree", &ast)
Ast::new("leo_tree", &ast).unwrap()
}
fn bench_big_if_else(c: &mut Criterion) {

View File

@ -14,12 +14,14 @@
// 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/>.
use crate::{Circuit, Function, FunctionInput, Identifier, ImportStatement, TestFunction};
use crate::{Circuit, DeprecatedError, Function, FunctionInput, Identifier, ImportStatement, TestFunction};
use leo_grammar::{
annotations::{Annotation, AnnotationArguments, AnnotationName},
definitions::{AnnotatedDefinition, Definition},
};
use std::convert::TryFrom;
use indexmap::IndexMap;
pub fn load_annotation(
@ -29,14 +31,21 @@ pub fn load_annotation(
_functions: &mut IndexMap<Identifier, Function>,
tests: &mut IndexMap<Identifier, TestFunction>,
_expected: &mut Vec<FunctionInput>,
) {
) -> Result<(), DeprecatedError> {
let ast_annotation = annotated_definition.annotation;
let ast_definition = *annotated_definition.definition;
match ast_definition {
Definition::Import(_) => unimplemented!("annotated imports are not supported yet"),
Definition::Circuit(_) => unimplemented!("annotated circuits are not supported yet"),
Definition::Import(_) => {
unimplemented!("annotated imports are not supported yet");
}
Definition::Circuit(_) => {
unimplemented!("annotated circuits are not supported yet");
}
Definition::Function(function) => match ast_annotation.name {
// 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()),
AnnotationName::Test(_) => {
let ident = Identifier::from(function.identifier.clone());
_functions.remove(&ident);
@ -46,10 +55,13 @@ pub fn load_annotation(
tests.insert(ident, test.clone());
load_annotated_test(test, ast_annotation, tests);
Ok(())
}
},
Definition::Deprecated(_) => {}
Definition::Annotated(_) => unimplemented!("nested annotations are not supported yet"),
Definition::Deprecated(_) => Ok(()),
Definition::Annotated(_) => {
unimplemented!("nested annotations are not supported yet");
}
}
}

View File

@ -14,11 +14,10 @@
// 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/>.
use leo_grammar::definitions::Deprecated;
use crate::{Error as FormattedError, Span};
use leo_grammar::{annotations::AnnotationName, definitions::Deprecated};
use std::path::Path;
use std::{convert::TryFrom, path::Path};
#[derive(Debug, Error)]
pub enum DeprecatedError {
@ -42,9 +41,23 @@ impl<'ast> From<Deprecated<'ast>> for DeprecatedError {
fn from(deprecated: Deprecated<'ast>) -> Self {
match deprecated {
Deprecated::TestFunction(test_function) => DeprecatedError::new_from_span(
"\"test function...\" is deprecated. Did you mean @test decorator?".to_string(),
"\"test function...\" is deprecated. Did you mean @test annotation?".to_string(),
Span::from(test_function.span.clone()),
),
}
}
}
impl<'ast> TryFrom<AnnotationName<'ast>> for DeprecatedError {
type Error = bool;
fn try_from(annotation_name: AnnotationName<'ast>) -> Result<Self, bool> {
match annotation_name {
AnnotationName::Context(context) => Ok(DeprecatedError::new_from_span(
"\"@context(...)\" is deprecated. Did you mean @test annotation?".to_string(),
Span::from(context.span.clone()),
)),
_ => Err(false),
}
}
}

View File

@ -27,12 +27,18 @@ fn to_leo_tree(filepath: &Path) -> Result<String, ParserError> {
let ast = Grammar::new(&program_filepath, &program_string)?;
// Parse the pest ast and constructs a ast.
let leo_ast = Ast::new("leo_tree", &ast);
let leo_ast_result = Ast::new("leo_tree", &ast);
// Serializes the tree into JSON format.
let serialized_leo_ast = Ast::to_json_string(&leo_ast)?;
match leo_ast_result {
Ok(leo_ast) => {
// Serializes the tree into JSON format.
let serialized_leo_ast = Ast::to_json_string(&leo_ast)?;
Ok(serialized_leo_ast)
Ok(serialized_leo_ast)
}
// How to deal with this?
Err(_) => Err(ParserError::SyntaxTreeError),
}
}
fn main() -> Result<(), ParserError> {

View File

@ -79,7 +79,7 @@ impl<'ast> Program {
Some(Err(DeprecatedError::from(deprecated)))
}
Definition::Annotated(annotated_definition) => {
load_annotation(
let loaded_annotation = load_annotation(
annotated_definition,
&mut imports,
&mut circuits,
@ -88,7 +88,10 @@ impl<'ast> Program {
&mut expected_input,
);
None
match loaded_annotation {
Ok(_) => None,
Err(deprecated_err) => Some(Err(deprecated_err))
}
}
})
.transpose()?;

View File

@ -23,9 +23,18 @@ use serde::Serialize;
#[derive(Clone, Debug, FromPest, PartialEq, Serialize)]
#[pest_ast(rule(Rule::annotation_name))]
pub enum AnnotationName<'ast> {
Context(Context<'ast>),
Test(Test<'ast>),
}
#[derive(Clone, Debug, FromPest, PartialEq, Serialize)]
#[pest_ast(rule(Rule::context))]
pub struct Context<'ast> {
#[pest_ast(outer())]
#[serde(with = "SpanDef")]
pub span: Span<'ast>,
}
#[derive(Clone, Debug, FromPest, PartialEq, Serialize)]
#[pest_ast(rule(Rule::test))]
pub struct Test<'ast> {

View File

@ -526,10 +526,12 @@ annotation_symbol = ${"@"}
// Declared in annotations/annotation_name.rs
annotation_name = {
test
context // deprecated
| test
}
// Declared in annotations/annotation_name.rs
context = {"context"}
test = {"test"}
// Declared in annotations/annotation_argument.rs

View File

@ -42,9 +42,12 @@ impl TestSymbolTable {
let grammar = Grammar::new(&file_path, program_string).unwrap();
// Get Leo syntax tree.
let ast = Ast::new(TEST_PROGRAM_PATH, &grammar);
let ast_result = Ast::new(TEST_PROGRAM_PATH, &grammar);
Self { ast }
// We always expect a valid ast for testing.
Self {
ast: ast_result.unwrap(),
}
}
///

View File

@ -46,8 +46,10 @@ impl TestTypeInference {
// Get parser syntax tree.
let ast = Grammar::new(&file_path, program_string).unwrap();
let result = Ast::new(TEST_PROGRAM_NAME, &ast);
// Get typed syntax tree.
let typed = Ast::new(TEST_PROGRAM_NAME, &ast);
// Always expect a valid SyntaxTree for testing.
let typed = result.unwrap();
let program = typed.into_repr();
// Create empty import parser.