Skip to content

Commit

Permalink
document compile contract
Browse files Browse the repository at this point in the history
The internal documentaion is a little lighter than
usual in `src/compile.rs`, so I wrote up the impression
of the compilation contract I developed by reading the
code. This is honestly a thinly veiled way to check my
own understanding of what is going on. Hopefully it will
be valuable to the next person who starts digging into
the compiler.
  • Loading branch information
Ethan Pailes committed Dec 8, 2017
1 parent 37bfbd9 commit 8411e22
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
22 changes: 22 additions & 0 deletions HACKING.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,3 +313,25 @@ https://github.com/BurntSushi/cargo-benchcmp

The `run-bench` utility can run benchmarks for PCRE and Oniguruma too. See
`./run-bench --help`.

## Dev Docs

When digging your teeth into the codebase for the first time, the
crate documentation can be a great resource. By default `rustdoc`
will strip out all documentation of private crate members in an
effort to help consumers of the crate focus on the *interface*
without having to concern themselves with the *implimentation*.
Normally this is a great thing, but if you want to start hacking
on regex internals it is not what you want. Many of the private members
of this crate are well documented with rustdoc style comments, and
it would be a shame to miss out on the opportunity that presents.
You can generate the private docs with:

```
> rustdoc --crate-name docs src/lib.rs -o target/doc -L target/debug/deps --no-defaults --passes collapse-docs --passes unindent-comments
```

Then just point your browser at `target/doc/regex/index.html`.

See https://github.com/rust-lang/rust/issues/15347 for more info
about generating developer docs for internal use.
52 changes: 52 additions & 0 deletions src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,58 @@ impl Compiler {
Ok(self.compiled)
}

/// Compile expr into self.insts, returning a patch on success,
/// or an error if we run out of memory.
///
/// All of the c_* methods of the compiler share the contract outlined
/// here.
///
/// The main thing that a c_* method does is mutate `self.insts`
/// to add a list of mostly compiled instructions required to execute
/// the given expression. `self.insts` contains MaybeInsts rather than
/// Insts because there is some backpatching required.
///
/// The `Patch` value returned by each c_* method provides metadata
/// about the compiled instructions emitted to `self.insts`. The
/// `entry` member of the patch refers to the first instruction
/// (the entry point), while the `hole` member contains zero or
/// more offsets to partial instructions that need to be backpatched.
/// The c_* routine can't know where its list of instructions are going to
/// jump to after execution, so it is up to the caller to patch
/// these jumps to point to the right place. So compiling some
/// expression, e, we would end up with a situation that looked like:
///
/// ```text
/// self.insts = [ ..., i1, i2, ..., iexit1, ..., iexitn, ...]
/// ^ ^ ^
/// | \ /
/// entry \ /
/// hole
/// ```
///
/// To compile two expressions, e1 and e2, concatinated together we
/// would do:
///
/// ```ignore
/// let patch1 = self.c(e1);
/// let patch2 = self.c(e2);
/// ```
///
/// while leaves us with a situation that looks like
///
/// ```text
/// self.insts = [ ..., i1, ..., iexit1, ..., i2, ..., iexit2 ]
/// ^ ^ ^ ^
/// | | | |
/// entry1 hole1 entry2 hole2
/// ```
///
/// Then to merge the two patches together into one we would backpatch
/// hole1 with entry2 and return a new patch that enters at entry1
/// and has hole2 for a hole. In fact, if you look at the c_concat
/// method you will see that it does exactly this, though it handles
/// a list of expressions rather than just the two that we use for
/// an example.
fn c(&mut self, expr: &Expr) -> Result {
use prog;
use syntax::Expr::*;
Expand Down

0 comments on commit 8411e22

Please sign in to comment.