Class: Ast::Merge::Comment::Parser
- Inherits:
-
Object
- Object
- Ast::Merge::Comment::Parser
- 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 (‘/* … */`)
Instance Attribute Summary collapse
-
#lines ⇒ Array<String>
readonly
The source lines.
-
#style ⇒ Style
readonly
The comment style configuration.
Class Method Summary collapse
-
.parse(lines, style: nil) ⇒ Array<AstNode>
Class method for convenient one-shot parsing.
Instance Method Summary collapse
-
#initialize(lines, style: nil) ⇒ Parser
constructor
Initialize a new Parser.
-
#parse ⇒ Array<AstNode>
Parse the lines into an AST.
Constructor Details
#initialize(lines, style: nil) ⇒ Parser
Initialize a new Parser.
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
#lines ⇒ Array<String> (readonly)
Returns The source lines.
41 42 43 |
# File 'lib/ast/merge/comment/parser.rb', line 41 def lines @lines end |
#style ⇒ Style (readonly)
Returns 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.
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
#parse ⇒ Array<AstNode>
Parse the lines into an AST.
Groups contiguous comment lines into Block nodes, and represents blank lines as Empty 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 |