PHPackages                             ffi/preprocessor - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. ffi/preprocessor

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

ffi/preprocessor
================

Simple C Preprocessor

0.2.3(1y ago)2637.0k13MITPHPPHP ^7.4|^8.0CI passing

Since Nov 3Pushed 1y ago2 watchersCompare

[ Source](https://github.com/php-ffi/preprocessor)[ Packagist](https://packagist.org/packages/ffi/preprocessor)[ RSS](/packages/ffi-preprocessor/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (5)Dependencies (13)Versions (6)Used By (13)

Simple C-lang Preprocessor
==========================

[](#simple-c-lang-preprocessor)

This implementation of a preprocessor based in part on [ISO/IEC 9899:TC2](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf).

Requirements
------------

[](#requirements)

- PHP &gt;= 7.4

Installation
------------

[](#installation)

Library is available as composer repository and can be installed using the following command in a root of your project.

```
$ composer require ffi/preprocessor
```

Usage
-----

[](#usage)

```
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;
//
```

Directives
----------

[](#directives)

### Supported Directives

[](#supported-directives)

- `#include "file.h"` local-first include
- `#include ` global-first include
- `#define name` defining directives
    - `#define name value` object-like macro
    - `#define name(arg) value` function-like macro
    - `#define name(arg) xxx##arg` concatenation
    - `#define name(arg) #arg` stringizing
- `#undef name` removing directives
- `#ifdef name` "if directive defined" condition
- `#ifndef name` "if directive not defined" condition
- `#if EXPRESSION` if condition
- `#elif EXPRESSION` else if condition
- `#else` else condition
- `#endif` completion of a condition
- `#error message` error message directive
- `#warning message` warning message directive
- `#line 66 "filename"` line and file override
- `#pragma XXX` compiler control
    - `#pragma once`
- `#assert XXX` compiler assertion
    - `#unassert XXX` compiler assertion
- `#ident XXX`
    - `#sccs XXX`

### Expression Grammar

[](#expression-grammar)

#### Comparison Operators

[](#comparison-operators)

- `A > B` greater than
- `A < B` less than
- `A == B` equal
- `A != B` not equal
- `A >= B` greater than or equal
- `A  B` bitwise right shift

#### Other Operators

[](#other-operators)

- `defined(X)` defined macro
- `A ? B : C` ternary
- `sizeof VALUE` sizeof
    - `sizeof(TYPE)` sizeof type

### Literals

[](#literals)

- `true`, `false` boolean
- `42` decimal integer literal
    - `42u`, `42U` unsigned int
    - `42l`, `42L` long int
    - `42ul`, `42UL` unsigned long int
    - `42ll`, `42LL` long long int
    - `42ull`, `42ULL` unsigned long long int
- `042` octal integer literal
- `0x42` hexadecimal integer literal
- `0b42` binary integer literal
- `"string"` string (char array)
    - `L"string"` string (wide char array)
    - `"\•"` escape sequences in strings
    - `"\•••"` arbitrary octal value in strings
    - `"\X••"` arbitrary hexadecimal value in strings
- `'x'` char literal
    - `'\•'` escape sequences
    - `'\•••'` arbitrary octal value
    - `'\X••'` arbitrary hexadecimal value
    - `L'x'` wide character literal
- `42.0` double
    - `42f`, `42F` float
    - `42l`, `42L` long double
    - `42E` exponential form
    - `0.42e23` exponential form
- `NULL` null macro

### Type Casting

[](#type-casting)

- `(char)42`
- `(short)42`
- `(int)42`
- `(long)42`
- `(float)42`
- `(double)42`
- `(bool)42` (Out of ISO/IEC 9899:TC2 specification)
- `(string)42` (Out of ISO/IEC 9899:TC2 specification)
- `(void)42`
- `(long type)42` Casting to a long type (`long int`, `long double`, etc)
- `(const type)42` Casting to a constant type (`const char`, etc)
- `(unsigned type)42` Casting to unsigned type (`unsigned int`, `unsigned long`, etc)
- `(signed type)42` Casting to signed type (`signed int`, `signed long`, etc)
- Pointers (`void *`, etc)
- References (`unsigned int`, `unsigned long`, etc)

### Object Like Directive

[](#object-like-directive)

```
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'));
```

Function Like Directive
-----------------------

[](#function-like-directive)

```
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'));
```

Include Directories
-------------------

[](#include-directories)

```
use FFI\Preprocessor\Preprocessor;

$pre = new Preprocessor();

$pre->include('/path/to/directory');
$pre->exclude('some');
```

Message Handling
----------------

[](#message-handling)

```
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")
');
```

###  Health Score

37

—

LowBetter than 83% of packages

Maintenance41

Moderate activity, may be stable

Popularity31

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~285 days

Total

5

Last Release

518d ago

PHP version history (2 changes)0.1.0PHP &gt;=7.4

0.2.2PHP ^7.4|^8.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/150420?v=4)[Ruslan Sharipov](/maintainers/Serafim)[@serafim](https://github.com/serafim)

---

Top Contributors

[![SerafimArts](https://avatars.githubusercontent.com/u/2461257?v=4)](https://github.com/SerafimArts "SerafimArts (21 commits)")

---

Tags

ccompilerphppreprocessorparserheaderscompilerffipreprocessorc

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/ffi-preprocessor/health.svg)

```
[![Health](https://phpackages.com/badges/ffi-preprocessor/health.svg)](https://phpackages.com/packages/ffi-preprocessor)
```

###  Alternatives

[league/commonmark

Highly-extensible PHP Markdown parser which fully supports the CommonMark spec and GitHub-Flavored Markdown (GFM)

3.0k404.0M702](/packages/league-commonmark)[composer-unused/symbol-parser

Toolkit to parse symbols from a composer package

145.7M2](/packages/composer-unused-symbol-parser)[type-lang/parser

Library for parsing and validating TypeLang syntax and converting it into AST nodes

5158.4k6](/packages/type-lang-parser)[mishal/iless

Less.js port to PHP

4737.0k3](/packages/mishal-iless)[smuuf/php-peg

PEG parser generator for PHP.

12118.4k3](/packages/smuuf-php-peg)[hexydec/htmldoc

A token based HTML document parser and minifier. Minify HTML documents including inline CSS, Javascript, and SVG's on the fly. Extract document text, attributes, and fragments. Full test suite.

2610.3k3](/packages/hexydec-htmldoc)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
