Mior
This commit is contained in:
parent
d30a2e4fac
commit
333e140a50
@ -3,13 +3,13 @@
|
|||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
|
|
||||||
use libsyntax2::{
|
use libsyntax2::{
|
||||||
parse, utils::dump_tree_green
|
parse, utils::dump_tree
|
||||||
};
|
};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let text = read_input();
|
let text = read_input();
|
||||||
let file = parse(text);
|
let file = parse(text);
|
||||||
let tree = dump_tree_green(&file);
|
let tree = dump_tree(&file);
|
||||||
println!("{}", tree);
|
println!("{}", tree);
|
||||||
}
|
}
|
||||||
|
|
52
src/lib.rs
52
src/lib.rs
@ -27,6 +27,8 @@
|
|||||||
mod parser;
|
mod parser;
|
||||||
mod syntax_kinds;
|
mod syntax_kinds;
|
||||||
mod yellow;
|
mod yellow;
|
||||||
|
/// Utilities for simple uses of the parser.
|
||||||
|
pub mod utils;
|
||||||
|
|
||||||
pub use {
|
pub use {
|
||||||
lexer::{tokenize, Token},
|
lexer::{tokenize, Token},
|
||||||
@ -42,53 +44,3 @@ pub fn parse(text: String) -> SyntaxNode {
|
|||||||
parser::parse::<yellow::GreenBuilder>(text, &tokens)
|
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
46
src/utils.rs
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -2,13 +2,13 @@
|
|||||||
extern crate testutils;
|
extern crate testutils;
|
||||||
|
|
||||||
use libsyntax2::parse;
|
use libsyntax2::parse;
|
||||||
use libsyntax2::utils::dump_tree_green;
|
use libsyntax2::utils::dump_tree;
|
||||||
use testutils::dir_tests;
|
use testutils::dir_tests;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parser_tests() {
|
fn parser_tests() {
|
||||||
dir_tests(&["parser/inline", "parser/ok", "parser/err"], |text| {
|
dir_tests(&["parser/inline", "parser/ok", "parser/err"], |text| {
|
||||||
let file = parse(text.to_string());
|
let file = parse(text.to_string());
|
||||||
dump_tree_green(&file)
|
dump_tree(&file)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -4,5 +4,4 @@ version = "0.1.0"
|
|||||||
authors = ["Aleksey Kladov <aleksey.kladov@gmail.com>"]
|
authors = ["Aleksey Kladov <aleksey.kladov@gmail.com>"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
file = "1.0"
|
|
||||||
difference = "2.0.0"
|
difference = "2.0.0"
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
extern crate difference;
|
extern crate difference;
|
||||||
extern crate file;
|
|
||||||
|
|
||||||
use std::fs::read_dir;
|
use std::{
|
||||||
use std::path::{Path, PathBuf};
|
fs,
|
||||||
|
path::{Path, PathBuf}
|
||||||
|
};
|
||||||
|
|
||||||
use difference::Changeset;
|
use difference::Changeset;
|
||||||
|
|
||||||
@ -18,7 +19,7 @@
|
|||||||
///
|
///
|
||||||
/// so this should always be correct.
|
/// so this should always be correct.
|
||||||
fn read_text(path: &Path) -> String {
|
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)
|
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!("\nfile: {}", path.display());
|
||||||
println!("No .txt file with expected result, creating...\n");
|
println!("No .txt file with expected result, creating...\n");
|
||||||
println!("{}\n{}", input_code, parse_tree);
|
println!("{}\n{}", input_code, parse_tree);
|
||||||
file::put_text(&path, parse_tree).unwrap();
|
fs::write(&path, parse_tree).unwrap();
|
||||||
panic!("No expected result")
|
panic!("No expected result")
|
||||||
}
|
}
|
||||||
let expected = read_text(&path);
|
let expected = read_text(&path);
|
||||||
@ -61,7 +62,7 @@ fn collect_tests(paths: &[&str]) -> Vec<PathBuf> {
|
|||||||
|
|
||||||
fn test_from_dir(dir: &Path) -> Vec<PathBuf> {
|
fn test_from_dir(dir: &Path) -> Vec<PathBuf> {
|
||||||
let mut acc = Vec::new();
|
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 file = file.unwrap();
|
||||||
let path = file.path();
|
let path = file.path();
|
||||||
if path.extension().unwrap_or_default() == "rs" {
|
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() {
|
if expected.trim() == actual.trim() {
|
||||||
println!("whitespace difference, rewriting");
|
println!("whitespace difference, rewriting");
|
||||||
println!("file: {}\n", path.display());
|
println!("file: {}\n", path.display());
|
||||||
file::put_text(path, actual).unwrap();
|
fs::write(path, actual).unwrap();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if REWRITE {
|
if REWRITE {
|
||||||
println!("rewriting {}", path.display());
|
println!("rewriting {}", path.display());
|
||||||
file::put_text(path, actual).unwrap();
|
fs::write(path, actual).unwrap();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let changeset = Changeset::new(actual, expected, "\n");
|
let changeset = Changeset::new(actual, expected, "\n");
|
||||||
|
Loading…
Reference in New Issue
Block a user