Skip to content

Commit efadcf7

Browse files
committed
Merge #15
15: Add minimal docs to most public symbols r=matklad a=CAD97 Also sticks a safety on some warnings that rustc provides. If you're just stubbing out a module and don't want rustc refusing to compile it because you haven't documented it yet, stick `#![allow(missing_docs)]` at the top.
2 parents 6d3caf5 + 50b9012 commit efadcf7

File tree

6 files changed

+60
-3
lines changed

6 files changed

+60
-3
lines changed

src/lexer/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use self::strings::{is_string_literal_start, scan_byte_char_or_string, scan_char
1717
mod comments;
1818
use self::comments::{scan_comment, scan_shebang};
1919

20+
/// Break a string up into its component tokens
2021
pub fn tokenize(text: &str) -> Vec<Token> {
2122
let mut text = text;
2223
let mut acc = Vec::new();
@@ -28,6 +29,7 @@ pub fn tokenize(text: &str) -> Vec<Token> {
2829
}
2930
acc
3031
}
32+
/// Get the next token from a string
3133
pub fn next_token(text: &str) -> Token {
3234
assert!(!text.is_empty());
3335
let mut ptr = Ptr::new(text);

src/lib.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
//! An experimental implementation of [Rust RFC#2256 libsyntax2.0][rfc#2256].
2+
//!
3+
//! The intent is to be an IDE-ready parser, i.e. one that offers
4+
//!
5+
//! - easy and fast incremental re-parsing,
6+
//! - graceful handling of errors, and
7+
//! - maintains all information in the source file.
8+
//!
9+
//! For more information, see [the RFC][rfc#2265], or [the working draft][RFC.md].
10+
//!
11+
//! [rfc#2256]: <https://github.com/rust-lang/rfcs/pull/2256>
12+
//! [RFC.md]: <https://github.com/matklad/libsyntax2/blob/master/docs/RFC.md>
13+
14+
#![forbid(missing_debug_implementations, unconditional_recursion, future_incompatible)]
15+
#![deny(bad_style, unsafe_code, missing_docs)]
16+
//#![warn(unreachable_pub)] // rust-lang/rust#47816
17+
118
extern crate unicode_xid;
219

320
mod text;
@@ -6,17 +23,20 @@ mod lexer;
623
mod parser;
724

825
#[cfg_attr(rustfmt, rustfmt_skip)]
26+
#[allow(missing_docs)]
927
pub mod syntax_kinds;
1028
pub use text::{TextRange, TextUnit};
1129
pub use tree::{File, FileBuilder, Node, Sink, SyntaxKind, Token};
1230
pub use lexer::{next_token, tokenize};
1331
pub use parser::parse;
1432

33+
/// Utilities for simple uses of the parser.
1534
pub mod utils {
1635
use std::fmt::Write;
1736

1837
use {File, Node};
1938

39+
/// Parse a file and create a string representation of the resulting parse tree.
2040
pub fn dump_tree(file: &File) -> String {
2141
let mut result = String::new();
2242
go(file.root(), &mut result, 0);

src/parser/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use tree::TOMBSTONE;
66
mod event_parser;
77
use self::event_parser::Event;
88

9+
/// Parse a sequence of tokens into the representative node tree
910
pub fn parse(text: String, tokens: &[Token]) -> File {
1011
let events = event_parser::parse(&text, tokens);
1112
from_events_to_file(text, tokens, events)

src/text.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
use std::fmt;
22
use std::ops;
33

4+
/// An text position in a source file
45
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
56
pub struct TextUnit(u32);
67

78
impl TextUnit {
9+
/// The positional offset required for one character
810
pub fn len_of_char(c: char) -> TextUnit {
911
TextUnit(c.len_utf8() as u32)
1012
}
1113

14+
#[allow(missing_docs)]
1215
pub fn new(val: u32) -> TextUnit {
1316
TextUnit(val)
1417
}
@@ -64,6 +67,7 @@ impl ops::SubAssign<TextUnit> for TextUnit {
6467
}
6568
}
6669

70+
/// A range of text in a source file
6771
#[derive(Clone, Copy, PartialEq, Eq)]
6872
pub struct TextRange {
6973
start: TextUnit,
@@ -83,10 +87,12 @@ impl fmt::Display for TextRange {
8387
}
8488

8589
impl TextRange {
90+
/// An length-0 range of text
8691
pub fn empty() -> TextRange {
8792
TextRange::from_to(TextUnit::new(0), TextUnit::new(0))
8893
}
8994

95+
/// The left-inclusive range (`[from..to)`) between to points in the text
9096
pub fn from_to(from: TextUnit, to: TextUnit) -> TextRange {
9197
assert!(from <= to, "Invalid text range [{}; {})", from, to);
9298
TextRange {
@@ -95,22 +101,27 @@ impl TextRange {
95101
}
96102
}
97103

104+
/// The range from some point over some length
98105
pub fn from_len(from: TextUnit, len: TextUnit) -> TextRange {
99106
TextRange::from_to(from, from + len)
100107
}
101108

109+
/// The starting position of this range
102110
pub fn start(&self) -> TextUnit {
103111
self.start
104112
}
105113

114+
/// The end position of this range
106115
pub fn end(&self) -> TextUnit {
107116
self.end
108117
}
109118

119+
/// The length of this range
110120
pub fn len(&self) -> TextUnit {
111121
self.end - self.start
112122
}
113123

124+
/// Is this range empty of any content?
114125
pub fn is_empty(&self) -> bool {
115126
self.start() == self.end()
116127
}

src/tree/file_builder.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// FIXME(CAD97): I don't understand this mod well enough to stub out docs for the public symbols yet
2+
#![allow(missing_docs)]
3+
14
use {SyntaxKind, TextRange, TextUnit};
25
use super::{File, NodeData, NodeIdx, SyntaxErrorData};
36

@@ -8,6 +11,7 @@ pub trait Sink {
811
fn error(&mut self) -> ErrorBuilder;
912
}
1013

14+
#[derive(Debug)]
1115
pub struct FileBuilder {
1216
text: String,
1317
nodes: Vec<NodeData>,
@@ -139,6 +143,7 @@ fn grow(left: &mut TextRange, right: TextRange) {
139143
*left = TextRange::from_to(left.start(), right.end())
140144
}
141145

146+
#[derive(Debug)]
142147
pub struct ErrorBuilder<'f> {
143148
message: Option<String>,
144149
builder: &'f mut FileBuilder,

src/tree/mod.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::cmp;
77
mod file_builder;
88
pub use self::file_builder::{FileBuilder, Sink};
99

10+
/// The kind of syntax node, e.g. `IDENT`, `USE_KW`, or `STRUCT_DEF`.
1011
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
1112
pub struct SyntaxKind(pub(crate) u32);
1213

@@ -37,19 +38,25 @@ pub(crate) struct SyntaxInfo {
3738
pub name: &'static str,
3839
}
3940

41+
/// A token of Rust source.
4042
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
4143
pub struct Token {
44+
/// The kind of token.
4245
pub kind: SyntaxKind,
46+
/// The length of the token.
4347
pub len: TextUnit,
4448
}
4549

50+
/// The contents of a Rust source file.
51+
#[derive(Debug)]
4652
pub struct File {
4753
text: String,
4854
nodes: Vec<NodeData>,
4955
errors: Vec<SyntaxErrorData>,
5056
}
5157

5258
impl File {
59+
/// The root node of this source file.
5360
pub fn root<'f>(&'f self) -> Node<'f> {
5461
assert!(!self.nodes.is_empty());
5562
Node {
@@ -59,35 +66,42 @@ impl File {
5966
}
6067
}
6168

69+
/// A reference to a token in a Rust source file.
6270
#[derive(Clone, Copy)]
6371
pub struct Node<'f> {
6472
file: &'f File,
6573
idx: NodeIdx,
6674
}
6775

6876
impl<'f> Node<'f> {
77+
/// The kind of the token at this node.
6978
pub fn kind(&self) -> SyntaxKind {
7079
self.data().kind
7180
}
7281

82+
/// The text range covered by the token at this node.
7383
pub fn range(&self) -> TextRange {
7484
self.data().range
7585
}
7686

87+
/// The text at this node.
7788
pub fn text(&self) -> &'f str {
7889
&self.file.text.as_str()[self.range()]
7990
}
8091

92+
/// The parent node to this node.
8193
pub fn parent(&self) -> Option<Node<'f>> {
8294
self.as_node(self.data().parent)
8395
}
8496

97+
/// The children nodes of this node.
8598
pub fn children(&self) -> Children<'f> {
8699
Children {
87100
next: self.as_node(self.data().first_child),
88101
}
89102
}
90103

104+
/// Any errors contained in this node.
91105
pub fn errors(&self) -> SyntaxErrors<'f> {
92106
let pos = self.file.errors.iter().position(|e| e.node == self.idx);
93107
let next = pos.map(|i| ErrorIdx(i as u32)).map(|idx| SyntaxError {
@@ -123,7 +137,7 @@ impl<'f> cmp::PartialEq<Node<'f>> for Node<'f> {
123137

124138
impl<'f> cmp::Eq for Node<'f> {}
125139

126-
#[derive(Clone, Copy)]
140+
#[derive(Clone, Copy, Debug)]
127141
pub struct SyntaxError<'f> {
128142
file: &'f File,
129143
idx: ErrorIdx,
@@ -162,6 +176,7 @@ impl<'f> SyntaxError<'f> {
162176
}
163177
}
164178

179+
#[derive(Debug)]
165180
pub struct Children<'f> {
166181
next: Option<Node<'f>>,
167182
}
@@ -176,6 +191,7 @@ impl<'f> Iterator for Children<'f> {
176191
}
177192
}
178193

194+
#[derive(Debug)]
179195
pub struct SyntaxErrors<'f> {
180196
next: Option<SyntaxError<'f>>,
181197
}
@@ -190,9 +206,10 @@ impl<'f> Iterator for SyntaxErrors<'f> {
190206
}
191207
}
192208

193-
#[derive(Clone, Copy, PartialEq, Eq)]
209+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
194210
struct NodeIdx(u32);
195211

212+
#[derive(Debug)]
196213
struct NodeData {
197214
kind: SyntaxKind,
198215
range: TextRange,
@@ -215,9 +232,10 @@ impl ::std::ops::IndexMut<NodeIdx> for Vec<NodeData> {
215232
}
216233
}
217234

218-
#[derive(Clone, Copy)]
235+
#[derive(Clone, Copy, Debug)]
219236
struct ErrorIdx(u32);
220237

238+
#[derive(Debug)]
221239
struct SyntaxErrorData {
222240
node: NodeIdx,
223241
message: String,

0 commit comments

Comments
 (0)