Skip to content

Commit 934380b

Browse files
committed
update parser chapter
1 parent 75a5f92 commit 934380b

File tree

4 files changed

+32
-23
lines changed

4 files changed

+32
-23
lines changed

src/SUMMARY.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
- [Incremental compilation](./queries/incremental-compilation.md)
3939
- [Incremental compilation In Detail](./queries/incremental-compilation-in-detail.md)
4040
- [Debugging and Testing](./incrcomp-debugging.md)
41-
- [The parser](./the-parser.md)
41+
- [Lexing and Parsing](./the-parser.md)
4242
- [`#[test]` Implementation](./test-implementation.md)
4343
- [Macro expansion](./macro-expansion.md)
4444
- [Name resolution](./name-resolution.md)

src/appendix/code-index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Item | Kind | Short description | Chapter |
2424
`SourceFile` | struct | Part of the `SourceMap`. Maps AST nodes to their source code for a single source file. Was previously called FileMap | [The parser] | [src/libsyntax_pos/lib.rs](https://doc.rust-lang.org/nightly/nightly-rustc/syntax/source_map/struct.SourceFile.html)
2525
`SourceMap` | struct | Maps AST nodes to their source code. It is composed of `SourceFile`s. Was previously called CodeMap | [The parser] | [src/libsyntax/source_map.rs](https://doc.rust-lang.org/nightly/nightly-rustc/syntax/source_map/struct.SourceMap.html)
2626
`Span` | struct | A location in the user's source code, used for error reporting primarily | [Emitting Diagnostics] | [src/libsyntax_pos/span_encoding.rs](https://doc.rust-lang.org/nightly/nightly-rustc/syntax_pos/struct.Span.html)
27-
`StringReader` | struct | This is the lexer used during parsing. It consumes characters from the raw source code being compiled and produces a series of tokens for use by the rest of the parser | [The parser] | [src/libsyntax/parse/lexer/mod.rs](https://doc.rust-lang.org/nightly/nightly-rustc/syntax/parse/lexer/struct.StringReader.html)
27+
`StringReader` | struct | This is the lexer used during parsing. It consumes characters from the raw source code being compiled and produces a series of tokens for use by the rest of the parser | [The parser] | [src/librustc_parse/lexer/mod.rs](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_parse/lexer/struct.StringReader.html)
2828
`syntax::token_stream::TokenStream` | struct | An abstract sequence of tokens, organized into `TokenTree`s | [The parser], [Macro expansion] | [src/libsyntax/tokenstream.rs](https://doc.rust-lang.org/nightly/nightly-rustc/syntax/tokenstream/struct.TokenStream.html)
2929
`TraitDef` | struct | This struct contains a trait's definition with type information | [The `ty` modules] | [src/librustc/ty/trait_def.rs](https://doc.rust-lang.org/nightly/nightly-rustc/rustc/ty/trait_def/struct.TraitDef.html)
3030
`TraitRef` | struct | The combination of a trait and its input types (e.g. `P0: Trait<P1...Pn>`) | [Trait Solving: Goals and Clauses], [Trait Solving: Lowering impls] | [src/librustc/ty/sty.rs](https://doc.rust-lang.org/nightly/nightly-rustc/rustc/ty/struct.TraitRef.html)

src/macro-expansion.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Macro expansion
22

3+
> `libsyntax`, `librustc_expand`, and `libsyntax_ext` are all undergoing
4+
> refactoring, so some of the links in this chapter may be broken.
5+
36
Macro expansion happens during parsing. `rustc` has two parsers, in fact: the
47
normal Rust parser, and the macro parser. During the parsing phase, the normal
58
Rust parser will set aside the contents of macros and their invocations. Later,

src/the-parser.md

+27-21
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,35 @@
1-
# The Parser
1+
# Lexing and Parsing
22

3-
The parser is responsible for converting raw Rust source code into a structured
4-
form which is easier for the compiler to work with, usually called an [*Abstract
5-
Syntax Tree*][ast]. An AST mirrors the structure of a Rust program in memory,
6-
using a `Span` to link a particular AST node back to its source text.
3+
> The parser and lexer are currently undergoing a lot of refactoring, so parts
4+
> of this chapter may be out of date.
5+
6+
The very first thing the compiler does is take the program (in Unicode
7+
characters) and turn it into something the compiler can work with more
8+
conveniently than strings. This happens in two stages: Lexing and Parsing.
79

8-
The bulk of the parser lives in the [libsyntax] crate.
10+
Lexing takes strings and turns them into streams of tokens. For example,
11+
`a.b + c` would be turned into the tokens `a`, `.`, `b`, `+`, and `c`.
12+
The lexer lives in [`librustc_lexer`][lexer].
913

10-
Like most parsers, the parsing process is composed of two main steps,
14+
[lexer]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_lexer/index.html
1115

12-
- lexical analysis – turn a stream of characters into a stream of token trees
13-
- parsing – turn the token trees into an AST
16+
Parsing then takes streams of tokens and turns them into a structured
17+
form which is easier for the compiler to work with, usually called an [*Abstract
18+
Syntax Tree*][ast] (AST). An AST mirrors the structure of a Rust program in memory,
19+
using a `Span` to link a particular AST node back to its source text.
1420

15-
The `syntax` crate contains several main players,
21+
The AST is defined in [`libsyntax`][libsyntax], along with some definitions for
22+
tokens and token streams, data structures/traits for mutating ASTs, and shared
23+
definitions for other AST-related parts of the compiler (like the lexer and
24+
macro-expansion).
1625

17-
- a [`SourceMap`] for mapping AST nodes to their source code
18-
- the [ast module] contains types corresponding to each AST node
19-
- a [`StringReader`] for lexing source code into tokens
20-
- the [parser module] and [`Parser`] struct are in charge of actually parsing
21-
tokens into AST nodes,
22-
- and a [visit module] for walking the AST and inspecting or mutating the AST
23-
nodes.
26+
The parser is defined in [`librustc_parse`][librustc_parse], along with a
27+
high-level interface to the lexer and some validation routines that run after
28+
macro expansion. In particular, the [`rustc_parser::parser`][parser] contains
29+
the parser implementation.
2430

2531
The main entrypoint to the parser is via the various `parse_*` functions in the
26-
[parser module]. They let you do things like turn a [`SourceFile`][sourcefile]
32+
[parser][parser]. They let you do things like turn a [`SourceFile`][sourcefile]
2733
(e.g. the source in a single file) into a token stream, create a parser from
2834
the token stream, and then execute the parser to get a `Crate` (the root AST
2935
node).
@@ -44,14 +50,14 @@ Code for lexical analysis is split between two crates:
4450
specific data structures. Specifically, it adds `Span` information to tokens
4551
returned by `rustc_lexer` and interns identifiers.
4652

47-
4853
[libsyntax]: https://doc.rust-lang.org/nightly/nightly-rustc/syntax/index.html
4954
[rustc_errors]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_errors/index.html
5055
[ast]: https://en.wikipedia.org/wiki/Abstract_syntax_tree
5156
[`SourceMap`]: https://doc.rust-lang.org/nightly/nightly-rustc/syntax/source_map/struct.SourceMap.html
5257
[ast module]: https://doc.rust-lang.org/nightly/nightly-rustc/syntax/ast/index.html
53-
[parser module]: https://doc.rust-lang.org/nightly/nightly-rustc/syntax/parse/index.html
58+
[librustc_parse]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_parse/index.html
59+
[parser]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_parse/parser/index.html
5460
[`Parser`]: https://doc.rust-lang.org/nightly/nightly-rustc/syntax/parse/parser/struct.Parser.html
55-
[`StringReader`]: https://doc.rust-lang.org/nightly/nightly-rustc/syntax/parse/lexer/struct.StringReader.html
61+
[`StringReader`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_parse/lexer/struct.StringReader.html
5662
[visit module]: https://doc.rust-lang.org/nightly/nightly-rustc/syntax/visit/index.html
5763
[sourcefile]: https://doc.rust-lang.org/nightly/nightly-rustc/syntax/source_map/struct.SourceFile.html

0 commit comments

Comments
 (0)