language-rust-0.1.0.0: Parsing and pretty printing of Rust code

Copyright(c) Alec Theriault 2017-2018
LicenseBSD-style
Maintaineralec.theriault@gmail.com
Stabilityexperimental
PortabilityGHC
Safe HaskellNone
LanguageHaskell2010

Language.Rust.Quote

Description

Quasiquoters for converting Rust code into the equivalent Haskell patterns and expressions. These are just convenience wrappers over dataToExpQ and dataToPatQ. These quasiquoters only work as expressions and patterns, not as declarations or types. The pattern quasiquoters recursively ignore any Span or Position fields (replacing them with wildcard patterns).

Using quasiquotes instead of manually writing out the AST means that even if the AST evolves (perhaps by adding extra fields to certain nodes), your code is likely to continue to work.

The examples below assume the following GHCi flag and import:

>>> :set -XQuasiQuotes
>>> import Control.Monad ( void )

Synopsis

Documentation

lit :: QuasiQuoter Source #

Quasiquoter for literals (see Lit).

>>> void [lit| 1.4e29f64 |]
Float 1.4e29 F64 ()

attr :: QuasiQuoter Source #

Quasiquoter for attributes (see Attribute)

>>> void [attr| #[no_mangle] |]
Attribute Outer (Path False [PathSegment "no_mangle" Nothing ()] ()) (Stream []) ()

ty :: QuasiQuoter Source #

Quasiquoter for types (see Ty)

>>> void [ty| &(_,_) |]
Rptr Nothing Immutable (TupTy [Infer (),Infer ()] ()) ()

pat :: QuasiQuoter Source #

Quasiquoter for patterns (see Pat)

>>> void [pat| x @ 1...5 |]
IdentP (ByValue Immutable) "x" (Just (RangeP (Lit [] (Int Dec 1 Unsuffixed ()) ())
                                             (Lit [] (Int Dec 5 Unsuffixed ()) ()) ())) ()

stmt :: QuasiQuoter Source #

Quasiquoter for statements (see Stmt)

>>> void [stmt| let x = 4i32; |]
Local (IdentP (ByValue Immutable) "x" Nothing ()) Nothing (Just (Lit [] (Int Dec 4 I32 ()) ())) [] ()

expr :: QuasiQuoter Source #

Quasiquoter for expressions (see Expr)

>>> void [expr| (x,) |]
TupExpr [] [PathExpr [] Nothing (Path False [PathSegment "x" Nothing ()] ()) ()] ()

item :: QuasiQuoter Source #

Quasiquoter for items (see Item)

>>> void [item| type Unit = (); |]
TyAlias [] InheritedV "Unit" (TupTy [] ()) (Generics [] [] (WhereClause [] ()) ()) ()

sourceFile :: QuasiQuoter Source #

Quasiquoter for a whole source file (see SourceFile)

>>> void [sourceFile| fn main() { } |]
SourceFile Nothing [] [Fn [] InheritedV "main"
                          (FnDecl [] Nothing False ())
                          Normal NotConst Rust
                          (Generics [] [] (WhereClause [] ()) ())
                          (Block [] Normal ()) ()]

implItem :: QuasiQuoter Source #

Quasiquoter for impl items (see ImplItem)

>>> void [implItem| type Item = (); |]
TypeI [] InheritedV Final "Item" (TupTy [] ()) ()

traitItem :: QuasiQuoter Source #

Quasiquoter for trait item (see TraitItem)

>>> void [traitItem| type Item; |]
TypeT [] "Item" [] Nothing ()

tokenTree :: QuasiQuoter Source #

Quasiquoter for token trees (see TokenTree)

>>> [tokenTree| fn |]
Token (Span (Position 1 2 14) (Position 3 2 16)) fn

block :: QuasiQuoter Source #

Quasiquoter for blocks (see Block)

>>> void [block| unsafe { 1i32 } |]
Block [NoSemi (Lit [] (Int Dec 1 I32 ()) ()) ()] Unsafe ()