Skip to content

Parse errors - #45

Open
hshkk wants to merge 26 commits into
mainfrom
parse-errors
Open

hshkk wants to merge 26 commits into
mainfrom
parse-errors

Conversation

@hshkk

@hshkk hshkk commented Nov 28, 2026 •

Copy link
Copy Markdown
Owner

This PR intends to create a preliminary implementation for parse errors.

@hshkk hshkk added priority This task is of higher priority implementation Implementation of a new feature labels Nov 28, 2026
@hshkk hshkk linked an issue Nov 28, 2026 that may be closed by this pull request
Comment thread test/LH/Test/Text/Parser.hs Outdated
Comment on lines 50 to 61
withBuild :: [FeatureName] -> (FilePath -> IO ()) -> IO ()
withBuild args tests = withSystemTempDirectory "LayeredHaskell-test" $ \dir -> do
let featureDir = dir </> "LH" </> "Feature"
-- Create ../LH/Feature/, which will then receive the superimposed files.
createDirectoryIfMissing True featureDir
superimpose args (addTrailingPathSeparator featureDir)
files <- listDirectory featureDir
superimpose args dir
files <- listFilesRecursive dir
-- For each Happy file, we execute Happy to produce a set of Haskell modules.
forM_ [file | file <- files, takeExtension file == ".y"] $ \y ->
run "happy" [featureDir </> y, "-g", "-a", "-c", "-o", featureDir </> replaceExtension y "hs"]
run "happy" [dir </> y, "-g", "-a", "-c", "-o", dir </> replaceExtension y "hs"]
-- Then, we can run cabal on the driver file.
_ <- run "cabal"
[ "exec", "--", "ghc", "-O0", "-package", "LayeredHaskell", "-i" ++ dir, "-outputdir", dir </> "build"
, "-o", dir </> "driver" <.> exeExtension, "test/LH/Test/Text/Parser/Driver.hs" ]
tests dir

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.

The tests have been sped up. Previously, cabal test took around 40 seconds to complete, but now it takes around 30 seconds. I'm actually not entirely sure why this is the case, but it seems that slightly adjusting the output of superimposition did something beneficial in this regard.

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.

It does look like the tests went back to the speed they were previously at. I'm not really sure what happened here, but this can probably be ignored.

In the future, I plan to generalize the parser tests into a general interface for all kinds of tests across feature combinations.
@hshkk

hshkk commented Nov 29, 2026 •

Copy link
Copy Markdown
Owner Author

The most interesting finding (for me) is that layer-related errors are quite difficult to handle within the feature files themselves. I think it is much more idiomatic to identify and generate an uppermost, or maximal layer, then identify discrepancies. Since any layer is a subset of the uppermost layer, we can accomplish the following check (say, with respect to the parser).

Call $P$ the parser of some layer, and $Upp$ the uppermost parser.

  1. If $P$ successfully parses a program, proceed.
  2. Otherwise, apply $Upp$ onto the program.
  3. If $Upp$ successful parses the program, produce a layer-related error.
  4. Otherwise, emit a normal syntax error.

This is what is written in the files so far. (See src-exe/Main.hs.) This means that the representation of parse errors is actually not layered, because it doesn't seem like, for instance, the wording of parse error messages should differ across features.

Comment thread src/LH/Text/Parser/Error.hs Outdated
Comment on lines +145 to +150
-- | Determines the source of a 'LayerError' (and thus a 'ParseError'), producing a label and a possible fix.
--
-- Some tokens alone are sufficient to identify the issue, like keywords or operators.
-- Others, like variables, aren't enough to figure out what went wrong, so 'layerReason' will search the AST produced by the
-- uppermost parser to identify the discrepancy.
layerReason :: LHsTextError -> Seq (L Upp.LHsDecl) -> (Maybe Text, Maybe Text)

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.

This is the primary place where layer-specific syntax errors are handled. We can make some checks without traversing the AST generated by the uppermost parser, but in some cases (like variables), we have to traverse and identify whether there was an issue at a pattern, at a type parameter, etc.

%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.

@hshkk
hshkk marked this pull request as ready for review November 30, 2026 05:46
@hshkk
hshkk marked this pull request as draft November 30, 2026 05:46
@hshkk

hshkk commented Nov 30, 2026 •

Copy link
Copy Markdown
Owner Author

I think I'll try to write some tests before marking this ready for review. I do recommend pulling from the parse-errors branch and trying out some test programs, see what the program produces as an error message, and recommending some changes if you find anything objectionable. While I hope that every possible error message is not misleading, I'm not super confident yet.

@hshkk

hshkk commented Nov 30, 2026 •

Copy link
Copy Markdown
Owner Author

Important

Here are some example programs for things to check and also for the purposes of reproducibility. (These are all conducted in BD with the arbitrarily-chosen file name s.hs.) These use commit hash 014914d.

Program 1

data X = C | | D
image

Program 2

f :: Int -> Int
f = let x in x = 5 -- note the syntax errors here too, but earlier errors take priority
-- this can happen since the token `->' alone is sufficient to identify the layer-specific error
image

Program 3

f x = x        -- this is a layer-specific error that needs to traverse the uppermost AST...
g = let y in y -- ...but the uppermost AST can't be produced because of the syntax error here

This program is a really interesting example, because the layer-specific errors rely on an AST traversal that can't be produced in this context. Here, I actually decided to display two errors, but the first (supposed to be a layer-specific error) just becomes a plain syntax error (while the second one shows the later, perhaps more important error). This isn't optimal, and it's a limitation of the approach.

image

The screenshot is slightly inaccurate, I think, and reflects a local change I made. You'll probably see the same label as is visible in the error message under "A (potential) solution". That's the intended behavior.

I'm not sure how this should be handled. I do really want the layer-specific error to be displayed, I'm just not sure if it's compatible with the given approach.

A (potential) solution

A solution to this dilemma is to make it so that plain syntax errors always take priority over (potentially) layer-specific ones. This is embodied by the commit 799944f.

Program 3 w.r.t. 799944f:
image

Notice that just one error is emitted, and it is the plain syntax error reported by the uppermost parser.

(The behavior on the other programs is the same.)

It would be prudent for the reviewer to pull both, test the given programs, test some programs that they write themselves, and observe the differing behavior of the implementations. I think this will help support a more friendly error message system.

@hshkk

hshkk commented Dec 2, 2026 •

Copy link
Copy Markdown
Owner Author

Improvements

The most recent commit (34c2408) makes an improvement. It will effectively narrow down the place of the error and remove everything past it so that syntax errors in later declarations won't affect layer-specific syntax errors. Program 3 now produces the following expected error message.

image

One continued issue is that syntax errors occurring later than a layer-specific one in the same declaration won't be able to report the layer-specific syntax error. So, it'll point towards the generic syntax error. Consider the following, which we'll call Program 4 (recall we are in BD).

f x = x $ y let x in y

The produced error message points at the unexpected let, not the $ (which is only provided by F).

image

I think this is much more optimal, because Program 4 is a much rarer case, I think. We could be even more granular and parse prefixes of expressions and types, but there would be incremental benefit. I don't think it's worth the effort, but it would improve error messages in this scenario. In general, the pattern here is to parse as much of the input as possible, and stop when we have a parse error. Happy has a %partial directive for cases like this, but it doesn't work as I expected, and it seems to always throw a parse error for the cases that we're interested in.

@hshkk
hshkk marked this pull request as ready for review December 2, 2026 19:06
@hshkk
hshkk requested a review from leoSallos December 3, 2026 02:15
There is a slight weakness in this approach in that prefix operators cannot be assigned an unambiguous source. The issue is that the erroneous position pointed to is the binary operator itself (via the `Bin` nonterminal), and not the beginning of `'(' Bin ')'`.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

implementation Implementation of a new feature priority This task is of higher priority

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implementation of parse errors

1 participant