This commit is contained in:
Aleksey Kladov 2018-07-30 15:25:52 +03:00
parent d30a2e4fac
commit 333e140a50
6 changed files with 61 additions and 63 deletions

View File

@ -3,13 +3,13 @@
use std::io::Read;
use libsyntax2::{
parse, utils::dump_tree_green
parse, utils::dump_tree
};
fn main() {
let text = read_input();
let file = parse(text);
let tree = dump_tree_green(&file);
let tree = dump_tree(&file);
println!("{}", tree);
}

View File

@ -27,6 +27,8 @@
mod parser;
mod syntax_kinds;
mod yellow;
/// Utilities for simple uses of the parser.
pub mod utils;
pub use {
lexer::{tokenize, Token},
@ -42,53 +44,3 @@ pub fn parse(text: String) -> SyntaxNode {
parser::parse::<yellow::GreenBuilder>(text, &tokens)
}
/// Utilities for simple uses of the parser.
pub mod utils {
use std::{collections::BTreeSet, fmt::Write};
use {SyntaxError, SyntaxNode, SyntaxNodeRef};
/// Parse a file and create a string representation of the resulting parse tree.
pub fn dump_tree_green(syntax: &SyntaxNode) -> String {
let syntax = syntax.borrow();
let mut errors: BTreeSet<_> = syntax.root.errors.iter().cloned().collect();
let mut result = String::new();
go(syntax, &mut result, 0, &mut errors);
return result;
fn go(
node: SyntaxNodeRef,
buff: &mut String,
level: usize,
errors: &mut BTreeSet<SyntaxError>,
) {
buff.push_str(&String::from(" ").repeat(level));
write!(buff, "{:?}\n", node).unwrap();
let my_errors: Vec<_> = errors
.iter()
.filter(|e| e.offset == node.range().start())
.cloned()
.collect();
for err in my_errors {
errors.remove(&err);
buff.push_str(&String::from(" ").repeat(level));
write!(buff, "err: `{}`\n", err.message).unwrap();
}
for child in node.children() {
go(child, buff, level + 1, errors)
}
let my_errors: Vec<_> = errors
.iter()
.filter(|e| e.offset == node.range().end())
.cloned()
.collect();
for err in my_errors {
errors.remove(&err);
buff.push_str(&String::from(" ").repeat(level));
write!(buff, "err: `{}`\n", err.message).unwrap();
}
}
}
}

46
src/utils.rs Normal file
View File

@ -0,0 +1,46 @@
use std::{collections::BTreeSet, fmt::Write};
use {SyntaxError, SyntaxNode, SyntaxNodeRef};
/// Parse a file and create a string representation of the resulting parse tree.
pub fn dump_tree(syntax: &SyntaxNode) -> String {
let syntax = syntax.borrow();
let mut errors: BTreeSet<_> = syntax.root.errors.iter().cloned().collect();
let mut result = String::new();
go(syntax, &mut result, 0, &mut errors);
return result;
fn go(
node: SyntaxNodeRef,
buff: &mut String,
level: usize,
errors: &mut BTreeSet<SyntaxError>,
) {
buff.push_str(&String::from(" ").repeat(level));
write!(buff, "{:?}\n", node).unwrap();
let my_errors: Vec<_> = errors
.iter()
.filter(|e| e.offset == node.range().start())
.cloned()
.collect();
for err in my_errors {
errors.remove(&err);
buff.push_str(&String::from(" ").repeat(level));
write!(buff, "err: `{}`\n", err.message).unwrap();
}
for child in node.children() {
go(child, buff, level + 1, errors)
}
let my_errors: Vec<_> = errors
.iter()
.filter(|e| e.offset == node.range().end())
.cloned()
.collect();
for err in my_errors {
errors.remove(&err);
buff.push_str(&String::from(" ").repeat(level));
write!(buff, "err: `{}`\n", err.message).unwrap();
}
}
}

View File

@ -2,13 +2,13 @@
extern crate testutils;
use libsyntax2::parse;
use libsyntax2::utils::dump_tree_green;
use libsyntax2::utils::dump_tree;
use testutils::dir_tests;
#[test]
fn parser_tests() {
dir_tests(&["parser/inline", "parser/ok", "parser/err"], |text| {
let file = parse(text.to_string());
dump_tree_green(&file)
dump_tree(&file)
})
}

View File

@ -4,5 +4,4 @@ version = "0.1.0"
authors = ["Aleksey Kladov <aleksey.kladov@gmail.com>"]
[dependencies]
file = "1.0"
difference = "2.0.0"

View File

@ -1,8 +1,9 @@
extern crate difference;
extern crate file;
use std::fs::read_dir;
use std::path::{Path, PathBuf};
use std::{
fs,
path::{Path, PathBuf}
};
use difference::Changeset;
@ -18,7 +19,7 @@
///
/// so this should always be correct.
fn read_text(path: &Path) -> String {
file::get_text(path).unwrap().replace("\r\n", "\n")
fs::read_to_string(path).unwrap().replace("\r\n", "\n")
}
pub fn dir_tests<F>(paths: &[&str], f: F)
@ -33,7 +34,7 @@ pub fn dir_tests<F>(paths: &[&str], f: F)
println!("\nfile: {}", path.display());
println!("No .txt file with expected result, creating...\n");
println!("{}\n{}", input_code, parse_tree);
file::put_text(&path, parse_tree).unwrap();
fs::write(&path, parse_tree).unwrap();
panic!("No expected result")
}
let expected = read_text(&path);
@ -61,7 +62,7 @@ fn collect_tests(paths: &[&str]) -> Vec<PathBuf> {
fn test_from_dir(dir: &Path) -> Vec<PathBuf> {
let mut acc = Vec::new();
for file in read_dir(&dir).unwrap() {
for file in fs::read_dir(&dir).unwrap() {
let file = file.unwrap();
let path = file.path();
if path.extension().unwrap_or_default() == "rs" {
@ -80,12 +81,12 @@ fn print_difference(expected: &str, actual: &str, path: &Path) {
if expected.trim() == actual.trim() {
println!("whitespace difference, rewriting");
println!("file: {}\n", path.display());
file::put_text(path, actual).unwrap();
fs::write(path, actual).unwrap();
return;
}
if REWRITE {
println!("rewriting {}", path.display());
file::put_text(path, actual).unwrap();
fs::write(path, actual).unwrap();
return;
}
let changeset = Changeset::new(actual, expected, "\n");