-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from jinroq/refactoring_parser
Refactor lrama/parser.rb
- Loading branch information
Showing
2 changed files
with
56 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
module Lrama | ||
class Parser | ||
class TokenScanner | ||
def initialize(tokens) | ||
@tokens = tokens | ||
@index = 0 | ||
end | ||
|
||
def current_token | ||
@tokens[@index] | ||
end | ||
|
||
def current_type | ||
current_token && current_token.type | ||
end | ||
|
||
def next | ||
token = current_token | ||
@index += 1 | ||
return token | ||
end | ||
|
||
def consume(*token_types) | ||
if token_types.include?(current_type) | ||
token = current_token | ||
self.next | ||
return token | ||
end | ||
|
||
return nil | ||
end | ||
|
||
def consume!(*token_types) | ||
consume(*token_types) || (raise "#{token_types} is expected but #{current_type}. #{current_token}") | ||
end | ||
|
||
def consume_multi(*token_types) | ||
a = [] | ||
|
||
while token_types.include?(current_type) | ||
a << current_token | ||
self.next | ||
end | ||
|
||
raise "No token is consumed. #{token_types}" if a.empty? | ||
|
||
return a | ||
end | ||
|
||
def eots? | ||
current_token.nil? | ||
end | ||
end | ||
end | ||
end |