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 ### Example
```rust ```rust
use php_parser_rs::*; use php_parser_rs::prelude::*;
let mut lexer = Lexer::new(None); fn main() -> ParseResult<()> {
let tokens = lexer.tokenize(&source_code[..]).unwrap(); let lexer = Lexer::new();
let parser = Parser::new();
let mut parser = Parser::new(None); let code = "
let ast = parser.parse(tokens).unwrap(); <?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::*;
use php_parser_rs::prelude::Parser;
fn main() { fn main() -> ParseResult<()> {
let file = match std::env::args().nth(1) { let file = match std::env::args().nth(1) {
Some(file) => file, Some(file) => file,
None => { None => {
@ -21,24 +20,13 @@ fn main() {
}; };
let lexer = Lexer::new(); 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 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); dbg!(ast);
Ok(())
} }

View File

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

View File

@ -30,10 +30,6 @@ impl Parser {
has_parent = true; has_parent = true;
} }
scoped!(
state,
Scope::Class(name.clone(), flags.clone(), has_parent),
{
let implements = if state.current.kind == TokenKind::Implements { let implements = if state.current.kind == TokenKind::Implements {
state.next(); state.next();
@ -44,10 +40,13 @@ impl Parser {
Vec::new() Vec::new()
}; };
let attributes = state.get_attributes();
self.lbrace(state)?; self.lbrace(state)?;
let attributes = state.get_attributes(); let body = scoped!(
state,
Scope::Class(name.clone(), flags.clone(), has_parent),
{
let mut body = Vec::new(); let mut body = Vec::new();
while state.current.kind != TokenKind::RightBrace { while state.current.kind != TokenKind::RightBrace {
state.gather_comments(); state.gather_comments();
@ -59,6 +58,11 @@ impl Parser {
body.push(self.class_like_statement(state)?); body.push(self.class_like_statement(state)?);
} }
body
}
);
self.rbrace(state)?; self.rbrace(state)?;
Ok(Statement::Class { Ok(Statement::Class {
@ -70,8 +74,6 @@ impl Parser {
flags, flags,
}) })
} }
)
}
pub(in crate::parser) fn interface_definition( pub(in crate::parser) fn interface_definition(
&self, &self,

View File

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

View File

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