2017-11-27 21:45:37 +03:00
|
|
|
{-# LANGUAGE DataKinds #-}
|
2018-07-19 21:45:04 +03:00
|
|
|
module Data.AST
|
|
|
|
( Node (..)
|
2018-09-21 23:46:25 +03:00
|
|
|
, nodeSpan
|
|
|
|
, nodeByteRange
|
2018-07-19 21:45:04 +03:00
|
|
|
, AST
|
|
|
|
) where
|
2017-11-27 21:45:37 +03:00
|
|
|
|
2018-09-21 23:46:25 +03:00
|
|
|
import Data.Location
|
2017-11-27 21:45:37 +03:00
|
|
|
import Data.Term
|
2018-05-01 19:51:34 +03:00
|
|
|
import Data.Aeson
|
2018-06-04 17:59:31 +03:00
|
|
|
import Data.Text (pack)
|
2018-05-01 19:51:34 +03:00
|
|
|
import Data.JSON.Fields
|
|
|
|
|
2017-11-27 21:45:37 +03:00
|
|
|
-- | An AST node labelled with symbols and source location.
|
|
|
|
type AST syntax grammar = Term syntax (Node grammar)
|
|
|
|
|
|
|
|
data Node grammar = Node
|
2018-06-04 17:59:31 +03:00
|
|
|
{ nodeSymbol :: !grammar
|
2018-09-21 23:46:25 +03:00
|
|
|
, nodeLocation :: {-# UNPACK #-} !Location
|
2017-11-27 21:45:37 +03:00
|
|
|
}
|
2018-06-28 20:20:44 +03:00
|
|
|
deriving (Eq, Ord, Show)
|
2017-11-27 21:45:37 +03:00
|
|
|
|
2018-05-01 19:51:34 +03:00
|
|
|
|
|
|
|
instance Show grammar => ToJSONFields (Node grammar) where
|
|
|
|
toJSONFields Node{..} =
|
2018-06-04 17:59:31 +03:00
|
|
|
[ "symbol" .= pack (show nodeSymbol)
|
2018-09-21 23:46:25 +03:00
|
|
|
, "span" .= locationSpan nodeLocation
|
2018-06-04 17:59:31 +03:00
|
|
|
]
|
2018-05-01 19:51:34 +03:00
|
|
|
|
2018-09-21 23:46:25 +03:00
|
|
|
nodeSpan :: Node grammar -> Span
|
|
|
|
nodeSpan = locationSpan . nodeLocation
|
2017-11-27 21:45:37 +03:00
|
|
|
|
2018-09-21 23:46:25 +03:00
|
|
|
nodeByteRange :: Node grammar -> Range
|
|
|
|
nodeByteRange = locationByteRange . nodeLocation
|