Skip to content

Commit 6e9d316

Browse files
authored
Merge pull request #158 from andreasabel/issue141
[ fixed #141 ] regex: allow arbitary repetitions
2 parents 6c4db72 + d6653f3 commit 6e9d316

File tree

6 files changed

+53
-4
lines changed

6 files changed

+53
-4
lines changed

alex.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ extra-source-files:
122122
tests/unicode.x
123123
tests/issue_71.x
124124
tests/issue_119.x
125+
tests/issue_141.x
125126

126127
source-repository head
127128
type: git

doc/alex.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -822,9 +822,9 @@ rexp0 := set
822822
| @string
823823
| '(' [ regexp ] ')'
824824

825-
repeat := '{' $digit '}'
826-
| '{' $digit ',' '}'
827-
| '{' $digit ',' $digit '}'</programlisting>
825+
repeat := '{' $digit+ '}'
826+
| '{' $digit+ ',' '}'
827+
| '{' $digit+ ',' $digit+ '}'</programlisting>
828828

829829
<para>The syntax of regular expressions is fairly standard, the
830830
only difference from normal lex-style regular expressions being

src/Parser.y

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ import Data.Char
5454
ID { T _ (IdT $$) }
5555
CODE { T _ (CodeT _) }
5656
CHAR { T _ (CharT $$) }
57+
NUM { T _ (NumT $$) }
5758
SMAC { T _ (SMacT _) }
5859
RMAC { T _ (RMacT $$) }
5960
SMAC_DEF { T _ (SMacDefT $$) }
@@ -166,11 +167,15 @@ rep :: { RExp -> RExp }
166167
: '*' { Star }
167168
| '+' { Plus }
168169
| '?' { Ques }
170+
-- Single digits are CHAR, not NUM.
169171
-- TODO: these don't check for digits
170172
-- properly.
171173
| '{' CHAR '}' { repeat_rng (digit $2) Nothing }
172174
| '{' CHAR ',' '}' { repeat_rng (digit $2) (Just Nothing) }
173175
| '{' CHAR ',' CHAR '}' { repeat_rng (digit $2) (Just (Just (digit $4))) }
176+
| '{' NUM '}' { repeat_rng $2 Nothing }
177+
| '{' NUM ',' '}' { repeat_rng $2 (Just Nothing) }
178+
| '{' NUM ',' NUM '}' { repeat_rng $2 (Just (Just $4)) }
174179

175180
rexp0 :: { RExp }
176181
: '(' ')' { Eps }

src/Scan.x

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ alex :-
5656
<0> \\ x $hexdig+ { hexch }
5757
<0> \\ o $octal+ { octch }
5858
<0> \\ $printable { escape }
59-
<0> $nonspecial # [\<] { char }
59+
<0> $nonspecial # [\<] { char } -- includes 1 digit numbers
60+
<0> $digit+ { num } -- should be after char
6061
<0> @smac { smac }
6162
<0> @rmac { rmac }
6263
@@ -120,6 +121,7 @@ decch (p,_,str) ln = return $ T p (CharT (do_ech 10 ln (take (ln-1) (tail st
120121
hexch (p,_,str) ln = return $ T p (CharT (do_ech 16 ln (take (ln-2) (drop 2 str))))
121122
octch (p,_,str) ln = return $ T p (CharT (do_ech 8 ln (take (ln-2) (drop 2 str))))
122123
char (p,_,str) _ = return $ T p (CharT (head str))
124+
num (p,_,str) ln = return $ T p $ NumT $ parseInt 10 $ take ln str
123125
smac (p,_,str) ln = return $ T p (SMacT (mac ln str))
124126
rmac (p,_,str) ln = return $ T p (RMacT (mac ln str))
125127
smacdef (p,_,str) ln = return $ T p (SMacDefT (macdef ln str))

tests/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ TESTS = \
4242
gscan_typeclass.x \
4343
issue_71.x \
4444
issue_119.x \
45+
issue_141.x \
4546
monad_typeclass.x \
4647
monad_typeclass_bytestring.x \
4748
monadUserState_typeclass.x \

tests/issue_141.x

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
-- Issue #141
3+
-- reported 2015-10-20 by Iavor S. Diatchki
4+
-- fixed 2020-01-31 by Andreas Abel
5+
--
6+
-- Problem was:
7+
-- Only one-digit numbers were accepted in repetition ranges.
8+
9+
module Main (main) where
10+
11+
import System.Exit
12+
}
13+
14+
%wrapper "posn"
15+
%token "Token"
16+
17+
:-
18+
19+
-- allow several digits in repetition ranges, e.g. 14
20+
"a"{14,14} { \ _ _ -> A }
21+
[\ \n\t]+ ;
22+
23+
{
24+
data Token = A
25+
deriving (Eq, Show)
26+
27+
-- 12345678901234
28+
input = "aaaaaaaaaaaaaa\n" -- fourteen a's
29+
expected_result = [A]
30+
31+
main :: IO ()
32+
main
33+
| result == expected_result = do
34+
exitWith ExitSuccess
35+
| otherwise = do
36+
print $ take 20 result
37+
exitFailure
38+
where
39+
result = alexScanTokens input
40+
}

0 commit comments

Comments
 (0)