Copyright | (c) Alec Theriault 2017-2018 |
---|---|
License | BSD-style |
Maintainer | alec.theriault@gmail.com |
Stability | experimental |
Portability | GHC |
Safe Haskell | None |
Language | Haskell2010 |
Selecting the right parser may require adding an annotation or using -XTypeApplications
to avoid
an "Ambiguous type variable" error.
Using void
(as in the examples below) uses the fact that most AST nodes are
instances of Functor
to discard the Span
annotation that is attached to most parsed AST nodes.
Conversely, if you wish to extract the Span
annotation, the Located
typeclass provides a spanOf
method.
The examples below assume the following GHCi flags and imports:
>>>
:set -XTypeApplications -XOverloadedStrings
>>>
import Language.Rust.Syntax.AST
>>>
import Control.Monad ( void )
>>>
import System.IO
- parse :: Parse a => InputStream -> Either ParseFail a
- parse' :: Parse a => InputStream -> a
- readSourceFile :: Handle -> IO (SourceFile Span)
- readTokens :: Handle -> IO [Spanned Token]
- class Parse a where
- data P a
- execParser :: P a -> InputStream -> Position -> Either ParseFail a
- execParserTokens :: P a -> [Spanned Token] -> Position -> Either ParseFail a
- initPos :: Position
- data Span
- lexToken :: P (Spanned Token)
- lexNonSpace :: P (Spanned Token)
- lexTokens :: P (Spanned Token) -> P [Spanned Token]
- translateLit :: LitTok -> Suffix -> a -> Lit a
- readInputStream :: FilePath -> IO InputStream
- hReadInputStream :: Handle -> IO InputStream
- inputStreamToString :: InputStream -> String
- inputStreamFromString :: String -> InputStream
- lexicalError :: P a
- parseError :: Show b => b -> P a
- data ParseFail = ParseFail Position String
Parsing
parse :: Parse a => InputStream -> Either ParseFail a Source #
Parse something from an input stream (it is assumed the initial position is initPos
).
>>>
fmap void $ parse @(Expr Span) "x + 1"
Right (Binary [] AddOp (PathExpr [] Nothing (Path False [PathSegment "x" Nothing ()] ()) ()) (Lit [] (Int Dec 1 Unsuffixed ()) ()) ())
>>>
fmap void $ parse @(Expr Span) "x + "
Left (parse failure at 1:4 (Syntax error: unexpected `<EOF>' (expected an expression)))
parse' :: Parse a => InputStream -> a Source #
Same as parse
, but throws a ParseFail
exception if it cannot parse. This function is
intended for situations in which you are already stuck catching exceptions - otherwise you should
prefer parse
.
>>>
void $ parse' @(Expr Span) "x + 1"
Binary [] AddOp (PathExpr [] Nothing (Path False [PathSegment "x" Nothing ()] ()) ()) (Lit [] (Int Dec 1 Unsuffixed ()) ()) ()
>>>
void $ parse' @(Expr Span) "x + "
*** Exception: parse failure at 1:4 (Syntax error: unexpected `<EOF>' (expected an expression))
readSourceFile :: Handle -> IO (SourceFile Span) Source #
Given a handle to a Rust source file, read that file and parse it into a SourceFile
>>>
writeFile "empty_main.rs" "fn main() { }"
>>>
fmap void $ withFile "empty_main.rs" ReadMode readSourceFile
SourceFile Nothing [] [Fn [] InheritedV "main" (FnDecl [] Nothing False ()) Normal NotConst Rust (Generics [] [] (WhereClause [] ()) ()) (Block [] Normal ()) ()]
readTokens :: Handle -> IO [Spanned Token] Source #
Given a path pointing to a Rust source file, read that file and lex it (ignoring whitespace)
>>>
writeFile "empty_main.rs" "fn main() { }"
>>>
withFile "empty_main.rs" ReadMode readTokens
[fn,main,(,),{,}]
Describes things that can be parsed
Parse TokenTree Source # | |
Parse TokenStream Source # | |
Parse (WhereClause Span) Source # | |
Parse (TyParam Span) Source # | |
Parse (Ty Span) Source # | |
Parse (TraitItem Span) Source # | |
Parse (Stmt Span) Source # | |
Parse (Pat Span) Source # | |
Parse (Lit Span) Source # | |
Parse (SourceFile Span) Source # | |
Parse (LifetimeDef Span) Source # | |
Parse (Item Span) Source # | |
Parse (ImplItem Span) Source # | |
Parse (Generics Span) Source # | |
Parse (Expr Span) Source # | |
Parse (Block Span) Source # | |
Parse (Attribute Span) Source # | |
Parsing and lexing monad. A value of type
represents a parser that can be run (using
P
aexecParser
) to possibly produce a value of type a
.
execParser :: P a -> InputStream -> Position -> Either ParseFail a Source #
Execute the given parser on the supplied input stream at the given start position, returning either the position of an error and the error message, or the value parsed.
execParserTokens :: P a -> [Spanned Token] -> Position -> Either ParseFail a Source #
Same as execParser
, but working from a list of tokens instead of an InputStream
.
Spans represent a contiguous region of code, delimited by two Position
s. The endpoints are
inclusive. Analogous to the information encoded in a selection.
Eq Span Source # | |
Data Span Source # | |
Ord Span Source # | |
Show Span Source # | Field names are not shown |
Generic Span Source # | |
Semigroup Span Source # | smallest covering |
Monoid Span Source # | |
NFData Span Source # | |
Located Span Source # | |
Pretty Span Source # | |
Parse (WhereClause Span) Source # | |
Parse (TyParam Span) Source # | |
Parse (Ty Span) Source # | |
Parse (TraitItem Span) Source # | |
Parse (Stmt Span) Source # | |
Parse (Pat Span) Source # | |
Parse (Lit Span) Source # | |
Parse (SourceFile Span) Source # | |
Parse (LifetimeDef Span) Source # | |
Parse (Item Span) Source # | |
Parse (ImplItem Span) Source # | |
Parse (Generics Span) Source # | |
Parse (Expr Span) Source # | |
Parse (Block Span) Source # | |
Parse (Attribute Span) Source # | |
type Rep Span Source # | |
Lexing
lexToken :: P (Spanned Token) Source #
Lexer for one Token
. The only token this cannot produce is Interpolated
.
lexNonSpace :: P (Spanned Token) Source #
Lexer for one non-whitespace Token
. The only tokens this cannot produce are Interpolated
and Space
(which includes comments that aren't doc comments).
lexTokens :: P (Spanned Token) -> P [Spanned Token] Source #
Apply the given lexer repeatedly until (but not including) the Eof
token. Meant for debugging
purposes - in general this defeats the point of a threaded lexer.
Input stream
readInputStream :: FilePath -> IO InputStream Source #
Read an encoded file into an InputStream
hReadInputStream :: Handle -> IO InputStream Source #
Read an InputStream
from a Handle
inputStreamToString :: InputStream -> String Source #
Convert InputStream
to String
.
inputStreamFromString :: String -> InputStream Source #
Convert a String
to an InputStream
.
Error reporting
lexicalError :: P a Source #
Signal a lexical error.
parseError :: Show b => b -> P a Source #
Signal a syntax error.