Class: I18n::Tasks::Scanners::LocalRubyParser
- Inherits:
-
Object
- Object
- I18n::Tasks::Scanners::LocalRubyParser
- Defined in:
- lib/i18n/tasks/scanners/local_ruby_parser.rb
Instance Method Summary collapse
-
#initialize ⇒ LocalRubyParser
constructor
A new instance of LocalRubyParser.
-
#normalize_comment_location(comment, location) ⇒ Parser::Source::Comment
Normalize location for comment.
- #normalize_location(node, location) ⇒ Parser::AST::Node
-
#parse(source, location: nil) ⇒ Object
Parse string and normalize location.
-
#updated_location(global_location, local_location) ⇒ Parser::Source::Map
Calculate location relative to a global location.
Constructor Details
#initialize ⇒ LocalRubyParser
Returns a new instance of LocalRubyParser.
7 8 9 |
# File 'lib/i18n/tasks/scanners/local_ruby_parser.rb', line 7 def initialize @parser = ::Parser::CurrentRuby.new end |
Instance Method Details
#normalize_comment_location(comment, location) ⇒ Parser::Source::Comment
Normalize location for comment
65 66 67 68 69 70 71 72 |
# File 'lib/i18n/tasks/scanners/local_ruby_parser.rb', line 65 def normalize_comment_location(comment, location) range = ::Parser::Source::Range.new( location.expression.source_buffer, location.expression.to_range.begin + comment.location.expression.to_range.begin, location.expression.to_range.begin + comment.location.expression.to_range.end ) ::Parser::Source::Comment.new(range) end |
#normalize_location(node, location) ⇒ Parser::AST::Node
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/i18n/tasks/scanners/local_ruby_parser.rb', line 28 def normalize_location(node, location) return node.map { |child| normalize_location(child, location) } if node.is_a?(Array) return node unless node.is_a?(::Parser::AST::Node) node.updated( nil, node.children.map { |child| normalize_location(child, location) }, { location: updated_location(location, node.location) } ) end |
#parse(source, location: nil) ⇒ Object
Parse string and normalize location
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/i18n/tasks/scanners/local_ruby_parser.rb', line 12 def parse(source, location: nil) buffer = ::Parser::Source::Buffer.new('(string)') buffer.source = source @parser.reset ast, comments = @parser.parse_with_comments(buffer) ast = normalize_location(ast, location) comments = comments.map { |comment| normalize_comment_location(comment, location) } [ast, comments] end |
#updated_location(global_location, local_location) ⇒ Parser::Source::Map
Calculate location relative to a global location
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/i18n/tasks/scanners/local_ruby_parser.rb', line 45 def updated_location(global_location, local_location) range = ::Parser::Source::Range.new( global_location.expression.source_buffer, global_location.expression.to_range.begin + local_location.expression.to_range.begin, global_location.expression.to_range.begin + local_location.expression.to_range.end ) ::Parser::Source::Map::Definition.new( range.begin, range.begin, range.begin, range.end ) end |