mirror of
https://github.com/danog/parser.git
synced 2024-11-30 04:29:13 +01:00
Merge pull request #55 from ryangjchandler/feature/global-statement
This commit is contained in:
commit
980b1c1d9c
@ -828,6 +828,7 @@ impl Lexer {
|
||||
#[allow(dead_code)]
|
||||
fn identifier_to_keyword(ident: &str) -> Option<TokenKind> {
|
||||
Some(match ident {
|
||||
"global" => TokenKind::Global,
|
||||
"match" => TokenKind::Match,
|
||||
"abstract" => TokenKind::Abstract,
|
||||
"array" => TokenKind::Array,
|
||||
|
@ -9,6 +9,7 @@ pub enum OpenTagKind {
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub enum TokenKind {
|
||||
Global,
|
||||
Abstract,
|
||||
Ampersand,
|
||||
And,
|
||||
@ -300,6 +301,7 @@ impl Display for TokenKind {
|
||||
Self::Variable(var) => &var[..],
|
||||
Self::Yield => "yield",
|
||||
Self::While => "while",
|
||||
Self::Global => "global",
|
||||
_ => todo!("format token: {:?}", self),
|
||||
}
|
||||
)
|
||||
|
@ -297,6 +297,9 @@ pub enum Statement {
|
||||
Block {
|
||||
body: Block,
|
||||
},
|
||||
Global {
|
||||
vars: Vec<Identifier>,
|
||||
},
|
||||
Noop,
|
||||
}
|
||||
|
||||
|
@ -169,6 +169,19 @@ impl Parser {
|
||||
self.skip_comments();
|
||||
|
||||
let statement = match &self.current.kind {
|
||||
TokenKind::Global => {
|
||||
self.next();
|
||||
|
||||
let mut vars = vec![];
|
||||
while self.current.kind != TokenKind::SemiColon {
|
||||
vars.push(self.var()?.into());
|
||||
|
||||
self.optional_comma()?;
|
||||
}
|
||||
|
||||
self.semi()?;
|
||||
Statement::Global { vars }
|
||||
}
|
||||
TokenKind::Static if matches!(self.peek.kind, TokenKind::Variable(_)) => {
|
||||
self.next();
|
||||
|
||||
@ -3305,6 +3318,26 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn global_statements() {
|
||||
assert_ast(
|
||||
"<?php global $a;",
|
||||
&[Statement::Global {
|
||||
vars: vec!["a".into()],
|
||||
}],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multiple_global_vars_in_statement() {
|
||||
assert_ast(
|
||||
"<?php global $a, $b;",
|
||||
&[Statement::Global {
|
||||
vars: vec!["a".into(), "b".into()],
|
||||
}],
|
||||
);
|
||||
}
|
||||
|
||||
fn assert_ast(source: &str, expected: &[Statement]) {
|
||||
let mut lexer = Lexer::new(None);
|
||||
let tokens = lexer.tokenize(source).unwrap();
|
||||
|
Loading…
Reference in New Issue
Block a user