From ffd7b27abd050dc50f5986c9b94f8fb5d120c2db Mon Sep 17 00:00:00 2001 From: Andreas Källberg Date: Mon, 11 Sep 2023 12:29:13 +0200 Subject: Improve syntax error messages Now you will get error messages like these: example.gf:1:21: Syntax error: Unexpected token '}'. Expected one of: - '{' - 'open' - an identifier --- src/compiler/GF/Grammar/Parser.y | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'src/compiler/GF/Grammar/Parser.y') 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" -- cgit v1.2.3