Skip to content

Commit 8360bbe

Browse files
committed
Get rid of template preprocessing
Instead of preprocessing an outer layer of CPP when building Alex, just always produce code that uses CPP. Combined with from-source bootstrapping, this means Alex an have a perfectly bog standard build system, with Makefiles and extra steps strictly optional. I gather Hugs, and possibly other Haskell implementations, out of the box doesn't support CPP, but I don't want this to stop us. Those can just manually run CPP on the generated code first.
1 parent e8ca2fe commit 8360bbe

File tree

12 files changed

+93
-274
lines changed

12 files changed

+93
-274
lines changed

Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ sdist ::
2222
$(ALEX) $(ALEX_OPTS) src/Scan.x -o src/Scan.hs
2323
mv src/Parser.y src/Parser.y.boot
2424
mv src/Scan.x src/Scan.x.boot
25-
$(CABAL) v2-run gen-alex-sdist
2625
$(CABAL) v2-sdist
2726
@if [ ! -f "${SDIST_DIR}/alex-$(ALEX_VER).tar.gz" ]; then \
2827
echo "Error: source tarball not found: dist/alex-$(ALEX_VER).tar.gz"; \

README.md

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,7 @@ they are in your `$PATH` for the next steps!).
5050

5151
### Variant A
5252

53-
First you need to generate the pre-processed templates via
54-
55-
$ cabal new-run gen-alex-sdist
56-
57-
(otherwise `cabal install` will complain about
58-
"`data/AlexTemplate: copyFile: does not exist (No such file or directory)`")
59-
60-
And then you can install `alex` simply by invoking
53+
You can install `alex` simply by invoking
6154

6255
$ cabal install
6356

@@ -67,8 +60,7 @@ from inside the Git folder.
6760

6861
Alternatively, you can use the `Makefile` which automates the steps of
6962
producing a self-contained pre-bootstrapped source distribution with
70-
pre-generated lexer/scanners (and which also performs the `cabal
71-
new-run gen-alex-sdist` pre-preprocessing step):
63+
pre-generated lexer/scanners:
7264

7365
$ make sdist
7466
$ cabal install dist/alex-*.tar.gz

alex.cabal

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -38,32 +38,8 @@ tested-with:
3838
data-dir: data/
3939

4040
data-files:
41-
AlexTemplate
42-
AlexTemplate-debug
43-
AlexTemplate-nopred
44-
AlexTemplate-nopred-debug
45-
AlexTemplate-latin1
46-
AlexTemplate-latin1-debug
47-
AlexTemplate-latin1-nopred
48-
AlexTemplate-latin1-nopred-debug
49-
AlexTemplate-ghc
50-
AlexTemplate-ghc-debug
51-
AlexTemplate-ghc-nopred
52-
AlexTemplate-ghc-nopred-debug
53-
AlexTemplate-ghc-latin1
54-
AlexTemplate-ghc-latin1-debug
55-
AlexTemplate-ghc-latin1-nopred
56-
AlexTemplate-ghc-latin1-nopred-debug
57-
AlexWrapper-basic
58-
AlexWrapper-basic-bytestring
59-
AlexWrapper-strict-bytestring
60-
AlexWrapper-posn
61-
AlexWrapper-posn-bytestring
62-
AlexWrapper-monad
63-
AlexWrapper-monad-bytestring
64-
AlexWrapper-monadUserState
65-
AlexWrapper-monadUserState-bytestring
66-
AlexWrapper-gscan
41+
AlexTemplate.hs
42+
AlexWrappers.hs
6743

6844
extra-source-files:
6945
CHANGELOG.md
@@ -94,8 +70,6 @@ extra-source-files:
9470
src/Parser.y.boot
9571
src/Scan.x.boot
9672
src/ghc_hooks.c
97-
templates/GenericTemplate.hs
98-
templates/wrappers.hs
9973
tests/Makefile
10074
tests/simple.x
10175
tests/null.x

cabal.project

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,2 @@
1-
21
packages:
32
./
4-
./gen-alex-sdist/
5-
6-

templates/GenericTemplate.hs renamed to data/AlexTemplate.hs

Lines changed: 40 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -8,71 +8,58 @@
88
-- INTERNALS and main scanner engine
99

1010
#ifdef ALEX_GHC
11-
#undef __GLASGOW_HASKELL__
12-
#define ALEX_IF_GHC_GT_500 #if __GLASGOW_HASKELL__ > 500
13-
#define ALEX_IF_GHC_LT_503 #if __GLASGOW_HASKELL__ < 503
14-
#define ALEX_IF_GHC_GT_706 #if __GLASGOW_HASKELL__ > 706
15-
#define ALEX_IF_GHC_GE_901 #if __GLASGOW_HASKELL__ >= 901
16-
#define ALEX_ELIF_GHC_500 #elif __GLASGOW_HASKELL__ == 500
17-
#define ALEX_IF_BIGENDIAN #ifdef WORDS_BIGENDIAN
18-
#define ALEX_ELSE #else
19-
#define ALEX_ENDIF #endif
20-
#define ALEX_DEFINE #define
21-
#endif
22-
23-
#ifdef ALEX_GHC
24-
#define ILIT(n) n#
25-
#define IBOX(n) (I# (n))
26-
#define FAST_INT Int#
11+
# define ILIT(n) n#
12+
# define IBOX(n) (I# (n))
13+
# define FAST_INT Int#
2714
-- Do not remove this comment. Required to fix CPP parsing when using GCC and a clang-compiled alex.
28-
ALEX_IF_GHC_GT_706
29-
ALEX_DEFINE GTE(n,m) (tagToEnum# (n >=# m))
30-
ALEX_DEFINE EQ(n,m) (tagToEnum# (n ==# m))
31-
ALEX_ELSE
32-
ALEX_DEFINE GTE(n,m) (n >=# m)
33-
ALEX_DEFINE EQ(n,m) (n ==# m)
34-
ALEX_ENDIF
35-
#define PLUS(n,m) (n +# m)
36-
#define MINUS(n,m) (n -# m)
37-
#define TIMES(n,m) (n *# m)
38-
#define NEGATE(n) (negateInt# (n))
39-
#define IF_GHC(x) (x)
15+
# if __GLASGOW_HASKELL__ > 706
16+
# define GTE(n,m) (tagToEnum# (n >=# m))
17+
# define EQ(n,m) (tagToEnum# (n ==# m))
18+
# else
19+
# define GTE(n,m) (n >=# m)
20+
# define EQ(n,m) (n ==# m)
21+
# endif
22+
# define PLUS(n,m) (n +# m)
23+
# define MINUS(n,m) (n -# m)
24+
# define TIMES(n,m) (n *# m)
25+
# define NEGATE(n) (negateInt# (n))
26+
# define IF_GHC(x) (x)
4027
#else
41-
#define ILIT(n) (n)
42-
#define IBOX(n) (n)
43-
#define FAST_INT Int
44-
#define GTE(n,m) (n >= m)
45-
#define EQ(n,m) (n == m)
46-
#define PLUS(n,m) (n + m)
47-
#define MINUS(n,m) (n - m)
48-
#define TIMES(n,m) (n * m)
49-
#define NEGATE(n) (negate (n))
50-
#define IF_GHC(x)
28+
# define ILIT(n) (n)
29+
# define IBOX(n) (n)
30+
# define FAST_INT Int
31+
# define GTE(n,m) (n >= m)
32+
# define EQ(n,m) (n == m)
33+
# define PLUS(n,m) (n + m)
34+
# define MINUS(n,m) (n - m)
35+
# define TIMES(n,m) (n * m)
36+
# define NEGATE(n) (negate (n))
37+
# define IF_GHC(x)
5138
#endif
5239

5340
#ifdef ALEX_GHC
5441
data AlexAddr = AlexA# Addr#
5542
-- Do not remove this comment. Required to fix CPP parsing when using GCC and a clang-compiled alex.
56-
ALEX_IF_GHC_LT_503
43+
#if __GLASGOW_HASKELL__ < 503
5744
uncheckedShiftL# = shiftL#
58-
ALEX_ENDIF
45+
#endif
5946

6047
{-# INLINE alexIndexInt16OffAddr #-}
6148
alexIndexInt16OffAddr :: AlexAddr -> Int# -> Int#
6249
alexIndexInt16OffAddr (AlexA# arr) off =
63-
ALEX_IF_BIGENDIAN
50+
#ifdef WORDS_BIGENDIAN
6451
narrow16Int# i
6552
where
6653
i = word2Int# ((high `uncheckedShiftL#` 8#) `or#` low)
6754
high = int2Word# (ord# (indexCharOffAddr# arr (off' +# 1#)))
6855
low = int2Word# (ord# (indexCharOffAddr# arr off'))
6956
off' = off *# 2#
70-
ALEX_ELSE
71-
ALEX_IF_GHC_GE_901
57+
#else
58+
#if __GLASGOW_HASKELL__ >= 901
7259
int16ToInt#
73-
ALEX_ENDIF
60+
#endif
7461
(indexInt16OffAddr# arr off)
75-
ALEX_ENDIF
62+
#endif
7663
#else
7764
alexIndexInt16OffAddr arr off = arr ! off
7865
#endif
@@ -81,7 +68,7 @@ alexIndexInt16OffAddr arr off = arr ! off
8168
{-# INLINE alexIndexInt32OffAddr #-}
8269
alexIndexInt32OffAddr :: AlexAddr -> Int# -> Int#
8370
alexIndexInt32OffAddr (AlexA# arr) off =
84-
ALEX_IF_BIGENDIAN
71+
#ifdef WORDS_BIGENDIAN
8572
narrow32Int# i
8673
where
8774
i = word2Int# ((b3 `uncheckedShiftL#` 24#) `or#`
@@ -92,24 +79,24 @@ ALEX_IF_BIGENDIAN
9279
b1 = int2Word# (ord# (indexCharOffAddr# arr (off' +# 1#)))
9380
b0 = int2Word# (ord# (indexCharOffAddr# arr off'))
9481
off' = off *# 4#
95-
ALEX_ELSE
96-
ALEX_IF_GHC_GE_901
82+
#else
83+
#if __GLASGOW_HASKELL__ >= 901
9784
int32ToInt#
98-
ALEX_ENDIF
85+
#endif
9986
(indexInt32OffAddr# arr off)
100-
ALEX_ENDIF
87+
#endif
10188
#else
10289
alexIndexInt32OffAddr arr off = arr ! off
10390
#endif
10491

10592
#ifdef ALEX_GHC
10693

107-
ALEX_IF_GHC_LT_503
94+
#if __GLASGOW_HASKELL__ < 503
10895
quickIndex arr i = arr ! i
109-
ALEX_ELSE
96+
#else
11097
-- GHC >= 503, unsafeAt is available from Data.Array.Base.
11198
quickIndex = unsafeAt
112-
ALEX_ENDIF
99+
#endif
113100
#else
114101
quickIndex arr i = arr ! i
115102
#endif
File renamed without changes.

gen-alex-sdist/Main.hs

Lines changed: 0 additions & 99 deletions
This file was deleted.

gen-alex-sdist/Setup.hs

Lines changed: 0 additions & 2 deletions
This file was deleted.

gen-alex-sdist/gen-alex-sdist.cabal

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/AbsSyn.hs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
module AbsSyn (
1313
Code, Directive(..), Scheme(..),
14-
wrapperName,
14+
wrapperCppDefs,
1515
Scanner(..),
1616
RECtx(..),
1717
RExp(..), nullable,
@@ -70,22 +70,23 @@ strtype :: Bool -> String
7070
strtype True = "ByteString.ByteString"
7171
strtype False = "String"
7272

73-
wrapperName :: Scheme -> Maybe String
74-
wrapperName Default {} = Nothing
75-
wrapperName GScan {} = Just "gscan"
76-
wrapperName Basic { basicStrType = Str } = Just "basic"
77-
wrapperName Basic { basicStrType = Lazy } = Just "basic-bytestring"
78-
wrapperName Basic { basicStrType = Strict } = Just "strict-bytestring"
79-
wrapperName Posn { posnByteString = False } = Just "posn"
80-
wrapperName Posn { posnByteString = True } = Just "posn-bytestring"
81-
wrapperName Monad { monadByteString = False,
82-
monadUserState = False } = Just "monad"
83-
wrapperName Monad { monadByteString = True,
84-
monadUserState = False } = Just "monad-bytestring"
85-
wrapperName Monad { monadByteString = False,
86-
monadUserState = True } = Just "monadUserState"
87-
wrapperName Monad { monadByteString = True,
88-
monadUserState = True } = Just "monadUserState-bytestring"
73+
74+
wrapperCppDefs :: Scheme -> Maybe [String]
75+
wrapperCppDefs Default {} = Nothing
76+
wrapperCppDefs GScan {} = Just ["ALEX_GSCAN"]
77+
wrapperCppDefs Basic { basicStrType = Str } = Just ["ALEX_BASIC"]
78+
wrapperCppDefs Basic { basicStrType = Lazy } = Just ["ALEX_BASIC_BYTESTRING"]
79+
wrapperCppDefs Basic { basicStrType = Strict } = Just ["ALEX_STRICT_BYTESTRING"]
80+
wrapperCppDefs Posn { posnByteString = False } = Just ["ALEX_POSN"]
81+
wrapperCppDefs Posn { posnByteString = True } = Just ["ALEX_POSN_BYTESTRING"]
82+
wrapperCppDefs Monad { monadByteString = False,
83+
monadUserState = False } = Just ["ALEX_MONAD"]
84+
wrapperCppDefs Monad { monadByteString = True,
85+
monadUserState = False } = Just ["ALEX_MONAD_BYTESTRING"]
86+
wrapperCppDefs Monad { monadByteString = False,
87+
monadUserState = True } = Just ["ALEX_MONAD", "ALEX_MONAD_USER_STATE"]
88+
wrapperCppDefs Monad { monadByteString = True,
89+
monadUserState = True } = Just ["ALEX_MONAD_BYTESTRING", "ALEX_MONAD_USER_STATE"]
8990

9091
-- TODO: update this comment
9192
--

0 commit comments

Comments
 (0)