{-
    BNF Converter: TreeSitter Grammar Generator
    Copyright (C) 2004  Author:  Markus Forsberg, Michael Pellauer,
                                 Bjorn Bringert

    Description   : This module generates the grammar.js input file for
                    tree-sitter.

    Author        : Kangjing Huang (huangkangjing@gmail.com)
    Created       : 23 Nov, 2023

-}

module BNFC.Backend.TreeSitter where

import System.FilePath

import BNFC.Backend.Base
import BNFC.Backend.TreeSitter.CFtoTreeSitter (cfToTreeSitter)
import BNFC.GetCF(fixTokenCats)
import BNFC.Utils(kebabCase_, snakeCase_)
import BNFC.CF
import BNFC.Options hiding (Backend)
import BNFC.PrettyPrint

-- | Entry point: create grammar.js file
makeTreeSitter :: SharedOptions -> CF -> Backend
makeTreeSitter :: SharedOptions -> CF -> Backend
makeTreeSitter SharedOptions
opts CF
cf = do
  String -> MakeComment -> String -> Backend
forall c. FileContent c => String -> MakeComment -> c -> Backend
mkfile (String
dir String -> MakeComment
</> String
"grammar.js") MakeComment
comment (String -> Backend) -> String -> Backend
forall a b. (a -> b) -> a -> b
$
    Doc -> String
render (String -> Cat -> CF -> Doc
cfToTreeSitter String
name Cat
wordCat CF
cf)
  where
    name :: String
name = MakeComment
snakeCase_ (SharedOptions -> String
lang SharedOptions
opts)
    dir :: String
dir = String
"tree-sitter-" String -> MakeComment
forall a. [a] -> [a] -> [a]
++ MakeComment
kebabCase_ (SharedOptions -> String
lang SharedOptions
opts)
    wordCat :: Cat
wordCat = [String] -> Cat -> Cat
forall a. FixTokenCats a => [String] -> a -> a
fixTokenCats (CF -> [String]
forall f. CFG f -> [String]
tokenNames CF
cf) (String -> Cat
strToCat (SharedOptions -> String
treeSitterWord SharedOptions
opts))

comment :: String -> String
comment :: MakeComment
comment = (String
"// " String -> MakeComment
forall a. [a] -> [a] -> [a]
++)

-- | TODO: Add Makefile generation for tree-sitter