chore: fix readme code

Signed-off-by: azjezz <azjezz@protonmail.com>
This commit is contained in:
azjezz 2022-12-05 20:17:21 +01:00
parent 59fc4af394
commit c25fe105f9
No known key found for this signature in database
GPG Key ID: B00E0A46B3F1C157
6 changed files with 70 additions and 55 deletions

View File

@ -26,13 +26,29 @@ cargo add php-parser-rs
### Example
```rust
use php_parser_rs::*;
use php_parser_rs::prelude::*;
let mut lexer = Lexer::new(None);
let tokens = lexer.tokenize(&source_code[..]).unwrap();
fn main() -> ParseResult<()> {
let lexer = Lexer::new();
let parser = Parser::new();
let mut parser = Parser::new(None);
let ast = parser.parse(tokens).unwrap();
let code = "
<?php
function hello(): void {
echo 'Hello, World!';
}
hello();
";
let tokens = lexer.tokenize(code.as_bytes())?;
let ast = parser.parse(tokens)?;
dbg!(ast);
Ok(())
}
```

View File

@ -1,7 +1,6 @@
use php_parser_rs::prelude::Lexer;
use php_parser_rs::prelude::Parser;
use php_parser_rs::prelude::*;
fn main() {
fn main() -> ParseResult<()> {
let file = match std::env::args().nth(1) {
Some(file) => file,
None => {
@ -21,24 +20,13 @@ fn main() {
};
let lexer = Lexer::new();
let tokens = match lexer.tokenize(contents.as_bytes()) {
Ok(tokens) => tokens,
Err(error) => {
println!("{}", error);
::std::process::exit(1);
}
};
let parser = Parser::new();
let ast = match parser.parse(tokens) {
Ok(ast) => ast,
Err(error) => {
println!("{}", error);
::std::process::exit(1);
}
};
let tokens = lexer.tokenize(contents.as_bytes())?;
let ast = parser.parse(tokens)?;
dbg!(ast);
Ok(())
}

View File

@ -1,5 +1,6 @@
use std::fmt::Display;
use crate::lexer::error::SyntaxError;
use crate::lexer::token::Span;
use crate::parser::ast::Type;
@ -7,6 +8,7 @@ pub type ParseResult<T> = Result<T, ParseError>;
#[derive(Debug, Eq, PartialEq)]
pub enum ParseError {
SyntaxError(SyntaxError),
ExpectedToken(Vec<String>, Option<String>, Span),
MultipleModifiers(String, Span),
MultipleAccessModifiers(Span),
@ -39,9 +41,16 @@ pub enum ParseError {
NestedDisjunctiveNormalFormTypes(Span),
}
impl From<SyntaxError> for ParseError {
fn from(e: SyntaxError) -> Self {
ParseError::SyntaxError(e)
}
}
impl Display for ParseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::SyntaxError(e) => e.fmt(f),
Self::ExpectedToken(expected, found, span) => {
let length = expected.len();
let expected = if length >= 2 {

View File

@ -30,24 +30,23 @@ impl Parser {
has_parent = true;
}
scoped!(
let implements = if state.current.kind == TokenKind::Implements {
state.next();
self.at_least_one_comma_separated::<Identifier>(state, &|parser, state| {
parser.full_name(state)
})?
} else {
Vec::new()
};
let attributes = state.get_attributes();
self.lbrace(state)?;
let body = scoped!(
state,
Scope::Class(name.clone(), flags.clone(), has_parent),
{
let implements = if state.current.kind == TokenKind::Implements {
state.next();
self.at_least_one_comma_separated::<Identifier>(state, &|parser, state| {
parser.full_name(state)
})?
} else {
Vec::new()
};
self.lbrace(state)?;
let attributes = state.get_attributes();
let mut body = Vec::new();
while state.current.kind != TokenKind::RightBrace {
state.gather_comments();
@ -59,18 +58,21 @@ impl Parser {
body.push(self.class_like_statement(state)?);
}
self.rbrace(state)?;
Ok(Statement::Class {
name,
attributes,
extends,
implements,
body,
flags,
})
body
}
)
);
self.rbrace(state)?;
Ok(Statement::Class {
name,
attributes,
extends,
implements,
body,
flags,
})
}
pub(in crate::parser) fn interface_definition(

View File

@ -49,8 +49,8 @@ impl Parser {
body.push(self.top_level_statement(state)?);
}
Ok(body)
})?;
body
});
Ok(Statement::Namespace { name, body })
}
@ -68,8 +68,8 @@ impl Parser {
body.push(self.top_level_statement(state)?);
}
Ok(body)
})?;
body
});
self.rbrace(state)?;

View File

@ -125,10 +125,10 @@ macro_rules! scoped {
let scope = $scope;
$state.enter(scope.clone());
let result = $block?;
let result = $block;
$state.exit();
Ok(result)
result
}};
}