parser: basic support for trait uses in classes

This commit is contained in:
Ryan Chandler 2022-07-25 17:01:36 +01:00
parent a9ced4460a
commit d20e35d1a8
No known key found for this signature in database
GPG Key ID: F113BCADDB3B0CCA
3 changed files with 32 additions and 0 deletions

View File

@ -0,0 +1,9 @@
<?php
trait Foo {
}
class Bar {
use Foo;
}

View File

@ -113,6 +113,8 @@ impl From<TokenKind> for ClassFlag {
}
}
#[derive(Debug, PartialEq, Clone, Serialize)]
pub enum Statement {
InlineHtml(String),
@ -150,6 +152,9 @@ pub enum Statement {
name: Identifier,
body: Block,
},
TraitUse {
traits: Vec<Identifier>,
},
Interface {
name: Identifier,
extends: Vec<Identifier>,

View File

@ -483,6 +483,24 @@ impl Parser {
fn class_statement(&mut self) -> ParseResult<Statement> {
match self.current.kind {
TokenKind::Use => {
self.next();
let mut traits = Vec::new();
while self.current.kind != TokenKind::SemiColon {
if self.current.kind == TokenKind::Comma {
self.next();
}
let t = expect!(self, TokenKind::Identifier(i) | TokenKind::QualifiedIdentifier(i) | TokenKind::FullyQualifiedIdentifier(i), i, "expected identifier");
traits.push(t.into());
}
expect!(self, TokenKind::SemiColon, "expected semi-colon");
Ok(Statement::TraitUse { traits })
},
TokenKind::Const => {
self.next();