Skip to content

Commit

Permalink
bit more cleanup and more test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
minestarks committed Oct 7, 2024
1 parent 38a9916 commit 93c3360
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 263 deletions.
60 changes: 12 additions & 48 deletions compiler/qsc_parse/src/expr/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,19 +348,19 @@ fn lit_int_hexadecimal_dot() {
expr,
"0x123.45",
&expect![[r#"
Error(
Rule(
"identifier",
Int(
Decimal,
),
Span {
lo: 6,
hi: 8,
},
Error(
Rule(
"identifier",
Int(
Decimal,
),
)
"#]],
Span {
lo: 6,
hi: 8,
},
),
)
"#]],
);
}

Expand Down Expand Up @@ -1763,42 +1763,6 @@ fn struct_copy_cons_empty() {
);
}

#[test]
fn struct_recovery() {
check(
expr,
"new Foo. ;",
&expect![[r#"
Expr _id_ [0-9]: Struct (Err IncompletePath [4-9]:
Ident _id_ [4-7] "Foo"): <empty>
[
Error(
Rule(
"identifier",
Semi,
Span {
lo: 9,
hi: 10,
},
),
),
Error(
Token(
Open(
Brace,
),
Semi,
Span {
lo: 9,
hi: 10,
},
),
),
]"#]],
);
}

#[test]
fn call_op_one() {
check(
Expand Down
40 changes: 6 additions & 34 deletions compiler/qsc_parse/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::{
barrier, path, recovering, recovering_path, recovering_semi, recovering_token, shorten,
},
stmt::check_semis,
ty::{array_or_arrow, strict_ty},
ty::array_or_arrow,
ErrorKind,
};
use qsc_ast::ast::{
Expand Down Expand Up @@ -198,7 +198,7 @@ pub fn parse_implicit_namespace(source_name: &str, s: &mut ParserContext) -> Res
/// Invalid or disallowed characters are cleaned up in a best effort manner.
fn source_name_to_namespace_name(raw: &str, span: Span) -> Result<Box<[Ident]>> {
let path = std::path::Path::new(raw);
let mut parts = Vec::new();
let mut namespace = Vec::new();
for component in path.components() {
match component {
std::path::Component::Normal(name) => {
Expand All @@ -216,7 +216,7 @@ fn source_name_to_namespace_name(raw: &str, span: Span) -> Result<Box<[Ident]>>
let mut ident = validate_namespace_name(span, name)?;
ident.span = span;

parts.push(*ident);
namespace.push(*ident);
}
_ => {
return Err(Error::new(ErrorKind::InvalidFileName(
Expand All @@ -227,7 +227,7 @@ fn source_name_to_namespace_name(raw: &str, span: Span) -> Result<Box<[Ident]>>
}
}

Ok(parts.into())
Ok(namespace.into())
}

fn clean_namespace_name(name: &str) -> String {
Expand Down Expand Up @@ -360,35 +360,7 @@ fn parse_newtype(s: &mut ParserContext) -> Result<Box<ItemKind>> {
let name = ident(s)?;
token(s, TokenKind::Eq)?;
let lo = s.peek().span.lo;
let mut def = match parse_ty_def(s) {
Ok(def) => def,
Err(error) if s.peek().span.lo > lo => return Err(error),
Err(error) => {
s.push_error(error);

// Grab the whitespace from the end of the last token until
// the beginning of the next token
let last_hi = s.span(0).hi;
let span = Span {
lo: last_hi,
hi: s.peek().span.lo,
};

Box::new(TyDef {
id: NodeId::default(),
span,
kind: Box::new(TyDefKind::Field(
None,
Box::new(Ty {
id: NodeId::default(),
span,
kind: Box::new(TyKind::Err),
}),
)),
})
}
};

let mut def = parse_ty_def(s)?;
if let Some(ty) = try_tydef_as_ty(def.as_ref()) {
let ty = array_or_arrow(s, ty, lo)?;
def = Box::new(TyDef {
Expand Down Expand Up @@ -456,7 +428,7 @@ fn parse_ty_def(s: &mut ParserContext) -> Result<Box<TyDef>> {
token(s, TokenKind::Close(Delim::Paren))?;
final_sep.reify(defs, TyDefKind::Paren, TyDefKind::Tuple)
} else {
let field_ty = strict_ty(s)?;
let field_ty = ty(s)?;
if token(s, TokenKind::Colon).is_ok() {
let name = ty_as_ident(field_ty)?;
let field_ty = ty(s)?;
Expand Down
115 changes: 0 additions & 115 deletions compiler/qsc_parse/src/item/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,56 +569,6 @@ fn ty_def_named_duplicate_comma() {
);
}

#[test]
fn ty_def_missing_type_with_semi() {
check(
parse,
"newtype Foo = ;",
&expect![[r#"
Item _id_ [0-15]:
New Type (Ident _id_ [8-11] "Foo"): TyDef _id_ [13-14]: Field:
Type _id_ [13-14]: Err
[
Error(
Rule(
"type",
Semi,
Span {
lo: 14,
hi: 15,
},
),
),
]"#]],
);
}

#[test]
fn ty_def_missing_type_no_semi() {
check(
parse,
"newtype Foo = ",
&expect![[r#"
Item _id_ [0-13]:
New Type (Ident _id_ [8-11] "Foo"): TyDef _id_ [13-14]: Field:
Type _id_ [13-14]: Err
[
Error(
Rule(
"type",
Eof,
Span {
lo: 14,
hi: 14,
},
),
),
]"#]],
);
}

#[test]
fn function_decl() {
check(
Expand Down Expand Up @@ -995,71 +945,6 @@ fn function_missing_output_ty() {
);
}

#[test]
fn function_missing_output_ty_after_colon() {
check(
parse,
"function Foo() : { body intrinsic; }",
&expect![[r#"
Item _id_ [0-36]:
Callable _id_ [0-36] (Function):
name: Ident _id_ [9-12] "Foo"
input: Pat _id_ [12-14]: Unit
output: Type _id_ [16-17]: Err
body: Specializations:
SpecDecl _id_ [19-34] (Body): Gen: Intrinsic
[
Error(
Rule(
"type",
Open(
Brace,
),
Span {
lo: 17,
hi: 18,
},
),
),
]"#]],
);
}

#[test]
fn function_missing_input_ty_after_colon() {
check(
parse,
"function Foo(x : ) : Unit { body intrinsic; }",
&expect![[r#"
Item _id_ [0-46]:
Callable _id_ [0-46] (Function):
name: Ident _id_ [9-12] "Foo"
input: Pat _id_ [12-19]: Paren:
Pat _id_ [13-16]: Bind:
Ident _id_ [13-14] "x"
Type _id_ [16-18]: Err
output: Type _id_ [22-26]: Path: Path _id_ [22-26] (Ident _id_ [22-26] "Unit")
body: Specializations:
SpecDecl _id_ [29-44] (Body): Gen: Intrinsic
[
Error(
Rule(
"type",
Close(
Paren,
),
Span {
lo: 18,
hi: 19,
},
),
),
]"#]],
);
}

#[test]
fn internal_ty() {
check(
Expand Down
25 changes: 10 additions & 15 deletions compiler/qsc_parse/src/prim/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,22 +260,17 @@ fn pat_missing_ty() {
pat,
"foo :",
&expect![[r#"
Pat _id_ [0-5]: Bind:
Ident _id_ [0-3] "foo"
Type _id_ [5-5]: Err
[
Error(
Rule(
"type",
Eof,
Span {
lo: 5,
hi: 5,
},
),
Error(
Rule(
"type",
Eof,
Span {
lo: 5,
hi: 5,
},
),
]"#]],
)
"#]],
);
}

Expand Down
29 changes: 1 addition & 28 deletions compiler/qsc_parse/src/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,9 @@ use crate::{
use qsc_ast::ast::{
CallableKind, Functor, FunctorExpr, FunctorExprKind, Ident, NodeId, SetOp, Ty, TyKind,
};
use qsc_data_structures::span::Span;

pub(super) fn ty(s: &mut ParserContext) -> Result<Ty> {
s.expect(WordKinds::PathTy);
if let Some(ty) = opt(s, strict_ty)? {
Ok(ty)
} else {
s.push_error(Error::new(ErrorKind::Rule(
"type",
s.peek().kind,
s.peek().span,
)));

// Grab the whitespace from the end of the last token until
// the beginning of the next token
let last_hi = s.span(0).hi;
let span = Span {
lo: last_hi,
hi: s.peek().span.lo,
};

Ok(Ty {
id: NodeId::default(),
span,
kind: Box::new(TyKind::Err),
})
}
}

pub(super) fn strict_ty(s: &mut ParserContext) -> Result<Ty> {
let lo = s.peek().span.lo;
let lhs = base(s)?;
array_or_arrow(s, lhs, lo)
Expand Down Expand Up @@ -122,7 +95,7 @@ fn base(s: &mut ParserContext) -> Result<Ty> {
} else if let Some(path) = opt(s, |s| recovering_path(s, WordKinds::PathTy))? {
Ok(TyKind::Path(path))
} else if token(s, TokenKind::Open(Delim::Paren)).is_ok() {
let (tys, final_sep) = seq(s, strict_ty)?;
let (tys, final_sep) = seq(s, ty)?;
token(s, TokenKind::Close(Delim::Paren))?;
Ok(final_sep.reify(tys, |t| TyKind::Paren(Box::new(t)), TyKind::Tuple))
} else {
Expand Down
23 changes: 0 additions & 23 deletions compiler/qsc_parse/src/ty/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,29 +397,6 @@ fn ty_incomplete() {
);
}

#[test]
fn ty_missing() {
check(
ty,
" ",
&expect![[r#"
Type _id_ [0-2]: Err
[
Error(
Rule(
"type",
Eof,
Span {
lo: 2,
hi: 2,
},
),
),
]"#]],
);
}

#[test]
fn ty_incomplete_in_tuple() {
check(
Expand Down
Loading

0 comments on commit 93c3360

Please sign in to comment.