This commit is contained in:
Aleksey Kladov 2018-07-30 14:08:06 +03:00
parent 6983091d6d
commit 1edb58a802
20 changed files with 130 additions and 141 deletions

View File

@ -6,7 +6,7 @@ matrix:
before_script:
- rustup component add rustfmt-preview
script:
- cargo fmt --all -- --write-mode=diff
- cargo fmt --all -- --check
- cargo test
- cargo gen-kinds --verify
- cargo gen-tests --verify

View File

@ -1,12 +1,17 @@
use unicode_xid::UnicodeXID;
pub fn is_ident_start(c: char) -> bool {
(c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_'
(c >= 'a' && c <= 'z')
|| (c >= 'A' && c <= 'Z')
|| c == '_'
|| (c > '\x7f' && UnicodeXID::is_xid_start(c))
}
pub fn is_ident_continue(c: char) -> bool {
(c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_'
(c >= 'a' && c <= 'z')
|| (c >= 'A' && c <= 'Z')
|| (c >= '0' && c <= '9')
|| c == '_'
|| (c > '\x7f' && UnicodeXID::is_xid_continue(c))
}

View File

@ -1,22 +1,22 @@
mod ptr;
mod comments;
mod strings;
mod numbers;
mod classes;
mod comments;
mod numbers;
mod ptr;
mod strings;
use {
TextUnit,
SyntaxKind::{self, *},
TextUnit,
};
use self::{
ptr::Ptr,
classes::*,
numbers::scan_number,
strings::{
is_string_literal_start, scan_byte_char_or_string, scan_char,
scan_raw_string, scan_string},
comments::{scan_comment, scan_shebang},
numbers::scan_number,
ptr::Ptr,
strings::{
is_string_literal_start, scan_byte_char_or_string, scan_char, scan_raw_string, scan_string,
},
};
/// A token of Rust source.

View File

@ -1,5 +1,5 @@
use lexer::ptr::Ptr;
use lexer::classes::*;
use lexer::ptr::Ptr;
use SyntaxKind::{self, *};

View File

@ -11,44 +11,42 @@
//! [rfc#2256]: <https://github.com/rust-lang/rfcs/pull/2256>
//! [RFC.md]: <https://github.com/matklad/libsyntax2/blob/master/docs/RFC.md>
#![forbid(missing_debug_implementations, unconditional_recursion, future_incompatible)]
#![forbid(
missing_debug_implementations,
unconditional_recursion,
future_incompatible
)]
#![deny(bad_style, missing_docs)]
#![allow(missing_docs)]
//#![warn(unreachable_pub)] // rust-lang/rust#47816
extern crate unicode_xid;
extern crate text_unit;
extern crate unicode_xid;
mod lexer;
mod parser;
mod yellow;
mod syntax_kinds;
mod yellow;
pub use {
text_unit::{TextRange, TextUnit},
syntax_kinds::SyntaxKind,
yellow::{SyntaxNode, SyntaxNodeRef},
lexer::{tokenize, Token},
syntax_kinds::SyntaxKind,
text_unit::{TextRange, TextUnit},
yellow::{SyntaxNode, SyntaxNodeRef},
};
pub(crate) use {
yellow::SyntaxError
};
pub(crate) use yellow::SyntaxError;
pub fn parse(text: String) -> SyntaxNode {
let tokens = tokenize(&text);
parser::parse::<yellow::GreenBuilder>(text, &tokens)
}
/// Utilities for simple uses of the parser.
pub mod utils {
use std::{
fmt::Write,
collections::BTreeSet
};
use std::{collections::BTreeSet, fmt::Write};
use {SyntaxNode, SyntaxNodeRef, SyntaxError};
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 {
@ -58,11 +56,19 @@ pub mod utils {
go(syntax, &mut result, 0, &mut errors);
return result;
fn go(node: SyntaxNodeRef, buff: &mut String, level: usize, errors: &mut BTreeSet<SyntaxError>) {
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();
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));
@ -73,8 +79,11 @@ pub mod utils {
go(child, buff, level + 1, errors)
}
let my_errors: Vec<_> = errors.iter().filter(|e| e.offset == node.range().end())
.cloned().collect();
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));

View File

@ -8,9 +8,9 @@
//! `start node`, `finish node`, and `FileBuilder` converts
//! this stream to a real tree.
use {
TextUnit,
SyntaxKind::{self, TOMBSTONE},
lexer::Token,
SyntaxKind::{self, TOMBSTONE},
TextUnit,
};
pub(crate) trait Sink {

View File

@ -1,9 +1,9 @@
use super::*;
mod structs;
mod use_item;
mod consts;
mod structs;
mod traits;
mod use_item;
pub(super) fn mod_contents(p: &mut Parser, stop_on_r_curly: bool) {
attributes::inner_attributes(p);
@ -12,9 +12,8 @@ pub(super) fn mod_contents(p: &mut Parser, stop_on_r_curly: bool) {
}
}
pub(super) const ITEM_FIRST: TokenSet = token_set![
EXTERN_KW, MOD_KW, USE_KW, STRUCT_KW, ENUM_KW, FN_KW, PUB_KW, POUND
];
pub(super) const ITEM_FIRST: TokenSet =
token_set![EXTERN_KW, MOD_KW, USE_KW, STRUCT_KW, ENUM_KW, FN_KW, PUB_KW, POUND];
fn item(p: &mut Parser) {
let item = p.start();

View File

@ -21,20 +21,17 @@
//! After adding a new inline-test, run `cargo collect-tests` to extract
//! it as a standalone text-fixture into `tests/data/parser/inline`, and
//! run `cargo test` once to create the "gold" value.
mod items;
mod attributes;
mod expressions;
mod types;
mod patterns;
mod items;
mod paths;
mod patterns;
mod type_params;
mod types;
use {
parser::{parser::Parser, token_set::TokenSet},
SyntaxKind::{self, *},
parser::{
parser::Parser,
token_set::TokenSet
}
};
pub(crate) fn file(p: &mut Parser) {

View File

@ -1,8 +1,4 @@
use {
SyntaxKind, TextRange, TextUnit,
SyntaxKind::EOF,
lexer::Token,
};
use {lexer::Token, SyntaxKind, SyntaxKind::EOF, TextRange, TextUnit};
use std::ops::{Add, AddAssign};

View File

@ -1,18 +1,14 @@
#[macro_use]
mod token_set;
mod parser;
mod input;
mod event;
mod grammar;
mod input;
mod parser;
use {
lexer::Token,
parser::event::{process}
};
use {lexer::Token, parser::event::process};
pub(crate) use self::event::Sink;
/// Parse a sequence of tokens into the representative node tree
pub(crate) fn parse<S: Sink>(text: String, tokens: &[Token]) -> S::Tree {
let events = {

View File

@ -1,5 +1,5 @@
use parser::input::{InputPosition, ParserInput};
use parser::event::Event;
use parser::input::{InputPosition, ParserInput};
use SyntaxKind::{self, EOF, TOMBSTONE};

View File

@ -1,7 +1,7 @@
mod generated;
use std::fmt;
use ::{SyntaxKind::*};
use SyntaxKind::*;
pub use self::generated::SyntaxKind;
@ -16,7 +16,6 @@ pub(crate) struct SyntaxInfo {
pub name: &'static str,
}
impl SyntaxKind {
pub(crate) fn is_trivia(self: SyntaxKind) -> bool {
match self {

View File

@ -1,7 +1,7 @@
use {
parser::Sink,
yellow::{GreenNode, GreenNodeBuilder, SyntaxError, SyntaxNode, SyntaxRoot},
SyntaxKind, TextRange, TextUnit,
yellow::{SyntaxNode, SyntaxRoot, GreenNode, GreenNodeBuilder, SyntaxError},
parser::Sink
};
pub(crate) struct GreenBuilder {
@ -12,9 +12,7 @@ pub(crate) struct GreenBuilder {
errors: Vec<SyntaxError>,
}
impl GreenBuilder {
}
impl GreenBuilder {}
impl Sink for GreenBuilder {
type Tree = SyntaxNode;
@ -53,7 +51,10 @@ impl Sink for GreenBuilder {
}
fn error(&mut self, message: String) {
self.errors.push(SyntaxError { message, offset: self.pos })
self.errors.push(SyntaxError {
message,
offset: self.pos,
})
}
fn finish(self) -> SyntaxNode {
@ -61,5 +62,3 @@ impl Sink for GreenBuilder {
SyntaxNode::new_owned(root)
}
}

View File

@ -1,5 +1,8 @@
use std::sync::Arc;
use {SyntaxKind::{self, *}, TextUnit};
use {
SyntaxKind::{self, *},
TextUnit,
};
#[derive(Clone, Debug)]
pub(crate) enum GreenNode {
@ -36,9 +39,7 @@ impl GreenNode {
fn go(node: &GreenNode, buff: &mut String) {
match node {
GreenNode::Leaf(l) => buff.push_str(&l.text()),
GreenNode::Branch(b) => {
b.children().iter().for_each(|child| go(child, buff))
}
GreenNode::Branch(b) => b.children().iter().for_each(|child| go(child, buff)),
}
}
}
@ -71,7 +72,6 @@ impl GreenNodeBuilder {
}
}
#[test]
fn assert_send_sync() {
fn f<T: Send + Sync>() {}
@ -80,14 +80,8 @@ fn assert_send_sync() {
#[derive(Clone, Debug)]
pub(crate) enum GreenLeaf {
Whitespace {
newlines: u8,
spaces: u8,
},
Token {
kind: SyntaxKind,
text: Arc<str>,
},
Whitespace { newlines: u8, spaces: u8 },
Token { kind: SyntaxKind, text: Arc<str> },
}
impl GreenLeaf {
@ -96,10 +90,16 @@ impl GreenLeaf {
let newlines = text.bytes().take_while(|&b| b == b'\n').count();
let spaces = text[newlines..].bytes().take_while(|&b| b == b' ').count();
if newlines + spaces == text.len() && newlines <= N_NEWLINES && spaces <= N_SPACES {
return GreenLeaf::Whitespace { newlines: newlines as u8, spaces: spaces as u8 };
return GreenLeaf::Whitespace {
newlines: newlines as u8,
spaces: spaces as u8,
};
}
}
GreenLeaf::Token { kind, text: text.to_owned().into_boxed_str().into() }
GreenLeaf::Token {
kind,
text: text.to_owned().into_boxed_str().into(),
}
}
pub(crate) fn kind(&self) -> SyntaxKind {
@ -141,7 +141,11 @@ pub(crate) struct GreenBranch {
impl GreenBranch {
fn new(kind: SyntaxKind, children: Vec<GreenNode>) -> GreenBranch {
let text_len = children.iter().map(|x| x.text_len()).sum::<TextUnit>();
GreenBranch { text_len, kind, children }
GreenBranch {
text_len,
kind,
children,
}
}
pub fn kind(&self) -> SyntaxKind {
@ -156,4 +160,3 @@ impl GreenBranch {
self.children.as_slice()
}
}

View File

@ -1,12 +1,12 @@
mod builder;
mod green;
mod red;
mod syntax;
mod builder;
pub use self::syntax::{SyntaxNode, SyntaxNodeRef};
pub(crate) use self::{
builder::GreenBuilder,
green::{GreenNode, GreenNodeBuilder},
red::RedNode,
syntax::{SyntaxError, SyntaxRoot},
builder::GreenBuilder,
};
pub use self::syntax::{SyntaxNode, SyntaxNodeRef};

View File

@ -1,11 +1,5 @@
use std::{
ptr,
sync::RwLock,
};
use {
TextUnit,
yellow::GreenNode,
};
use std::{ptr, sync::RwLock};
use {yellow::GreenNode, TextUnit};
#[derive(Debug)]
pub(crate) struct RedNode {
@ -22,9 +16,7 @@ struct ParentData {
}
impl RedNode {
pub fn new_root(
green: GreenNode,
) -> RedNode {
pub fn new_root(green: GreenNode) -> RedNode {
RedNode::new(green, None)
}
@ -42,13 +34,14 @@ impl RedNode {
RedNode::new(green, Some(parent_data))
}
fn new(
green: GreenNode,
parent: Option<ParentData>,
) -> RedNode {
fn new(green: GreenNode, parent: Option<ParentData>) -> RedNode {
let n_children = green.children().len();
let children = (0..n_children).map(|_| None).collect();
RedNode { green, parent, children: RwLock::new(children) }
RedNode {
green,
parent,
children: RwLock::new(children),
}
}
pub(crate) fn green(&self) -> &GreenNode {
@ -75,12 +68,15 @@ impl RedNode {
if children[idx].is_none() {
let green_children = self.green.children();
let start_offset = self.start_offset()
+ green_children[..idx].iter().map(|x| x.text_len()).sum::<TextUnit>();
let child = RedNode::new_child(green_children[idx].clone(), self.into(), start_offset, idx);
+ green_children[..idx]
.iter()
.map(|x| x.text_len())
.sum::<TextUnit>();
let child =
RedNode::new_child(green_children[idx].clone(), self.into(), start_offset, idx);
children[idx] = Some(child)
}
children[idx].as_ref().unwrap().into()
}
pub(crate) fn parent(&self) -> Option<ptr::NonNull<RedNode>> {

View File

@ -1,17 +1,12 @@
use std::{
fmt,
sync::Arc,
ptr,
ops::Deref,
};
use std::{fmt, ops::Deref, ptr, sync::Arc};
use {
TextRange, TextUnit,
yellow::{GreenNode, RedNode},
SyntaxKind::{self, *},
yellow::{RedNode, GreenNode},
TextRange, TextUnit,
};
pub trait TreeRoot: Deref<Target=SyntaxRoot> + Clone {}
pub trait TreeRoot: Deref<Target = SyntaxRoot> + Clone {}
impl TreeRoot for Arc<SyntaxRoot> {}
impl<'a> TreeRoot for &'a SyntaxRoot {}
@ -50,7 +45,10 @@ impl SyntaxNode<Arc<SyntaxRoot>> {
pub(crate) fn new_owned(root: SyntaxRoot) -> Self {
let root = Arc::new(root);
let red_weak = ptr::NonNull::from(&root.red);
SyntaxNode { root, red: red_weak }
SyntaxNode {
root,
red: red_weak,
}
}
}
@ -68,10 +66,7 @@ impl<ROOT: TreeRoot> SyntaxNode<ROOT> {
pub fn range(&self) -> TextRange {
let red = self.red();
TextRange::offset_len(
red.start_offset(),
red.green().text_len(),
)
TextRange::offset_len(red.start_offset(), red.green().text_len())
}
pub fn text(&self) -> String {

View File

@ -1,8 +1,8 @@
extern crate libsyntax2;
extern crate testutils;
use libsyntax2::{parse};
use libsyntax2::utils::{dump_tree_green};
use libsyntax2::parse;
use libsyntax2::utils::dump_tree_green;
use testutils::dir_tests;
#[test]

View File

@ -1,8 +1,8 @@
extern crate difference;
extern crate file;
use std::path::{Path, PathBuf};
use std::fs::read_dir;
use std::path::{Path, PathBuf};
use difference::Changeset;

View File

@ -1,18 +1,14 @@
extern crate clap;
#[macro_use]
extern crate failure;
extern crate tera;
extern crate ron;
extern crate walkdir;
extern crate itertools;
extern crate ron;
extern crate tera;
extern crate walkdir;
use std::{
fs,
path::{Path},
collections::HashSet,
};
use clap::{App, Arg, SubCommand};
use itertools::Itertools;
use std::{collections::HashSet, fs, path::Path};
type Result<T> = ::std::result::Result<T, failure::Error>;
@ -29,7 +25,7 @@ fn main() -> Result<()> {
Arg::with_name("verify")
.long("--verify")
.help("Verify that generated code is up-to-date")
.global(true)
.global(true),
)
.subcommand(SubCommand::with_name("gen-kinds"))
.subcommand(SubCommand::with_name("gen-tests"))
@ -66,9 +62,8 @@ fn update(path: &Path, contents: &str, verify: bool) -> Result<()> {
fn get_kinds() -> Result<String> {
let grammar = grammar()?;
let template = fs::read_to_string(SYNTAX_KINDS_TEMPLATE)?;
let ret = tera::Tera::one_off(&template, &grammar, false).map_err(|e| {
format_err!("template error: {}", e)
})?;
let ret = tera::Tera::one_off(&template, &grammar, false)
.map_err(|e| format_err!("template error: {}", e))?;
Ok(ret)
}
@ -142,7 +137,8 @@ fn tests_from_dir(dir: &Path) -> Result<HashSet<Test>> {
fn collect_tests(s: &str) -> Vec<Test> {
let mut res = vec![];
let prefix = "// ";
let comment_blocks = s.lines()
let comment_blocks = s
.lines()
.map(str::trim_left)
.group_by(|line| line.starts_with(prefix));
@ -181,4 +177,3 @@ fn existing_tests(dir: &Path) -> Result<HashSet<Test>> {
}
Ok(res)
}