Skip to content

Commit

Permalink
Fix #388: check token defs for empty regular expression
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasabel committed Oct 5, 2021
1 parent ac02292 commit 29b9ffb
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
2 changes: 2 additions & 0 deletions source/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Unreleased

* LBNF: empty tokens types are now forbidden [#388]

# 2.9.3

Andreas Abel <andreas.abel@gu.se> September 2021
Expand Down
24 changes: 22 additions & 2 deletions source/src/BNFC/GetCF.hs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import System.IO (hPutStrLn, stderr)
-- Local imports:

import qualified BNFC.Abs as Abs
import BNFC.Abs (Reg(RAlts))
import BNFC.Par

import BNFC.CF
Expand Down Expand Up @@ -537,9 +538,16 @@ checkComments cf = concat
where
prags = cfgPragmas cf

-- | Check if any of the user-defined terminal categories is nullable.
-- | Check if any of the user-defined terminal categories is nullable or empty.
checkTokens :: CFG f -> Maybe String
checkTokens cf
checkTokens cf =
case catMaybes [ checkTokensEmpty cf, checkTokensNullable cf ] of
[] -> Nothing
ss -> Just $ concat ss

-- | Check if any of the user-defined terminal categories is nullable.
checkTokensNullable :: CFG f -> Maybe String
checkTokensNullable cf
| null pxs = Nothing
| otherwise = Just $ unlines $ concat
[ [ "ERROR: The following tokens accept the empty string:" ]
Expand All @@ -548,6 +556,18 @@ checkTokens cf
where
pxs = [ px | TokenReg px _ regex <- cfgPragmas cf, nullable regex ]

-- | Check if any of the user-defined terminal categories is nullable.
checkTokensEmpty :: CFG f -> Maybe String
checkTokensEmpty cf
| null pxs = Nothing
| otherwise = Just $ unlines $ concat
[ [ "ERROR: The following tokens accept nothing:" ]
, printNames pxs
]
where
-- The regular expression is already simplified, so we match against 0 directly.
pxs = [ px | TokenReg px _ (RAlts "") <- cfgPragmas cf ]


-- we should actually check that
-- (1) coercions are always between variants
Expand Down
3 changes: 3 additions & 0 deletions testing/fail-lbnf/388-empty-regex.cf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- 2021-10-05 Issue #388, empty regular expression

token Empty [""]+ | [""] | (char - char);
4 changes: 4 additions & 0 deletions testing/fail-lbnf/388-empty-regex.err
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ERROR: The following tokens accept nothing:
fail-lbnf/388-empty-regex.cf:3:7: Empty

Aborting. (Use option --force to continue despite errors.)

0 comments on commit 29b9ffb

Please sign in to comment.