mirror of
https://github.com/danog/parser.git
synced 2024-11-26 20:04:57 +01:00
chore: fix readme code
Signed-off-by: azjezz <azjezz@protonmail.com>
This commit is contained in:
parent
59fc4af394
commit
c25fe105f9
26
README.md
26
README.md
@ -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(())
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
26
src/main.rs
26
src/main.rs
@ -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(())
|
||||
}
|
||||
|
@ -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 {
|
||||
|
@ -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(
|
||||
|
@ -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)?;
|
||||
|
||||
|
@ -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
|
||||
}};
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user