-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
87 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ pub mod contracts; | |
pub mod expressions; | ||
pub mod functions; | ||
pub mod module; | ||
pub mod traits; | ||
pub mod types; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use crate::ast::Trait; | ||
use crate::grammar::functions::parse_single_word_stmt; | ||
use crate::node::{Node, Span}; | ||
use crate::{ParseFailed, ParseResult, Parser, TokenKind}; | ||
|
||
/// Parse a trait definition. | ||
/// # Panics | ||
/// Panics if the next token isn't `trait`. | ||
pub fn parse_trait_def(par: &mut Parser, trait_pub_qual: Option<Span>) -> ParseResult<Node<Trait>> { | ||
let trait_tok = par.assert(TokenKind::Trait); | ||
|
||
// trait Event: | ||
// pass | ||
// | ||
|
||
let trait_name = par.expect_with_notes( | ||
TokenKind::Name, | ||
"failed to parse trait definition", | ||
|_| vec!["Note: `trait` must be followed by a name, which must start with a letter and contain only letters, numbers, or underscores".into()], | ||
)?; | ||
|
||
let header_span = trait_tok.span + trait_name.span; | ||
par.enter_block(header_span, "trait definition")?; | ||
|
||
loop { | ||
match par.peek() { | ||
Some(TokenKind::Pass) => { | ||
parse_single_word_stmt(par)?; | ||
} | ||
Some(TokenKind::Dedent) => { | ||
par.next()?; | ||
break; | ||
} | ||
None => break, | ||
Some(_) => { | ||
let tok = par.next()?; | ||
par.unexpected_token_error( | ||
tok.span, | ||
"failed to parse trait definition body", | ||
vec![], | ||
); | ||
return Err(ParseFailed); | ||
} | ||
}; | ||
} | ||
|
||
let span = header_span + trait_pub_qual; | ||
Ok(Node::new( | ||
Trait { | ||
name: Node::new(trait_name.text.into(), trait_name.span), | ||
pub_qual: trait_pub_qual, | ||
}, | ||
span, | ||
)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
trait Foo: | ||
pass | ||
|
||
contract Meh: | ||
|
||
pub fn bar<T>(x: T) -> bool: | ||
return true |