mirror of
https://github.com/AleoHQ/leo.git
synced 2024-12-26 02:53:08 +03:00
add restriction
This commit is contained in:
parent
93b4634cad
commit
79b13138ff
@ -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,
|
||||
}
|
||||
|
||||
|
@ -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),
|
||||
});
|
||||
|
||||
|
@ -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::*;
|
||||
|
@ -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")]
|
||||
|
25
ast/src/imports/package_name.rs
Normal file
25
ast/src/imports/package_name.rs
Normal 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)
|
||||
}
|
||||
}
|
@ -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 = {
|
||||
|
@ -1,4 +1,4 @@
|
||||
import test_import.foo;
|
||||
import test-import.foo;
|
||||
|
||||
function main() {
|
||||
assert_eq!(foo(), 1u32);
|
||||
|
@ -1,4 +1,4 @@
|
||||
import test_import.( // local import
|
||||
import test-import.( // local import
|
||||
Point,
|
||||
foo,
|
||||
);
|
||||
|
@ -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
|
||||
|
@ -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]
|
||||
|
@ -1,4 +1,4 @@
|
||||
import test_import.(
|
||||
import test-import.(
|
||||
Point,
|
||||
foo
|
||||
);
|
||||
|
5
compiler/tests/import/names.leo
Normal file
5
compiler/tests/import/names.leo
Normal file
@ -0,0 +1,5 @@
|
||||
import a0-f.foo;
|
||||
import a-9.bar;
|
||||
import hello-world.hello;
|
||||
|
||||
function main() {}
|
3
compiler/tests/import/names_a_dash.leo
Normal file
3
compiler/tests/import/names_a_dash.leo
Normal file
@ -0,0 +1,3 @@
|
||||
import a-.foo;
|
||||
|
||||
function main() {}
|
3
compiler/tests/import/names_dash_a.leo
Normal file
3
compiler/tests/import/names_dash_a.leo
Normal file
@ -0,0 +1,3 @@
|
||||
import -a.foo;
|
||||
|
||||
function main() {}
|
3
compiler/tests/import/names_dollar.leo
Normal file
3
compiler/tests/import/names_dollar.leo
Normal file
@ -0,0 +1,3 @@
|
||||
import money$.foo;
|
||||
|
||||
function main() {}
|
3
compiler/tests/import/names_underscore.leo
Normal file
3
compiler/tests/import/names_underscore.leo
Normal file
@ -0,0 +1,3 @@
|
||||
import hello_world.foo;
|
||||
|
||||
function main() {}
|
1
compiler/tests/import/src/a-9.leo
Normal file
1
compiler/tests/import/src/a-9.leo
Normal file
@ -0,0 +1 @@
|
||||
function bar() {}
|
1
compiler/tests/import/src/a0-f.leo
Normal file
1
compiler/tests/import/src/a0-f.leo
Normal file
@ -0,0 +1 @@
|
||||
function foo() {}
|
1
compiler/tests/import/src/hello-world.leo
Normal file
1
compiler/tests/import/src/hello-world.leo
Normal file
@ -0,0 +1 @@
|
||||
function hello() {}
|
@ -1,4 +1,4 @@
|
||||
import test_import.*;
|
||||
import test-import.*;
|
||||
|
||||
function main() {
|
||||
let a = Point { x: 1u32, y: 0u32 };
|
||||
|
@ -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,
|
||||
}
|
||||
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user