Class: Foxtail::Syntax::Parser::AST::StringLiteral

Inherits:
BaseLiteral show all
Defined in:
lib/foxtail/syntax/parser/ast/string_literal.rb

Overview

Represents quoted string literals with escape sequence processing

Instance Attribute Summary

Attributes inherited from BaseLiteral

#value

Attributes inherited from SyntaxNode

#span

Attributes inherited from BaseNode

#type

Instance Method Summary collapse

Methods inherited from BaseLiteral

#initialize

Methods inherited from SyntaxNode

#add_span

Methods inherited from BaseNode

#==, #accept, #children, #initialize, #to_h

Constructor Details

This class inherits a constructor from Foxtail::Syntax::Parser::AST::BaseLiteral

Instance Method Details

#parseHash

Parse the string literal value, processing escape sequences Handles backslash escapes, Unicode escapes (uHHHH, UHHHHHH), and validates Unicode scalar values

Returns:

  • (Hash)

    Hash containing the parsed string value



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/foxtail/syntax/parser/ast/string_literal.rb', line 12

def parse
  # Backslash backslash, backslash double quote, uHHHH, UHHHHHH.
  known_escapes = /(?:\\\\|\\"|\\u(\h{4})|\\U(\h{6}))/

  escaped_value = @value.gsub(known_escapes) {|match|
    codepoint4 = $1
    codepoint6 = $2

    case match
    when "\\\\"
      "\\"
    when '\\"'
      '"'
    else
      codepoint = (codepoint4 || codepoint6).to_i(16)
      if codepoint <= 0xd7ff || 0xe000 <= codepoint
        # It's a Unicode scalar value.
        codepoint.chr(Encoding::UTF_8)
      else
        # Escape sequences representing surrogate code points are
        # well-formed but invalid in Fluent. Replace them with U+FFFD
        # REPLACEMENT CHARACTER.
        "\uFFFD"
      end
    end
  }

  {value: escaped_value}
end