add restriction

This commit is contained in:
collin 2020-08-15 01:58:33 -07:00
parent 93b4634cad
commit 79b13138ff
23 changed files with 128 additions and 11 deletions

View File

@ -17,7 +17,7 @@ pub enum ParserError {
#[error("{}", _0)]
SyntaxError(#[from] SyntaxError),
#[error("Unable to construct abstract syntax tree")]
#[error("Unable to construct program abstract syntax tree")]
SyntaxTreeError,
}

View File

@ -35,6 +35,11 @@ impl From<Error<Rule>> for SyntaxError {
Rule::operation_div => "`/`".to_owned(),
Rule::operation_pow => "`**`".to_owned(),
Rule::package => "package. Check package and import names for errors.".to_owned(),
Rule::package_name => {
"package name. Please use lowercase letters, numbers, and dashes `-` only.".to_owned()
}
rule => format!("{:?}", rule),
});

View File

@ -10,5 +10,8 @@ pub use package::*;
pub mod package_access;
pub use package_access::*;
pub mod package_name;
pub use package_name::*;
pub mod star;
pub use star::*;

View File

@ -1,4 +1,8 @@
use crate::{ast::Rule, common::Identifier, imports::PackageAccess, SpanDef};
use crate::{
ast::Rule,
imports::{PackageAccess, PackageName},
SpanDef,
};
use pest::Span;
use pest_ast::FromPest;
@ -7,7 +11,7 @@ use serde::Serialize;
#[derive(Clone, Debug, FromPest, PartialEq, Serialize)]
#[pest_ast(rule(Rule::package))]
pub struct Package<'ast> {
pub name: Identifier<'ast>,
pub name: PackageName<'ast>,
pub access: PackageAccess<'ast>,
#[pest_ast(outer())]
#[serde(with = "SpanDef")]

View File

@ -0,0 +1,25 @@
use crate::{
ast::{span_into_string, Rule},
SpanDef,
};
use pest::Span;
use pest_ast::FromPest;
use serde::Serialize;
use std::fmt;
#[derive(Clone, Debug, FromPest, PartialEq, Serialize)]
#[pest_ast(rule(Rule::package_name))]
pub struct PackageName<'ast> {
#[pest_ast(outer(with(span_into_string)))]
pub value: String,
#[pest_ast(outer())]
#[serde(with = "SpanDef")]
pub span: Span<'ast>,
}
impl<'ast> fmt::Display for PackageName<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.value)
}
}

View File

@ -389,8 +389,11 @@ input_tuple = _{ "(" ~ NEWLINE* ~ (input ~ ("," ~ NEWLINE* ~ input)* ~ ","?)? ~
// Declared in imports/import.rs
import = { "import " ~ package ~ LINE_END}
package_name = @{ ((ASCII_ALPHA_LOWER | ASCII_DIGIT) ~ ( "-" ~ (ASCII_ALPHA_LOWER | ASCII_DIGIT))*)+ }
// Declared in imports/package.rs
package = { identifier ~ "." ~ package_access }
package = { package_name ~ "." ~ package_access }
// Declared in imports/package_access
package_access = {

View File

@ -1,4 +1,4 @@
import test_import.foo;
import test-import.foo;
function main() {
assert_eq!(foo(), 1u32);

View File

@ -1,4 +1,4 @@
import test_import.( // local import
import test-import.( // local import
Point,
foo,
);

View File

@ -1,4 +1,4 @@
import test_import.*; // local import
import test-import.*; // local import
import bar.*; // imports directory import
import bar.baz.*; // imports directory import

View File

@ -66,6 +66,54 @@ fn test_alias() {
assert_satisfied(program);
}
// naming tests
#[test]
#[ignore]
fn test_names_pass() {
set_local_dir();
let bytes = include_bytes!("names.leo");
let program = parse_program(bytes).unwrap();
assert_satisfied(program);
}
#[test]
#[ignore]
fn test_names_fail_1() {
set_local_dir();
let bytes = include_bytes!("names_dash_a.leo");
assert!(parse_program(bytes).is_err());
}
#[test]
#[ignore]
fn test_names_fail_2() {
set_local_dir();
let bytes = include_bytes!("names_a_dash.leo");
assert!(parse_program(bytes).is_err());
}
#[test]
#[ignore]
fn test_names_fail_3() {
set_local_dir();
let bytes = include_bytes!("names_underscore.leo");
assert!(parse_program(bytes).is_err());
}
#[test]
#[ignore]
fn test_names_fail_4() {
set_local_dir();
let bytes = include_bytes!("names_dollar.leo");
assert!(parse_program(bytes).is_err());
}
// more complex tests
#[test]
#[ignore]

View File

@ -1,4 +1,4 @@
import test_import.(
import test-import.(
Point,
foo
);

View File

@ -0,0 +1,5 @@
import a0-f.foo;
import a-9.bar;
import hello-world.hello;
function main() {}

View File

@ -0,0 +1,3 @@
import a-.foo;
function main() {}

View File

@ -0,0 +1,3 @@
import -a.foo;
function main() {}

View File

@ -0,0 +1,3 @@
import money$.foo;
function main() {}

View File

@ -0,0 +1,3 @@
import hello_world.foo;
function main() {}

View File

@ -0,0 +1 @@
function bar() {}

View File

@ -0,0 +1 @@
function foo() {}

View File

@ -0,0 +1 @@
function hello() {}

View File

@ -1,4 +1,4 @@
import test_import.*;
import test-import.*;
function main() {
let a = Point { x: 1u32, y: 0u32 };

View File

@ -31,7 +31,7 @@ pub enum InputParserError {
#[error("{}", _0)]
SyntaxError(#[from] InputSyntaxError),
#[error("Unable to construct abstract syntax tree")]
#[error("Unable to construct program input abstract syntax tree")]
SyntaxTreeError,
}

View File

@ -1,5 +1,5 @@
use crate::Span;
use leo_ast::common::Identifier as AstIdentifier;
use leo_ast::{common::Identifier as AstIdentifier, imports::PackageName as AstPackageName};
use leo_input::common::Identifier as InputAstIdentifier;
use serde::{
@ -37,6 +37,15 @@ impl<'ast> From<AstIdentifier<'ast>> for Identifier {
}
}
impl<'ast> From<AstPackageName<'ast>> for Identifier {
fn from(name: AstPackageName<'ast>) -> Self {
Self {
name: name.value,
span: Span::from(name.span),
}
}
}
impl<'ast> From<InputAstIdentifier<'ast>> for Identifier {
fn from(identifier: InputAstIdentifier<'ast>) -> Self {
Self {