From 4414929d57b7aa808acd3caf4504708d996d32fc Mon Sep 17 00:00:00 2001 From: iphydf Date: Tue, 23 Jan 2024 13:07:34 +0000 Subject: [PATCH] fix: Add support for cmp.c and cmp.h. These were almost cimple-compliant (with some changes), but require that cimple knows about a few things it does that aren't standard. --- src/Language/Cimple/Lexer.x | 5 +++- src/Language/Cimple/Parser.y | 44 ++++++++++++++++++++------------ src/Language/Cimple/TreeParser.y | 7 ++++- 3 files changed, 38 insertions(+), 18 deletions(-) diff --git a/src/Language/Cimple/Lexer.x b/src/Language/Cimple/Lexer.x index 50a8074..51d1550 100644 --- a/src/Language/Cimple/Lexer.x +++ b/src/Language/Cimple/Lexer.x @@ -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 } @@ -270,7 +272,7 @@ tokens :- [@\\][a-z]+ { mkL CmtCommand } "*"[A-Za-z][A-Za-z0-9_']*"*" { mkL CmtWord } "#"[A-Za-z][A-Za-z0-9_]* { mkL CmtRef } - [A-Za-z][A-Za-z0-9_']* { mkL CmtWord } + "_"*[A-Za-z][A-Za-z0-9_']* { mkL CmtWord } "#"[0-9]+ { mkL CmtWord } "http://"[^\ ]+ { mkL CmtWord } [0-9]+"%" { mkL LitInteger } @@ -278,6 +280,7 @@ tokens :- "`"([^`]|"\`")+"`" { mkL CmtCode } "${"([^\}])+"}" { mkL CmtCode } "-"+ { mkL CmtWord } + [&\|]+ { mkL CmtWord } "–" { mkL CmtWord } "*/" { mkL CmtEnd `andBegin` 0 } \n { mkL PpNewline `andBegin` cmtNewlineSC } diff --git a/src/Language/Cimple/Parser.y b/src/Language/Cimple/Parser.y index 61b5c4c..6443c39 100644 --- a/src/Language/Cimple/Parser.y +++ b/src/Language/Cimple/Parser.y @@ -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 } @@ -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 } @@ -345,7 +349,7 @@ ExternC '#endif' ToplevelDecls '#ifdef' ID_CONST - '}' + '}' Comment '#endif' {% externC $2 $4 (reverse $7) $9 } Stmts :: { [NonTerm] } @@ -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 } @@ -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 @@ -419,6 +427,7 @@ SwitchCaseBody :: { NonTerm } SwitchCaseBody : CompoundStmt { $1 } | SwitchCase { $1 } +| break ';' { Fix $ Break } | return Expr ';' { Fix $ Return (Just $2) } DeclStmt :: { NonTerm } @@ -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 diff --git a/src/Language/Cimple/TreeParser.y b/src/Language/Cimple/TreeParser.y index b4dcf4c..5902f69 100644 --- a/src/Language/Cimple/TreeParser.y +++ b/src/Language/Cimple/TreeParser.y @@ -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