How to generate your own PHP syntax file

From EditPlus Wiki
Revision as of 04:52, 23 February 2010 by Efvay (Talk | contribs) (Created page with '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 synt...')

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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 directory it is run from, based on informations 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

if (false)
{
	$GLOBALS;$_GET;$_POST;$_REQUEST;$_COOKIE;$_ENV;$_SERVER;$_SESSION;$_FILES;
}

$global_vars = array_keys(get_defined_vars());

$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><b>(.*?)</b></caption>(.*?)</table>#s', $html, $matches, PREG_SET_ORDER);

foreach ($matches as $section)
{
	list(, $title, $contents) = $section;
	preg_match_all('#<a href=".*?" class=".*?">([a-z_]+)\\(?\\)?</a>#i', $contents, $m);
	$file .= "\n#keyword=" . $title . "\n" . implode("\n", $m[1]) . "\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"><i>(.*?)</i></a>#', $html, $m);
$magic_methods = $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 Variables";
sort($global_vars);
foreach ($global_vars as $varname)
{
	$file .= "\n" . $varname;
}

$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());
sort($constants);
$file .= implode("\n", $constants);

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