Skip to content

Commit f87fb61

Browse files
committed
Added interfaces
1 parent 4f9b1b9 commit f87fb61

File tree

7 files changed

+134
-8
lines changed

7 files changed

+134
-8
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "didix16/php-grammar",
33
"type": "library",
4-
"description": "A simple library to make Lexer and Parsers to build a language",
4+
"description": "A simple library for make Lexer and Parsers to build a language",
55
"keywords": ["lexer", "parser", "token", "grammar", "tokenizer", "simple", "build"],
66
"homepage": "https://github.com/didix16/php-grammar",
77
"license": "MIT",

src/Lexer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Class Lexer
88
* @package didix16\Grammar
99
*/
10-
abstract class Lexer {
10+
abstract class Lexer implements LexerInterface {
1111

1212
const NULL_TOKEN_TYPE = Token::NULL_TOKEN_VALUE;
1313
const NULL_TOKEN_VALUE = Token::NULL_TOKEN_TYPE;

src/LexerInterface.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace didix16\Grammar;
4+
5+
/**
6+
* Interface LexerInterface
7+
* @package didix16\Grammar
8+
*/
9+
interface LexerInterface {
10+
11+
public function __construct(string $input);
12+
13+
/**
14+
* From input string, consume a character by default.
15+
* This method may be override to consume a word or some regexp
16+
* @return $this
17+
*/
18+
public function consume(): self;
19+
20+
/**
21+
* Check if current character is an ASCII letter
22+
* @return bool
23+
*/
24+
public function isLetter();
25+
26+
/**
27+
* Consume contiguous white spaces
28+
*/
29+
public function WS();
30+
31+
/**
32+
* Returns the last character readed by this lexer
33+
* @return string
34+
*/
35+
public function lastCharacter(): string;
36+
37+
/**
38+
* Return the last token founded
39+
* @return Token
40+
*/
41+
public function lastToken(): Token;
42+
43+
/**
44+
* Returns the next token identified by this lexer
45+
* @return Token
46+
*/
47+
public function nextToken(): Token;
48+
49+
/**
50+
* Given a token type returns its token name
51+
* @param $tokenType
52+
* @return string
53+
*/
54+
public function getTokenName($tokenType): string;
55+
}

src/Parser.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@
88
* Class Parser
99
* @package didix16\Grammar
1010
*/
11-
abstract class Parser {
11+
abstract class Parser implements ParserInterface {
1212

1313
/**
1414
* The lexer from we get the tokens
15-
* @var Lexer
15+
* @var LexerInterface
1616
*/
1717
protected $input;
1818

1919
/**
2020
* Current lookahead Token
21-
* @var Token
21+
* @var TokenInterface
2222
*/
2323
protected $lookahead;
2424

@@ -28,7 +28,7 @@ abstract class Parser {
2828
*/
2929
protected $tokenList = [];
3030

31-
public function __construct(Lexer $input)
31+
public function __construct(LexerInterface $input)
3232
{
3333
$this->input = $input;
3434
$this->lookahead = $this->input->nextToken();
@@ -73,7 +73,7 @@ public function getTokens(){
7373
/**
7474
* The main entry point of the grammar expression
7575
* Returns the list of tokens founded
76-
* @return array
76+
* @return TokenInterface[]
7777
*/
7878
public abstract function parse() : array ;
7979

src/ParserInterface.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace didix16\Grammar;
4+
5+
/**
6+
* Interface ParserInterface
7+
* @package didix16\Grammar
8+
*/
9+
interface ParserInterface {
10+
11+
public function __construct(LexerInterface $input);
12+
13+
/**
14+
* If token type passed matches the expected token type then consume next token
15+
* else throws an exception
16+
* @param $tokenType
17+
* @throws Exception
18+
*/
19+
public function match($tokenType);
20+
21+
/**
22+
* Consume current token and store it to token list
23+
*/
24+
public function consume();
25+
26+
/**
27+
* Get all the evaluated tokens
28+
* @return array
29+
*/
30+
public function getTokens();
31+
32+
/**
33+
* The main entry point of the grammar expression
34+
* Returns the list of tokens founded
35+
* @return array
36+
*/
37+
public function parse() : array ;
38+
}

src/Token.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Class Token
99
* @package didix16\Grammar
1010
*/
11-
class Token
11+
class Token implements TokenInterface
1212
{
1313
const NULL_TOKEN_TYPE = 0;
1414
const NULL_TOKEN_VALUE = NULL;

src/TokenInterface.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace didix16\Grammar;
4+
5+
/**
6+
* Interface TokenInterface
7+
* @package didix16\Grammar
8+
*/
9+
interface TokenInterface {
10+
11+
/**
12+
* Token constructor.
13+
* @param $type
14+
* @param $value
15+
*/
16+
public function __construct($type, $value);
17+
18+
/**
19+
* Returns a representation of this token
20+
*/
21+
public function __toString(): string;
22+
23+
/**
24+
* Returns the type of the token
25+
*/
26+
public function getType();
27+
28+
/**
29+
* Returns the value this token is holding
30+
*/
31+
public function getValue();
32+
33+
}

0 commit comments

Comments
 (0)