Sitellite Application Framework
Class Tree         Index         All Elements

Class: Parser

Source Location: Program_Root/Parser/Parser.php

Class Overview


Generic parser class from which new and complex parsers can be derived.


Author(s)

Version

  • 1.0, 2003-01-18, $Id: Parser.php,v 1.2 2005/07/06 15:30:56 lux Exp $

Copyright

  • Copyright (C) 2001-2003, Simian Systems Inc.

Variables

Methods


Child Classes

CSS_Parser
Parses CSS data into 3-dimensional associative arrays. Properly handles multiple declaration blocks for the same element, but will only keep the final value of a property that is declared again, which should not be a problem for most cases. Also properly handles /* style comments.
XMLDocQuery
XMLDocQuery provides XPath-compatible searching and referencing capabilities to the XMLDoc package. Here is a list of supported Xpath features:
XTExpression
XTExpression (XTE) is the expression syntax to the XT XML-based template engine.

Inherited Variables

Inherited Methods


Class Details

[line 119]
Generic parser class from which new and complex parsers can be derived.

Provides basic lexical analysis via regular expressions, which are assigned to callback functions. These functions are used to iterate over the resulting token list. Where you extend this class is in the syntax analysis and code generation/execution stages. Parser can also be used as a Finite State Machine (FSM), in which case saf.Parser.Buffer is handy for implementing the data structure creation.


1 <?php
2
3 // this example creates a comma-separated values (CSV) parser,
4 // which can be accomplished in PHP much easier than this, but
5 // this does serve as an example of what Parser can do, and hopefully
6 // you'll see more complex and more interesting uses for it.
7
8 class CSVParser extends Parser {
9
10 function CSVParser () {
11 // define our tokens
12 $this->addInternal ('_comma', ',');
13 $this->addInternal ('_newline', "\n");
14 $this->addInternal ('_escape', '\\');
15
16 // define our internal variables.
17 // we define $list as an array
18 // since we're parsing CSV files to create
19 // 2D arrays. note: in this case we're
20 // not using $output, but we don't want to
21 // override $output or the internal variables.
22 // in this case, we'll consider $output and
23 // $struct and $tokens and $regex reserved
24 // words.
25 $this->list = array ();
26 $this->skip = false;
27 $this->row = 0;
28 $this->column = 0;
29 $this->list[$this->row] = array ();
30 }
31
32 function _default ($token, $name) {
33 $this->list[$this->row][$this->column] .= $token;
34 }
35
36 function _comma ($token, $name) {
37 // commas are the separators
38 if ($this->skip) {
39 $this->list[$this->row][$this->column] .= ',';
40 $this->skip = false;
41 } else {
42 $this->column++;
43 }
44 }
45
46 function _newline ($token, $name) {
47 // increment
48 $this->row++;
49 $this->column = 0;
50 $this->list[$this->row] = array ();
51 }
52
53 function _escape ($token, $name) {
54 if ($this->skip) {
55 $this->list[$this->row][$this->column] .= '\\';
56 $this->skip = false;
57 } else {
58 $this->skip = true;
59 }
60 }
61 }
62
63 $data = 'Joe,Smith,joe@yoursite.com
64 Phil,Johnson,phil@yoursite.com
65 Bert,Morris,bert@yoursite.com';
66
67 $csv = new CSVParser ();
68 $csv->parse ($data);
69
70 echo '<pre>';
71 print_r ($csv->list);
72 echo '</pre>';
73
74 ? >




Tags:

access:  public
version:  1.0, 2003-01-18, $Id: Parser.php,v 1.2 2005/07/06 15:30:56 lux Exp $
license:  http://www.sitellite.org/index/license
copyright:  Copyright (C) 2001-2003, Simian Systems Inc.
author:  John Luxford <mailto:lux@simian.ca>


[ Top ]


Class Variables

$original =

[line 128]

Contains the original data sent to parse().



Tags:

access:  public

Type:   mixed


[ Top ]

$output =

[line 153]

Contains the output of parse().



Tags:

access:  public

Type:   mixed


[ Top ]

$regex =

[line 161]

Contains the output of makeRegex() on the current token list.



Tags:

access:  public

Type:   mixed


[ Top ]

$struct =

[line 136]

Contains the array of parsed elements, aka tokens.



Tags:

access:  public

Type:   mixed


[ Top ]

$switches =  's'

[line 174]

Contains a list of switches to the preg_split() and

preg_match() regular expression evaluations Default is 's', for dot-all mode. Note that these are PCRE (Perl-Compatible Regular Expression) expressions, not ereg() calls. For more info about switches, check out the PHP documentation at http://www.php.net/manual/en/pcre.pattern.modifiers.php




Tags:

access:  public

Type:   mixed


[ Top ]

$tokens =

[line 145]

Contains all registered tokens as hashes containing 'name', 'token', 'callback', and 'object' keys.



Tags:

access:  public

Type:   mixed


[ Top ]



Class Methods


constructor Parser [line 184]

Parser Parser( )

Constructor method.



Tags:

access:  public


[ Top ]

method addInternal [line 197]

void addInternal( string $name, string $token, [boolean $quote = true])

Alias of addToken().



Tags:

access:  public


Parameters:

string   $name  
string   $token  
boolean   $quote  

[ Top ]

method addToken [line 214]

void addToken( string $name, string $token, [boolean $quote = true])

Defines a token whose callback function has the same name as

$name, and is a method defined in the subclass of Parser (the class you create when you create a custom parser). Tokens are literal strings, unless $quote is set to false, in which case their values become active pieces of the token parsing regular expression.




Tags:

access:  public


Parameters:

string   $name  
string   $token  
boolean   $quote  

[ Top ]

method makeRegex [line 236]

string makeRegex( )

Turns the $tokens list into a regular expression.



Tags:

access:  public


[ Top ]

method parse [line 255]

string parse( string $data)

This is the mainloop of the parser.



Tags:

access:  public


Parameters:

string   $data  

[ Top ]

method _default [line 293]

string _default( string $token, string $name)

This is the default token handler. It merely returns

the token sent to it, which will be added to the output string in the parse() method, thereby recreating the original source data. This method is usually overridden when this class is extended.




Tags:

access:  public


Overridden in child classes as:

CSS_Parser::_default()
XTExpression::_default()

Parameters:

string   $token  
string   $name  

[ Top ]


Copyright © 2007, SIMIAN systems Inc.
All rights reserved. Privacy policy
Documentation generated on Tue, 13 Feb 2007 17:18:32 -0600 by Sitellite AppDoc and phpDocumentor 1.2.2