lib/vernacular/ast_parser.rb in vernacular-0.1.1 vs lib/vernacular/ast_parser.rb in vernacular-0.1.2
- old
+ new
@@ -10,11 +10,11 @@
source = extend_parser(source, parser_extension)
end
end
write_parser(source)
- load 'vernacular/parser.rb'
+ load PARSER_PATH
Parser::Vernacular.new(builder)
end
class << self
def parse(string)
@@ -46,14 +46,16 @@
end
end.new
end
def compile_parser(filepath)
- output = File.expand_path('../parser.rb', __FILE__)
exec_path = Gem.activate_bin_path('racc', 'racc', [])
- `#{exec_path} --superclass=Parser::Base -o #{output} #{filepath}`
- File.write(output, File.read(output).gsub('Ruby24', 'Vernacular'))
+ `#{exec_path} --superclass=Parser::Base -o #{PARSER_PATH} #{filepath}`
+ File.write(
+ PARSER_PATH,
+ File.read(PARSER_PATH).gsub("Ruby#{parser_version}", 'Vernacular')
+ )
end
# rubocop:disable Metrics/MethodLength
def extend_parser(source, parser_extension)
needle = "#{parser_extension.symbol}:"
@@ -71,10 +73,15 @@
end
# rubocop:enable Metrics/MethodLength
def parser_source
filepath, = Parser.method(:check_for_encoding_support).source_location
- File.read(File.expand_path('../../lib/parser/ruby24.y', filepath))
+ grammar_filepath = "../../lib/parser/ruby#{parser_version}.y"
+ File.read(File.expand_path(grammar_filepath, filepath))
+ end
+
+ def parser_version
+ @parser_version ||= RUBY_VERSION.gsub(/\A(\d)\.(\d).+/, '\1\2')
end
def write_parser(source)
file = Tempfile.new(['parser-', '.y'])
file.write(source)