language-rust: Parsing and pretty printing of Rust code

[ bsd3, language, library ] [ Propose Tags ] [ Report a vulnerability ]

Language Rust is a library for the analysis of Rust code. It includes a complete, well tested parser and pretty printer.


[Skip to Readme]

Flags

Automatic Flags
NameDescriptionDefault
usebytestrings

Use ByteString instead of String as InputStream datatype

Enabled
enablequasiquotes

Provide the experimental Language.Rust.Quote module

Enabled

Use -f <flag> to enable a flag, or -f -<flag> to disable that flag. More info

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

Versions [RSS] 0.1.0.0, 0.1.0.26, 0.1.1.26, 0.2.0.27 (info)
Change log ChangeLog.md
Dependencies array (>=0.5 && <0.6), base (>=4.8 && <5.0), bytestring (>=0.10), deepseq (>=1.1 && <1.5), prettyprinter (>=1.0 && <2.0), semigroups (>=0.18), template-haskell (>=2.10), transformers (>=0.4 && <0.6), utf8-string (>=1.0) [details]
License BSD-3-Clause
Copyright (c) 2017-2018 Alec Theriault
Author Alec Theriault
Maintainer alec.theriault@gmail.com
Category Language
Home page https://github.com/harpocrates/language-rust
Bug tracker https://github.com/harpocrates/language-rust/issues
Source repo head: git clone https://github.com/harpocrates/language-rust.git
Uploaded by harpocrates at 2018-04-22T16:07:58Z
Distributions
Reverse Dependencies 1 direct, 0 indirect [details]
Downloads 1984 total (16 in the last 30 days)
Rating 2.25 (votes: 2) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2018-04-22 [all 1 reports]

Readme for language-rust-0.2.0.27

[back to package description]

Parser and pretty printer for Rust Build Status Windows build status Hackage Version

language-rust aspires to efficiently and accurately parse and pretty print the Rust language. The underlying AST structures are also intended to be as similar as possible to the libsyntax AST that rustc itself uses.

A typical use looks like:

>>> :set -XTypeApplications +t
>>> import Language.Rust.Syntax

>>> -- Sample use of the parser
>>> import Language.Rust.Parser
>>> let inp = inputStreamFromString "fn main () { println!(\"Hello world!\"); }"
inp :: InputStream
>>> let sourceFile = parse' @(SourceFile Span) inp
sourceFile :: SourceFile Span

>>> -- Sample use of the pretty printer
>>> import Language.Rust.Pretty
>>> pretty' sourceFile
fn main() {
  println!("Hello world!");
}
it :: Doc b

Building

Cabal

With Cabal and GHC, run

cabal install happy --constraint 'happy >= 1.19.8'
cabal install alex
cabal configure
cabal build

Stack

With the Stack tool installed, run

stack init
stack build

The second command is responsible for pulling in all of the dependencies (including executable tools like Alex, Happy, and GHC itself) and then compiling everything. If Stack complains about the version of Happy installed, you can explicitly install a recent one with stack install happy-1.19.8.

Evolution of Rust

As Rust evolves, so will language-rust. A best effort will be made to support unstable features from nightly as they come out, but only compatibility with stable is guaranteed. The last component of the version number indicates the nightly Rust compiler version against which tests were run. For example, 0.1.0.26 is tested against rustc 1.26.0-nightly.

Bugs

Please report any bugs to the github issue tracker.

Parser

Any difference between what is accepted by the rustc parser and the language-rust parser indicates one of

  • a bug in language-rust (this is almost always the case)
  • a bug in rustc
  • that there is a newer version of rustc which made a breaking change to this syntax

If the AST/parser of rustc changes, the rustc-tests test suite should start failing - it compares the JSON AST debug output of rustc to our parsed AST.

Pretty printer

For the pretty printer, bugs are a bit tougher to list exhaustively. Suggestions for better layout algorithms are most welcome! The fmt-rfcs repo is loosely used as the reference for "correct" pretty printing.