Skip to content

Commit

Permalink
feat: Add support for constant as alternative to #define.
Browse files Browse the repository at this point in the history
  • Loading branch information
iphydf committed Jan 31, 2024
1 parent 61daedb commit 731970b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Language/Cimple/Lexer.x
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ tokens :-
<0,ppSC> "break" { mkL KwBreak }
<0,ppSC> "case" { mkL KwCase }
<0,ppSC> "const" { mkL KwConst }
<0,ppSC> "constant" { mkL KwConstant }
<0,ppSC> "continue" { mkL KwContinue }
<0,ppSC> "default" { mkL KwDefault }
<0,ppSC> "do" { mkL KwDo }
Expand Down
12 changes: 11 additions & 1 deletion src/Language/Cimple/Parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import Language.Cimple.Tokens (LexemeClass (..))
break { L _ KwBreak _ }
case { L _ KwCase _ }
const { L _ KwConst _ }
constant { L _ KwConstant _ }
continue { L _ KwContinue _ }
default { L _ KwDefault _ }
do { L _ KwDo _ }
Expand Down Expand Up @@ -214,9 +215,18 @@ ToplevelDecl
| PreprocIf(ToplevelDecls) { $1 }
| PreprocInclude { $1 }
| PreprocUndef { $1 }
| ConstantDefn { $1 }
| StaticAssert { $1 }
| TypedefDecl { $1 }

ConstantDefn :: { NonTerm }
ConstantDefn
: constant '(' ID_CONST ',' ConstantValue ')' ';' { Fix $ PreprocDefineConst $3 $5 }

ConstantValue :: { NonTerm }
ConstantValue
: PreprocSafeExpr(ConstExpr) { $1 }

StaticAssert :: { NonTerm }
StaticAssert
: static_assert '(' ConstExpr ',' LIT_STRING ')' ';' { Fix $ StaticAssert $3 $5 }
Expand Down Expand Up @@ -310,7 +320,7 @@ PreprocInclude
PreprocDefine :: { NonTerm }
PreprocDefine
: '#define' ID_CONST '\n' { Fix $ PreprocDefine $2 }
| '#define' ID_CONST PreprocSafeExpr(ConstExpr) '\n' { Fix $ PreprocDefineConst $2 $3 }
| '#define' ID_CONST ConstantValue '\n' { Fix $ PreprocDefineConst $2 $3 }
| '#define' ID_CONST MacroParamList MacroBody '\n' { Fix $ PreprocDefineMacro $2 $3 $4 }

PreprocUndef :: { NonTerm }
Expand Down
1 change: 1 addition & 0 deletions src/Language/Cimple/Tokens.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ data LexemeClass
| KwBreak
| KwCase
| KwConst
| KwConstant
| KwContinue
| KwDefault
| KwDo
Expand Down

0 comments on commit 731970b

Please sign in to comment.