-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace ext/tokenizer/tokenizer_data_gen.php with CMake-based script
As the ext/tokenizer/tokenizer_data_gen.php is a very simple PHP command-line script, it can be also implemented directly in CMake. It simplifies building when PHP is not installed. There are simply too many dependencies and issues around these files - Zend's language parser files, PHP stubs, tokenizer generated data source file and PHP binary or installed PHP.
- Loading branch information
Showing
2 changed files
with
130 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
# This is CMake-based alternative of ext/tokenizer/tokenizer_data_gen.php | ||
# | ||
# Run as: | ||
# cmake -D PHP_SOURCE_DIR=<php-src> -P GenerateTokenizerData.cmake | ||
|
||
cmake_minimum_required(VERSION 3.25...3.31) | ||
|
||
if(NOT CMAKE_SCRIPT_MODE_FILE) | ||
message(FATAL_ERROR "This is a command-line script.") | ||
endif() | ||
|
||
if(NOT PHP_SOURCE_DIR) | ||
message(FATAL_ERROR "PHP_SOURCE_DIR variable is required.") | ||
endif() | ||
|
||
set(regex "^%token [^T]*(T_[^ \n]+)") | ||
|
||
file( | ||
STRINGS | ||
"${PHP_SOURCE_DIR}/Zend/zend_language_parser.y" | ||
lines | ||
REGEX "${regex}" | ||
) | ||
|
||
# Bypass the [ and ] characters issue in lists: | ||
# https://cmake.org/cmake/help/latest/manual/cmake-language.7.html#cmake-language-lists | ||
string(REPLACE "[" "LEFT_BRACKET" lines "${lines}") | ||
string(REPLACE "]" "RIGHT_BRACKET" lines "${lines}") | ||
|
||
# Get a list of tokens. | ||
set(tokens "") | ||
foreach(line IN LISTS lines) | ||
if(line MATCHES "${regex}") | ||
set(token "${CMAKE_MATCH_1}") | ||
|
||
if(token MATCHES "^T_(NOELSE|ERROR)$") | ||
continue() | ||
endif() | ||
|
||
list(APPEND tokens "${token}") | ||
endif() | ||
endforeach() | ||
|
||
set(content "") | ||
foreach(token IN LISTS tokens) | ||
string( | ||
APPEND | ||
content | ||
"/**\n * @var int\n * @cvalue ${token}\n */\n" | ||
"const ${token} = UNKNOWN;\n" | ||
) | ||
endforeach() | ||
string(STRIP "${content}" content) | ||
|
||
file( | ||
CONFIGURE | ||
OUTPUT "${PHP_SOURCE_DIR}/ext/tokenizer/tokenizer_data.stub.php" | ||
CONTENT [[ | ||
<?php | ||
|
||
/** @generate-class-entries */ | ||
|
||
@content@ | ||
/** | ||
* @var int | ||
* @cvalue T_PAAMAYIM_NEKUDOTAYIM | ||
*/ | ||
const T_DOUBLE_COLON = UNKNOWN; | ||
]] @ONLY) | ||
|
||
set(content "") | ||
foreach(token IN LISTS tokens) | ||
if(token STREQUAL "T_PAAMAYIM_NEKUDOTAYIM") | ||
string( | ||
APPEND | ||
content | ||
"\t\tcase T_PAAMAYIM_NEKUDOTAYIM: return \"T_DOUBLE_COLON\";\n" | ||
) | ||
else() | ||
string(APPEND content "\t\tcase ${token}: return \"${token}\";\n") | ||
endif() | ||
endforeach() | ||
|
||
set(tab "\t") | ||
|
||
file( | ||
CONFIGURE | ||
OUTPUT "${PHP_SOURCE_DIR}/ext/tokenizer/tokenizer_data.c" | ||
CONTENT [[ | ||
/* | ||
+----------------------------------------------------------------------+ | ||
| Copyright (c) The PHP Group | | ||
+----------------------------------------------------------------------+ | ||
| This source file is subject to version 3.01 of the PHP license, | | ||
| that is bundled with this package in the file LICENSE, and is | | ||
| available through the world-wide-web at the following url: | | ||
| https://www.php.net/license/3_01.txt | | ||
| If you did not receive a copy of the PHP license and are unable to | | ||
| obtain it through the world-wide-web, please send a note to | | ||
| [email protected] so we can mail you a copy immediately. | | ||
+----------------------------------------------------------------------+ | ||
| Author: Johannes Schlueter <[email protected]> | | ||
+----------------------------------------------------------------------+ | ||
*/ | ||
|
||
/* | ||
DO NOT EDIT THIS FILE! | ||
This file is generated using tokenizer_data_gen.php | ||
*/ | ||
|
||
#include "php.h" | ||
#include "zend.h" | ||
#include <zend_language_parser.h> | ||
|
||
char *get_token_type_name(int token_type) | ||
{ | ||
@tab@switch (token_type) { | ||
|
||
@content@ | ||
@tab@} | ||
@tab@return NULL; | ||
} | ||
|
||
]] @ONLY) |