Check for duplicate token names; skip comment lines
This commit is contained in:
parent
7f27b3fd6f
commit
04393dcc51
@ -1,18 +1,30 @@
|
|||||||
module Imbecile
|
module Imbecile
|
||||||
class Grammar
|
class Grammar
|
||||||
|
|
||||||
|
def initialize
|
||||||
|
@tokens = {}
|
||||||
|
@rules = {}
|
||||||
|
end
|
||||||
|
|
||||||
# @return [Boolean]
|
# @return [Boolean]
|
||||||
# Whether loading was successful.
|
# Whether loading was successful.
|
||||||
def load(input_file)
|
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*#/
|
||||||
|
# Skip comment line.
|
||||||
|
elsif 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]*$/
|
||||||
$stderr.puts "Invalid token name #{name} on line #{line_number}"
|
$stderr.puts "Invalid token name #{name} on line #{line_number}"
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
if @tokens[name]
|
||||||
|
$stderr.puts "Duplicate token name #{name} on line #{line_number}"
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
@tokens[name] = expr
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user