From 3a9c5ac8aef88d21d4e1e9838e6cb3f304aa9e71 Mon Sep 17 00:00:00 2001 From: Mart Lubbers Date: Sat, 11 Jun 2016 08:15:15 +0200 Subject: [PATCH] parse example toegevoegd --- deliverables/report/pars.tex | 42 ++++++++++++++++++++++++++++++++++ deliverables/report/report.tex | 1 + 2 files changed, 43 insertions(+) diff --git a/deliverables/report/pars.tex b/deliverables/report/pars.tex index c7df619..c849e1c 100644 --- a/deliverables/report/pars.tex +++ b/deliverables/report/pars.tex @@ -81,3 +81,45 @@ handles some syntactic sugar(Section~\ref{ssec:synsugar}). For example the parser expands the literal lists and literal strings to the according list or string representation. Moreover the parser transforms the let expressions to real functions representing constant values. + +As an example we show the parsing of a \CI{FunDecl} in +Listing~\ref{lst:fundecl}. The \CI{FunDecl} is one of the most complex parsers +and is composed of as complex subparsers. A \CI{FunDecl} parser exactly one +complete function. + +First we do the non consuming \CI{peekPos} to make sure the positionality of +the function is guaranteed in the \AST{}, after that the identifier is parsed +which is basically a parser that transforms an \CI{IdToken} into a \CI{String}. + +\CI{parseBBraces} is a parser combinator built from the basic combinators that +parses a parser when the contents are surrounded by braces. This is followed by +a yet another helper function to parse comma separated items. + +Since the next element of the \ADT{} is a \CI{Maybe} the next parser is +combined with the optional combinator since the type of a function is optional +and when not given it will be inferred. + +A function consists of \emph{many} \CI{VarDecl}s and then \emph{many} +\CI{Stmt}s. The word \emph{many} in \Yard{} means zero or more while the word +\emph{some} means one or more. Thus we allow a function to be empty. The reason +why \CI{flatten} is mapped on to the last argument of the \CI{liftM6} is +because the \CI{parseStmt} returns possibly multiple \CI{Stmt}s. This is +because in the parsing phase we expand the \CI{print} functions into multiple +instances and thus it can be the case that from one \CI{Stmt} we actually get +multiple. + +\begin{lstlisting}[ + language=Clean, + label={lst:fundecl}, + caption={Parsing a \CI{FunDecl}}] +:: FunDecl = FunDecl Pos String [String] (Maybe Type) [VarDecl] [Stmt] + +parseFunDecl :: Parser Token FunDecl +parseFunDecl = liftM6 FunDecl + (peekPos) + (parseIdent) + (parseBBraces $ parseSepList CommaToken parseIdent) + (optional (satTok DoubleColonToken *> parseFunType)) + (satTok CBraceOpenToken *> many parseVarDecl) + (flatten <$> (many parseStmt <* satTok CBraceCloseToken)) +\end{lstlisting} diff --git a/deliverables/report/report.tex b/deliverables/report/report.tex index 6de42e5..e8deb22 100644 --- a/deliverables/report/report.tex +++ b/deliverables/report/report.tex @@ -23,6 +23,7 @@ \newcommand{\SSM}{\texttt{SSM}} \newcommand{\Yard}{\textsc{Yard}} \newcommand{\AST}{\emph{AST}} +\newcommand{\ADT}{\emph{ADT}} \let\tt\texttt -- 2.20.1