Skip to content

Commit

Permalink
Implement primitive typenames
Browse files Browse the repository at this point in the history
  • Loading branch information
jrandolf-2 authored and anweiss committed Jul 31, 2023
1 parent ea20e2e commit 69986a1
Showing 1 changed file with 153 additions and 97 deletions.
250 changes: 153 additions & 97 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use super::{
token::{self, SocketPlug, Token},
};

use std::{cmp::Ordering, marker::PhantomData, mem, result};
use std::{cmp::Ordering, collections::HashSet, marker::PhantomData, mem, result};

Check failure on line 11 in src/parser.rs

View workflow job for this annotation

GitHub Actions / compilation-check / Compilation check (stable, ubuntu-latest)

unresolved import `std::collections`

use codespan_reporting::{
diagnostic::{Diagnostic, Label},
Expand All @@ -26,6 +26,7 @@ use std::borrow::Cow;
use alloc::{
borrow::{Cow, ToOwned},
boxed::Box,
collections::HashSet,

Check failure on line 29 in src/parser.rs

View workflow job for this annotation

GitHub Actions / compilation-check / Compilation check (stable, ubuntu-latest)

unresolved import `alloc::collections::HashSet`
string::{String, ToString},
vec::Vec,
};
Expand Down Expand Up @@ -56,6 +57,7 @@ pub struct Parser<'a> {
#[cfg(not(feature = "ast-span"))]
visited_rule_idents: Vec<&'a str>,
current_rule_generic_param_idents: Option<Vec<&'a str>>,
typenames: HashSet<&'a str>,
}

/// Parsing error types
Expand Down Expand Up @@ -120,6 +122,48 @@ impl<'a> Parser<'a> {
parser_position: Position::default(),
visited_rule_idents: Vec::default(),
current_rule_generic_param_idents: None,
typenames: HashSet::from([
"any",
"uint",
"nint",
"int",
"bstr",
"bytes",
"tstr",
"text",
"tdate",
"time",
"number",
"biguint",
"bignint",
"bigint",
"integer",
"unsigned",
"decfrac",
"bigfloat",
"eb64url",
"eb64legacy",
"eb16",
"encoded-cbor",
"uri",
"b64url",
"b64legacy",
"regexp",
"mime-message",
"cbor-any",
"float16",
"float32",
"float64",
"float16-32",
"float32-64",
"float",
"false",
"true",
"bool",
"nil",
"null",
"undefined",
]),
};

p.next_token()?;
Expand Down Expand Up @@ -658,6 +702,7 @@ impl<'a> Parser<'a> {
self.advance_newline()?;

self.current_rule_generic_param_idents = None;
self.typenames.insert(ident.ident);

Ok(Rule::Type {
rule: TypeRule {
Expand Down Expand Up @@ -766,6 +811,7 @@ impl<'a> Parser<'a> {
}

self.current_rule_generic_param_idents = None;
self.typenames.insert(ident.ident);

return Ok(Rule::Type {
rule: TypeRule {
Expand Down Expand Up @@ -809,6 +855,7 @@ impl<'a> Parser<'a> {
}

self.current_rule_generic_param_idents = None;
self.typenames.insert(ident.ident);

return Ok(Rule::Type {
rule: TypeRule {
Expand Down Expand Up @@ -900,6 +947,7 @@ impl<'a> Parser<'a> {
);

self.current_rule_generic_param_idents = None;
self.typenames.insert(ident.ident);

Ok(Rule::Type {
rule: TypeRule {
Expand Down Expand Up @@ -2170,61 +2218,65 @@ impl<'a> Parser<'a> {

#[cfg(feature = "ast-span")]
if let Some((name, generic_args, _)) = entry_type.groupname_entry() {
if name.socket.is_none()
&& token::lookup_ident(name.ident)
.in_standard_prelude()
.is_none()
{
if let Some(params) = &self.current_rule_generic_param_idents {
if !params.iter().any(|&p| p == name.ident) {
if !self.typenames.contains(name.ident) {
if name.socket.is_none()
&& token::lookup_ident(name.ident)
.in_standard_prelude()
.is_none()
{
if let Some(params) = &self.current_rule_generic_param_idents {
if !params.iter().any(|&p| p == name.ident) {
self.visited_rule_idents.push((name.ident, name.span));
}
} else {
self.visited_rule_idents.push((name.ident, name.span));
}
} else {
self.visited_rule_idents.push((name.ident, name.span));
}
}

return Ok(GroupEntry::TypeGroupname {
ge: TypeGroupnameEntry {
occur,
name,
generic_args,
},
#[cfg(feature = "ast-comments")]
leading_comments: comments_before_type_or_group,
#[cfg(feature = "ast-comments")]
trailing_comments,
span,
});
return Ok(GroupEntry::TypeGroupname {
ge: TypeGroupnameEntry {
occur,
name,
generic_args,
},
#[cfg(feature = "ast-comments")]
leading_comments: comments_before_type_or_group,
#[cfg(feature = "ast-comments")]
trailing_comments,
span,
});
}
}

#[cfg(not(feature = "ast-span"))]
if let Some((name, generic_args)) = entry_type.groupname_entry() {
if name.socket.is_none()
&& token::lookup_ident(name.ident)
.in_standard_prelude()
.is_none()
{
if let Some(params) = &self.current_rule_generic_param_idents {
if !params.iter().any(|&p| p == name.ident) {
if !self.typenames.contains(name.ident) {
if name.socket.is_none()
&& token::lookup_ident(name.ident)
.in_standard_prelude()
.is_none()
{
if let Some(params) = &self.current_rule_generic_param_idents {
if !params.iter().any(|&p| p == name.ident) {
self.visited_rule_idents.push(name.ident);
}
} else {
self.visited_rule_idents.push(name.ident);
}
} else {
self.visited_rule_idents.push(name.ident);
}
}

return Ok(GroupEntry::TypeGroupname {
ge: TypeGroupnameEntry {
occur,
name,
generic_args,
},
#[cfg(feature = "ast-comments")]
leading_comments: comments_before_type_or_group,
#[cfg(feature = "ast-comments")]
trailing_comments,
});
return Ok(GroupEntry::TypeGroupname {
ge: TypeGroupnameEntry {
occur,
name,
generic_args,
},
#[cfg(feature = "ast-comments")]
leading_comments: comments_before_type_or_group,
#[cfg(feature = "ast-comments")]
trailing_comments,
});
}
}

// A parse tree that returns a type instead of a member key needs to
Expand Down Expand Up @@ -2406,77 +2458,81 @@ impl<'a> Parser<'a> {

#[cfg(feature = "ast-span")]
if let Some((name, generic_args, _)) = entry_type.groupname_entry() {
if generic_args.is_some() && self.peek_token_is(&Token::LANGLEBRACKET) {
while !self.peek_token_is(&Token::RANGLEBRACKET) {
if !self.typenames.contains(name.ident) {
if generic_args.is_some() && self.peek_token_is(&Token::LANGLEBRACKET) {
while !self.peek_token_is(&Token::RANGLEBRACKET) {
self.next_token()?;
}

self.next_token()?;
}

self.next_token()?;
}

if name.socket.is_none()
&& token::lookup_ident(name.ident)
.in_standard_prelude()
.is_none()
{
if let Some(params) = &self.current_rule_generic_param_idents {
if !params.iter().any(|&p| p == name.ident) {
if name.socket.is_none()
&& token::lookup_ident(name.ident)
.in_standard_prelude()
.is_none()
{
if let Some(params) = &self.current_rule_generic_param_idents {
if !params.iter().any(|&p| p == name.ident) {
self.visited_rule_idents.push((name.ident, name.span));
}
} else {
self.visited_rule_idents.push((name.ident, name.span));
}
} else {
self.visited_rule_idents.push((name.ident, name.span));
}
}

return Ok(GroupEntry::TypeGroupname {
ge: TypeGroupnameEntry {
occur,
name,
generic_args,
},
#[cfg(feature = "ast-comments")]
leading_comments: None,
#[cfg(feature = "ast-comments")]
trailing_comments,
span,
});
return Ok(GroupEntry::TypeGroupname {
ge: TypeGroupnameEntry {
occur,
name,
generic_args,
},
#[cfg(feature = "ast-comments")]
leading_comments: None,
#[cfg(feature = "ast-comments")]
trailing_comments,
span,
});
}
}

#[cfg(not(feature = "ast-span"))]
if let Some((name, generic_args)) = entry_type.groupname_entry() {
if generic_args.is_some() && self.peek_token_is(&Token::LANGLEBRACKET) {
while !self.peek_token_is(&Token::RANGLEBRACKET) {
if !self.typenames.contains(name.ident) {
if generic_args.is_some() && self.peek_token_is(&Token::LANGLEBRACKET) {
while !self.peek_token_is(&Token::RANGLEBRACKET) {
self.next_token()?;
}

self.next_token()?;
}

self.next_token()?;
}

if name.socket.is_none()
&& token::lookup_ident(name.ident)
.in_standard_prelude()
.is_none()
{
if let Some(params) = &self.current_rule_generic_param_idents {
if !params.iter().any(|&p| p == name.ident) {
if name.socket.is_none()
&& token::lookup_ident(name.ident)
.in_standard_prelude()
.is_none()
{
if let Some(params) = &self.current_rule_generic_param_idents {
if !params.iter().any(|&p| p == name.ident) {
self.visited_rule_idents.push(name.ident);
}
} else {
self.visited_rule_idents.push(name.ident);
}
} else {
self.visited_rule_idents.push(name.ident);
}
}

return Ok(GroupEntry::TypeGroupname {
ge: TypeGroupnameEntry {
occur,
name,
generic_args,
},
#[cfg(feature = "ast-comments")]
leading_comments: None,
#[cfg(feature = "ast-comments")]
trailing_comments,
});
return Ok(GroupEntry::TypeGroupname {
ge: TypeGroupnameEntry {
occur,
name,
generic_args,
},
#[cfg(feature = "ast-comments")]
leading_comments: None,
#[cfg(feature = "ast-comments")]
trailing_comments,
});
}
}

#[cfg(feature = "ast-span")]
Expand Down

0 comments on commit 69986a1

Please sign in to comment.