Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
2251620
Superimposing files in subdirectories
hshkk Nov 27, 2026
0d7f416
Token properties and preliminary definitions for error codes
hshkk Nov 27, 2026
38c67c1
Improved errors for unclosed brackets and incomplete definitions
hshkk Nov 28, 2026
8c3afa8
Modularizing `LH.Text.Parser.Monad`
hshkk Nov 28, 2026
afbd1a1
Generating source files for the uppermost layer (and undoing `LH.Feat…
hshkk Nov 28, 2026
7b9fa72
Adding a 'LayerError' constructor
hshkk Nov 29, 2026
e2a76d4
Including `superimposeUpp` in parser tests
hshkk Nov 29, 2026
b98f970
Refactoring formatting functions
hshkk Nov 29, 2026
8f2d18f
`Eq` instances for annotated `haskell-src-exts` ASTs
hshkk Nov 30, 2026
49e3e60
Reasoning about sources of layer-specific syntax errors
hshkk Nov 30, 2026
9d6b046
`happy:happy ^>=1.20.11 => 2.2`
hshkk Nov 30, 2026
a5387c0
Adding lists of expected tokens
hshkk Nov 30, 2026
df77634
Adding `%error.expected` to all parsers
hshkk Nov 30, 2026
014914d
"Resolving" an odd interaction between parse errors occurring at diff…
hshkk Nov 30, 2026
799944f
A simpler alternative to multiple syntax errors
hshkk Nov 30, 2026
26b1cd4
Reading tokens
hshkk Nov 30, 2026
cfc0a62
Synonyms for `Label`s and `PossibleFix`es
hshkk Nov 30, 2026
2c9d99d
Preliminary definitions for interpreter and typechecker monads
hshkk Dec 1, 2026
103f8a3
Resolving incorrect placement of list and tuple expressions in `L`'s …
hshkk Dec 1, 2026
d61cd8a
Incorporating a combinator `firstAtWith`
hshkk Dec 2, 2026
e05c3f7
Improving hints in lexical errors
hshkk Dec 2, 2026
1613686
Revising design documents
hshkk Dec 2, 2026
f258d85
Resolving typos in documentation
hshkk Dec 2, 2026
34c2408
Parsing prefixes of programs for improved error identification
hshkk Dec 2, 2026
1c56e07
Updating interface for error codes
hshkk Dec 2, 2026
4357116
(Attempted) handling of prefix operators
hshkk Dec 3, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions LayeredHaskell-make/src-exe/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import System.FilePath ((</>))
import qualified Paths_LayeredHaskell_make as LHMake (version)

import LHMake.Data.Feature
(superimpose,
(superimpose, superimposeUpp,
list, listAll, listDeps,
generate)
import LHMake.Text.Label (infoLabel, hintLabel, warnLabel, errLabel)
Expand All @@ -17,8 +17,8 @@ features :: FilePath
features = "LayeredHaskell-make" </> "src" </> "LHMake" </> "Feature" </> "Features"

-- | The destination directory for generated source files.
dir :: FilePath
dir = "src" </> "LH" </> "Feature"
src :: FilePath
src = "src"

-- | The @LayeredHaskell-make@ executable facilitates superimposition.
-- If @info@ is passed in as a command-line argument,
Expand All @@ -30,7 +30,8 @@ main = do
case args of
[] -> do
names <- getNames
superimpose names dir
superimpose names src
superimposeUpp src
-- General information
["help"] -> help
["version"] -> version
Expand All @@ -46,7 +47,7 @@ main = do
"set":names
| null names -> do
putStrLn $ errLabel "One or more features must be enabled."
putStrLn $ hintLabel "For information on 'set', pass 'help' as an argument to LayeredHaskell-make."
putStrLn $ hintLabel "For information on `set', pass `help' as an argument to LayeredHaskell-make."
| otherwise -> do
writeFile features (unlines names)
putStrLn $ infoLabel "Enabled just now: " ++ unwords names
Expand All @@ -55,8 +56,9 @@ main = do
putStrLn $ warnLabel "One or more command-line arguments were given, but they are not recognized."
putStrLn $ warnLabel "Proceeding as if there were none..."
names <- getNames
superimpose names dir
putStrLn $ hintLabel "For a list of all commands, pass 'help' as an argument to LayeredHaskell-make."
superimpose names src
superimposeUpp src
putStrLn $ hintLabel "For a list of all commands, pass `help' as an argument to LayeredHaskell-make."
where
getNames = do
args <- readFile features
Expand All @@ -73,7 +75,7 @@ help = do
putStrLn " ... = c : ...all non-redundant feature combinations"
putStrLn " ... = d : ...all feature dependencies and implications"
putStrLn "set <args> : Enable a set of features"
putStrLn " .... = Any feature combination from 'ls c', separated by spaces"
putStrLn " .... = Any feature combination from `ls c', separated by spaces"
putStrLn "show : The set of currently enabled features"
putStrLn "version : The version"

Expand Down
283 changes: 147 additions & 136 deletions LayeredHaskell-make/src/LHMake/Control/Superimposition.hs

Large diffs are not rendered by default.

188 changes: 120 additions & 68 deletions LayeredHaskell-make/src/LHMake/Data/Feature.hs

Large diffs are not rendered by default.

12 changes: 2 additions & 10 deletions LayeredHaskell-make/src/LHMake/Feature/B/Parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,10 @@ Stability : experimental

This module provides a parser for Layered Haskell programs with B enabled.
-}
{-# LANGUAGE OverloadedStrings #-}
module LH.Feature.Parser (
-- * Parsing
parseProgram
parseProgram, parsePartialProgram
) where

import Data.Sequence (Seq)

import LH.Data.SrcLoc
import LH.Feature.AbsSyn
import LH.Text.Lexer
import LH.Text.Parser.Monad
import LH.Text.Token
}

%name program Block
Expand All @@ -33,6 +24,7 @@ import LH.Text.Token
%monad { LHsParser }
%lexer { lexer } { LHsLexeme _ LHsTkEOF }
%error { parseError }
%error.expected

-- There must be no shift/reduce conflicts.
%expect 0
Expand Down
19 changes: 5 additions & 14 deletions LayeredHaskell-make/src/LHMake/Feature/C/Parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,8 @@ This module provides a parser for Layered Haskell programs with C enabled.
-}
module LH.Feature.Parser (
-- * Parsing
parseProgram
parseProgram, parsePartialProgram
) where

import Control.Monad.Except (throwError)

import Data.Sequence (Seq, (<|))

import LH.Data.SrcLoc
import LH.Feature.AbsSyn
import LH.Text.Lexer
import LH.Text.Parser.Monad
import LH.Text.Token
}

%name program Block
Expand All @@ -34,6 +24,7 @@ import LH.Text.Token
%monad { LHsParser }
%lexer { lexer } { LHsLexeme _ LHsTkEOF }
%error { parseError }
%error.expected

-- There must be no shift/reduce conflicts.
%expect 0
Expand All @@ -55,8 +46,8 @@ Expr :: { L LHsExpr }
{ let alts = $5 <| $6 in L ($1 ><* fmap snd alts) (LHsCase $2 alts) }

CaseAlts :: { Seq LHsAlt }
: SEMI CaseAlt CaseAlts { $2 <| $3 }
| {- ε -} { Seq.empty }
: SEMI CaseAlt CaseAlts { $2 <| $3 }
| {- ε -} { Seq.empty }

CaseAlt :: { LHsAlt }
: Pat '->' Expr { ($1, $3) }
: Pat '->' Expr { ($1, $3) }
18 changes: 3 additions & 15 deletions LayeredHaskell-make/src/LHMake/Feature/D/Parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,8 @@ This module provides a parser for Layered Haskell programs with D enabled.
-}
module LH.Feature.Parser (
-- * Parsing
parseProgram
parseProgram, parsePartialProgram
) where

import Control.Monad.Except (throwError)

import Data.Sequence (Seq, (|>))

import qualified Data.Sequence as Seq

import LH.Data.SrcLoc
import LH.Feature.AbsSyn
import LH.Text.Lexer
import LH.Text.Parser.Monad
import LH.Text.Token
}

%name program Block
Expand All @@ -36,6 +24,7 @@ import LH.Text.Token
%monad { LHsParser }
%lexer { lexer } { LHsLexeme _ LHsTkEOF }
%error { parseError }
%error.expected

-- There must be no shift/reduce conflicts.
%expect 0
Expand All @@ -61,8 +50,7 @@ import LH.Text.Token

-- A sequence of constructor declarations (separated by pipes).
Cons :: { Seq (L (Id, Seq (L LHsType))) }
: {- ε -} { Seq.empty }
| Con { Seq.singleton $1 }
: Con { Seq.singleton $1 }
| Cons '|' Con { $1 |> $3 }

-- A constructor declaration.
Expand Down
9 changes: 6 additions & 3 deletions LayeredHaskell-make/src/LHMake/Feature/E/AbsSyn.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ module LH.Feature.AbsSyn (
LHsPat (..),
-- ** Types
LHsType,
-- ** Environments
LHsEnv
-- * Environments
LHsEnv, LHsTEnv
) where

import Data.Map (Map)
Expand Down Expand Up @@ -69,5 +69,8 @@ data LHsPat
-- -- | A type.
-- data LHsType

-- | An environment of variable-value bindings.
-- | Environments map variables to values.
type LHsEnv = Map Id LHsVal

-- | Type environments map variables to types.
type LHsTEnv = Map Id LHsType
115 changes: 86 additions & 29 deletions LayeredHaskell-make/src/LHMake/Feature/E/Parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,31 @@ Stability : experimental

This module provides a parser for Layered Haskell programs with E enabled.
-}
{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_HADDOCK hide #-}
module LH.Feature.Parser (
-- * Parsing
parseProgram
parseProgram, parsePartialProgram
) where

import Control.Monad.Except (throwError)
import Control.Monad.State.Strict (get)

import Data.Char (isSpace)
import Data.Sequence (Seq, (|>), (<|))
import Data.Text (Text)

import qualified Data.Sequence as Seq
import qualified Data.Text as Text

import LH.Data.Error (Label)
import LH.Data.SrcLoc
import LH.Feature.AbsSyn
import LH.Text.Lexer
import LH.Text.Parser.Error (LHsTextError(ParseError, PossibleEq), prevLabel)
import LH.Text.Parser.ExpList (HappyExpList, toPhrases)
import LH.Text.Parser.Monad
import LH.Text.Pretty ((<+>), quotes)
import LH.Text.Token
}

Expand All @@ -38,6 +46,7 @@ import LH.Text.Token
%monad { LHsParser }
%lexer { lexer } { LHsLexeme _ LHsTkEOF }
%error { parseError }
%error.expected

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For generic syntax errors, we can summarize and report a series of expected tokens. This is achieved by adding the %error.expected directive to each of the parser files, in which case Happy passes a list [String] to the parseError function, and we can apply a function (which I've called toPhrases) that summarizes it for students to understand.


-- There must be no shift/reduce conflicts.
%expect 0
Expand All @@ -64,78 +73,93 @@ import LH.Text.Token

-- A block of declarations, which also serves as a program.
Block :: { Seq (L LHsDecl) }
: OPEN Decls End { $2 }
: OPEN Decls End { $2 }

-- A block of declarations in let expressions.
LetBlock :: { Seq (L LHsDecl) }
: OPEN LetDecls End { $2 }

-- The end of a block.
-- Outside of a virtual closing brace, a parse error can also end a block!
End :: { () }
: CLOSE { () }
| error {% popLayout }
: CLOSE { () }
| error {% popLayout }

-------------------------------------------------------------------------------
-- Declarations

-- A sequence of declarations (separated by virtual semicolons).
Decls :: { Seq (L LHsDecl) }
: {- ε -} { Seq.empty }
| Decl { Seq.singleton $1 }
| Decls SEMI { $1 }
| Decls SEMI Decl { $1 |> $3 }
: {- ε -} { Seq.empty }
| Decl { Seq.singleton $1 }
| Decls SEMI { $1 }
| Decls SEMI Decl { $1 |> $3 }

-- A non-empty sequence of function declarations or type signatures (separated by virtual semicolons).
-- As one could surmise from the name of the nonterminal, these are used only in let expressions.
LetDecls :: { Seq (L LHsDecl) }
: FunSig { Seq.singleton $1 }
| LetDecls SEMI { $1 }
| LetDecls SEMI FunSig { $1 |> $3 }

-- A single declaration.
Decl :: { L LHsDecl }
: var Pats '=' Expr { L ($1><$4) (LHsEqn (ident $1) $2 $4) }
| var '::' Type { L ($1><$3) (LHsSig (ident $1) $3) }
: FunSig { $1 }

-- A function declaration or a type signature.
FunSig :: { L LHsDecl }
: var Pats '=' Expr { L ($1><$4) (LHsEqn (ident $1) $2 $4) }
| var '::' Type { L ($1><$3) (LHsSig (ident $1) $3) }

-------------------------------------------------------------------------------
-- Expressions

Expr :: { L LHsExpr }
: 'let' Block 'in' Expr { L ($1><$4) (LHsLet $2 $4) }
| App { $1 }
: 'let' LetBlock 'in' Expr { L ($1><$4) (LHsLet $2 $4) }
| App { $1 }

App :: { L LHsExpr }
: App Atom { L ($1><$2) (LHsApp $1 $2) }
| Atom { $1 }
: App Atom { L ($1><$2) (LHsApp $1 $2) }
| Atom { $1 }

Atom :: { L LHsExpr }
: var { L (toSpan $1) (LHsVar (ident $1)) }
| con { L (toSpan $1) (LHsCon (ident $1)) }
| '(' Expr ')' { L ($1><$3) (un $2) }
: var { L (toSpan $1) (LHsVar (ident $1)) }
| con { L (toSpan $1) (LHsCon (ident $1)) }
| '(' Expr ')' { L ($1><$3) (un $2) }

-------------------------------------------------------------------------------
-- Patterns

-- A sequence of patterns (separated by juxtaposition).
-- E contains no patterns, so this rule is empty.
Pats :: { Seq (L LHsPat) }
: {- ε -} { Seq.empty }
: {- ε -} { Seq.empty }

Pat :: { L LHsPat }
: PatCon { $1 }
: PatCon { $1 }

PatCon :: { L LHsPat }
: PatAtom { $1 }
: PatAtom { $1 }

PatApp :: { L (Seq (L LHsPat)) }
: PatAtom Pats { L ($1 ><* $2) ($1 <| $2) }
: PatAtom Pats { L ($1 ><* $2) ($1 <| $2) }

PatAtom :: { L LHsPat }
: '(' Pat ')' { L ($1 >< $3) (un $2) }
: '(' Pat ')' { L ($1 >< $3) (un $2) }

-------------------------------------------------------------------------------
-- Types
--
-- The following rules are intentionally non-terminating, since E has no meaningful types.

Type :: { L LHsType }
: TyApp { $1 }
: TyApp { $1 }

TyApp :: { L LHsType }
: TyAtom { $1 }
: TyAtom { $1 }

TyAtom :: { L LHsType }
: '(' Type ')' { L ($1 >< $3) (un $2) }
: '(' Type ')' { L ($1 >< $3) (un $2) }

{
-- | Gives the parser the next token.
Expand All @@ -146,10 +170,43 @@ lexer f = scanToken >>= f
parseProgram :: Text -> Either LHsTextError (Seq (L LHsDecl))
parseProgram = runLHsParser (beginProgram >> program)

-- | Reports a token that the parser did not expect.
parseError :: LHsLexeme -> LHsParser a
parseError (LHsLexeme sp tok) = do
throwError (ParseError sp tok)
-- | Parses a partial program, producing a prefix of syntactically correct declarations ('LHsDecl').
parsePartialProgram :: Text -> Seq (L LHsDecl)
parsePartialProgram src =
case parseProgram src of
-- If the parser succeeds, we can produce the program in its entirety.
Right decls -> decls
-- If the parser emits a syntax error, we can pick out the troublesome line and remove everything after it.
Left (ParseError (LHsSpan (LHsPos l _) _) _ _ _) ->
let lns = Text.lines src
-- The line where the erroneous declaration begins.
-- We'll drop everything that follows it.
rmv = last (0 : [ i | (i, ln) <- zip [0..] (take l lns)
, not (Text.null ln)
, not (isSpace (Text.head ln)) ])
in if rmv == 0 then Seq.empty else parsePartialProgram (Text.unlines (take rmv lns))
-- We perform a recursive call in case removing lines leads to another parse error.
-- This would happen in a case like the following.
--
-- > g = let x = y
-- > in x
--
-- rmv would drop `in x', but the remaining portion of `g' is syntactically incorrect.
-- So, we invoke parsePartialProgram again to drop the definition of `g' as well!
-- Otherwise, we produce the empty sequence.
Left _ -> Seq.empty

-- | Reports a token that the parser didn't expect.
parseError :: LHsLexeme -> HappyExpList -> LHsParser a
parseError (LHsLexeme sp tok) exs = do
st <- get
let brs = stBrack st
let nv = stNVirt st
-- If the unexpected token is an equals, and '==' is possible as an alternative,
-- we emit PossibleEq instead of ParseError.
if tok == LHsTkEq && "'=='" `elem` exs
then throwError (PossibleEq sp (toPhrases tok exs))
else throwError (ParseError sp tok (prevLabel tok brs nv) (toPhrases tok exs))

-- | Parses an identifier.
ident :: LHsLexeme -> Id
Expand Down
Loading
Loading