Class: Astel::SourceFile
- Inherits:
-
Object
- Object
- Astel::SourceFile
- Defined in:
- lib/astel/source_file.rb
Instance Attribute Summary collapse
-
#ast ⇒ Object
readonly
Returns the value of attribute ast.
-
#comments ⇒ Object
readonly
Returns the value of attribute comments.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#parse_result ⇒ Object
readonly
Returns the value of attribute parse_result.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Class Method Summary collapse
Instance Method Summary collapse
- #bof ⇒ Object
- #first_line_location ⇒ Object
-
#initialize(code, path:, version: nil) ⇒ SourceFile
constructor
A new instance of SourceFile.
- #lines ⇒ Object
- #magic_comment?(name) ⇒ Boolean
- #valid? ⇒ Boolean
Constructor Details
#initialize(code, path:, version: nil) ⇒ SourceFile
Returns a new instance of SourceFile.
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/astel/source_file.rb', line 17 def initialize(code, path:, version: nil) @path = path.to_s.freeze raw_source = code.dup.freeze = { filepath: @path } [:version] = normalize_version(version) if version @parse_result = Prism.parse(raw_source, **) @source = @parse_result.source.source.freeze @ast = @parse_result.value @comments = @parse_result.comments.freeze @errors = @parse_result.errors.freeze @lines = nil end |
Instance Attribute Details
#ast ⇒ Object (readonly)
Returns the value of attribute ast.
7 8 9 |
# File 'lib/astel/source_file.rb', line 7 def ast @ast end |
#comments ⇒ Object (readonly)
Returns the value of attribute comments.
7 8 9 |
# File 'lib/astel/source_file.rb', line 7 def comments @comments end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
7 8 9 |
# File 'lib/astel/source_file.rb', line 7 def errors @errors end |
#parse_result ⇒ Object (readonly)
Returns the value of attribute parse_result.
7 8 9 |
# File 'lib/astel/source_file.rb', line 7 def parse_result @parse_result end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
7 8 9 |
# File 'lib/astel/source_file.rb', line 7 def path @path end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
7 8 9 |
# File 'lib/astel/source_file.rb', line 7 def source @source end |
Class Method Details
.from_string(code, path: '(string)', version: nil) ⇒ Object
13 14 15 |
# File 'lib/astel/source_file.rb', line 13 def self.from_string(code, path: '(string)', version: nil) new(code, path: path, version: version) end |
.parse(path:, version: nil) ⇒ Object
9 10 11 |
# File 'lib/astel/source_file.rb', line 9 def self.parse(path:, version: nil) from_string(File.binread(path), path: path, version: version) end |
Instance Method Details
#bof ⇒ Object
38 39 40 |
# File 'lib/astel/source_file.rb', line 38 def bof Location.point(offset: 0, line: 1, column: 0) end |
#first_line_location ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/astel/source_file.rb', line 42 def first_line_location line = lines.first || '' Location.new( start_offset: 0, end_offset: line.bytesize, start_line: 1, start_column: 0, end_line: 1, end_column: line.delete_suffix("\n").delete_suffix("\r").length ) end |
#lines ⇒ Object
30 31 32 |
# File 'lib/astel/source_file.rb', line 30 def lines @lines ||= source.lines(chomp: false).map!(&:freeze).freeze end |
#magic_comment?(name) ⇒ Boolean
54 55 56 57 58 59 60 |
# File 'lib/astel/source_file.rb', line 54 def magic_comment?(name) pattern = /\A#\s*#{Regexp.escape(name.to_s)}\s*:\s*true\b/ lines.first(2).any? do |line| candidate = line.start_with?("\uFEFF") ? line.delete_prefix("\uFEFF") : line candidate.match?(pattern) end end |
#valid? ⇒ Boolean
34 35 36 |
# File 'lib/astel/source_file.rb', line 34 def valid? errors.empty? end |