@@ -17,48 +17,34 @@ public class Parser {
1717 currentToken = lexer. getNextToken ( )
1818 }
1919
20- private func eatInteger( ) {
21- switch currentToken {
22- case . integer:
23- currentToken = lexer. getNextToken ( )
24- default :
25- fatalError ( " Expected integer, got \( currentToken) " )
26- }
27- }
28-
29- private func eatOperation( _ operation: Operation ) {
30- switch currentToken {
31- case . operation( operation) :
32- currentToken = lexer. getNextToken ( )
33- default :
34- fatalError ( " Expected \( operation) , got \( currentToken) " )
35- }
36- }
20+ /**
21+ Compares the current token with the given token, if they match, the next token is read, otherwise an error is thrown
3722
38- private func eatParenthesis( _ parenthesis: Parenthesis ) {
39- switch currentToken {
40- case . parenthesis( parenthesis) :
23+ - Parameter token: Expected token
24+ */
25+ private func eat( _ token: Token ) {
26+ if currentToken == token {
4127 currentToken = lexer. getNextToken ( )
42- default :
43- fatalError ( " Expected \( parenthesis ) , got \( currentToken) " )
28+ } else {
29+ fatalError ( " Syntax error, expected token , got \( currentToken) " )
4430 }
4531 }
4632
4733 /**
4834 Factor for the grammar described in the `expr` method
4935
50- Returns: Int value
36+ Returns: AST node
5137 */
5238 private func factor( ) -> AST {
5339 let token = currentToken
5440 switch token {
5541 case let . integer( value) :
56- eatInteger ( )
42+ eat ( . integer ( value ) )
5743 return . number( value)
5844 case . parenthesis( . left) :
59- eatParenthesis ( . left)
45+ eat ( . parenthesis ( . left) )
6046 let result = expr ( )
61- eatParenthesis ( . right)
47+ eat ( . parenthesis ( . right) )
6248 return result
6349 default :
6450 fatalError ( " Syntax error " )
@@ -68,18 +54,18 @@ public class Parser {
6854 /**
6955 Term for the grammar described in the `expr` method
7056
71- Returns: Int value
57+ Returns: AST node
7258 */
7359 private func term( ) -> AST {
7460 var node = factor ( )
7561
7662 while [ . operation( . mult) , . operation( . div) ] . contains ( currentToken) {
7763 let token = currentToken
7864 if token == . operation( . mult) {
79- eatOperation ( . mult)
65+ eat ( . operation ( . mult) )
8066 node = . binaryOperation( left: node, operation: . mult, right: factor ( ) )
8167 } else if token == . operation( . div) {
82- eatOperation ( . div)
68+ eat ( . operation ( . div) )
8369 node = . binaryOperation( left: node, operation: . div, right: factor ( ) )
8470 }
8571 }
@@ -94,7 +80,7 @@ public class Parser {
9480 term : factor ((MUL | DIV) factor)*
9581 factor : INTEGER | LPAREN factor RPAREN
9682
97- Returns: Int value
83+ Returns: AST node
9884 */
9985 public func expr( ) -> AST {
10086
@@ -103,10 +89,10 @@ public class Parser {
10389 while [ . operation( . plus) , . operation( . minus) ] . contains ( currentToken) {
10490 let token = currentToken
10591 if token == . operation( . plus) {
106- eatOperation ( . plus)
92+ eat ( . operation ( . plus) )
10793 node = . binaryOperation( left: node, operation: . plus, right: term ( ) )
10894 } else if token == . operation( . minus) {
109- eatOperation ( . minus)
95+ eat ( . operation ( . minus) )
11096 node = . binaryOperation( left: node, operation: . minus, right: term ( ) )
11197 }
11298 }
0 commit comments