Class: Markbridge::Parsers::BBCode::ParserState
- Inherits:
-
Object
- Object
- Markbridge::Parsers::BBCode::ParserState
- Defined in:
- lib/markbridge/parsers/bbcode/parser_state.rb
Overview
Manages parsing state
Constant Summary collapse
- MAX_DEPTH =
100
Instance Attribute Summary collapse
-
#auto_closed_count ⇒ Object
readonly
Returns the value of attribute auto_closed_count.
-
#current ⇒ Object
readonly
Returns the value of attribute current.
-
#depth ⇒ Object
readonly
Returns the value of attribute depth.
-
#depth_exceeded_count ⇒ Object
readonly
Returns the value of attribute depth_exceeded_count.
-
#unclosed_raw_tags ⇒ Object
readonly
Returns the value of attribute unclosed_raw_tags.
Instance Method Summary collapse
-
#add_child(node) ⇒ Object
Add a child to current node without changing context.
-
#auto_close!(count = 1) ⇒ Object
Increment the count of auto-closed tags after external reconciliation.
-
#elements_from_current(limit = nil) ⇒ Array<AST::Node>
Return elements from the current node downward.
-
#initialize(root) ⇒ ParserState
constructor
A new instance of ParserState.
-
#mark_unclosed_raw!(tag_name) ⇒ Object
Mark a raw tag as unclosed (for tracking parsing issues).
-
#pop ⇒ AST::Element
Pop current element and return to parent.
-
#push(element, token: nil) ⇒ Boolean
Add element as child to current node and push the element onto the stack Uses graceful degradation: if max depth is exceeded and token is provided, treats the tag as text instead of raising.
Constructor Details
#initialize(root) ⇒ ParserState
Returns a new instance of ParserState.
12 13 14 15 16 17 18 19 20 |
# File 'lib/markbridge/parsers/bbcode/parser_state.rb', line 12 def initialize(root) @root = root @current = root @depth = 0 @node_stack = [root] @auto_closed_count = 0 @depth_exceeded_count = 0 @unclosed_raw_tags = Hash.new(0) end |
Instance Attribute Details
#auto_closed_count ⇒ Object (readonly)
Returns the value of attribute auto_closed_count.
10 11 12 |
# File 'lib/markbridge/parsers/bbcode/parser_state.rb', line 10 def auto_closed_count @auto_closed_count end |
#current ⇒ Object (readonly)
Returns the value of attribute current.
10 11 12 |
# File 'lib/markbridge/parsers/bbcode/parser_state.rb', line 10 def current @current end |
#depth ⇒ Object (readonly)
Returns the value of attribute depth.
10 11 12 |
# File 'lib/markbridge/parsers/bbcode/parser_state.rb', line 10 def depth @depth end |
#depth_exceeded_count ⇒ Object (readonly)
Returns the value of attribute depth_exceeded_count.
10 11 12 |
# File 'lib/markbridge/parsers/bbcode/parser_state.rb', line 10 def depth_exceeded_count @depth_exceeded_count end |
#unclosed_raw_tags ⇒ Object (readonly)
Returns the value of attribute unclosed_raw_tags.
10 11 12 |
# File 'lib/markbridge/parsers/bbcode/parser_state.rb', line 10 def @unclosed_raw_tags end |
Instance Method Details
#add_child(node) ⇒ Object
Add a child to current node without changing context
63 64 65 |
# File 'lib/markbridge/parsers/bbcode/parser_state.rb', line 63 def add_child(node) @current << node end |
#auto_close!(count = 1) ⇒ Object
Increment the count of auto-closed tags after external reconciliation
69 70 71 |
# File 'lib/markbridge/parsers/bbcode/parser_state.rb', line 69 def auto_close!(count = 1) @auto_closed_count += count end |
#elements_from_current(limit = nil) ⇒ Array<AST::Node>
Return elements from the current node downward
82 83 84 85 86 87 88 89 |
# File 'lib/markbridge/parsers/bbcode/parser_state.rb', line 82 def elements_from_current(limit = nil) return [] if @node_stack.empty? limit = (@node_stack.size - 1) if limit.nil? limit = [limit, @node_stack.size - 1].min (0..limit).map { |offset| @node_stack[@node_stack.size - 1 - offset] } end |
#mark_unclosed_raw!(tag_name) ⇒ Object
Mark a raw tag as unclosed (for tracking parsing issues)
75 76 77 |
# File 'lib/markbridge/parsers/bbcode/parser_state.rb', line 75 def mark_unclosed_raw!(tag_name) @unclosed_raw_tags[tag_name] += 1 end |
#pop ⇒ AST::Element
Pop current element and return to parent
52 53 54 55 56 57 58 59 |
# File 'lib/markbridge/parsers/bbcode/parser_state.rb', line 52 def pop return @root if @node_stack.size <= 1 @node_stack.pop @current = @node_stack.last @depth -= 1 @current end |
#push(element, token: nil) ⇒ Boolean
Add element as child to current node and push the element onto the stack Uses graceful degradation: if max depth is exceeded and token is provided, treats the tag as text instead of raising. If no token is provided, raises MaxDepthExceededError (for backwards compatibility).
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/markbridge/parsers/bbcode/parser_state.rb', line 30 def push(element, token: nil) if @depth >= MAX_DEPTH if token # Graceful degradation: treat as text @current << AST::Text.new(token.source) @depth_exceeded_count += 1 return false else # Legacy behavior: raise error raise MaxDepthExceededError, MAX_DEPTH end end @current << element @current = element @node_stack << element @depth += 1 true end |