ffi / preprocessor
简单的C预处理器
0.2.2
2023-08-08 21:26 UTC
Requires
- php: ^7.4|^8.0
- ffi/preprocessor-contracts: ^1.0
- phplrt/lexer: ^3.2
- phplrt/parser: ^3.2
- psr/log: ^1.0|^2.0|^3.0
- symfony/polyfill-ctype: ^1.27
- symfony/polyfill-php80: ^1.27
Requires (Dev)
- jetbrains/phpstorm-attributes: ^1.0
- monolog/monolog: ^2.9|^3.0
- phplrt/phplrt: ^3.2
- phpunit/phpunit: ^9.6
- squizlabs/php_codesniffer: ^3.7
- vimeo/psalm: ^5.14
Provides
This package is auto-updated.
Last update: 2024-09-08 23:50:27 UTC
README
本实现基于部分ISO/IEC 9899:TC2的预处理器。
要求
- PHP >= 7.4
安装
库作为composer存储库可用,您可以使用以下命令在项目根目录中安装。
$ composer require ffi/preprocessor
用法
use FFI\Preprocessor\Preprocessor; $pre = new Preprocessor(); echo $pre->process(' #define VK_DEFINE_HANDLE(object) typedef struct object##_T* object; #if !defined(VK_DEFINE_NON_DISPATCHABLE_HANDLE) #if defined(__LP64__) || defined(_WIN64) || (defined(__x86_64__) && !defined(__ILP32__) ) || defined(_M_X64) || defined(__ia64) || defined (_M_IA64) || defined(__aarch64__) || defined(__powerpc64__) #define VK_DEFINE_NON_DISPATCHABLE_HANDLE(object) typedef struct object##_T *object; #else #define VK_DEFINE_NON_DISPATCHABLE_HANDLE(object) typedef uint64_t object; #endif #endif VK_DEFINE_HANDLE(VkInstance) VK_DEFINE_NON_DISPATCHABLE_HANDLE(VkSemaphore) '); // // Expected Output: // // typedef struct VkInstance_T* VkInstance; // typedef uint64_t VkSemaphore; //
指令
支持的指令
-
#include "file.h"本地优先包含 -
#include <file.h>全局优先包含 -
#define name定义指令-
#define name value对象型宏 -
#define name(arg) value函数型宏 -
#define name(arg) xxx##arg连接 -
#define name(arg) #arg字符串化
-
-
#undef name移除指令 -
#ifdef name"指令已定义" 条件 -
#ifndef name"指令未定义" 条件 -
#if EXPRESSION如果条件 -
#elif EXPRESSION否则如果条件 -
#else否则条件 -
#endif条件完成 -
#error message错误消息指令 -
#warning message警告消息指令 -
#line 66 "filename"覆盖行和文件 -
#pragma XXX编译器控制-
#pragma once
-
-
#assert XXX编译器断言-
#unassert XXX编译器断言
-
-
#ident XXX-
#sccs XXX
-
表达式语法
比较运算符
-
A > B大于 -
A < B小于 -
A == B等于 -
A != B不等于 -
A >= B大于等于 -
A <= B小于等于
逻辑运算符
-
! A逻辑非 -
A && B逻辑与 -
A || B逻辑或 -
(...)分组
算术运算符
-
A + B数学加法 -
A - B数学减法 -
A * B数学乘法 -
A / B数学除法 -
A % B取模 -
A++自增-
++A前缀形式
-
-
A--自减-
--A前缀形式
-
-
+A一元加 -
-A一元减 -
&A一元地址 -
*A一元指针
位运算符
-
~A位非 -
A & B位与 -
A | B位或 -
A ^ B位异或 -
A << B位左移 -
A >> B位右移
其他运算符
-
defined(X)已定义宏 -
A ? B : C三元 -
sizeof VALUEsizeof-
sizeof(TYPE)sizeof 类型
-
字面量
-
true,false布尔 -
42十进制整数字面量-
42u,42U无符号整型 -
42l,42L长整型 -
42ul,42UL无符号长整型 -
42ll,42LL长长整型 -
42ull,42ULL无符号长长整型
-
-
042八进制整数字面量 -
0x42十六进制整数字面量 -
0b42二进制整数字面量 -
"string"字符串(字符数组)-
L"string"字符串(宽字符数组) -
"\•"字符串中的转义序列 -
"\•••"字符串中的任意八进制值 -
"\X••"字符串中的任意十六进制值
-
-
'x'字符字面量-
'\•'转义序列 -
'\•••'任意的八进制值 -
'\X••'任意的十六进制值 -
L'x'宽字符字面量
-
-
42.0双精度浮点数-
42f、42F浮点数 -
42l、42L长双精度浮点数 -
42E指数形式 -
0.42e23指数形式
-
-
NULL空宏
类型转换
-
(char)42 -
(short)42 -
(int)42 -
(long)42 -
(float)42 -
(double)42 -
(bool)42(超出ISO/IEC 9899:TC2规范) -
(string)42(超出ISO/IEC 9899:TC2规范) -
(void)42 -
(long type)42转换为长类型(long int、long double等) -
(const type)42转换为常量类型(const char等) -
(unsigned type)42转换为无符号类型(unsigned int、unsigned long等) -
(signed type)42转换为有符号类型(signed int、signed long等) - 指针(
void *等) - 引用(
unsigned int、unsigned long等)
对象类指令
use FFI\Preprocessor\Preprocessor; use FFI\Preprocessor\Directive\ObjectLikeDirective; $pre = new Preprocessor(); // #define A $pre->define('A'); // #define B 42 $pre->define('B', '42'); // #define С 42 $pre->define('С', new ObjectLikeDirective('42'));
函数类指令
use FFI\Preprocessor\Preprocessor; use FFI\Preprocessor\Directive\FunctionLikeDirective; $pre = new Preprocessor(); // #define C(object) object##_T* object; $pre->define('C', function (string $arg) { return "${arg}_T* ${arg};"; }); // #define D(object) object##_T* object; $pre->define('D', new FunctionLikeDirective(['object'], 'object##_T* object'));
包含目录
use FFI\Preprocessor\Preprocessor; $pre = new Preprocessor(); $pre->include('/path/to/directory'); $pre->exclude('some');
消息处理
use FFI\Preprocessor\Preprocessor; $logger = new Psr3LoggerImplementation(); $pre = new Preprocessor($logger); $pre->process(' #error Error message // Will be sent to the logger: // - LoggerInterface::error("Error message") #warning Warning message // Will be sent to the logger: // - LoggerInterface::warning("Warning message") ');