Rename GrammarParser -> Grammar

This commit is contained in:
Josh Holtrop 2021-05-01 09:33:35 -04:00
parent 23b7782a5d
commit 37ad87d602
2 changed files with 11 additions and 5 deletions

View File

@ -1,5 +1,5 @@
require_relative "imbecile/cli" require_relative "imbecile/cli"
require_relative "imbecile/grammar_parser" require_relative "imbecile/grammar"
require_relative "imbecile/version" require_relative "imbecile/version"
module Imbecile module Imbecile
@ -7,7 +7,10 @@ module Imbecile
class << self class << self
def run(input_file) def run(input_file)
gp = GrammarParser.new(input_file) grammar = Grammar.new
unless grammar.load(input_file)
return 2
end
end end
end end

View File

@ -1,14 +1,17 @@
module Imbecile module Imbecile
class GrammarParser class Grammar
def initialize(input_file) # @return [Boolean]
# Whether loading was successful.
def load(input_file)
File.read(input_file).each_line.each_with_index do |line, line_index| File.read(input_file).each_line.each_with_index do |line, line_index|
line = line.chomp line = line.chomp
line_number = line_index + 1 line_number = line_index + 1
if line =~ /^\s*token\s+(\S+)\s+(.*)$/ if line =~ /^\s*token\s+(\S+)\s+(.*)$/
name, expr = $1, $2 name, expr = $1, $2
unless name =~ /^[a-zA-Z_][a-zA-Z_0-9]*$/ unless name =~ /^[a-zA-Z_][a-zA-Z_0-9]*$/
raise "Invalid token name #{name} on line #{line_number}" $stderr.puts "Invalid token name #{name} on line #{line_number}"
return false
end end
end end
end end