Skip to content

Commit

Permalink
Merge pull request #207 from jrandolf/jrandolf/sub-parse
Browse files Browse the repository at this point in the history
Fix inline group parsing
  • Loading branch information
anweiss authored Aug 22, 2023
2 parents a9a87c0 + 1359a80 commit 606bd0e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 12 deletions.
53 changes: 41 additions & 12 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2273,7 +2273,12 @@ impl<'a> Parser<'a> {
if self.groupnames.contains(name.ident) || matches!(name.socket, Some(SocketPlug::GROUP))
{
if name.socket.is_none() {
self.unknown_rule_idents.pop();
self.unknown_rule_idents = self
.unknown_rule_idents
.clone()
.into_iter()
.filter(|(ident, _)| ident != &name.ident)
.collect();
}
return Ok(GroupEntry::TypeGroupname {
ge: TypeGroupnameEntry {
Expand All @@ -2294,8 +2299,13 @@ impl<'a> Parser<'a> {
if let Some((name, generic_args)) = entry_type.groupname_entry() {
if self.groupnames.contains(name.ident) || matches!(name.socket, Some(SocketPlug::GROUP))
{
if name.socket == None {
self.unknown_rule_idents.pop();
if name.socket.is_none() {
self.unknown_rule_idents = self
.unknown_rule_idents
.clone()
.into_iter()
.filter(|ident| ident != &name.ident)
.collect();
}
return Ok(GroupEntry::TypeGroupname {
ge: TypeGroupnameEntry {
Expand Down Expand Up @@ -2433,7 +2443,12 @@ impl<'a> Parser<'a> {
}

if name.socket.is_none() {
self.unknown_rule_idents.pop();
self.unknown_rule_idents = self
.unknown_rule_idents
.clone()
.into_iter()
.filter(|(ident, _)| ident != &name.ident)
.collect();
}
return Ok(GroupEntry::TypeGroupname {
ge: TypeGroupnameEntry {
Expand Down Expand Up @@ -2462,8 +2477,13 @@ impl<'a> Parser<'a> {
self.next_token()?;
}

if name.socket == None {
self.unknown_rule_idents.pop();
if name.socket.is_none() {
self.unknown_rule_idents = self
.unknown_rule_idents
.clone()
.into_iter()
.filter(|ident| ident != &name.ident)
.collect();
}
return Ok(GroupEntry::TypeGroupname {
ge: TypeGroupnameEntry {
Expand Down Expand Up @@ -2913,20 +2933,27 @@ impl<'a> Parser<'a> {
}
}

// Create a new parser for the previously-lexed tokens.
let mut parser = Parser::new(self.str_input, Box::new(tokens.into_iter()))?;
parser.groupnames = self.groupnames.clone();
parser.typenames = self.typenames.clone();

// Parse tokens vec as group
if has_group_entries {
let mut p = Parser::new(self.str_input, Box::new(tokens.into_iter()))?;
let group = match p.parse_group() {
let group = match parser.parse_group() {
Ok(g) => g,
Err(Error::INCREMENTAL) => {
for e in p.errors.into_iter() {
for e in parser.errors.into_iter() {
self.errors.push(e);
}

return Err(Error::INCREMENTAL);
}
Err(e) => return Err(e),
};
self
.unknown_rule_idents
.append(&mut parser.unknown_rule_idents);

return Ok(Some(MemberKey::NonMemberKey {
non_member_key: NonMemberKey::Group(group),
Expand All @@ -2938,18 +2965,20 @@ impl<'a> Parser<'a> {
}

// Parse tokens vec as type
let mut p = Parser::new(self.str_input, Box::new(tokens.into_iter()))?;
let t = match p.parse_type(None) {
let t = match parser.parse_type(None) {
Ok(t) => t,
Err(Error::INCREMENTAL) => {
for e in p.errors.into_iter() {
for e in parser.errors.into_iter() {
self.errors.push(e);
}

return Err(Error::INCREMENTAL);
}
Err(e) => return Err(e),
};
self
.unknown_rule_idents
.append(&mut parser.unknown_rule_idents);

#[cfg(feature = "ast-comments")]
let comments_before_cut = self.collect_comments()?;
Expand Down
12 changes: 12 additions & 0 deletions tests/fixtures/cddl/arrays.cddl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
foo = {
p1: "foo",
(bar // baz)
}

bar = (
p2: "bar"
)

baz = (
p3: "baz"
)

0 comments on commit 606bd0e

Please sign in to comment.