Skip to content

Commit

Permalink
fix: Slightly better support for code in comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
iphydf committed Jan 16, 2024
1 parent 032d57b commit 85736b9
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
5 changes: 4 additions & 1 deletion BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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",
],
Expand Down
2 changes: 1 addition & 1 deletion cimple.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ library
, bytestring
, containers
, data-fix
, file-embed
, filepath
, monad-parallel
, mtl
, recursion-schemes
, split
, text
, transformers-compat
Expand Down
21 changes: 20 additions & 1 deletion src/Language/Cimple/CommentParser.y
Original file line number Diff line number Diff line change
Expand Up @@ -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 _ }
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions src/Language/Cimple/Parser.y
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
}

0 comments on commit 85736b9

Please sign in to comment.