ndmitchell / record-dot-preprocessor

A preprocessor for a Haskell record syntax using dot
Other
129 stars 19 forks source link

Preprocessor breaks if code contains '[JSON] from Servant #25

Closed DoctorRyner closed 4 years ago

DoctorRyner commented 4 years ago

Pragma doesn't work if dot syntax is encountered in this code, it treats it like unexistent

{-# OPTIONS_GHC -F -pgmF=record-dot-preprocessor #-}

module Route.Root where

import           Servant
import           Types

handler :: AppM NoContent
handler = pure NoContent

type Type = Get '[JSON] NoContent
ndmitchell commented 4 years ago

What's the error? What's the output of running record-dot-preprocessor over the file on the command line?

DoctorRyner commented 4 years ago

It happens only after encountering that syntax, as you can see test1 does work

Screenshot 2019-10-31 at 4 18 57 PM
DoctorRyner commented 4 years ago

Oh, sorry, my mistake, this is output

{-# LINE 1 "api/src/Route/Root.hs" #-}
{-# LANGUAGE DuplicateRecordFields, DataKinds, FlexibleInstances, TypeApplications, FlexibleContexts, MultiParamTypeClasses, OverloadedLabels #-}
{-# LINE 1 "api/src/Route/Root.hs" #-}
{-# OPTIONS_GHC -F -pgmF=record-dot-preprocessor #-}

module Route.Root where

import qualified GHC.Records.Extra as Z
{-# LINE 5 "api/src/Route/Root.hs" #-}
import           Servant
import           Types

handler :: AppM NoContent
handler = pure NoContent

data V3 a = V3 { x, y, z :: a }

test2 :: Int
test2 = (Z.getField @"x" (V3 0 0 0))

type Type = Get '[JSON] NoContent

test :: Int
test = (V3 0 0 0).x

instance Z.HasField "x" (V3 a) (a) where hasField _r = (\_x -> _r{x=_x}, (x :: (V3 a) -> a) _r)
instance Z.HasField "y" (V3 a) (a) where hasField _r = (\_x -> _r{y=_x}, (y :: (V3 a) -> a) _r)
instance Z.HasField "z" (V3 a) (a) where hasField _r = (\_x -> _r{z=_x}, (z :: (V3 a) -> a) _r)
_preprocessor_unused_Route_Root :: Z.HasField "" r a => r -> a;_preprocessor_unused_Route_Root = Z.getField @""
ndmitchell commented 4 years ago

Thanks, the problem was the lexer thought '[JSON] was the start of a character literal, and thus assumed the rest of the file was part of that literal. I've changed the lexer so that shouldn't happen, added a test and released 0.2.1.