Skip to content

Commit

Permalink
fix: Add support for cmp.c and cmp.h.
Browse files Browse the repository at this point in the history
These were almost cimple-compliant (with some changes), but require that
cimple knows about a few things it does that aren't standard.
  • Loading branch information
iphydf committed Jan 23, 2024
1 parent c29231a commit 4414929
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 18 deletions.
5 changes: 4 additions & 1 deletion src/Language/Cimple/Lexer.x
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,10 @@ tokens :-
<0,ppSC> [A-Z][A-Z0-9_]{1,2} { mkL IdSueType }
<0,ppSC> _*[A-Z][A-Z0-9_]* { mkL IdConst }
<0,ppSC> [A-Z][A-Za-z0-9_]*[a-z][A-Za-z0-9_]* { mkL IdSueType }
<0,ppSC> "cmp_"[a-z][a-z0-9_]*_[stu] { mkL IdSueType }
<0,ppSC> [a-z][a-z0-9_]*_t { mkL IdStdType }
<0,ppSC> [a-z][a-z0-9_]*_cb { mkL IdFuncType }
<0,ppSC> "cmp_"("reader"|"writer"|"skipper") { mkL IdFuncType }
<0,ppSC> [a-z][A-Za-z0-9_]* { mkL IdVar }
<0,ppSC,cmtSC> [0-9]+[LU]* { mkL LitInteger }
<0,ppSC,cmtSC> [0-9]+"."[0-9]+[Ff]? { mkL LitInteger }
Expand Down Expand Up @@ -270,14 +272,15 @@ tokens :-
<cmtSC> [@\\][a-z]+ { mkL CmtCommand }
<cmtSC> "*"[A-Za-z][A-Za-z0-9_']*"*" { mkL CmtWord }
<cmtSC> "#"[A-Za-z][A-Za-z0-9_]* { mkL CmtRef }
<cmtSC> [A-Za-z][A-Za-z0-9_']* { mkL CmtWord }
<cmtSC> "_"*[A-Za-z][A-Za-z0-9_']* { mkL CmtWord }
<cmtSC> "#"[0-9]+ { mkL CmtWord }
<cmtSC> "http://"[^\ ]+ { mkL CmtWord }
<cmtSC> [0-9]+"%" { mkL LitInteger }
<cmtSC> "-1" { mkL LitInteger }
<cmtSC> "`"([^`]|"\`")+"`" { mkL CmtCode }
<cmtSC> "${"([^\}])+"}" { mkL CmtCode }
<cmtSC> "-"+ { mkL CmtWord }
<cmtSC> [&\|]+ { mkL CmtWord }
<cmtSC> "–" { mkL CmtWord }
<cmtSC> "*/" { mkL CmtEnd `andBegin` 0 }
<cmtSC> \n { mkL PpNewline `andBegin` cmtNewlineSC }
Expand Down
44 changes: 28 additions & 16 deletions src/Language/Cimple/Parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,11 @@ IgnoreBody
| IgnoreBody IGN_BODY { $2 : $1 }

PreprocIfdef(decls)
: '#ifdef' ID_CONST decls PreprocElse(decls) '#endif' { Fix $ PreprocIfdef $2 (reverse $3) $4 }
| '#ifndef' ID_CONST decls PreprocElse(decls) '#endif' { Fix $ PreprocIfndef $2 (reverse $3) $4 }
: '#ifdef' ID_CONST decls PreprocElse(decls) Endif { Fix $ PreprocIfdef $2 (reverse $3) $4 }
| '#ifndef' ID_CONST decls PreprocElse(decls) Endif { Fix $ PreprocIfndef $2 (reverse $3) $4 }

PreprocIf(decls)
: '#if' PreprocConstExpr '\n' decls PreprocElif(decls) '#endif' { Fix $ PreprocIf $2 (reverse $4) $5 }
: '#if' PreprocConstExpr '\n' decls PreprocElif(decls) Endif { Fix $ PreprocIf $2 (reverse $4) $5 }

PreprocElif(decls)
: PreprocElse(decls) { $1 }
Expand All @@ -298,6 +298,10 @@ PreprocElse(decls)
: { Fix $ PreprocElse [] }
| '#else' decls { Fix $ PreprocElse (reverse $2) }

Endif :: { [Term] }
Endif
: '#endif' Comment { [$1] }

PreprocInclude :: { NonTerm }
PreprocInclude
: '#include' LIT_STRING { Fix $ PreprocInclude $2 }
Expand Down Expand Up @@ -345,7 +349,7 @@ ExternC
'#endif'
ToplevelDecls
'#ifdef' ID_CONST
'}'
'}' Comment
'#endif' {% externC $2 $4 (reverse $7) $9 }

Stmts :: { [NonTerm] }
Expand All @@ -360,7 +364,8 @@ Stmt
| PreprocDefine Stmts PreprocUndef { Fix $ PreprocScopedDefine $1 (reverse $2) $3 }
| DeclStmt { $1 }
| CompoundStmt { $1 }
| IfStmt { $1 }
| IfStmt(ReturnStmt) { $1 }
| IfStmt(CompoundStmt) { $1 }
| ForStmt { $1 }
| WhileStmt { $1 }
| DoWhileStmt { $1 }
Expand All @@ -372,16 +377,19 @@ Stmt
| goto ID_CONST ';' { Fix $ Goto $2 }
| ID_CONST ':' Stmt { Fix $ Label $1 $3 }
| continue ';' { Fix $ Continue }
| return ';' { Fix $ Return Nothing }
| return Expr ';' { Fix $ Return (Just $2) }
| switch '(' Expr ')' '{' SwitchCases '}' { Fix $ SwitchStmt $3 (reverse $6) }
| ReturnStmt { $1 }
| Comment { $1 }

IfStmt :: { NonTerm }
IfStmt
: if '(' Expr ')' CompoundStmt { Fix $ IfStmt $3 $5 Nothing }
| if '(' Expr ')' CompoundStmt else IfStmt { Fix $ IfStmt $3 $5 (Just $7) }
| if '(' Expr ')' CompoundStmt else CompoundStmt { Fix $ IfStmt $3 $5 (Just $7) }
ReturnStmt :: { NonTerm }
ReturnStmt
: return ';' { Fix $ Return Nothing }
| return Expr ';' { Fix $ Return (Just $2) }

IfStmt(x)
: if '(' Expr ')' x { Fix $ IfStmt $3 $5 Nothing }
| if '(' Expr ')' x else x { Fix $ IfStmt $3 $5 (Just $7) }
| if '(' Expr ')' x else IfStmt(x) { Fix $ IfStmt $3 $5 (Just $7) }

ForStmt :: { NonTerm }
ForStmt
Expand Down Expand Up @@ -419,6 +427,7 @@ SwitchCaseBody :: { NonTerm }
SwitchCaseBody
: CompoundStmt { $1 }
| SwitchCase { $1 }
| break ';' { Fix $ Break }
| return Expr ';' { Fix $ Return (Just $2) }

DeclStmt :: { NonTerm }
Expand Down Expand Up @@ -604,17 +613,20 @@ EnumDecl
EnumeratorList :: { [NonTerm] }
EnumeratorList
: '{' Enumerators '}' { reverse $2 }
| '{' Enumerators ',' '}' { reverse $2 }
| '{' Enumerators Comment '}' { reverse $2 } -- TODO(iphydf): Don't throw away the comment.
| '{' Enumerators ',' Comment '}' { reverse $2 } -- TODO(iphydf): Don't throw away the comment.

Enumerators :: { [NonTerm] }
Enumerators
: Enumerator { [$1] }
| Enumerators Enumerator { $2 : $1 }
| Enumerators ',' Enumerator { $3 : $1 }

Enumerator :: { NonTerm }
Enumerator
: EnumeratorName ',' { Fix $ Enumerator $1 Nothing }
| EnumeratorName '=' ConstExpr ',' { Fix $ Enumerator $1 (Just $3) }
| Comment { $1 }
: EnumeratorName { Fix $ Enumerator $1 Nothing }
| EnumeratorName '=' ConstExpr { Fix $ Enumerator $1 (Just $3) }
| Comment Enumerator { Fix $ Commented $1 $2 }

EnumeratorName :: { Term }
EnumeratorName
Expand Down
7 changes: 6 additions & 1 deletion src/Language/Cimple/TreeParser.y
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,12 @@ TranslationUnit

Header :: { NonTerm }
Header
: preprocIfndef {% recurse parseHeaderBody $1 }
: preprocIfndef ModeLine {% recurse parseHeaderBody $1 }

ModeLine :: { Maybe NonTerm }
ModeLine
: { Nothing }
| comment { Just $1 }

HeaderBody :: { [NonTerm] }
HeaderBody
Expand Down

0 comments on commit 4414929

Please sign in to comment.