Browse files

Merge pull request #8 from akkie/master

Merge changes with base repository
  • Loading branch information...
2 parents 50c3775 + e7f6d6c commit 693fee3a3e27af98ef791b813bbfd62c20e8ae43 @akkie akkie committed Apr 6, 2012
View
3 README.textile
@@ -52,9 +52,8 @@ So lets talk about the steps to create your own language.
use com\mohiva\pyramid\Parser;
use com\mohiva\pyramid\example\Lexer;
use com\mohiva\pyramid\example\Grammar;
-use com\mohiva\common\parser\TokenStream;
-$lexer = new Lexer(new TokenStream);
+$lexer = new Lexer();
$stream = $lexer->scan('1.1 + 1.5');
$parser = new Parser(new Grammar());
View
43 src/com/mohiva/pyramid/example/Lexer.php
@@ -76,55 +76,30 @@ class Lexer {
);
/**
- * The token stream.
- *
- * @var \com\mohiva\common\parser\TokenStream
- */
- private $stream = null;
-
- /**
- * The class constructor.
- *
- * @param \com\mohiva\common\parser\TokenStream $stream The token stream to use for this lexer.
- */
- public function __construct(TokenStream $stream) {
-
- $this->stream = $stream;
- }
-
- /**
- * Return the token stream instance.
- *
- * @return \com\mohiva\common\parser\TokenStream The token stream to use for this lexer.
- */
- public function getStream() {
-
- return $this->stream;
- }
-
- /**
* Tokenize the given input string and return the resulting token stream.
*
* @param string $input The string input to scan.
* @return \com\mohiva\common\parser\TokenStream The resulting token stream.
*/
public function scan($input) {
- $this->stream->flush();
- $this->stream->setSource($input);
- $this->tokenize($input);
- $this->stream->rewind();
+ $stream = $this->tokenize($input);
+ $stream->rewind();
- return $this->stream;
+ return $stream;
}
/**
* Transform the input string into a token stream.
*
* @param string $input The string input to tokenize.
+ * @return \com\mohiva\common\parser\TokenStream The resulting token stream.
*/
private function tokenize($input) {
+ $stream = new TokenStream();
+ $stream->setSource($input);
+
$pattern = '/' . implode('|', $this->lexemes) . '/';
$flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE;
$matches = preg_split($pattern, $input, -1, $flags);
@@ -141,11 +116,13 @@ private function tokenize($input) {
$code = self::T_NONE;
}
- $this->stream->push(new Token(
+ $stream->push(new Token(
$code,
$match[0],
$match[1]
));
}
+
+ return $stream;
}
}
View
16 tests/com/mohiva/test/pyramid/ParserTest.php
@@ -41,7 +41,7 @@ class ParserTest extends \PHPUnit_Framework_TestCase {
*/
public function testBinaryOperation() {
- $lexer = new Lexer(new TokenStream);
+ $lexer = new Lexer();
$stream = $lexer->scan('1.1 + 1.5');
$parser = new Parser(new Grammar());
@@ -55,7 +55,7 @@ public function testBinaryOperation() {
*/
public function testUnaryOperation() {
- $lexer = new Lexer(new TokenStream);
+ $lexer = new Lexer();
$stream = $lexer->scan('-1.2 + 1 * 5');
$parser = new Parser(new Grammar());
@@ -69,7 +69,7 @@ public function testUnaryOperation() {
*/
public function testUnaryBetweenBinaryOperation() {
- $lexer = new Lexer(new TokenStream);
+ $lexer = new Lexer();
$stream = $lexer->scan('4 / -1 * 5');
$parser = new Parser(new Grammar());
@@ -83,7 +83,7 @@ public function testUnaryBetweenBinaryOperation() {
*/
public function testUnaryPowerOperation() {
- $lexer = new Lexer(new TokenStream);
+ $lexer = new Lexer();
$stream = $lexer->scan('5^-1 + -1^5');
$parser = new Parser(new Grammar());
@@ -97,7 +97,7 @@ public function testUnaryPowerOperation() {
*/
public function testParenthesesOperation() {
- $lexer = new Lexer(new TokenStream);
+ $lexer = new Lexer();
$stream = $lexer->scan('-(-1.2) + 1 + (( -2 + 5) * 6 * (5 - 5))');
$parser = new Parser(new Grammar());
@@ -111,7 +111,7 @@ public function testParenthesesOperation() {
*/
public function testComplexOperation() {
- $lexer = new Lexer(new TokenStream);
+ $lexer = new Lexer();
$stream = $lexer->scan('-(-1.1 + 1) * 5^6^3 / (4 / +5.568 * (2 % 8))');
$parser = new Parser(new Grammar());
@@ -127,7 +127,7 @@ public function testComplexOperation() {
*/
public function testParsePrimaryThrowsException() {
- $lexer = new Lexer(new TokenStream);
+ $lexer = new Lexer();
$stream = $lexer->scan('1 + 1 +');
$parser = new Parser(new Grammar());
@@ -140,7 +140,7 @@ public function testParsePrimaryThrowsException() {
*/
public function testParseOperandThrowsException() {
- $lexer = new Lexer(new TokenStream);
+ $lexer = new Lexer();
$stream = $lexer->scan('1 + test');
$parser = new Parser(new Grammar());
View
12 tests/com/mohiva/test/pyramid/example/LexerTest.php
@@ -38,10 +38,10 @@ class LexerTest extends \PHPUnit_Framework_TestCase {
*/
public function testCalculationSyntax() {
- $lexer = new Lexer(new TokenStream());
- $lexer->scan(' 12+4-1/3 * +0.4 + (-12 + 5^3) ');
+ $lexer = new Lexer();
+ $stream = $lexer->scan(' 12+4-1/3 * +0.4 + (-12 + 5^3) ');
- $actual = $this->buildActualTokens($lexer->getStream());
+ $actual = $this->buildActualTokens($stream);
$expected = array(
array(Lexer::T_NUMBER => '12'),
array(Lexer::T_PLUS => '+'),
@@ -72,10 +72,10 @@ public function testCalculationSyntax() {
*/
public function testNoneToken() {
- $lexer = new Lexer(new TokenStream());
- $lexer->scan(' # ');
+ $lexer = new Lexer();
+ $stream = $lexer->scan(' # ');
- $actual = $this->buildActualTokens($lexer->getStream());
+ $actual = $this->buildActualTokens($stream);
$expected = array(
array(Lexer::T_NONE => '#'),
);

0 comments on commit 693fee3

Please sign in to comment.