From 731970b701a777ca858a2aea4bb97d88a1e4ae9e Mon Sep 17 00:00:00 2001 From: iphydf Date: Wed, 31 Jan 2024 15:07:10 +0000 Subject: [PATCH] feat: Add support for `constant` as alternative to `#define`. See https://github.com/TokTok/c-toxcore/pull/2619 --- src/Language/Cimple/Lexer.x | 1 + src/Language/Cimple/Parser.y | 12 +++++++++++- src/Language/Cimple/Tokens.hs | 1 + 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Language/Cimple/Lexer.x b/src/Language/Cimple/Lexer.x index c2311f1..fd5f86a 100644 --- a/src/Language/Cimple/Lexer.x +++ b/src/Language/Cimple/Lexer.x @@ -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 } diff --git a/src/Language/Cimple/Parser.y b/src/Language/Cimple/Parser.y index 6443c39..cc80946 100644 --- a/src/Language/Cimple/Parser.y +++ b/src/Language/Cimple/Parser.y @@ -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 _ } @@ -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 } @@ -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 } diff --git a/src/Language/Cimple/Tokens.hs b/src/Language/Cimple/Tokens.hs index 1324080..c1359f7 100644 --- a/src/Language/Cimple/Tokens.hs +++ b/src/Language/Cimple/Tokens.hs @@ -16,6 +16,7 @@ data LexemeClass | KwBreak | KwCase | KwConst + | KwConstant | KwContinue | KwDefault | KwDo