mirror of
https://github.com/AleoHQ/leo.git
synced 2024-12-18 23:02:35 +03:00
parse input changes for private public const
This commit is contained in:
parent
ffa2705a4e
commit
f6aa32da35
@ -21,6 +21,9 @@ use crate::{Expression, Identifier, Type};
|
||||
/// Definitions should be structured as: `<name>: <type_> = <value>;`
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct Definition {
|
||||
pub const_: bool,
|
||||
pub private: bool,
|
||||
pub public: bool,
|
||||
pub type_: Type,
|
||||
pub name: Identifier,
|
||||
pub value: Expression,
|
||||
|
@ -21,7 +21,6 @@ use super::*;
|
||||
pub struct ProgramInput {
|
||||
pub main: Definitions,
|
||||
pub registers: Definitions,
|
||||
pub constants: Definitions,
|
||||
}
|
||||
|
||||
impl TryFrom<ParsedInputFile> for ProgramInput {
|
||||
@ -29,20 +28,15 @@ impl TryFrom<ParsedInputFile> for ProgramInput {
|
||||
fn try_from(input: ParsedInputFile) -> Result<Self> {
|
||||
let mut main = IndexMap::new();
|
||||
let mut registers = IndexMap::new();
|
||||
let mut constants = IndexMap::new();
|
||||
|
||||
for section in input.sections {
|
||||
let target = match section.name {
|
||||
sym::main => &mut main,
|
||||
sym::registers => &mut registers,
|
||||
sym::constants => &mut constants,
|
||||
_ => {
|
||||
return Err(InputError::unexpected_section(
|
||||
&["main", "registers", "constants"],
|
||||
section.name,
|
||||
§ion.span,
|
||||
return Err(
|
||||
InputError::unexpected_section(&["main", "registers"], section.name, §ion.span).into(),
|
||||
)
|
||||
.into())
|
||||
}
|
||||
};
|
||||
|
||||
@ -54,10 +48,6 @@ impl TryFrom<ParsedInputFile> for ProgramInput {
|
||||
}
|
||||
}
|
||||
|
||||
Ok(ProgramInput {
|
||||
main,
|
||||
registers,
|
||||
constants,
|
||||
})
|
||||
Ok(ProgramInput { main, registers })
|
||||
}
|
||||
}
|
||||
|
@ -66,6 +66,16 @@ impl ParserContext<'_> {
|
||||
/// `<identifier> : <type> = <expression>;`
|
||||
/// Returns [`Definition`].
|
||||
pub fn parse_input_definition(&mut self) -> Result<Definition> {
|
||||
let const_ = self.eat(Token::Const).is_some();
|
||||
let private = self.eat(Token::Private).is_some();
|
||||
let public = self.eat(Token::Public).is_some();
|
||||
|
||||
match (const_, private, public) {
|
||||
(true, false, false) | (false, true, false) | (false, false, true) => {}
|
||||
(false, false, false) => return Err(ParserError::inputs_no_variable_type_specified().into()),
|
||||
_ => return Err(ParserError::inputs_multpe_variable_types_specified().into()),
|
||||
}
|
||||
|
||||
let name = self.expect_ident()?;
|
||||
self.expect(Token::Colon)?;
|
||||
let (type_, span) = self.parse_type()?;
|
||||
@ -74,6 +84,9 @@ impl ParserContext<'_> {
|
||||
self.expect(Token::Semicolon)?;
|
||||
|
||||
Ok(Definition {
|
||||
const_,
|
||||
private,
|
||||
public,
|
||||
name,
|
||||
type_,
|
||||
value,
|
||||
|
@ -358,4 +358,20 @@ create_errors!(
|
||||
msg: format!("Found the char `{}`, but expected `{}`", found, expected),
|
||||
help: None,
|
||||
}
|
||||
|
||||
/// For when a user does not specify a type of input.
|
||||
@backtraced
|
||||
inputs_no_variable_type_specified {
|
||||
args: (),
|
||||
msg: "An input must be either const, private, or public.",
|
||||
help: None,
|
||||
}
|
||||
|
||||
/// For when a user specified more than a type of input.
|
||||
@backtraced
|
||||
inputs_multpe_variable_types_specified {
|
||||
args: (),
|
||||
msg: "An input can only be one of const, private, or public.",
|
||||
help: None,
|
||||
}
|
||||
);
|
||||
|
Loading…
Reference in New Issue
Block a user