Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Structs #29

Merged
merged 3 commits into from
Jan 28, 2018
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
5 changes: 4 additions & 1 deletion grammar.ron
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,8 @@ Grammar(
"LITERAL",
"ALIAS",
"VISIBILITY",
"TYPE_PARAM_LIST",
"LIFETIME_PARAM",
"TYPE_PARAM",
]
)
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use super::*;

mod structs;

pub(super) fn mod_contents(p: &mut Parser, stop_on_r_curly: bool) {
attributes::inner_attributes(p);
while !p.at(EOF) && !(stop_on_r_curly && p.at(R_CURLY)) {
Expand Down Expand Up @@ -29,7 +31,7 @@ fn item(p: &mut Parser) {
USE_ITEM
}
STRUCT_KW => {
struct_item(p);
structs::struct_item(p);
STRUCT_ITEM
}
FN_KW => {
Expand Down Expand Up @@ -57,90 +59,50 @@ fn item(p: &mut Parser) {
item.complete(p, item_kind);
}

fn struct_item(p: &mut Parser) {
assert!(p.at(STRUCT_KW));
p.bump();

if !p.expect(IDENT) {
fn type_param_list(p: &mut Parser) {
if !p.at(L_ANGLE) {
return;
}
generic_parameters(p);
match p.current() {
WHERE_KW => {
where_clause(p);
match p.current() {
SEMI => {
p.bump();
return;
}
L_CURLY => named_fields(p),
_ => {
//TODO: special case `(` error message
p.error().message("expected `;` or `{`").emit();
return;
}
}
}
SEMI => {
p.bump();
return;
}
L_CURLY => named_fields(p),
L_PAREN => {
pos_fields(p);
p.expect(SEMI);
}
_ => {
p.error().message("expected `;`, `{`, or `(`").emit();
return;
}
}
}

fn named_fields(p: &mut Parser) {
assert!(p.at(L_CURLY));
let m = p.start();
p.bump();
while !p.at(R_CURLY) && !p.at(EOF) {
named_field(p);
if !p.at(R_CURLY) {
p.expect(COMMA);
}
}
p.expect(R_CURLY);

fn named_field(p: &mut Parser) {
let field = p.start();
visibility(p);
if p.expect(IDENT) {
p.expect(COLON);
types::type_ref(p);
field.complete(p, NAMED_FIELD);
} else {
field.abandon(p);
p.err_and_bump("expected field declaration");
while !p.at(EOF) && !p.at(R_ANGLE) {
match p.current() {
LIFETIME => lifetime_param(p),
IDENT => type_param(p),
_ => p.err_and_bump("expected type parameter"),
}
if !p.at(R_ANGLE) && !p.expect(COMMA) {
break;
}
}
}
p.expect(R_ANGLE);
m.complete(p, TYPE_PARAM_LIST);

fn pos_fields(p: &mut Parser) {
if !p.expect(L_PAREN) {
return;
fn lifetime_param(p: &mut Parser) {
assert!(p.at(LIFETIME));
let m = p.start();
p.bump();
if p.eat(COLON) {
while p.at(LIFETIME) {
p.bump();
if !p.eat(PLUS) {
break;
}
}
}
m.complete(p, LIFETIME_PARAM);
}
while !p.at(R_PAREN) && !p.at(EOF) {
let pos_field = p.start();
visibility(p);
types::type_ref(p);
pos_field.complete(p, POS_FIELD);

if !p.at(R_PAREN) {
p.expect(COMMA);
}
fn type_param(p: &mut Parser) {
assert!(p.at(IDENT));
let m = p.start();
p.bump();
m.complete(p, TYPE_PARAM);
//TODO: bounds
}
p.expect(R_PAREN);
}

fn generic_parameters(_: &mut Parser) {}

fn where_clause(_: &mut Parser) {}

fn extern_crate_item(p: &mut Parser) {
Expand Down
83 changes: 83 additions & 0 deletions src/parser/event_parser/grammar/items/structs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use super::*;

pub(super) fn struct_item(p: &mut Parser) {
assert!(p.at(STRUCT_KW));
p.bump();

if !p.expect(IDENT) {
return;
}
type_param_list(p);
match p.current() {
WHERE_KW => {
where_clause(p);
match p.current() {
SEMI => {
p.bump();
return;
}
L_CURLY => named_fields(p),
_ => {
//TODO: special case `(` error message
p.error().message("expected `;` or `{`").emit();
return;
}
}
}
SEMI => {
p.bump();
return;
}
L_CURLY => named_fields(p),
L_PAREN => {
pos_fields(p);
p.expect(SEMI);
}
_ => {
p.error().message("expected `;`, `{`, or `(`").emit();
return;
}
}
}

fn named_fields(p: &mut Parser) {
assert!(p.at(L_CURLY));
p.bump();
while !p.at(R_CURLY) && !p.at(EOF) {
named_field(p);
if !p.at(R_CURLY) {
p.expect(COMMA);
}
}
p.expect(R_CURLY);

fn named_field(p: &mut Parser) {
let field = p.start();
visibility(p);
if p.expect(IDENT) {
p.expect(COLON);
types::type_ref(p);
field.complete(p, NAMED_FIELD);
} else {
field.abandon(p);
p.err_and_bump("expected field declaration");
}
}
}

fn pos_fields(p: &mut Parser) {
if !p.expect(L_PAREN) {
return;
}
while !p.at(R_PAREN) && !p.at(EOF) {
let pos_field = p.start();
visibility(p);
types::type_ref(p);
pos_field.complete(p, POS_FIELD);

if !p.at(R_PAREN) {
p.expect(COMMA);
}
}
p.expect(R_PAREN);
}
6 changes: 6 additions & 0 deletions src/syntax_kinds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ pub enum SyntaxKind {
LITERAL,
ALIAS,
VISIBILITY,
TYPE_PARAM_LIST,
LIFETIME_PARAM,
TYPE_PARAM,

// Technical SyntaxKinds: they appear temporally during parsing,
// but never end up in the final tree
Expand Down Expand Up @@ -187,6 +190,9 @@ impl SyntaxKind {
LITERAL => &SyntaxInfo { name: "LITERAL" },
ALIAS => &SyntaxInfo { name: "ALIAS" },
VISIBILITY => &SyntaxInfo { name: "VISIBILITY" },
TYPE_PARAM_LIST => &SyntaxInfo { name: "TYPE_PARAM_LIST" },
LIFETIME_PARAM => &SyntaxInfo { name: "LIFETIME_PARAM" },
TYPE_PARAM => &SyntaxInfo { name: "TYPE_PARAM" },

TOMBSTONE => &SyntaxInfo { name: "TOMBSTONE" },
EOF => &SyntaxInfo { name: "EOF" },
Expand Down
5 changes: 5 additions & 0 deletions tests/data/parser/err/0009_broken_struct_type_parameter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
struct S<90 + 2> {
f: u32
}

struct T;
42 changes: 42 additions & 0 deletions tests/data/parser/err/0009_broken_struct_type_parameter.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
FILE@[0; 43)
STRUCT_ITEM@[0; 12)
STRUCT_KW@[0; 6)
WHITESPACE@[6; 7)
IDENT@[7; 8)
TYPE_PARAM_LIST@[8; 12)
L_ANGLE@[8; 9)
ERROR@[9; 12)
err: `expected type parameter`
INT_NUMBER@[9; 11)
WHITESPACE@[11; 12)
err: `expected COMMA`
err: `expected R_ANGLE`
err: `expected `;`, `{`, or `(``
ERROR@[12; 14)
err: `expected item`
PLUS@[12; 13)
WHITESPACE@[13; 14)
ERROR@[14; 15)
err: `expected item`
INT_NUMBER@[14; 15)
ERROR@[15; 17)
err: `expected item`
R_ANGLE@[15; 16)
WHITESPACE@[16; 17)
ERROR@[17; 33)
err: `expected item`
L_CURLY@[17; 18)
WHITESPACE@[18; 23)
IDENT@[23; 24)
COLON@[24; 25)
WHITESPACE@[25; 26)
IDENT@[26; 29)
WHITESPACE@[29; 30)
R_CURLY@[30; 31)
WHITESPACE@[31; 33)
STRUCT_ITEM@[33; 43)
STRUCT_KW@[33; 39)
WHITESPACE@[39; 40)
IDENT@[40; 41)
SEMI@[41; 42)
WHITESPACE@[42; 43)
17 changes: 17 additions & 0 deletions tests/data/parser/ok/0018_struct_type_params.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
struct S1<T>;
struct S2<T>(u32);
struct S3<T> { u: u32 }

struct S4<>;
struct S5<'a>;
struct S6<'a:>;
struct S7<'a: 'b>;
struct S8<'a: 'b + >;
struct S9<'a: 'b + 'c>;
struct S10<'a,>;
struct S11<'a, 'b>;
struct S12<'a: 'b+, 'b: 'c,>;

struct S13<T>;
struct S14<T, U>;
struct S15<'a, T, U>;
Loading