Class: Rucoa::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/rucoa/parser.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text:, uri:) ⇒ Parser

Returns a new instance of Parser.

Parameters:

  • text (String)
  • uri (String)


36
37
38
39
40
41
42
# File 'lib/rucoa/parser.rb', line 36

def initialize(
  text:,
  uri:
)
  @text = text
  @uri = uri
end

Class Method Details

.call(text:, uri:) ⇒ Rucoa::ParseResult

Examples:

returns non-failed parse result for valid Ruby source

result = Rucoa::Parser.call(
  text: 'foo',
  uri: 'file:///path/to/foo.rb'
)
expect(result).not_to be_failed

returns failed parse result for invalid Ruby source

result = Rucoa::Parser.call(
  text: 'foo(',
  uri: 'file:///path/to/foo.rb'
)
expect(result).to be_failed

Parameters:

  • text (String)
  • uri (String)

Returns:



23
24
25
26
27
28
29
30
31
# File 'lib/rucoa/parser.rb', line 23

def call(
  text:,
  uri:
)
  new(
    text: text,
    uri: uri
  ).call
end

Instance Method Details

#callRucoa::ParseResult

Returns:



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rucoa/parser.rb', line 45

def call
  root_node, comments = parser.parse_with_comments(
    ::Parser::Source::Buffer.new(
      @uri,
      source: @text
    )
  )
  ParseResult.new(
    associations: ::Parser::Source::Comment.associate_locations(
      root_node,
      comments
    ),
    root_node: root_node
  )
rescue ::Parser::SyntaxError
  ParseResult.new(failed: true)
end