Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 61 additions & 14 deletions lib/parser/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,60 @@ class Base < Racc::Parser
# @return [Parser::AST::Node]
#
def self.parse(string, file='(string)', line=1)
parser = default_parser
source_buffer = setup_source_buffer(file, line, string, parser.default_encoding)
parser.parse(source_buffer)
end

##
# Parses a string of Ruby code and returns the AST and comments. If the
# source cannot be parsed, {SyntaxError} is raised and a diagnostic is
# printed to `stderr`.
#
# @example
# Parser::Base.parse_with_comments('puts "hello"')
#
# @param [String] string The block of code to parse.
# @param [String] file The name of the file the code originated from.
# @param [Numeric] line The initial line number.
# @return [Array]
#
def self.parse_with_comments(string, file='(string)', line=1)
parser = default_parser
source_buffer = setup_source_buffer(file, line, string, parser.default_encoding)
parser.parse_with_comments(source_buffer)
end

##
# Parses Ruby source code by reading it from a file. If the source
# cannot be parsed, {SyntaxError} is raised and a diagnostic is
# printed to `stderr`.
#
# @param [String] filename Path to the file to parse.
# @return [Parser::AST::Node]
# @see #parse
#
def self.parse_file(filename)
parse(File.read(filename), filename)
end

##
# Parses Ruby source code by reading it from a file and returns the AST and
# comments. If the source cannot be parsed, {SyntaxError} is raised and a
# diagnostic is printed to `stderr`.
#
# @param [String] filename Path to the file to parse.
# @return [Array]
# @see #parse
#
def self.parse_file_with_comments(filename)
parse_with_comments(File.read(filename), filename)
end

##
# @return [Parser::Base] parser with the default options set.
#
def self.default_parser
parser = new

parser.diagnostics.all_errors_are_fatal = true
Expand All @@ -35,7 +89,11 @@ def self.parse(string, file='(string)', line=1)
$stderr.puts(diagnostic.render)
end

string = string.dup.force_encoding(parser.default_encoding)
parser
end

def self.setup_source_buffer(file, line, string, encoding)
string = string.dup.force_encoding(encoding)

source_buffer = Source::Buffer.new(file, line)

Expand All @@ -45,20 +103,9 @@ def self.parse(string, file='(string)', line=1)
source_buffer.source = string
end

parser.parse(source_buffer)
end

##
# Parses Ruby source code by reading it from a file. If the source
# cannot be parsed, {SyntaxError} is raised and a diagnostic is
# printed to `stderr`.
#
# @param [String] filename Path to the file to parse.
# @see #parse
#
def self.parse_file(filename)
parse(File.read(filename), filename)
source_buffer
end
private_class_method :setup_source_buffer

attr_reader :diagnostics
attr_reader :builder
Expand Down