Class: Prism::Merge::Comment::Parser
- Inherits:
-
Object
- Object
- Prism::Merge::Comment::Parser
- Defined in:
- lib/prism/merge/comment/parser.rb
Overview
Ruby-specific comment parser.
Produces Prism::Merge::Comment::Line and Prism::Merge::Comment::Block
nodes instead of the generic Ast::Merge::Comment::* classes, enabling
Ruby-specific features like magic comment detection.
Instance Attribute Summary collapse
-
#lines ⇒ Array<String>
readonly
The source lines.
Class Method Summary collapse
-
.parse(lines) ⇒ Array<Ast::Merge::AstNode>
Class method for convenient one-shot parsing.
Instance Method Summary collapse
-
#initialize(lines) ⇒ Parser
constructor
Initialize a new Ruby comment Parser.
-
#parse ⇒ Array<Ast::Merge::AstNode>
Parse the lines into Ruby-specific comment AST.
Constructor Details
#initialize(lines) ⇒ Parser
Initialize a new Ruby comment Parser.
24 25 26 |
# File 'lib/prism/merge/comment/parser.rb', line 24 def initialize(lines) @lines = lines || [] end |
Instance Attribute Details
#lines ⇒ Array<String> (readonly)
Returns The source lines.
19 20 21 |
# File 'lib/prism/merge/comment/parser.rb', line 19 def lines @lines end |
Class Method Details
.parse(lines) ⇒ Array<Ast::Merge::AstNode>
Class method for convenient one-shot parsing.
85 86 87 |
# File 'lib/prism/merge/comment/parser.rb', line 85 def parse(lines) new(lines).parse end |
Instance Method Details
#parse ⇒ Array<Ast::Merge::AstNode>
Parse the lines into Ruby-specific comment AST.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/prism/merge/comment/parser.rb', line 31 def parse return [] if lines.empty? nodes = [] current_block = [] header_magic_comment_types = Prism::Merge::MagicCommentSupport.header_magic_comment_types_for_lines(lines) lines.each_with_index do |line, idx| line_number = idx + 1 raw = line.to_s.chomp # Classification uses trailing-space-insensitive text, but parsed nodes # keep the raw line so merge output can preserve owned trailing spaces. stripped = raw.rstrip if stripped.empty? # Blank line - flush current block and add Empty if current_block.any? nodes << build_block(current_block) current_block = [] end nodes << Ast::Merge::Comment::Empty.new(line_number: line_number, text: raw) elsif stripped.start_with?('#') # Ruby comment line current_block << Line.new( text: raw, line_number: line_number, magic_comment_type: header_magic_comment_types[line_number] ) else # Non-comment content (shouldn't happen in comment-only files) if current_block.any? nodes << build_block(current_block) current_block = [] end # Add as generic line nodes << Ast::Merge::Comment::Line.new( text: raw, line_number: line_number, style: :hash_comment ) end end # Flush remaining block nodes << build_block(current_block) if current_block.any? nodes end |