Difference between revisions of "How to generate your own PHP syntax file"

From EditPlus Wiki
Jump to: navigation, search
(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...')
 
(Updated for the current version of PHP and PHP.net)
 
Line 1: Line 1:
 
Using PHP, it is possible to create your own personalized syntax file, custom-tailored to your PHP version and configuration.
 
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 [http://php.net/manual/en/filesystem.configuration.php allow_url_fopen] to be enabled.)
+
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 [http://php.net/manual/en/filesystem.configuration.php allow_url_fopen] to be enabled.)
 
+
  
 
<pre><?php
 
<pre><?php
 
if (false)
 
{
 
$GLOBALS;$_GET;$_POST;$_REQUEST;$_COOKIE;$_ENV;$_SERVER;$_SESSION;$_FILES;
 
}
 
 
$global_vars = array_keys(get_defined_vars());
 
  
 
$classes = get_declared_classes();
 
$classes = get_declared_classes();
Line 34: Line 26:
  
 
#keyword=Variable
 
#keyword=Variable
 +
 
';
 
';
  
 
$html = file_get_contents('http://docs.php.net/manual/en/reserved.keywords.php');
 
$html = file_get_contents('http://docs.php.net/manual/en/reserved.keywords.php');
 
$html = preg_replace('#\\n[\\n\\r\\t ]*#', '', $html);
 
$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);
+
preg_match_all('#<table class="doctable table"><caption><strong>(.*?)</strong></caption>(.*?)</table>#s', $html, $matches,  
 +
PREG_SET_ORDER);
  
 
foreach ($matches as $section)
 
foreach ($matches as $section)
 
{
 
{
 
list(, $title, $contents) = $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";
+
$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 = file_get_contents('http://docs.php.net/manual/en/language.oop5.magic.php');
 
$html = preg_replace('#\\n[\\n\\r\\t ]*#', '', $html);
 
$html = preg_replace('#\\n[\\n\\r\\t ]*#', '', $html);
preg_match_all('#<a href=".*?" class="link"><i>(.*?)</i></a>#', $html, $m);
+
preg_match_all('#<'.'a href=".*?" class="link">(__[a-z_]+)\\(\\)</a>#i', $html, $m);
$magic_methods = $m[1];
+
$magic_methods = array_unique($m[1]);
  
 
$file .= "\n#keyword=Magic Methods\n" . implode("\n", $magic_methods);
 
$file .= "\n#keyword=Magic Methods\n" . implode("\n", $magic_methods);
 
  
 
$file .= "\n\n#keyword=Built-in functions";
 
$file .= "\n\n#keyword=Built-in functions";
Line 83: Line 92:
 
{
 
{
 
$file .= "\n" . $function;
 
$file .= "\n" . $function;
}
 
 
$file .= "\n\n#keyword=Predefined Variables";
 
sort($global_vars);
 
foreach ($global_vars as $varname)
 
{
 
$file .= "\n" . $varname;
 
 
}
 
}
  
Line 99: Line 101:
  
 
$file .= "\n\n#keyword=Predefined Constants\n";
 
$file .= "\n\n#keyword=Predefined Constants\n";
$constants = array_keys(get_defined_constants());
+
$constants = array_keys(get_defined_constants() + array('true' => 1, 'false' => 1, 'null' => 1));
 
sort($constants);
 
sort($constants);
 
$file .= implode("\n", $constants);
 
$file .= implode("\n", $constants);
  
file_put_contents(dirname(__FILE__) . '/php.stx', $file);</pre>
+
$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);</pre>

Latest revision as of 12:34, 11 August 2012

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