Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parsing logic for field access #188

Merged
merged 2 commits into from
May 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/parser/box_construct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@ impl BoxConstruct {
box_construct! {mock_declaration}
box_construct! {incl}
box_construct! {method_call}
box_construct! {field_access}
}
52 changes: 52 additions & 0 deletions src/parser/constructs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ impl Construct {
let (input, value) = alt((
Construct::binary_op,
BoxConstruct::method_call,
BoxConstruct::field_access,
BoxConstruct::function_declaration,
BoxConstruct::type_declaration,
BoxConstruct::ext_declaration,
Expand Down Expand Up @@ -765,6 +766,20 @@ impl Construct {
))(input)
}

/// Parse a viable instance for a field access.
/// This is similar to Construct::method_caller, without allowing constants, as they
/// contain no fields.
fn instance(input: &str) -> ParseResult<Box<dyn Instruction>> {
alt((
BoxConstruct::function_call,
BoxConstruct::variable,
BoxConstruct::if_else,
BoxConstruct::block,
BoxConstruct::any_loop,
BoxConstruct::jinko_inst,
))(input)
}

/// Parse a method like function call, that shall be desugared
/// to a simple function call later on
///
Expand All @@ -776,6 +791,18 @@ impl Construct {

Ok((input, MethodCall::new(caller, method)))
}

/// Parse a field access on a custom type. This is very similar to a method call: The
/// only difference is that the method call shall have parentheses
///
/// `<identifier>.<identifier>`
pub fn field_access(input: &str) -> ParseResult<Var /* FIXME */> {
let (input, _instance) = Construct::instance(input)?;
let (input, _) = Token::dot(input)?;
let (input, _field) = Token::identifier(input)?;

Ok((input, Var::new(String::from(_field) /* FIXME */)))
}
}

#[cfg(test)]
Expand Down Expand Up @@ -1539,4 +1566,29 @@ mod tests {
assert_eq!(Construct::instruction("1 2").unwrap().0, "2");
assert_eq!(Construct::instruction("a b").unwrap().0, "b");
}

#[test]
fn t_field_access_valid() {
assert!(Construct::field_access("s.a").is_ok());
assert!(Construct::field_access("longer_identifier.a").is_ok());
assert!(Construct::field_access("longer_identifier.with_numbers32").is_ok());
}

#[test]
fn t_field_access_with_constant_field() {
assert!(Construct::field_access("s.1").is_err());
assert!(Construct::field_access("a.\"string\"").is_err());
}

#[test]
fn t_field_access_invalid_with_constant_instance() {
assert!(Construct::field_access("1.a").is_err());
assert!(Construct::field_access("\"string\".a").is_err());
}

#[test]
fn t_field_access_with_spaces() {
assert!(Construct::field_access("sdot. space").is_err());
assert!(Construct::field_access("s .dotspace").is_err());
}
}