class Parser::Base
@return [Parser::StaticEnvironment]
@!attribute [r] static_env
@return [Parser::Diagnostic::Engine]
@!attribute [r] diagnostics
@api public
Base class for version-specific parsers.
#
def self.default_parser
-
(Parser::Base)
- parser with the default options set.
def self.default_parser parser = new parser.diagnostics.all_errors_are_fatal = true parser.diagnostics.ignore_warnings = true parser.diagnostics.consumer = lambda do |diagnostic| $stderr.puts(diagnostic.render) end parser end
def self.parse(string, file='(string)', line=1)
-
(Parser::AST::Node)
-
Parameters:
-
line
(Numeric
) -- The initial line number. -
file
(String
) -- The name of the file the code originated from. -
string
(String
) -- The block of code to parse.
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
def self.parse_file(filename)
- See: #parse -
Returns:
-
(Parser::AST::Node)
-
Parameters:
-
filename
(String
) -- Path to the file to parse.
def self.parse_file(filename) parse(File.read(filename), filename) end
def self.parse_file_with_comments(filename)
- See: #parse -
Returns:
-
(Array)
-
Parameters:
-
filename
(String
) -- Path to the file to parse.
def self.parse_file_with_comments(filename) parse_with_comments(File.read(filename), filename) end
def self.parse_with_comments(string, file='(string)', line=1)
-
(Array)
-
Parameters:
-
line
(Numeric
) -- The initial line number. -
file
(String
) -- The name of the file the code originated from. -
string
(String
) -- The block of code to parse.
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
def self.setup_source_buffer(file, line, string, encoding)
def self.setup_source_buffer(file, line, string, encoding) string = string.dup.force_encoding(encoding) source_buffer = Source::Buffer.new(file, line) if name == 'Parser::Ruby18' source_buffer.raw_source = string else source_buffer.source = string end source_buffer end
def check_kwarg_name(name_t)
def check_kwarg_name(name_t) case name_t[0] when /^[a-z_]/ # OK when /^[A-Z]/ diagnostic :error, :argument_const, name_t end end
def diagnostic(level, kind, location_t, highlights_ts=[])
def diagnostic(level, kind, location_t, highlights_ts=[]) _, location = location_t highlights = highlights_ts.map do |token| _, range = token range end message = ERRORS[kind] @diagnostics.process( Diagnostic.new(level, message, location, highlights)) if level == :error yyerror end end
def in_def?
-
(TrueClass|FalseClass)
-
Other tags:
- Api: - private
def in_def? @def_level > 0 end
def initialize(builder=Parser::Builders::Default.new)
-
builder
(Parser::Builders::Default
) -- The AST builder to use.
def initialize(builder=Parser::Builders::Default.new) @diagnostics = Diagnostic::Engine.new if RUBY_PLATFORM != 'ruby' # This is a workaround for a Racc bug. In the pure Ruby Racc runtime, # which gets activated on JRuby/RBX, there is a bug in error token # popping, which results in an infinite loop. @diagnostics.all_errors_are_fatal = true end @static_env = StaticEnvironment.new @lexer = Lexer.new(version) @lexer.diagnostics = @diagnostics @lexer.static_env = @static_env @builder = builder @builder.parser = self if self.class::Racc_debug_parser && ENV['RACC_DEBUG'] @yydebug = true end reset end
def next_token
def next_token @lexer.advance end
def on_error(error_token_id, error_value, value_stack)
def on_error(error_token_id, error_value, value_stack) token_name = token_to_str(error_token_id) _, location = error_value message = ERRORS[:unexpected_token] % { :token => token_name } @diagnostics.process( Diagnostic.new(:error, message, location)) end
def parse(source_buffer)
-
(Parser::AST::Node)
-
Parameters:
-
source_buffer
(Parser::Source::Buffer
) -- The source buffer to parse.
def parse(source_buffer) @lexer.source_buffer = source_buffer @source_buffer = source_buffer do_parse ensure # Don't keep references to the source file. @source_buffer = nil @lexer.source_buffer = nil end
def parse_with_comments(source_buffer)
-
(Array)
-
Other tags:
- See: #parse -
def parse_with_comments(source_buffer) @lexer.comments = [] [ parse(source_buffer), @lexer.comments ] ensure @lexer.comments = nil end
def reset
Resets the state of the parser.
#
def reset @source_buffer = nil @def_level = 0 # count of nested def's. @lexer.reset @static_env.reset self end
def tokenize(source_buffer)
-
(Array)
-
Parameters:
-
source_buffer
(Parser::Source::Buffer
) --
def tokenize(source_buffer) @lexer.tokens = [] ast, comments = parse_with_comments(source_buffer) [ ast, comments, @lexer.tokens ] ensure @lexer.tokens = nil end