Class: Yoda::Parsing::Parser

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

Instance Method Summary collapse

Instance Method Details

#parse(string) ⇒ AST::Vnode

Parameters:

  • string (String)

Returns:



8
9
10
# File 'lib/yoda/parsing/parser.rb', line 8

def parse(string)
  parse_with_comments(string).first
end

#parse_with_comments(string) ⇒ (AST::Vnode, Array<::Parser::Source::Comment>)

Parameters:

  • string (String)

Returns:

  • ((AST::Vnode, Array<::Parser::Source::Comment>))


14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/yoda/parsing/parser.rb', line 14

def parse_with_comments(string)
  source_buffer = create_source_buffer(source: string)
  node, comments = parser.parse_with_comments(source_buffer)
  comments_by_node = begin
    if ::Parser::Source::Comment.respond_to?(:associate_by_identity)
      ::Parser::Source::Comment.associate_by_identity(node, comments)
    else
      ::Parser::Source::Comment.associate(node, comments)
    end
  end
  [AST.wrap(node, comments_by_node: comments_by_node), comments]
end

#parse_with_comments_if_valid(string) ⇒ (::Parser::AST::Node, Array<::Parser::Source::Comment>)?

Parameters:

  • string (String)

Returns:

  • ((::Parser::AST::Node, Array<::Parser::Source::Comment>), nil)


29
30
31
32
33
# File 'lib/yoda/parsing/parser.rb', line 29

def parse_with_comments_if_valid(string)
  parse_with_comments(string)
rescue ::Parser::SyntaxError
  nil
end

#tokenize(source, recover: false) ⇒ Array

Returns See Parser::Base.tokenize.

Parameters:

  • source (String)
  • recover (Boolean) (defaults to: false) —

    .

Returns:

  • (Array) —

    See Parser::Base.tokenize



38
39
40
# File 'lib/yoda/parsing/parser.rb', line 38

def tokenize(source, recover: false)
  parser.tokenize(create_source_buffer(source: source), recover)
end