forked from GitHub/gf-core
Merge pull request #160 from anka-213/prettier-syntax-errors
Improve syntax error messages
This commit is contained in:
6
.github/workflows/build-all-versions.yml
vendored
6
.github/workflows/build-all-versions.yml
vendored
@@ -33,7 +33,7 @@ jobs:
|
|||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v2
|
||||||
if: github.event.action == 'opened' || github.event.action == 'synchronize' || github.event.ref == 'refs/heads/master'
|
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
|
id: setup-haskell-cabal
|
||||||
name: Setup Haskell
|
name: Setup Haskell
|
||||||
with:
|
with:
|
||||||
@@ -62,7 +62,7 @@ jobs:
|
|||||||
|
|
||||||
stack:
|
stack:
|
||||||
name: stack / ghc ${{ matrix.ghc }}
|
name: stack / ghc ${{ matrix.ghc }}
|
||||||
runs-on: ubuntu-latest
|
runs-on: ${{ matrix.ghc == '7.10.3' && 'ubuntu-20.04' || 'ubuntu-latest' }}
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
stack: ["latest"]
|
stack: ["latest"]
|
||||||
@@ -73,7 +73,7 @@ jobs:
|
|||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v2
|
||||||
if: github.event.action == 'opened' || github.event.action == 'synchronize' || github.event.ref == 'refs/heads/master'
|
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
|
name: Setup Haskell Stack
|
||||||
with:
|
with:
|
||||||
ghc-version: ${{ matrix.ghc }}
|
ghc-version: ${{ matrix.ghc }}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
module GF.Grammar.Lexer
|
module GF.Grammar.Lexer
|
||||||
( Token(..), Posn(..)
|
( Token(..), Posn(..)
|
||||||
, P, runP, runPartial, token, lexer, getPosn, failLoc
|
, P, runP, runPartial, token, lexer, getPosn, failLoc
|
||||||
, isReservedWord
|
, isReservedWord, invMap
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Control.Applicative
|
import Control.Applicative
|
||||||
@@ -134,7 +134,7 @@ data Token
|
|||||||
| T_Double Double -- double precision float literals
|
| T_Double Double -- double precision float literals
|
||||||
| T_Ident Ident
|
| T_Ident Ident
|
||||||
| T_EOF
|
| T_EOF
|
||||||
-- deriving Show -- debug
|
deriving (Eq, Ord, Show) -- debug
|
||||||
|
|
||||||
res = eitherResIdent
|
res = eitherResIdent
|
||||||
eitherResIdent :: (Ident -> Token) -> Ident -> Token
|
eitherResIdent :: (Ident -> Token) -> Ident -> Token
|
||||||
@@ -224,6 +224,13 @@ resWords = Map.fromList
|
|||||||
]
|
]
|
||||||
where b s t = (identS s, t)
|
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 :: String -> String
|
||||||
unescapeInitTail = unesc . tail where
|
unescapeInitTail = unesc . tail where
|
||||||
unesc s = case s of
|
unesc s = case s of
|
||||||
|
|||||||
@@ -37,6 +37,9 @@ import PGF(mkCId)
|
|||||||
%name pBNFCRules ListCFRule
|
%name pBNFCRules ListCFRule
|
||||||
%name pEBNFRules ListEBNFRule
|
%name pEBNFRules ListEBNFRule
|
||||||
|
|
||||||
|
%errorhandlertype explist
|
||||||
|
%error { happyError }
|
||||||
|
|
||||||
-- no lexer declaration
|
-- no lexer declaration
|
||||||
%monad { P } { >>= } { return }
|
%monad { P } { >>= } { return }
|
||||||
%lexer { lexer } { T_EOF }
|
%lexer { lexer } { T_EOF }
|
||||||
@@ -702,8 +705,18 @@ Posn
|
|||||||
|
|
||||||
{
|
{
|
||||||
|
|
||||||
happyError :: P a
|
happyError :: (Token, [String]) -> P a
|
||||||
happyError = fail "syntax error"
|
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,mkConsId,mkBaseId :: Ident -> Ident
|
||||||
mkListId = prefixIdent "List"
|
mkListId = prefixIdent "List"
|
||||||
|
|||||||
Reference in New Issue
Block a user