How to generate your own PHP syntax file

From EditPlus Wiki
Jump to: navigation, search

Using PHP, it is possible to create your own personalized syntax file, custom-tailored to your PHP version and configuration.

For instance, the script below will generate a syntax file in the current directory, based on information gathered at runtime for predefined classes and their methods, predefined constants and variables, as well as built-in functions. It also retrieves the list of reserved words directly from PHP's website (requires allow_url_fopen to be enabled.)

<?php

$classes = get_declared_classes();

$file = '#TITLE=PHP ' . PHP_VERSION . '
#CASE=y
#DELIMITER=,(){}[]-+*%/="`\'~!&|<>?:;.#@\\
#QUOTATION1=\'
#QUOTATION2="
#CONTINUE_QUOTE=y
#LINECOMMENT=//
#LINECOMMENT2=#
#COMMENTON=/*
#COMMENTOFF=*/
#ESCAPE=\
#PREFIX1=$
#HTML_EMBEDDED=y
#SCRIPT_BEGIN=<?
#SCRIPT_END=?>
#SKIP_QUOTE=y
#NUMBER_PATTERN=cpp

#keyword=Variable

';

$html = file_get_contents('http://docs.php.net/manual/en/reserved.keywords.php');
$html = preg_replace('#\\n[\\n\\r\\t ]*#', '', $html);
preg_match_all('#<table class="doctable table"><caption><strong>(.*?)</strong></caption>(.*?)</table>#s', $html, $matches, 
PREG_SET_ORDER);

foreach ($matches as $section)
{
	list(, $title, $contents) = $section;

	$keywords = array();

	preg_match_all('#<td>.*?</td>#', $contents, $m);

	foreach ($m[0] as $html)
	{
		$keyword = trim(preg_replace('#\\(.*#', '', strip_tags($html)));

		if ($keyword !== '')
		{
			$keywords[] = $keyword;
		}
	}

	sort($keywords);

	$file .= "\n#keyword=" . $title . "\n" . implode("\n", $keywords) . "\n";
}

$html = file_get_contents('http://docs.php.net/manual/en/language.oop5.magic.php');
$html = preg_replace('#\\n[\\n\\r\\t ]*#', '', $html);
preg_match_all('#<'.'a href=".*?" class="link">(__[a-z_]+)\\(\\)</a>#i', $html, $m);
$magic_methods = array_unique($m[1]);

$file .= "\n#keyword=Magic Methods\n" . implode("\n", $magic_methods);

$file .= "\n\n#keyword=Built-in functions";

$methods = array_flip($magic_methods);;
foreach ($classes as $class)
{
	$class_methods = get_class_methods($class);
	sort($class_methods);

	$file .= "\n;{$class}\n";
	foreach ($class_methods as $method)
	{
		if (isset($methods[$method]))
		{
			continue;
		}

		$methods[$method] = 0;
		$file .= $method . "\n";
	}
}
unset($methods);

$file .= "\n;PHP functions";
$functions = get_defined_functions();
sort($functions['internal']);
foreach ($functions['internal'] as &$function)
{
	$file .= "\n" . $function;
}

$file .= "\n\n#keyword=Predefined Classes\nself\nparent\n";
$file .= implode("\n", $classes) . "\n";

$file .= "\n;Predefined Interfaces\n";
$file .= implode("\n", get_declared_interfaces()) . "\n";

$file .= "\n\n#keyword=Predefined Constants\n";
$constants = array_keys(get_defined_constants() + array('true' => 1, 'false' => 1, 'null' => 1));
sort($constants);
$file .= implode("\n", $constants);

$file .= '
#keyword=Predefined Variables
$GLOBALS
$_SERVER
$_GET
$_POST
$_FILES
$_REQUEST
$_SESSION
$_ENV
$_COOKIE
$php_errormsg
$HTTP_RAW_POST_DATA
$http_response_header
$argc
$argv
';

file_put_contents('./php.stx', $file);