Changeset - 5c3630dcfeb6
[Not reviewed]
0 3 0
mh - 4 years ago 2021-12-11 18:02:27
contact@maxhenger.nl
Add some tests for tuples, fix some initial bugs
3 files changed with 74 insertions and 4 deletions:
0 comments (0 inline, 0 general)
src/protocol/parser/pass_definitions_types.rs
Show inline comments
 
@@ -88,35 +88,36 @@ impl ParserTypeParser {
 
                if let Some(TokenKind::OpenAngle) = iter.next() {
 
                    self.consume_open_angle(iter);
 
                    ParseState::PolyArgStart
 
                } else {
 
                    ParseState::TypeMaybePolyArgs
 
                }
 
            },
 
            Some(TokenKind::OpenParen) => {
 
                let tuple_start_pos = iter.next_start_position();
 
                self.first_pos = tuple_start_pos; // last pos will be set later, this is a tuple
 

	
 
                let tuple_entry_index = self.entries.len() as u32;
 
                let tuple_depth = self.cur_depth();
 
                self.push_depth(DepthKind::Tuple, tuple_entry_index, tuple_start_pos);
 
                self.entries.push(Entry{
 
                    element: ParserTypeElement{
 
                        element_span: InputSpan::from_positions(tuple_start_pos, tuple_start_pos),
 
                        variant: ParserTypeVariant::Tuple(0),
 
                    },
 
                    depth: self.cur_depth(),
 
                    depth: tuple_depth,
 
                });
 
                iter.consume();
 

	
 
                ParseState::PolyArgStart
 
                ParseState::TupleStart
 
            },
 
            _ => return Err(ParseError::new_error_str_at_pos(source, iter.last_valid_pos(), "expected a type")),
 
        };
 

	
 
        self.parse_state = initial_state;
 

	
 
        // Depth stack and entries are initialized, continue until depth stack
 
        // is empty, or until an unexpected set of tokens is encountered
 
        while !self.depths.is_empty() {
 
            let next = iter.next();
 

	
 
            match self.parse_state {
 
@@ -222,25 +223,24 @@ impl ParserTypeParser {
 

	
 
            // If we're dealing with a tuple then we don't need to determine if
 
            // the number of embedded types is correct, we simply need to set it
 
            // to whatever what was encountered.
 
            if let ParserTypeVariant::Tuple(_) = cur_element.element.variant {
 
                self.entries[el_index].element.variant = ParserTypeVariant::Tuple(encountered_embedded);
 
            } else {
 
                let expected_embedded = cur_element.element.variant.num_embedded() as u32;
 
                if expected_embedded != encountered_embedded {
 
                    if encountered_embedded == 0 {
 
                        // Every polymorphic argument should be inferred
 
                        if !allow_inference {
 
                            println!("DEBUG: Ended up with {:?}", self.entries);
 
                            return Err(ParseError::new_error_str_at_span(
 
                                source, cur_element.element.element_span,
 
                                "type inference is not allowed here"
 
                            ));
 
                        }
 

	
 
                        // Insert missing types
 
                        let inserted_span = cur_element.element.element_span;
 
                        let inserted_depth = cur_element.depth + 1;
 
                        self.entries.reserve(expected_embedded as usize);
 
                        for _ in 0..expected_embedded {
 
                            self.entries.insert(el_index + 1, Entry {
 
@@ -258,25 +258,29 @@ impl ParserTypeParser {
 
                            expected_embedded, encountered_embedded
 
                        ));
 
                    }
 
                }
 
            }
 
        }
 

	
 
        // Convert the results from parsing into the `ParserType`
 
        let mut elements = Vec::with_capacity(self.entries.len());
 
        debug_assert!(!self.entries.is_empty());
 

	
 
        for entry in self.entries.drain(..) {
 
            elements.push(entry.element)
 
            if ParserTypeVariant::Tuple(1) == entry.element.variant {
 
                // We strip these ones
 
            } else {
 
                elements.push(entry.element);
 
            }
 
        }
 

	
 
        return Ok(ParserType{
 
            elements,
 
            full_span: InputSpan::from_positions(self.first_pos, self.last_pos),
 
        });
 
    }
 

	
 
    // --- Parsing Utilities
 

	
 
    #[inline]
 
    fn consume_type_idents(
src/protocol/parser/type_table.rs
Show inline comments
 
@@ -1487,24 +1487,25 @@ impl TypeTable {
 
                PTV::UInt16    => Some(CTP::UInt16),
 
                PTV::UInt32    => Some(CTP::UInt32),
 
                PTV::UInt64    => Some(CTP::UInt64),
 
                PTV::SInt8     => Some(CTP::SInt8),
 
                PTV::SInt16    => Some(CTP::SInt16),
 
                PTV::SInt32    => Some(CTP::SInt32),
 
                PTV::SInt64    => Some(CTP::SInt64),
 
                PTV::Character => Some(CTP::Character),
 
                PTV::String    => Some(CTP::String),
 
                PTV::Array     => Some(CTP::Array),
 
                PTV::Input     => Some(CTP::Input),
 
                PTV::Output    => Some(CTP::Output),
 
                PTV::Tuple(num) => Some(CTP::Tuple(*num)),
 
                PTV::Definition(definition_id, num) => Some(CTP::Instance(*definition_id, *num)),
 
                _              => None
 
            }
 
        }
 

	
 
        let mut parts = Vec::with_capacity(member_type.elements.len()); // usually a correct estimation, might not be
 
        for member_part in &member_type.elements {
 
            // Check if we have a regular builtin type
 
            if let Some(part) = parser_to_concrete_part(&member_part.variant) {
 
                parts.push(part);
 
                continue;
 
            }
src/protocol/tests/parser_validation.rs
Show inline comments
 
@@ -343,24 +343,89 @@ fn test_incorrect_union_instance() {
 
        union Foo{ A(s32) }
 
        func bar() -> Foo { return Foo::A(false); }
 
        "
 
    ).error(|e| { e
 
        .assert_occurs_at(0, "Foo::A")
 
        .assert_msg_has(0, "failed to fully resolve")
 
        .assert_occurs_at(1, "false")
 
        .assert_msg_has(1, "has been resolved to 's32'")
 
        .assert_msg_has(1, "has been resolved to 'bool'");
 
    });
 
}
 

	
 
#[test]
 
fn test_correct_tuple_members() {
 
    // Tuples with zero members
 
    Tester::new_single_source_expect_ok(
 
        "single zero-tuple",
 
        "struct Foo{ () bar }"
 
    ).for_struct("Foo", |s| { s
 
        .for_field("bar", |f| { f.assert_parser_type("()"); })
 
        .assert_size_alignment("Foo", 0, 1);
 
    });
 

	
 
    Tester::new_single_source_expect_ok(
 
        "triple zero-tuple",
 
        "struct Foo{ () bar, () baz, () qux }"
 
    ).for_struct("Foo", |s| { s
 
        .assert_size_alignment("Foo", 0, 1);
 
    });
 

	
 
    // Tuples with one member (which are elided, because due to ambiguity
 
    // between a one-tuple literal and a parenthesized expression, we're not
 
    // going to be able to construct one-tuples).
 
    Tester::new_single_source_expect_ok(
 
        "single elided one-tuple",
 
        "struct Foo{ (u32) bar }"
 
    ).for_struct("Foo", |s| { s
 
        .for_field("bar", |f| { f.assert_parser_type("u32"); })
 
        .assert_size_alignment("Foo", 4, 4);
 
    });
 

	
 
    Tester::new_single_source_expect_ok(
 
        "triple elided one-tuple",
 
        "struct Foo{ (u8) bar, (u16) baz, (u32) qux }"
 
    ).for_struct("Foo", |s| { s
 
        .assert_size_alignment("Foo", 8, 4);
 
    });
 

	
 
    // Tuples with three members
 
    Tester::new_single_source_expect_ok(
 
        "single three-tuple",
 
        "struct Foo{ (u8, u16, u32) bar }"
 
    ).for_struct("Foo", |s| { s
 
        .for_field("bar", |f| { f.assert_parser_type("(u8,u16,u32)"); })
 
        .assert_size_alignment("Foo", 8, 4);
 
    });
 

	
 
    Tester::new_single_source_expect_ok(
 
        "double three-tuple",
 
        "struct Foo{ (u8,u16,u32,) bar, (s8,s16,s32,) baz }"
 
    ).for_struct("Foo", |s| { s
 
        .for_field("bar", |f| { f.assert_parser_type("(u8,u16,u32)"); })
 
        .for_field("baz", |f| { f.assert_parser_type("(s8,s16,s32)"); })
 
        .assert_size_alignment("Foo", 16, 4);
 
    });
 
}
 

	
 
#[test]
 
fn test_correct_tuple_polymorph_args() {
 
    todo!("write");
 
}
 

	
 
#[test]
 
fn test_incorrect_tuple_polymorph_args() {
 
    todo!("write");
 
}
 

	
 
#[test]
 
fn test_polymorph_array_types() {
 
    Tester::new_single_source_expect_ok(
 
        "array of polymorph in struct",
 
        "
 
        struct Foo<T> { T[] hello }
 
        struct Bar { Foo<u32>[] world }
 
        "
 
    ).for_struct("Bar", |s| { s
 
        .for_field("world", |f| { f.assert_parser_type("Foo<u32>[]"); });
 
    });
 

	
0 comments (0 inline, 0 general)