mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-11-24 07:48:04 +03:00
make suggested changes
This commit is contained in:
parent
1604170adc
commit
cc5552c7ef
@ -20,17 +20,32 @@ use leo_span::Span;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub enum ParamMode {
|
||||
Const,
|
||||
Private,
|
||||
Public,
|
||||
}
|
||||
|
||||
impl fmt::Display for ParamMode {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
use ParamMode::*;
|
||||
|
||||
match self {
|
||||
Const => write!(f, "const"),
|
||||
Private => write!(f, "private"),
|
||||
Public => write!(f, "public"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A function parameter.
|
||||
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct FunctionInputVariable {
|
||||
/// The name the parameter is accessible as in the function's body.
|
||||
pub identifier: Identifier,
|
||||
/// Is it a const parameter?
|
||||
pub const_: bool,
|
||||
/// Is it a public parameter?
|
||||
pub public: bool,
|
||||
/// Is it a mutable parameter?
|
||||
pub mutable: bool,
|
||||
pub mode: ParamMode,
|
||||
/// What's the parameter's type?
|
||||
pub type_: Type,
|
||||
/// The parameters span from any annotations to its type.
|
||||
@ -39,17 +54,7 @@ pub struct FunctionInputVariable {
|
||||
|
||||
impl FunctionInputVariable {
|
||||
fn format(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
if self.public {
|
||||
write!(f, "public ")?;
|
||||
} else {
|
||||
write!(f, "private ")?;
|
||||
}
|
||||
if self.const_ {
|
||||
write!(f, "const ")?;
|
||||
}
|
||||
if self.mutable {
|
||||
write!(f, "mut ")?;
|
||||
}
|
||||
write!(f, "{} ", self.mode)?;
|
||||
write!(f, "{}: ", self.identifier)?;
|
||||
write!(f, "{}", self.type_)
|
||||
}
|
||||
|
@ -15,14 +15,13 @@
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use super::*;
|
||||
use crate::{Expression, Identifier, Type};
|
||||
use crate::{Expression, Identifier, ParamMode, Type};
|
||||
|
||||
/// A single definition inside a section in a state or an input file.
|
||||
/// Definitions should be structured as: `<name>: <type_> = <value>;`
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct Definition {
|
||||
pub const_: bool,
|
||||
pub public: bool,
|
||||
pub mode: ParamMode,
|
||||
pub type_: Type,
|
||||
pub name: Identifier,
|
||||
pub value: Expression,
|
||||
|
@ -274,9 +274,7 @@ pub trait ReconstructingReducer {
|
||||
) -> Result<FunctionInputVariable> {
|
||||
Ok(FunctionInputVariable {
|
||||
identifier,
|
||||
const_: variable.const_,
|
||||
mutable: variable.mutable,
|
||||
public: variable.public,
|
||||
mode: variable.mode,
|
||||
type_,
|
||||
span: variable.span.clone(),
|
||||
})
|
||||
|
@ -61,12 +61,23 @@ impl ParserContext<'_> {
|
||||
)
|
||||
}
|
||||
|
||||
pub fn parse_function_parameter_mode(&mut self) -> Result<ParamMode> {
|
||||
let public = self.eat(Token::Public);
|
||||
let const_ = self.eat(Token::Const);
|
||||
|
||||
match (public, const_) {
|
||||
(None, Some(_)) => Ok(ParamMode::Const),
|
||||
(None, None) => Ok(ParamMode::Private),
|
||||
(Some(_), None) => Ok(ParamMode::Public),
|
||||
(Some(p), Some(c)) => Err(ParserError::inputs_multiple_variable_types_specified(&(p.span + c.span)).into()),
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
/// Returns a [`FunctionInput`] AST node if the next tokens represent a function parameter.
|
||||
///
|
||||
pub fn parse_function_parameters(&mut self) -> Result<FunctionInput> {
|
||||
let public = self.eat(Token::Public).is_some();
|
||||
let const_ = self.eat(Token::Const);
|
||||
let mode = self.parse_function_parameter_mode()?;
|
||||
let mutable = self.eat(Token::Mut);
|
||||
|
||||
let name = self.expect_ident()?;
|
||||
@ -78,9 +89,7 @@ impl ParserContext<'_> {
|
||||
self.expect(Token::Colon)?;
|
||||
let type_ = self.parse_type()?.0;
|
||||
Ok(FunctionInput::Variable(FunctionInputVariable {
|
||||
const_: const_.is_some(),
|
||||
mutable: const_.is_none(),
|
||||
public,
|
||||
mode,
|
||||
type_,
|
||||
span: name.span.clone(),
|
||||
identifier: name,
|
||||
|
@ -67,8 +67,7 @@ impl ParserContext<'_> {
|
||||
/// `<identifier> : <type> = <expression>;`
|
||||
/// Returns [`Definition`].
|
||||
pub fn parse_input_definition(&mut self) -> Result<Definition> {
|
||||
let public = self.eat(Token::Public).is_some();
|
||||
let const_ = self.eat(Token::Const).is_some();
|
||||
let mode = self.parse_function_parameter_mode()?;
|
||||
|
||||
let name = self.expect_ident()?;
|
||||
self.expect(Token::Colon)?;
|
||||
@ -78,8 +77,7 @@ impl ParserContext<'_> {
|
||||
self.expect(Token::Semicolon)?;
|
||||
|
||||
Ok(Definition {
|
||||
const_,
|
||||
public,
|
||||
mode,
|
||||
name,
|
||||
type_,
|
||||
value,
|
||||
|
@ -358,4 +358,12 @@ create_errors!(
|
||||
msg: format!("Found the char `{}`, but expected `{}`", found, expected),
|
||||
help: None,
|
||||
}
|
||||
|
||||
/// For when a user specified more than a type on a parameter.
|
||||
@formatted
|
||||
inputs_multiple_variable_types_specified {
|
||||
args: (),
|
||||
msg: "A parameter cannot be both public and const.",
|
||||
help: None,
|
||||
}
|
||||
);
|
||||
|
@ -131,7 +131,7 @@ symbols! {
|
||||
main,
|
||||
Mut: "mut",
|
||||
prelude,
|
||||
Public: "public",
|
||||
Public,
|
||||
Return: "return",
|
||||
Star: "*",
|
||||
std,
|
||||
|
Loading…
Reference in New Issue
Block a user