diff --git a/BUILD.bazel b/BUILD.bazel index 6b2d890..3962a9e 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -81,6 +81,8 @@ happy_parser( haskell_library( name = "parser", srcs = [":Parser"], + extra_srcs = ["src/Language/Cimple/Parser.preproc.y"], + ghcopts = ["-DSOURCE=\"$(location src/Language/Cimple/Parser.preproc.y)\""], src_strip_prefix = "src", tags = ["no-cross"], visibility = [ @@ -93,8 +95,9 @@ haskell_library( "//third_party/haskell:aeson", "//third_party/haskell:array", "//third_party/haskell:base", + "//third_party/haskell:bytestring", "//third_party/haskell:data-fix", - "//third_party/haskell:recursion-schemes", + "//third_party/haskell:file-embed", "//third_party/haskell:text", "//third_party/haskell:transformers-compat", ], diff --git a/cimple.cabal b/cimple.cabal index 056ab4b..f2f2012 100644 --- a/cimple.cabal +++ b/cimple.cabal @@ -55,10 +55,10 @@ library , bytestring , containers , data-fix + , file-embed , filepath , monad-parallel , mtl - , recursion-schemes , split , text , transformers-compat diff --git a/src/Language/Cimple/CommentParser.y b/src/Language/Cimple/CommentParser.y index 8a7f60f..1206268 100644 --- a/src/Language/Cimple/CommentParser.y +++ b/src/Language/Cimple/CommentParser.y @@ -38,11 +38,14 @@ import Language.Cimple.Tokens (LexemeClass (..)) '@return' { L _ CmtCommand "@return" } '@retval' { L _ CmtCommand "@retval" } '@see' { L _ CmtCommand "@see" } + '@code' { L _ CmtCode "@code" } + '@endcode' { L _ CmtCode "@endcode" } ' ' { L _ CmtIndent " " } 'INDENT1' { L _ CmtIndent " " } 'INDENT2' { L _ CmtIndent " " } 'INDENT3' { L _ CmtIndent " " } + 'INDENT' { L _ CmtIndent _ } '(' { L _ PctLParen _ } ')' { L _ PctRParen _ } @@ -143,6 +146,22 @@ Command(x) | '@implements' CMT_WORD { Fix $ DocImplements $2 } | '@extends' CMT_WORD { Fix $ DocExtends $2 } | '@private' { Fix DocPrivate } +| '@code' Code '@endcode' { Fix $ DocLine $ Fix (DocWord $1) : (reverse $2) ++ [Fix (DocWord $3)] } + +Code :: { [NonTerm] } +Code +: CodeWord { [$1] } +| Code CodeWord { $2 : $1 } + +CodeWord :: { NonTerm } +CodeWord +: '\n' { Fix $ DocWord $1 } +| ' ' { Fix $ DocWord $1 } +| 'INDENT1' { Fix $ DocWord $1 } +| 'INDENT2' { Fix $ DocWord $1 } +| 'INDENT3' { Fix $ DocWord $1 } +| 'INDENT' { Fix $ DocWord $1 } +| CMT_CODE { Fix $ DocWord $1 } BulletListItem :: { NonTerm } BulletListItem @@ -232,7 +251,7 @@ prepend x (Fix (DocLine xs):rest) = Fix (DocLine (x:xs)) : rest failAt :: Lexeme Text -> String -> ParseResult a failAt n msg = - fail $ Text.unpack (sloc "" n) <> ": unexpected " <> describeLexeme n <> msg + fail $ Text.unpack (sloc "" n) <> ": unexpected in comment: " <> describeLexeme n <> msg parseError :: ([Lexeme Text], [String]) -> ParseResult a parseError ([], options) = fail $ " end of comment; expected one of " <> show options diff --git a/src/Language/Cimple/Parser.y b/src/Language/Cimple/Parser.y index 942825d..20c533e 100644 --- a/src/Language/Cimple/Parser.y +++ b/src/Language/Cimple/Parser.y @@ -1,11 +1,16 @@ { +{-# LANGUAGE CPP #-} {-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE TemplateHaskell #-} module Language.Cimple.Parser ( parseExpr , parseStmt , parseTranslationUnit + , source ) where +import qualified Data.ByteString as BS +import Data.FileEmbed (embedFile) import Data.Fix (Fix (..)) import Data.Text (Text) import qualified Data.Text as Text @@ -774,4 +779,11 @@ macroBodyStmt decls (L _ _ "0") = macroBodyStmt _ cond = alexError $ show cond <> ": macro do-while body must end in 'while (0)'" + +source :: Maybe BS.ByteString +#ifdef SOURCE +source = Just $(embedFile SOURCE) +#else +source = Nothing +#endif }