Hi,
here's a simple parser for a very simple query language. it's based on marcus' SimpleLexer found in SimpleTest. i had some difficulties understanding the concept behind the entry and exit stuff, but hey.... it works good enough for an hour of toying around with it
PHP Code:
<?php
// Include the parser
require_once 'parser.php';
class OQLParser
{
function writePlaintext($match, $state)
{
var_dump('writePlaintext : '.$match." ( $state )");
return true;
}
function writeOperator($match, $state)
{
var_dump('writeOperator : '.$match." ( $state )");
return true;
}
function writeEntity($match, $state)
{
var_dump('writeEntity : '.$match." ( $state )");
return true;
}
function writeOpenBrace($match, $state)
{
var_dump('writeOpenBrace : '.$match." ( $state )");
return true;
}
function writeCloseBrace($match, $state)
{
var_dump('writeCloseBrace : '.$match." ( $state )");
return true;
}
function writeString($match, $state)
{
var_dump('writeString : '.$match." ( $state )");
return true;
}
function writeNumeric($match, $state)
{
var_dump('writeNumeric : '.$match." ( $state )");
return true;
}
}
// Create the OQL parser
$Parser = & new OQLParser();
$Lexer = &new SimpleLexer($Parser, 'writePlaintext', false);
## Operators
$Lexer->addSpecialPattern('=|<|>| like | in ', 'writePlaintext', 'writeOperator');
## Booleans
$Lexer->addSpecialPattern(' and | or | not ', 'writePlaintext', 'writeOperator');
## Braces
$Lexer->addSpecialPattern('(', 'writePlaintext', 'writeOpenBrace');
$Lexer->addSpecialPattern(')', 'writePlaintext', 'writeCloseBrace');
## String
$Lexer->addSpecialPattern('"[^"]+"', 'writePlaintext', 'writeString');
## Numeric
$Lexer->addSpecialPattern('\d[\d]*\.{0,1}[\d]*', 'writePlaintext', 'writeNumeric');
## Entity
$Lexer->addSpecialPattern('[a-zA-Z]+[\.a-zA-Z]+', 'writePlaintext', 'writeEntity');
$template = '( Customer.Name LikE "1" ) OR ( Customer.Id>10000.1234) OR (Customer.Managment.Count IN (1,"3 OR",4))';
#$template = '(Customer IN (1,3,4))';
$Lexer->parse($template);
?>
cheers
Chris
Bookmarks