Class: I18n::Tasks::Scanners::LocalRubyParser

Inherits:
Object
  • Object
show all
Defined in:
lib/i18n/tasks/scanners/local_ruby_parser.rb

Constant Summary collapse

BLOCK_EXPR =
/\s*((\s+|\))do|\{)(\s*\|[^|]*\|)?\s*\Z/.freeze

Instance Method Summary collapse

Constructor Details

#initialize(ignore_blocks: false) ⇒ LocalRubyParser

Returns a new instance of LocalRubyParser.



11
12
13
14
# File 'lib/i18n/tasks/scanners/local_ruby_parser.rb', line 11

def initialize(ignore_blocks: false)
  @parser = ::Parser::CurrentRuby.new
  @ignore_blocks = ignore_blocks
end

Instance Method Details

#normalize_comment_location(comment, location) ⇒ Parser::Source::Comment

Normalize location for comment

Parameters:

  • comment (Parser::Source::Comment)

    A comment with local location

  • location (Parser::Source::Map)

    Global location for the parsed string

Returns:

  • (Parser::Source::Comment)


76
77
78
79
80
81
82
83
# File 'lib/i18n/tasks/scanners/local_ruby_parser.rb', line 76

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

Parameters:

  • node (Parser::AST::Node)

    Node in parsed code

  • location (Parser::Source::Map)

    Global location for the parsed string

Returns:

  • (Parser::AST::Node)


37
38
39
40
41
42
43
44
45
46
47
# File 'lib/i18n/tasks/scanners/local_ruby_parser.rb', line 37

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



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/i18n/tasks/scanners/local_ruby_parser.rb', line 17

def parse(source, location: nil)
  buffer = ::Parser::Source::Buffer.new('(string)')
  buffer.source = if @ignore_blocks
                    source.sub(BLOCK_EXPR, '')
                  else
                    source
                  end

  @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

Parameters:

  • global_location (Parser::Source::Map)

    Global location where the code was parsed

  • local_location (Parser::Source::Map)

    Local location in the parsed string

Returns:

  • (Parser::Source::Map)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/i18n/tasks/scanners/local_ruby_parser.rb', line 54

def updated_location(global_location, local_location)
  return global_location if local_location.expression.nil?

  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