scvalex / interpol Goto Github PK
View Code? Open in Web Editor NEWGHC preprocessor to enable variable interpolation in strings
License: GNU General Public License v3.0
GHC preprocessor to enable variable interpolation in strings
License: GNU General Public License v3.0
The preprocessor should import qualified Text.Interpol
to avoid conflicts with other (^-^)
operators. The following code results in "ambiguous occurrence" errors:
{-# OPTIONS_GHC -F -pgmF interpol #-}
module Bug where
(^-^) :: String -> Int -> String
(^-^) s n = s ++ "hello {n}"
As an aside, I think you should just add the import every time. The strstr "^-^" (prettyPrint m)
check in the preprocessor is expensive and isn't even accurate (it matches strings containing "^-^"
in addition to uses of the operator). To avoid unused import warnings, you can use the form import qualified Text.Interpol ()
.
After interpol preprocessing, there's a single LINE 1
pragma at the top of the file, but no others, even though comments are stripped and whitespace is changed. This leads to GHC's error messages pointing at wrong lines. Consider for example:
-- Hello!
{-# OPTIONS_GHC -F -pgmF interpol #-}
-- Hey there!
module Bug where
-- Greetings!
foo :: Int
foo = "wrong type"
-- Bye!
After ghc -E
, we're left with:
{-# LINE 1 "asdf.hs" #-}
{-# OPTIONS_GHC -F -pgmF interpol #-}
module Bug where
foo :: Int
foo = "wrong type"
And the error message points to asdf.hs:5:7
instead of asdf.hs:14:7
. If foo
here were to contain a string interpolation, it'd point to line 6 instead of 5, as the import of Text.Interpol
would be added.
The Test/NoWarn.hs
file in the repository is another example of a file that would suffer from this, if it were to contain any type errors. (For example, try removing putStrLn
from the definition of foo
there.)
I'm not sure what can be done about this as I'm not at all familiar with haskell-src-exts. In my mind, the simplest solution would be to not modify the original source in any way except to perform the string interpolation (duh), add the import, and immediately after the import add a LINE
pragma for the appropriate line number. If that's not an option, you need a LINE
pragma after every removed comment and blank line...
After the first make test
, when doing it again:
% make test
cabal build
Building interpol-0.2.3...
Preprocessing library interpol-0.2.3...
Registering interpol-0.2.3...
Preprocessing executable 'interpol' for interpol-0.2.3...
rm -f Test/*.{hi,o}
rm -f Test/Simple
rm -f Test/One
rm -f Test/String
rm -f Test/NoImport
rm -f Test/Standalone
rm -f Test/Pure
rm -f Test/PureString
rm -f Test/NoWarn
rm -f Test/JustShow
rm -f Test/OperatorConflicts
cabal test
cabal: No test suites enabled. Did you remember to configure with
'--enable-tests'?
make: *** [test] Error 1
Probably some Makefile
business. @scvalex can you have a look?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.