rust/crates/libsyntax2/src/lib.rs

171 lines
4.9 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 13:08:06 +02:00
#![forbid(
missing_debug_implementations,
unconditional_recursion,
future_incompatible
)]
2018-07-29 12:51:55 +02:00
#![deny(bad_style, missing_docs)]
#![allow(missing_docs)]
//#![warn(unreachable_pub)] // rust-lang/rust#47816
2018-07-31 14:40:40 +02:00
extern crate itertools;
2018-07-30 13:08:06 +02:00
extern crate unicode_xid;
2018-08-01 13:55:37 +02:00
extern crate drop_bomb;
2018-08-01 21:07:09 +02:00
extern crate parking_lot;
2018-08-13 13:24:22 +02:00
extern crate smol_str;
extern crate text_unit;
2017-12-29 21:33:04 +01:00
2018-07-31 14:40:40 +02:00
pub mod algo;
pub mod ast;
2017-12-28 22:56:36 +01:00
mod lexer;
2018-07-31 22:38:19 +02:00
#[macro_use]
mod parser_api;
mod grammar;
mod parser_impl;
2018-07-29 14:16:07 +02:00
mod syntax_kinds;
2018-08-08 18:44:16 +02:00
mod yellow;
2018-07-30 14:25:52 +02:00
/// Utilities for simple uses of the parser.
pub mod utils;
2018-08-24 12:41:25 +02:00
pub mod text_utils;
2018-07-29 14:16:07 +02:00
pub use {
2018-08-18 11:42:28 +02:00
text_unit::{TextRange, TextUnit},
smol_str::SmolStr,
2018-08-25 10:40:17 +02:00
ast::AstNode,
2018-07-30 13:08:06 +02:00
lexer::{tokenize, Token},
2018-07-29 14:16:07 +02:00
syntax_kinds::SyntaxKind,
2018-08-17 20:10:55 +02:00
yellow::{SyntaxNode, SyntaxNodeRef, OwnedRoot, RefRoot, TreeRoot, SyntaxError},
2018-07-29 14:16:07 +02:00
};
2018-08-25 12:17:54 +02:00
use {
SyntaxKind::*,
yellow::{GreenNode, SyntaxRoot},
parser_api::Parser,
};
2018-08-25 11:10:35 +02:00
2018-08-25 10:40:17 +02:00
#[derive(Clone, Debug)]
2018-08-25 10:44:58 +02:00
pub struct File {
2018-08-25 10:40:17 +02:00
root: SyntaxNode
}
2018-08-25 10:44:58 +02:00
impl File {
2018-08-25 11:10:35 +02:00
fn new(root: GreenNode, errors: Vec<SyntaxError>) -> File {
let root = SyntaxRoot::new(root, errors);
let root = SyntaxNode::new_owned(root);
validate_block_structure(root.borrowed());
2018-08-25 10:44:58 +02:00
File { root }
2018-08-25 10:40:17 +02:00
}
2018-08-25 11:44:26 +02:00
pub fn parse(text: &str) -> File {
2018-08-25 11:10:35 +02:00
let tokens = tokenize(&text);
let (root, errors) = parser_impl::parse::<yellow::GreenBuilder>(text, &tokens);
File::new(root, errors)
}
2018-08-25 12:17:54 +02:00
pub fn reparse(&self, edit: &AtomEdit) -> File {
self.incremental_reparse(edit).unwrap_or_else(|| {
self.full_reparse(edit)
})
}
fn incremental_reparse(&self, edit: &AtomEdit) -> Option<File> {
let (node, reparser) = find_reparsable_node(self.syntax(), edit.delete)?;
None
}
fn full_reparse(&self, edit: &AtomEdit) -> File {
let start = u32::from(edit.delete.start()) as usize;
let end = u32::from(edit.delete.end()) as usize;
let mut text = self.syntax().text();
text.replace_range(start..end, &edit.insert);
File::parse(&text)
}
2018-08-25 10:44:17 +02:00
pub fn ast(&self) -> ast::Root {
ast::Root::cast(self.syntax()).unwrap()
2018-08-25 10:40:17 +02: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 18:27:30 +02:00
}
#[cfg(not(debug_assertions))]
fn validate_block_structure(_: SyntaxNodeRef) {}
#[cfg(debug_assertions)]
2018-08-24 18:27:30 +02: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(),
"unpaired curleys:\n{}",
utils::dump_tree(root),
);
2018-08-24 18:27:30 +02:00
assert!(
node.next_sibling().is_none() && pair.prev_sibling().is_none(),
"floating curlys at {:?}\nfile:\n{}\nerror:\n{}\n",
node,
root.text(),
node.text(),
);
}
}
_ => (),
}
}
2018-07-29 14:16:07 +02:00
}
2018-08-25 11:44:26 +02: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 12:17:54 +02: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)
}
}