mirror of
https://github.com/danog/parser.git
synced 2024-12-02 17:38:16 +01:00
ac42958b4f
- added `prelude` - moved `ast` to `parser` - moved `Lexer` from `crate::lexer::lexer::Lexer` to `crate::lexer::Lexer` - renamed `LexerError` to `SyntaxError` - moved `SyntaxError` to `token::error` - implemented `Display` for `SyntaxError` - add `tokens.txt` to integration tests - removed all unit tests for `Lexer` ( everything is tested in integration tests ) - improved syntax error assertations
91 lines
3.0 KiB
Rust
91 lines
3.0 KiB
Rust
use php_parser_rs::prelude::{Lexer, Parser};
|
|
use std::env;
|
|
use std::fs::read_dir;
|
|
use std::path::PathBuf;
|
|
|
|
fn main() {
|
|
let manifest = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
|
|
let mut entries = read_dir(manifest.join("tests"))
|
|
.unwrap()
|
|
.flatten()
|
|
.map(|entry| entry.path())
|
|
.filter(|entry| entry.is_dir())
|
|
.collect::<Vec<PathBuf>>();
|
|
|
|
entries.sort();
|
|
|
|
let mut content = String::new();
|
|
content.push_str("/// this file is auto-generated by the build script.\n");
|
|
content.push_str("/// you should never manually change it.\n\n\n");
|
|
|
|
for entry in entries {
|
|
let code_filename = entry.join("code.php");
|
|
let ast_filename = entry.join("ast.txt");
|
|
let tokens_filename = entry.join("tokens.txt");
|
|
let lexer_error_filename = entry.join("lexer-error.txt");
|
|
let parser_error_filename = entry.join("parser-error.txt");
|
|
|
|
if !code_filename.exists() {
|
|
continue;
|
|
}
|
|
|
|
if ast_filename.exists() {
|
|
std::fs::remove_file(&ast_filename).unwrap();
|
|
}
|
|
|
|
if tokens_filename.exists() {
|
|
std::fs::remove_file(&tokens_filename).unwrap();
|
|
}
|
|
|
|
if lexer_error_filename.exists() {
|
|
std::fs::remove_file(&lexer_error_filename).unwrap();
|
|
}
|
|
|
|
if parser_error_filename.exists() {
|
|
std::fs::remove_file(&parser_error_filename).unwrap();
|
|
}
|
|
|
|
let code = std::fs::read_to_string(&code_filename).unwrap();
|
|
let mut lexer = Lexer::new();
|
|
let tokens = lexer.tokenize(code.as_bytes());
|
|
|
|
match tokens {
|
|
Ok(tokens) => {
|
|
std::fs::write(tokens_filename, format!("{:#?}\n", tokens)).unwrap();
|
|
println!(
|
|
"✅ generated `tokens.txt` for `{}`",
|
|
entry.to_string_lossy()
|
|
);
|
|
|
|
let mut parser = Parser::new(None);
|
|
let ast = parser.parse(tokens);
|
|
match ast {
|
|
Ok(ast) => {
|
|
std::fs::write(ast_filename, format!("{:#?}\n", ast)).unwrap();
|
|
println!("✅ generated `ast.txt` for `{}`", entry.to_string_lossy());
|
|
}
|
|
Err(error) => {
|
|
std::fs::write(
|
|
parser_error_filename,
|
|
format!("{:?} -> {}\n", error, error),
|
|
)
|
|
.unwrap();
|
|
println!(
|
|
"✅ generated `parser-error.txt` for `{}`",
|
|
entry.to_string_lossy()
|
|
);
|
|
}
|
|
}
|
|
}
|
|
Err(error) => {
|
|
std::fs::write(lexer_error_filename, format!("{:?} -> {}\n", error, error))
|
|
.unwrap();
|
|
println!(
|
|
"✅ generated `lexer-error.txt` for `{}`",
|
|
entry.to_string_lossy()
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|