diff options
| author | Inari Listenmaa <inari.listenmaa@gmail.com> | 2023-09-13 08:24:07 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-09-13 08:24:07 +0200 |
| commit | 318b710a14e3bcbfb8386e4e357d4ed64b1a0ae1 (patch) | |
| tree | 9a3a2710a18225713c89ba27eab2384d47995d8b /src/compiler/GF/Grammar/Parser.y | |
| parent | 096b36c21d9657d1dee93638b436dc7bb35af439 (diff) | |
| parent | b90666455efd3b83345ad4b77eac4b64373e4c83 (diff) | |
Merge pull request #160 from anka-213/prettier-syntax-errors
Improve syntax error messages
Diffstat (limited to 'src/compiler/GF/Grammar/Parser.y')
| -rw-r--r-- | src/compiler/GF/Grammar/Parser.y | 17 |
1 files changed, 15 insertions, 2 deletions
diff --git a/src/compiler/GF/Grammar/Parser.y b/src/compiler/GF/Grammar/Parser.y index 85b081cd7..6aedcd399 100644 --- a/src/compiler/GF/Grammar/Parser.y +++ b/src/compiler/GF/Grammar/Parser.y @@ -37,6 +37,9 @@ import PGF(mkCId) %name pBNFCRules ListCFRule %name pEBNFRules ListEBNFRule +%errorhandlertype explist +%error { happyError } + -- no lexer declaration %monad { P } { >>= } { return } %lexer { lexer } { T_EOF } @@ -702,8 +705,18 @@ Posn { -happyError :: P a -happyError = fail "syntax error" +happyError :: (Token, [String]) -> P a +happyError (t,strs) = fail $ + "Syntax error:\n Unexpected " ++ showToken t ++ ".\n Expected one of:\n" + ++ unlines (map ((" - "++).cleanupToken) strs) + + where + cleanupToken "Ident" = "an identifier" + cleanupToken x = x + showToken (T_Ident i) = "identifier '" ++ showIdent i ++ "'" + showToken t = case Map.lookup t invMap of + Nothing -> show t + Just s -> "token '" ++ s ++"'" mkListId,mkConsId,mkBaseId :: Ident -> Ident mkListId = prefixIdent "List" |
