Skip to content

Commit

Permalink
Merge pull request #29 from jinroq/refactoring_parser
Browse files Browse the repository at this point in the history
Refactor lrama/parser.rb
  • Loading branch information
yui-knk authored May 19, 2023
2 parents 48c9687 + 7573fba commit 7c7d772
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 52 deletions.
53 changes: 1 addition & 52 deletions lib/lrama/parser.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "lrama/report"
require "lrama/parser/token_scanner"

module Lrama
# Parser for parse.y, generates a grammar
Expand All @@ -7,58 +8,6 @@ class Parser

T = Lrama::Lexer::Token

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

def initialize(text)
@text = text
end
Expand Down
55 changes: 55 additions & 0 deletions lib/lrama/parser/token_scanner.rb
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

0 comments on commit 7c7d772

Please sign in to comment.