Skip to content
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
2 changes: 2 additions & 0 deletions src/parser/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub enum ParseError {
NestedNamespaceDeclarations(Span),
ForbiddenTypeUsedInProperty(String, String, Type, Span),
MatchExpressionWithMultipleDefaultArms(Span),
CannotFindTypeInCurrentScope(String, Span),
}

impl Display for ParseError {
Expand Down Expand Up @@ -80,6 +81,7 @@ impl Display for ParseError {
Self::UnpredictableState(span) => write!(f, "Parse Error: Reached an unpredictable state on line {} column {}", span.0, span.1),
Self::ForbiddenTypeUsedInProperty(class, prop, ty, span) => write!(f, "Parse Error: Property {}::${} cannot have type `{}` on line {} column {}", class, prop, ty, span.0, span.1),
Self::MatchExpressionWithMultipleDefaultArms(span) => write!(f, "Parse Error: Match expressions may only contain one default arm on line {} column {}", span.0, span.1),
Self::CannotFindTypeInCurrentScope(ty, span) => write!(f, "Parse Error: Cannot find type `{}` in this scope on line {} on column {}", ty, span.0, span.1),
}
}
}
92 changes: 50 additions & 42 deletions src/parser/internal/classish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,47 +21,53 @@ impl Parser {

let name = self.ident(state)?;

scoped!(state, Scope::Class(name.clone(), flags.clone()), {
let mut extends: Option<Identifier> = None;
let mut has_parent = false;
let mut extends: Option<Identifier> = None;

if state.current.kind == TokenKind::Extends {
state.next();
extends = Some(self.full_name(state)?.into());
}
if state.current.kind == TokenKind::Extends {
state.next();
extends = Some(self.full_name(state)?.into());
has_parent = true;
}

let implements = if state.current.kind == TokenKind::Implements {
state.next();
scoped!(
state,
Scope::Class(name.clone(), flags.clone(), has_parent),
{
let implements = if state.current.kind == TokenKind::Implements {
state.next();

self.at_least_one_comma_separated::<Identifier>(state, &|parser, state| {
Ok(parser.full_name(state)?.into())
})?
} else {
Vec::new()
};
self.at_least_one_comma_separated::<Identifier>(state, &|parser, state| {
Ok(parser.full_name(state)?.into())
})?
} else {
Vec::new()
};

self.lbrace(state)?;
self.lbrace(state)?;

let mut body = Vec::new();
while state.current.kind != TokenKind::RightBrace {
state.gather_comments();
let mut body = Vec::new();
while state.current.kind != TokenKind::RightBrace {
state.gather_comments();

if state.current.kind == TokenKind::RightBrace {
state.clear_comments();
break;
if state.current.kind == TokenKind::RightBrace {
state.clear_comments();
break;
}

body.push(self.class_like_statement(state)?);
}
self.rbrace(state)?;

body.push(self.class_like_statement(state)?);
Ok(Statement::Class {
name: name.into(),
extends,
implements,
body,
flags,
})
}
self.rbrace(state)?;

Ok(Statement::Class {
name: name.into(),
extends,
implements,
body,
flags,
})
})
)
}

pub(in crate::parser) fn interface_definition(
Expand Down Expand Up @@ -140,20 +146,22 @@ impl Parser {
expect_token!([TokenKind::New], state, ["`new`"]);
expect_token!([TokenKind::Class], state, ["`class`"]);

scoped!(state, Scope::AnonymousClass, {
let mut args = vec![];
let mut args = vec![];

if state.current.kind == TokenKind::LeftParen {
args = self.args_list(state)?;
}
if state.current.kind == TokenKind::LeftParen {
args = self.args_list(state)?;
}

let mut extends: Option<Identifier> = None;
let mut has_parent = false;
let mut extends: Option<Identifier> = None;

if state.current.kind == TokenKind::Extends {
state.next();
extends = Some(self.full_name(state)?.into());
}
if state.current.kind == TokenKind::Extends {
state.next();
extends = Some(self.full_name(state)?.into());
has_parent = true;
}

scoped!(state, Scope::AnonymousClass(has_parent), {
let mut implements = Vec::new();
if state.current.kind == TokenKind::Implements {
state.next();
Expand Down
6 changes: 2 additions & 4 deletions src/parser/internal/classish_statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,8 @@ impl Parser {
}

let class_name: String = expected_scope!([
Scope::Class(name, _) => state.named(&name),
Scope::Trait(name) => state.named(&name),
Scope::AnonymousClass => state.named(&"class@anonymous".into()),
Scope::Trait(name) | Scope::Class(name, _, _) => state.named(&name),
Scope::AnonymousClass(_) => state.named(&"class@anonymous".into()),
], state);

if flags.contains(&PropertyFlag::Readonly) {
Expand All @@ -158,7 +157,6 @@ impl Parser {
}

if value.is_some() {

return Err(ParseError::ReadonlyPropertyHasDefaultValue(class_name, var.to_string(), state.current.span));
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/parser/internal/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ impl Parser {
let name = self.ident_maybe_reserved(state)?;

let has_body = expected_scope!([
Scope::Class(_, cf) => {
Scope::Class(_, cf, _) => {
if !cf.contains(&ClassFlag::Abstract) && flags.contains(&MethodFlag::Abstract) {
return Err(ParseError::AbstractModifierOnNonAbstractClassMethod(
state.current.span,
Expand All @@ -220,7 +220,7 @@ impl Parser {

true
},
Scope::AnonymousClass => true,
Scope::AnonymousClass(_) => true,
], state);

scoped!(state, Scope::Method(name.clone(), flags.clone()), {
Expand Down
4 changes: 2 additions & 2 deletions src/parser/internal/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ impl Parser {
// can only have abstract ctor
Scope::Interface(_) => 1,
// can only have concret ctor
Scope::AnonymousClass => {
Scope::AnonymousClass(_) => {
class_name = state.named(&"class@anonymous".into());

2
}
// can have either abstract or concret ctor,
// depens on method flag.
Scope::Class(name, _) | Scope::Trait(name) => {
Scope::Class(name, _, _) | Scope::Trait(name) => {
if flags.contains(&MethodFlag::Abstract) {
1
} else {
Expand Down
Loading