parse input changes for private public const

This commit is contained in:
gluax 2022-04-04 14:51:46 -07:00
parent ffa2705a4e
commit f6aa32da35
4 changed files with 35 additions and 13 deletions

View File

@ -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,

View File

@ -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,
&section.span,
return Err(
InputError::unexpected_section(&["main", "registers"], section.name, &section.span).into(),
)
.into())
}
};
@ -54,10 +48,6 @@ impl TryFrom<ParsedInputFile> for ProgramInput {
}
}
Ok(ProgramInput {
main,
registers,
constants,
})
Ok(ProgramInput { main, registers })
}
}

View File

@ -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,

View File

@ -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,
}
);