/** * Test for expected tokens in a sample file */ public function testToken() { $str = file_get_contents($this->sample1); $this->say($this->sample1); self::AssertTrue(is_string($str), "should be string"); $tokenizer = new \gi\parse\Scanner($str); $tok = $tokenizer->nextToken(); // this $val = $tokenizer->token(); self::AssertEquals($tok, \gi\parse\Scanner::WORD); /* self::AssertEquals( $val, 'this', "got $val" ); $tok = $tokenizer->nextToken(); // EOL self::AssertEquals( $tok, \gi\parse\Scanner::EOL ); $tok = $tokenizer->nextToken(); // is $tok = $tokenizer->nextToken(); // SPACE self::AssertEquals( $tok, \gi\parse\Scanner::WHITESPACE ); self::AssertEquals( $tokenizer->getTypeString(), "WHITESPACE" ); $tok = $tokenizer->nextToken(); // a $tok = $tokenizer->nextToken(); // SPACE (four) $val = $tokenizer->token(); self::AssertEquals( $tok, \gi\parse\Scanner::WHITESPACE ); self::AssertEquals( $val, " " ); $tok = $tokenizer->nextToken(); // sample $tok = $tokenizer->nextToken(); // EOL $tok = $tokenizer->nextToken(); // < $val = $tokenizer->token(); self::AssertEquals( $tok, \gi\parse\Scanner::CHAR ); self::AssertEquals( $val, "<" ); while ( ($tok = $tokenizer->nextToken()) != \gi\parse\Scanner::EOF ) { // should not go into infinite loop! $count++; if ( $count > 1000 ) { self::AssertTrue( false, 'EOF not encountered' ); } } self::AssertTrue( true ); */ }
<?php require_once "gi/parse/Scanner.php"; require_once "gi/parse/Context.php"; require_once "gi/parse/StringReader.php"; $context = new \gi\parse\Context(); $user_in = "\$input equals '4' or \$input equals 'four'"; $reader = new \gi\parse\StringReader($user_in); $scanner = new \gi\parse\Scanner($reader, $context); while ($scanner->nextToken() != \gi\parse\Scanner::EOF) { print $scanner->token(); print "\t{$scanner->char_no()}"; print "\t{$scanner->getTypeString()}\n"; }