Class: Bullematic::AST::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/bullematic/ast/parser.rb,
sig/generated/bullematic/ast/parser.rbs

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_code, filepath: nil) ⇒ Parser

Returns a new instance of Parser.

RBS:

  • source_code: String

  • filepath: String?

  • return: void

Parameters:

  • source_code (String)
  • filepath: (String, nil) (defaults to: nil)


28
29
30
31
# File 'lib/bullematic/ast/parser.rb', line 28

def initialize(source_code, filepath: nil)
  @source_code = source_code
  @filepath = filepath
end

Instance Attribute Details

#filepathObject (readonly)

RBS:

  • attr_reader source_code: String
    attr_reader filepath: String?

Returns:

  • (Object)


23
24
25
# File 'lib/bullematic/ast/parser.rb', line 23

def filepath
  @filepath
end

#source_codeObject (readonly)

RBS:

  • attr_reader source_code: String
    attr_reader filepath: String?

Returns:

  • (Object)


23
24
25
# File 'lib/bullematic/ast/parser.rb', line 23

def source_code
  @source_code
end

Class Method Details

.parse_file(filepath) ⇒ Object

RBS:

  • filepath: String

  • return: untyped

Parameters:

  • filepath (String)

Returns:

  • (Object)


15
16
17
# File 'lib/bullematic/ast/parser.rb', line 15

def parse_file(filepath)
  new(File.binread(filepath), filepath: filepath).parse
end

Instance Method Details

#parseObject

: () -> untyped

Returns:

  • (Object)


34
35
36
37
38
39
# File 'lib/bullematic/ast/parser.rb', line 34

def parse
  result = Prism.parse(@source_code, filepath: @filepath)
  raise ParseError, result.errors.map(&:message).join("\n") if result.failure?

  result
end