Module: EagerEye::SourceParser
- Defined in:
- lib/eager_eye/source_parser.rb
Overview
Single entry point for turning Ruby source into an AST. Differs from
Parser::CurrentRuby.parse in two ways that matter for a CLI tool:
* No stderr side effects. The default parser prints every lexer/parser
diagnostic straight to $stderr — so one unparseable file (e.g. a model
holding a binary string literal whose escapes are invalid UTF-8) dumps
a raw three-line diagnostic once per analysis pass. Diagnostics stay
quiet here; failures surface only as the raised exception.
* The buffer is named after the real file, so callers can report
"app/models/coupon.rb", not "(string)".
Raises like the original — Parser::SyntaxError, EncodingError, or
Parser::UnknownEncodingInMagicComment (an ArgumentError, NOT a
SyntaxError, raised for e.g. # encoding: utf8 typos); callers decide
whether and how to report.
Constant Summary collapse
- PARSER_CLASS =
begin Parser.const_get(:"Ruby#{RUBY_VERSION.split(".").first(2).join}", false) rescue NameError Parser::CurrentRuby end
Class Method Summary collapse
Class Method Details
.parse(source, file_path = "(string)") ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/eager_eye/source_parser.rb', line 40 def self.parse(source, file_path = "(string)") parser = PARSER_CLASS.new parser.diagnostics.all_errors_are_fatal = true parser.diagnostics.ignore_warnings = true buffer = Parser::Source::Buffer.new(file_path, 1) buffer.source = source.dup.force_encoding(parser.default_encoding) parser.parse(buffer) end |