Skip to content

Commit

Permalink
Merge pull request GrammaticalFramework#160 from anka-213/prettier-sy…
Browse files Browse the repository at this point in the history
…ntax-errors

Improve syntax error messages
  • Loading branch information
inariksit authored Sep 13, 2023
2 parents 096b36c + b906664 commit 318b710
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/build-all-versions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
- uses: actions/checkout@v2
if: github.event.action == 'opened' || github.event.action == 'synchronize' || github.event.ref == 'refs/heads/master'

- uses: haskell/actions/setup@v1.2.9
- uses: haskell/actions/setup@v2
id: setup-haskell-cabal
name: Setup Haskell
with:
Expand Down Expand Up @@ -62,7 +62,7 @@ jobs:

stack:
name: stack / ghc ${{ matrix.ghc }}
runs-on: ubuntu-latest
runs-on: ${{ matrix.ghc == '7.10.3' && 'ubuntu-20.04' || 'ubuntu-latest' }}
strategy:
matrix:
stack: ["latest"]
Expand All @@ -73,7 +73,7 @@ jobs:
- uses: actions/checkout@v2
if: github.event.action == 'opened' || github.event.action == 'synchronize' || github.event.ref == 'refs/heads/master'

- uses: haskell/actions/setup@v1.2.9
- uses: haskell/actions/setup@v2
name: Setup Haskell Stack
with:
ghc-version: ${{ matrix.ghc }}
Expand Down
11 changes: 9 additions & 2 deletions src/compiler/GF/Grammar/Lexer.x
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
module GF.Grammar.Lexer
( Token(..), Posn(..)
, P, runP, runPartial, token, lexer, getPosn, failLoc
, isReservedWord
, isReservedWord, invMap
) where

import Control.Applicative
Expand Down Expand Up @@ -134,7 +134,7 @@ data Token
| T_Double Double -- double precision float literals
| T_Ident Ident
| T_EOF
-- deriving Show -- debug
deriving (Eq, Ord, Show) -- debug
res = eitherResIdent
eitherResIdent :: (Ident -> Token) -> Ident -> Token
Expand Down Expand Up @@ -224,6 +224,13 @@ resWords = Map.fromList
]
where b s t = (identS s, t)
invMap :: Map.Map Token String
invMap = res
where
lst = Map.toList resWords
flp = map (\(k,v) -> (v,showIdent k)) lst
res = Map.fromList flp
unescapeInitTail :: String -> String
unescapeInitTail = unesc . tail where
unesc s = case s of
Expand Down
17 changes: 15 additions & 2 deletions src/compiler/GF/Grammar/Parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 318b710

Please sign in to comment.