Giter VIP home page Giter VIP logo

hpp's Introduction

The hpp Executable

hpp is a Haskell pre-processor that is also a C90-compatible pre-processor (with the addition of a --cpp flag). It is packaged as both a library and an executable.

To use as a Haskell preprocessor for resolving #ifdef conditionals and macro expansion, an invocation might look like,

hpp -DDEBUG Foo.hs

To use as a C preprocessor, an invocation might look like,

hpp -DDEBUG --cpp foo.c

To have GHC use hpp as the C pre-processor, add this line to the top of a Haskell source file that makes use of the CPP LANGUAGE pragma,

{-# OPTIONS_GHC -cpp -pgmPhpp #-}

Or add this line to your .cabal file:

ghc-options: -pgmPhpp

Note that you will need to ensure that the hpp executable is available in your build environment (e.g. you can add hpp as a build-depends in your .cabal file).

The hpp Library

The hpp executable is a command-line interface to the hpp library. While the hpp package has been designed to have minimal dependencies beyond what the GHC compiler itself uses, it does include a few small, framework-free unit tests that demonstrate basic usage as a library. In the testIf example, we preprocess the sourceIfdef input with a starting definition equivalent to #define FOO 1. In testArith1, we exercise basic integer arithmetic and comparison. The hppHelper function shows how to run your source input through the preprocessor: expand initialState (preproces mySource). If you want to support #includeing other files, you will use runHpp or streamHpp that operate in IO and support searching among a list of include paths.

{-# LANGUAGE OverloadedStrings #-}
import Control.Monad.Trans.Except
import Data.ByteString.Char8 (ByteString)
import Data.Maybe (fromMaybe)
import Data.Monoid ((<>))
import Hpp
import System.Exit

sourceIfdef :: [ByteString]
sourceIfdef = [ "#ifdef FOO"
              , "x = 42"
              , "#else"
              , "x = 99"
              , "#endif" ]

sourceArith1 :: ByteString -> [ByteString]
sourceArith1 s = [ "#define x 3"
                 , "#if 5 + x > " <> s
                 , "yay"
                 , "#else"
                 , "boo"
                 , "#endif" ]

hppHelper :: HppState -> [ByteString] -> [ByteString] -> IO Bool
hppHelper st src expected =
  case runExcept (expand st (preprocess src)) of
    Left e -> putStrLn ("Error running hpp: " ++ show e) >> return False
    Right (res, _) -> if hppOutput res == expected
                      then return True
                      else do putStr ("Expected "++show expected++", got")
                              print (hppOutput res)
                              return False

testElse :: IO Bool
testElse = hppHelper emptyHppState sourceIfdef ["x = 99\n","\n"]

testIf :: IO Bool
testIf = hppHelper (fromMaybe (error "Preprocessor definition did not parse")
                              (addDefinition "FOO" "1" emptyHppState))
                   sourceIfdef
                   ["x = 42\n","\n"]

testArith1 :: IO Bool
testArith1 = (&&) <$> hppHelper emptyHppState (sourceArith1 "7") ["yay\n","\n"]
                  <*> hppHelper emptyHppState (sourceArith1 "8") ["boo\n","\n"]

main :: IO ()
main = do results <- sequenceA [testElse, testIf, testArith1]
          if and results then exitWith ExitSuccess else exitWith (ExitFailure 1)

hpp's People

Contributors

acowley avatar fumieval avatar hvr avatar rahulmutt avatar rimmington avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

hpp's Issues

streamHpp fails to include nested includes

Steps to reproduce:

  1. Create files:
    • f1.txt which has #include "dir1/f2.txt" and some text
    • dir1/f2.txt which has #include "dir2/f3.txt" and some text
    • dir1/dir2/f3.txt
  2. Use streamHpp to process f1.txt.

As can be seen from output it succeeds to include f2, but fails to include f3. If we add dir1/dir2 as default include directory. It works. But it should also work without explicit specification.

Handling of escaped character literals

I've recently swapped out cpphs for hpp inside of the Eta compiler and I discovered some interesting behavior.

Input:

'a'

Output:

7

This behavior causes the GHC.Show module to fail when compiling with Eta.

What was the rationale for this part of the tokenizer? Do escaped character literals have a different behavior when inside of a preprocessor directive definition/body? If so, I think there needs to be a step where the transformation to a number is undone so that the source code isn't mutated like that.

Improper line numbers with #ifdef..#endif

Hi it appears that hpp produces some erratic line numbers. When hpp sees a #ifdef..#endif (without an #else), the line numbers will be different to what is actually in the original file. Here I have a file called test with the line numbers annotated at the beginning:

#ifdef FOO
2: __LINE__: aaaa
#endif
4: __LINE__:
#ifdef FOO
6: __LINE__: aaaa
#endif
8: __LINE__:
#ifdef FOO
10: __LINE__: aaaa
#endif

When I run hpp test it produces:

#line 4
4: 3:
#line 7
8: 6:
#line 10

And when I run hpp -DFOO test it produces:

2: 2: aaaa
#line 4
4: 5:
6: 7: aaaa
#line 9
8: 10:
10: 12: aaaa
#line 14

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.