rust/crates/libsyntax2/src/lib.rs

236 lines
6.8 KiB
Rust
Raw Normal View History

//! An experimental implementation of [Rust RFC#2256 libsyntax2.0][rfc#2256].
//!
//! The intent is to be an IDE-ready parser, i.e. one that offers
//!
//! - easy and fast incremental re-parsing,
//! - graceful handling of errors, and
//! - maintains all information in the source file.
//!
//! For more information, see [the RFC][rfc#2265], or [the working draft][RFC.md].
//!
//! [rfc#2256]: <https://github.com/rust-lang/rfcs/pull/2256>
//! [RFC.md]: <https://github.com/matklad/libsyntax2/blob/master/docs/RFC.md>
2018-07-30 06:08:06 -05:00
#![forbid(
missing_debug_implementations,
unconditional_recursion,
future_incompatible
)]
2018-07-29 05:51:55 -05:00
#![deny(bad_style, missing_docs)]
#![allow(missing_docs)]
//#![warn(unreachable_pub)] // rust-lang/rust#47816
2018-07-31 07:40:40 -05:00
extern crate itertools;
2018-07-30 06:08:06 -05:00
extern crate unicode_xid;
2018-08-01 06:55:37 -05:00
extern crate drop_bomb;
2018-08-01 14:07:09 -05:00
extern crate parking_lot;
2018-08-13 06:24:22 -05:00
extern crate smol_str;
extern crate text_unit;
2017-12-29 14:33:04 -06:00
2018-07-31 07:40:40 -05:00
pub mod algo;
pub mod ast;
2017-12-28 15:56:36 -06:00
mod lexer;
2018-07-31 15:38:19 -05:00
#[macro_use]
mod parser_api;
mod grammar;
mod parser_impl;
2018-07-29 07:16:07 -05:00
mod syntax_kinds;
2018-08-08 11:44:16 -05:00
mod yellow;
2018-07-30 07:25:52 -05:00
/// Utilities for simple uses of the parser.
pub mod utils;
2018-08-24 05:41:25 -05:00
pub mod text_utils;
2018-07-29 07:16:07 -05:00
pub use {
2018-08-18 04:42:28 -05:00
text_unit::{TextRange, TextUnit},
smol_str::SmolStr,
2018-08-25 03:40:17 -05:00
ast::AstNode,
2018-07-30 06:08:06 -05:00
lexer::{tokenize, Token},
2018-07-29 07:16:07 -05:00
syntax_kinds::SyntaxKind,
2018-08-17 13:10:55 -05:00
yellow::{SyntaxNode, SyntaxNodeRef, OwnedRoot, RefRoot, TreeRoot, SyntaxError},
2018-07-29 07:16:07 -05:00
};
2018-08-25 05:17:54 -05:00
use {
SyntaxKind::*,
yellow::{GreenNode, SyntaxRoot},
parser_api::Parser,
};
2018-08-25 04:10:35 -05:00
2018-08-25 03:40:17 -05:00
#[derive(Clone, Debug)]
2018-08-25 03:44:58 -05:00
pub struct File {
2018-08-25 03:40:17 -05:00
root: SyntaxNode
}
2018-08-25 03:44:58 -05:00
impl File {
2018-08-25 05:57:13 -05:00
fn new(green: GreenNode, errors: Vec<SyntaxError>) -> File {
let root = SyntaxRoot::new(green, errors);
2018-08-25 04:10:35 -05:00
let root = SyntaxNode::new_owned(root);
validate_block_structure(root.borrowed());
2018-08-25 03:44:58 -05:00
File { root }
2018-08-25 03:40:17 -05:00
}
2018-08-25 04:44:26 -05:00
pub fn parse(text: &str) -> File {
2018-08-25 04:10:35 -05:00
let tokens = tokenize(&text);
2018-08-25 06:45:17 -05:00
let (green, errors) = parser_impl::parse_with::<yellow::GreenBuilder>(
text, &tokens, grammar::root,
);
2018-08-25 05:57:13 -05:00
File::new(green, errors)
2018-08-25 04:10:35 -05:00
}
2018-08-25 05:17:54 -05:00
pub fn reparse(&self, edit: &AtomEdit) -> File {
2018-08-25 05:42:40 -05:00
self.incremental_reparse(edit).unwrap_or_else(|| self.full_reparse(edit))
2018-08-25 05:17:54 -05:00
}
2018-08-25 06:45:17 -05:00
pub fn incremental_reparse(&self, edit: &AtomEdit) -> Option<File> {
2018-08-25 05:17:54 -05:00
let (node, reparser) = find_reparsable_node(self.syntax(), edit.delete)?;
2018-08-25 05:57:13 -05:00
let text = replace_range(
2018-08-28 06:06:30 -05:00
node.text().to_string(),
2018-08-25 05:57:13 -05:00
edit.delete - node.range().start(),
&edit.insert,
);
let tokens = tokenize(&text);
if !is_balanced(&tokens) {
return None;
}
2018-08-25 06:45:17 -05:00
let (green, new_errors) = parser_impl::parse_with::<yellow::GreenBuilder>(
&text, &tokens, reparser,
);
let green_root = node.replace_with(green);
2018-08-25 07:12:17 -05:00
let errors = merge_errors(self.errors(), new_errors, node, edit);
2018-08-25 06:45:17 -05:00
Some(File::new(green_root, errors))
2018-08-25 05:17:54 -05:00
}
fn full_reparse(&self, edit: &AtomEdit) -> File {
2018-08-28 06:06:30 -05:00
let text = replace_range(self.syntax().text().to_string(), edit.delete, &edit.insert);
2018-08-25 05:17:54 -05:00
File::parse(&text)
}
2018-08-25 03:44:17 -05:00
pub fn ast(&self) -> ast::Root {
ast::Root::cast(self.syntax()).unwrap()
2018-08-25 03:40:17 -05:00
}
pub fn syntax(&self) -> SyntaxNodeRef {
self.root.borrowed()
}
pub fn errors(&self) -> Vec<SyntaxError> {
self.syntax().root.syntax_root().errors.clone()
}
2018-08-24 11:27:30 -05:00
}
#[cfg(not(debug_assertions))]
fn validate_block_structure(_: SyntaxNodeRef) {}
#[cfg(debug_assertions)]
2018-08-24 11:27:30 -05:00
fn validate_block_structure(root: SyntaxNodeRef) {
let mut stack = Vec::new();
for node in algo::walk::preorder(root) {
match node.kind() {
SyntaxKind::L_CURLY => {
stack.push(node)
}
SyntaxKind::R_CURLY => {
if let Some(pair) = stack.pop() {
assert_eq!(
node.parent(),
pair.parent(),
2018-09-03 16:49:21 -05:00
"\nunpaired curleys:\n{}\n{}\n",
root.text(),
utils::dump_tree(root),
);
2018-08-24 11:27:30 -05:00
assert!(
node.next_sibling().is_none() && pair.prev_sibling().is_none(),
2018-08-26 01:12:18 -05:00
"\nfloating curlys at {:?}\nfile:\n{}\nerror:\n{}\n",
2018-08-24 11:27:30 -05:00
node,
root.text(),
node.text(),
);
}
}
_ => (),
}
}
2018-07-29 07:16:07 -05:00
}
2018-08-25 04:44:26 -05:00
#[derive(Debug, Clone)]
pub struct AtomEdit {
pub delete: TextRange,
pub insert: String,
}
impl AtomEdit {
pub fn replace(range: TextRange, replace_with: String) -> AtomEdit {
AtomEdit { delete: range, insert: replace_with }
}
pub fn delete(range: TextRange) -> AtomEdit {
AtomEdit::replace(range, String::new())
}
pub fn insert(offset: TextUnit, text: String) -> AtomEdit {
AtomEdit::replace(TextRange::offset_len(offset, 0.into()), text)
}
}
2018-08-25 05:17:54 -05:00
fn find_reparsable_node(node: SyntaxNodeRef, range: TextRange) -> Option<(SyntaxNodeRef, fn(&mut Parser))> {
let node = algo::find_covering_node(node, range);
return algo::ancestors(node)
.filter_map(|node| reparser(node).map(|r| (node, r)))
.next();
fn reparser(node: SyntaxNodeRef) -> Option<fn(&mut Parser)> {
let res = match node.kind() {
BLOCK => grammar::block,
NAMED_FIELD_DEF_LIST => grammar::named_field_def_list,
_ => return None,
};
Some(res)
}
}
2018-08-25 05:57:13 -05:00
2018-08-25 06:45:17 -05:00
pub /*(meh)*/ fn replace_range(mut text: String, range: TextRange, replace_with: &str) -> String {
2018-08-25 05:57:13 -05:00
let start = u32::from(range.start()) as usize;
let end = u32::from(range.end()) as usize;
text.replace_range(start..end, replace_with);
text
}
fn is_balanced(tokens: &[Token]) -> bool {
if tokens.len() == 0
|| tokens.first().unwrap().kind != L_CURLY
|| tokens.last().unwrap().kind != R_CURLY {
return false
}
let mut balance = 0usize;
for t in tokens.iter() {
match t.kind {
L_CURLY => balance += 1,
R_CURLY => balance = match balance.checked_sub(1) {
Some(b) => b,
None => return false,
},
_ => (),
}
}
balance == 0
}
2018-08-25 06:45:17 -05:00
fn merge_errors(
old_errors: Vec<SyntaxError>,
new_errors: Vec<SyntaxError>,
2018-08-25 07:12:17 -05:00
old_node: SyntaxNodeRef,
2018-08-25 06:45:17 -05:00
edit: &AtomEdit,
) -> Vec<SyntaxError> {
let mut res = Vec::new();
for e in old_errors {
2018-08-25 07:12:17 -05:00
if e.offset < old_node.range().start() {
2018-08-25 06:45:17 -05:00
res.push(e)
2018-08-25 07:12:17 -05:00
} else if e.offset > old_node.range().end() {
2018-08-25 06:45:17 -05:00
res.push(SyntaxError {
msg: e.msg,
offset: e.offset + TextUnit::of_str(&edit.insert) - edit.delete.len(),
})
}
}
for e in new_errors {
res.push(SyntaxError {
msg: e.msg,
2018-08-25 07:12:17 -05:00
offset: e.offset + old_node.range().start(),
2018-08-25 06:45:17 -05:00
})
}
res
}