disco-0.1.5: Functional programming language for teaching discrete math.
Copyrightdisco team and contributors
LicenseBSD-3-Clause
Maintainerbyorgey@gmail.com
Safe HaskellNone
LanguageHaskell2010

Disco.Parser

Description

Parser to convert concrete Disco syntax into an (untyped, surface language) AST.

Synopsis

Parser type and utilities

type Parser = StateT ParserState (Parsec DiscoParseError String) Source #

A parser is a megaparsec parser of strings, with an extra layer of state to keep track of the current indentation level and language extensions, and some custom error messages.

runParser :: Parser a -> FilePath -> String -> Either (ParseErrorBundle String DiscoParseError) a Source #

Run a parser from the initial state.

withExts :: Set Ext -> Parser a -> Parser a Source #

Locally set the enabled extensions within a subparser.

indented :: Parser a -> Parser a Source #

indented p is just like p, except that every token must not start in the first column.

thenIndented :: Parser a -> Parser a Source #

indented p is just like p, except that every token after the first must not start in the first column.

Lexer

Basic lexemes

sc :: Parser () Source #

Generically consume whitespace, including comments.

lexeme :: Parser a -> Parser a Source #

Parse a lexeme, that is, a parser followed by consuming whitespace.

symbol :: String -> Parser String Source #

Parse a given string as a lexeme.

reservedOp :: String -> Parser () Source #

Parse a reserved operator.

natural :: Parser Integer Source #

Parse a natural number.

reserved :: String -> Parser () Source #

Parse a reserved word.

reservedWords :: [String] Source #

The list of all reserved words.

ident :: Parser (Name Term) Source #

Parse an identifier and turn it into a Name.

Punctuation

lambda :: Parser String Source #

The symbol that starts an anonymous function (either a backslash or a Greek λ).

Disco parser

Modules

wholeModule :: LoadingMode -> Parser Module Source #

Parse the entire input as a module (with leading whitespace and no leftovers).

parseModule :: LoadingMode -> Parser Module Source #

Parse an entire module (a list of declarations ended by semicolons). The LoadingMode parameter tells us whether to include or replace any language extensions enabled at the top level. We include them when parsing a module entered at the REPL, and replace them when parsing a standalone module.

parseExtName :: Parser Ext Source #

Parse the name of a language extension (case-insensitive).

parseTopLevel :: Parser TopLevel Source #

Parse a top level item (either documentation or a declaration), which must start at the left margin.

parseDecl :: Parser Decl Source #

Parse a single top-level declaration (either a type declaration or single definition clause).

parseImport :: Parser String Source #

Parse an import, of the form import modulename.

parseModuleName :: Parser String Source #

Parse the name of a module.

Terms

term :: Parser Term Source #

Parse the entire input as a term (with leading whitespace and no leftovers).

parseTerm :: Parser Term Source #

Parse a term, consisting of a parseTerm' optionally followed by an ascription.

parseTerm' :: Parser Term Source #

Parse a non-atomic, non-ascribed term.

parseExpr :: Parser Term Source #

Parse an expression built out of unary and binary operators.

parseAtom :: Parser Term Source #

Parse an atomic term.

parseContainer :: Container -> Parser Term Source #

Parse a container, like a literal list, set, bag, or a comprehension (not including the square or curly brackets).

container
  ::= '[' container-contents ']'
    | '{' container-contents '}'

container-contents
  ::= empty | nonempty-container

nonempty-container
  ::= term [ ellipsis ]
    | term container-end

container-end
  ::= '|' comprehension
    | ',' [ term (',' item)* ] [ ellipsis ]

comprehension ::= qual [ ',' qual ]*

qual
  ::= ident 'in' term
    | term

ellipsis ::= '..' [ term ]

parseEllipsis :: Parser (Ellipsis Term) Source #

Parse an ellipsis at the end of a literal list, of the form .. t. Any number > 1 of dots may be used, just for fun.

parseContainerComp :: Container -> Term -> Parser Term Source #

Parse the part of a list comprehension after the | (without square brackets), i.e. a list of qualifiers.

q [,q]*

parseQual :: Parser Qual Source #

Parse a qualifier in a comprehension: either a binder x in t or a guard t.

parseLet :: Parser Term Source #

Parse a let expression (let x1 = t1, x2 = t2, ... in t).

Case and patterns

parseCase :: Parser Term Source #

Parse a case expression.

parseBranch :: Parser Branch Source #

Parse one branch of a case expression.

parseGuards :: Parser (Telescope Guard) Source #

Parse the list of guards in a branch. otherwise can be used interchangeably with an empty list of guards.

parseGuard :: Parser Guard Source #

Parse a single guard (if, if ... is, or let)

parsePattern :: Parser Pattern Source #

Parse a pattern, by parsing a term and then attempting to convert it to a pattern.

parseAtomicPattern :: Parser Pattern Source #

Parse an atomic pattern, by parsing a term and then attempting to convert it to a pattern.

Types

parseType :: Parser Type Source #

Parse a type expression built out of binary operators.

parseAtomicType :: Parser Type Source #

Parse an atomic type.