[ Index ] |
PHP Cross Reference of vtigercrm-6.1.0 |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * PHP_LexerGenerator, a php 5 lexer generator. 4 * 5 * This lexer generator translates a file in a format similar to 6 * re2c ({@link http://re2c.org}) and translates it into a PHP 5-based lexer 7 * 8 * PHP version 5 9 * 10 * LICENSE: 11 * 12 * Copyright (c) 2006, Gregory Beaver <[email protected]> 13 * All rights reserved. 14 * 15 * Redistribution and use in source and binary forms, with or without 16 * modification, are permitted provided that the following conditions 17 * are met: 18 * 19 * * Redistributions of source code must retain the above copyright 20 * notice, this list of conditions and the following disclaimer. 21 * * Redistributions in binary form must reproduce the above copyright 22 * notice, this list of conditions and the following disclaimer in 23 * the documentation and/or other materials provided with the distribution. 24 * * Neither the name of the PHP_LexerGenerator nor the names of its 25 * contributors may be used to endorse or promote products derived 26 * from this software without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 29 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 30 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 32 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 33 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 34 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 35 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 36 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 37 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 38 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 39 * 40 * @category php 41 * @package PHP_LexerGenerator 42 * @author Gregory Beaver <[email protected]> 43 * @copyright 2006 Gregory Beaver 44 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License 45 * @version CVS: $Id$ 46 * @since File available since Release 0.1.0 47 */ 48 /** 49 * The Lexer generation parser 50 */ 51 require_once 'PHP/LexerGenerator/Parser.php'; 52 /** 53 * Hand-written lexer for lex2php format files 54 */ 55 require_once 'PHP/LexerGenerator/Lexer.php'; 56 /** 57 * The basic home class for the lexer generator. A lexer scans text and 58 * organizes it into tokens for usage by a parser. 59 * 60 * Sample Usage: 61 * <code> 62 * require_once 'PHP/LexerGenerator.php'; 63 * $lex = new PHP_LexerGenerator('/path/to/lexerfile.plex'); 64 * </code> 65 * 66 * A file named "/path/to/lexerfile.php" will be created. 67 * 68 * File format consists of a PHP file containing specially 69 * formatted comments like so: 70 * 71 * <code> 72 * /*!lex2php 73 * {@*} 74 * </code> 75 * 76 * The first lex2php comment must contain several declarations and define 77 * all regular expressions. Declarations (processor instructions) start with 78 * a "%" symbol and must be: 79 * 80 * - %counter 81 * - %input 82 * - %token 83 * - %value 84 * - %line 85 * 86 * token and counter should define the class variables used to define lexer input 87 * and the index into the input. token and value should be used to define the class 88 * variables used to store the token number and its textual value. Finally, line 89 * should be used to define the class variable used to define the current line number 90 * of scanning. 91 * 92 * For example: 93 * <code> 94 * /*!lex2php 95 * %counter {$this->N} 96 * %input {$this->data} 97 * %token {$this->token} 98 * %value {$this->value} 99 * %line {%this->linenumber} 100 * {@*} 101 * </code> 102 * 103 * Patterns consist of an identifier containing upper or lower-cased letters, and 104 * a descriptive match pattern. 105 * 106 * Descriptive match patterns may either be regular expressions (regexes) or 107 * quoted literal strings. Here are some examples: 108 * 109 * <pre> 110 * pattern = "quoted literal" 111 * ANOTHER = /[a-zA-Z_]+/ 112 * </pre> 113 * 114 * Quoted strings must escape the \ and " characters with \" and \\. 115 * 116 * Regex patterns must be in Perl-compatible regular expression format (preg). 117 * special characters (like \t \n or \x3H) can only be used in regexes, all 118 * \ will be escaped in literal strings. 119 * 120 * Any sub-patterns must be defined using (?:) instead of (): 121 * 122 * <code> 123 * /*!lex2php 124 * %counter {$this->N} 125 * %input {$this->data} 126 * %token {$this->token} 127 * %value {$this->value} 128 * %line {%this->linenumber} 129 * alpha = /[a-zA-Z]/ 130 * alphaplus = /[a-zA-Z]+/ 131 * number = /[0-9]/ 132 * numerals = /[0-9]+/ 133 * whitespace = /[ \t\n]+/ 134 * blah = "$\"" 135 * blahblah = /a\$/ 136 * GAMEEND = @(?:1\-0|0\-1|1/2\-1/2)@ 137 * PAWNMOVE = /P?[a-h](?:[2-7]|[18]\=(?:Q|R|B|N))|P?[a-h]x[a-h](?:[2-7]|[18]\=(?:Q|R|B|N))/ 138 * {@*} 139 * </code> 140 * 141 * All regexes must be delimited. Any legal preg delimiter can be used (as in @ or / in 142 * the example above) 143 * 144 * 145 * @package PHP_LexerGenerator 146 * @author Gregory Beaver <[email protected]> 147 * @copyright 2006 Gregory Beaver 148 * @license http://www.php.net/license/3_01.txt PHP License 3.01 149 * @version 0.2.0 150 * @since Class available since Release 0.1.0 151 * @example TestLexer.plex Example lexer source 152 * @example TestLexer.php Example lexer php code 153 * @example usage.php Example usage of PHP_LexerGenerator 154 * @example Lexer.plex File_ChessPGN lexer source (complex) 155 * @example Lexer.php File_ChessPGN lexer php code 156 */ 157 158 class PHP_LexerGenerator 159 { 160 private $lex; 161 private $parser; 162 private $outfile; 163 /** 164 * Create a lexer file from its skeleton plex file. 165 * 166 * @param string $lexerfile path to the plex file 167 */ 168 function __construct($lexerfile) 169 { 170 $this->lex = new PHP_LexerGenerator_Lexer(file_get_contents($lexerfile)); 171 $info = pathinfo($lexerfile); 172 $this->outfile = $info['dirname'] . DIRECTORY_SEPARATOR . 173 substr($info['basename'], 0, 174 strlen($info['basename']) - strlen($info['extension'])) . 'php'; 175 $this->parser = new PHP_LexerGenerator_Parser($this->outfile, $this->lex); 176 $this->parser->PrintTrace(); 177 while ($this->lex->advance($this->parser)) { 178 $this->parser->doParse($this->lex->token, $this->lex->value); 179 } 180 $this->parser->doParse(0, 0); 181 } 182 } 183 //$a = new PHP_LexerGenerator('/development/File_ChessPGN/ChessPGN/Lexer.plex'); 184 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:08:37 2014 | Cross-referenced by PHPXref 0.7.1 |