Class: Ast::Merge::Comment::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/ast/merge/comment/parser.rb

Overview

Parser for building comment AST from source lines.

This parser takes an array of source lines and produces an array of AstNode objects (Block, Line, Empty) that represent the structure of a comment-only file or section.

The parser is style-aware and can handle:

  • Line comments (‘#`, `//`, `–`, `;`)

  • HTML-style comments (‘<!– … –>`)

  • C-style block comments (‘/* … */`)

Examples:

Parsing Ruby-style comments

lines = ["# frozen_string_literal: true", "", "# A comment block"]
parser = Parser.new(lines)
nodes = parser.parse
# => [Block(...), Empty(...), Block(...)]

Parsing C-style block comments

lines = ["/* Header comment", " * with multiple lines", " */"]
parser = Parser.new(lines, style: :c_style_block)
nodes = parser.parse
# => [Block(raw_content: "/* Header comment\n * with multiple lines\n */")]

Auto-detecting comment style

lines = ["// JavaScript comment", "// continues here"]
parser = Parser.new(lines, style: :auto)
nodes = parser.parse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lines, style: nil) ⇒ Parser

Initialize a new Parser.

Parameters:

  • lines (Array<String>)

    Source lines (without trailing newlines)

  • style (Style, Symbol, nil) (defaults to: nil)

    The comment style (:hash_comment, :c_style_line, etc.) Pass :auto to attempt auto-detection.



51
52
53
54
# File 'lib/ast/merge/comment/parser.rb', line 51

def initialize(lines, style: nil)
  @lines = lines || []
  @style = resolve_style(style)
end

Instance Attribute Details

#linesArray<String> (readonly)

Returns The source lines.

Returns:

  • (Array<String>)

    The source lines



41
42
43
# File 'lib/ast/merge/comment/parser.rb', line 41

def lines
  @lines
end

#styleStyle (readonly)

Returns The comment style configuration.

Returns:

  • (Style)

    The comment style configuration



44
45
46
# File 'lib/ast/merge/comment/parser.rb', line 44

def style
  @style
end

Class Method Details

.parse(lines, style: nil) ⇒ Array<AstNode>

Class method for convenient one-shot parsing.

Parameters:

  • lines (Array<String>)

    Source lines

  • style (Style, Symbol, nil) (defaults to: nil)

    Comment style

Returns:

  • (Array<AstNode>)

    Parsed nodes



77
78
79
# File 'lib/ast/merge/comment/parser.rb', line 77

def self.parse(lines, style: nil)
  new(lines, style: style).parse
end

Instance Method Details

#parseArray<AstNode>

Parse the lines into an AST.

Groups contiguous comment lines into Block nodes, and represents blank lines as Empty nodes.

Returns:

  • (Array<AstNode>)

    Array of parsed nodes



62
63
64
65
66
67
68
69
70
# File 'lib/ast/merge/comment/parser.rb', line 62

def parse
  return [] if lines.empty?

  if style.supports_block_comments?
    parse_with_block_comments
  else
    parse_line_comments
  end
end