stgi-1.1: Educational implementation of the STG (Spineless Tagless G-machine)

Safe HaskellNone
LanguageHaskell2010

Stg.Parser.Parser

Contents

Description

A parser for the STG language, modeled after the grammar given in the description in the 1992 paper (link) with a couple of differences to enhance usability:

  • Function application uses no parentheses or commas like in Haskell (f x y z), not with curly parentheses and commas like in the paper (f {x,y,z}).
  • Comment syntax like in Haskell: -- inline, {- multiline -}.
  • Constructors may end with a # to allow labelling primitive boxes e.g. with Int#.
  • A lambda's head is written \(free) bound -> body, where free and bound are space-separated variable lists, instead of the paper's {free} \n {bound} -> body, which uses comma-separated lists. The update flag \u is signified using a double arrow => instead of the normal arrow ->.

Synopsis

General parsing

parse :: StgParser ast -> Text -> Either Doc ast Source #

Parse STG source using a user-specified parser. To parse a full program, use parse program.

>>> parse program "id = \\x -> x"
Right (Program (Binds [(Var "id",LambdaForm [] NoUpdate [Var "x"] (AppF (Var "x") []))]))

data StgParser ast Source #

A parser for an STG syntax element.

Instances

Monad StgParser Source # 

Methods

(>>=) :: StgParser a -> (a -> StgParser b) -> StgParser b #

(>>) :: StgParser a -> StgParser b -> StgParser b #

return :: a -> StgParser a #

fail :: String -> StgParser a #

Functor StgParser Source # 

Methods

fmap :: (a -> b) -> StgParser a -> StgParser b #

(<$) :: a -> StgParser b -> StgParser a #

Applicative StgParser Source # 

Methods

pure :: a -> StgParser a #

(<*>) :: StgParser (a -> b) -> StgParser a -> StgParser b #

(*>) :: StgParser a -> StgParser b -> StgParser b #

(<*) :: StgParser a -> StgParser b -> StgParser a #

Alternative StgParser Source # 

Methods

empty :: StgParser a #

(<|>) :: StgParser a -> StgParser a -> StgParser a #

some :: StgParser a -> StgParser [a] #

many :: StgParser a -> StgParser [a] #

TokenParsing StgParser Source # 
CharParsing StgParser Source # 
Parsing StgParser Source # 

Parser rules

program :: StgParser Program Source #

Parse an STG program.

binds :: StgParser Binds Source #

Parse a collection of bindings, used by let(rec) expressions and at the top level of a program.

lambdaForm :: StgParser LambdaForm Source #

Parse a lambda form, consisting of a list of free variables, and update flag, a list of bound variables, and the function body.

expr :: StgParser Expr Source #

Parse an expression, which can be

  • let, let(rec) ... in ...
  • case, case ... of ...
  • function application, f (...)
  • constructor application, C (...)
  • primitive application, p# (...)
  • literal, 1#

alts :: StgParser Alts Source #

Parse the alternatives given in a case expression.

nonDefaultAlts :: StgParser NonDefaultAlts Source #

Parse non-default alternatives. The list of alternatives can be either empty, all algebraic, or all primitive.

Nil -> ...
Cons x xs -> ...
1# -> ...
2# -> ...

algebraicAlt :: StgParser AlgebraicAlt Source #

Parse a single algebraic alternative.

Cons x xs -> ...

primitiveAlt :: StgParser PrimitiveAlt Source #

Parse a single primitive alternative, such as 1#.

1# -> ...

defaultAlt :: StgParser DefaultAlt Source #

Parse the default alternative, taken if none of the other alternatives in a case expression match.

default -> ...
v -> ...

literal :: StgParser Literal Source #

Parse an integer literal

1#
-123#

primOp :: StgParser PrimOp Source #

Parse a primitive operation.

+#

atom :: StgParser Atom Source #

Parse an atom

var :: StgParser Var Source #

Parse a variable identifier. Variables start with a lower-case letter or _, followed by a string consisting of alphanumeric characters or ', _.

con :: StgParser Constr Source #

Parse a constructor identifier. Constructors follow the same naming conventions as variables, but start with an upper-case character instead, and may end with a # symbol.