Class: Strling::Core::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/strling/core/parser.rb

Overview

Main parser class

Constant Summary collapse

CONTROL_ESCAPES =

Control character escapes mapping

{
  'n' => "\n",
  'r' => "\r",
  't' => "\t",
  'f' => "\f",
  'v' => "\v"
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ Parser

Returns a new instance of Parser.



98
99
100
101
102
103
104
105
106
# File 'lib/strling/core/parser.rb', line 98

def initialize(text)
  # Store original text for error reporting
  @original_text = text
  # Extract directives first
  @flags, @src = parse_directives(text)
  @cur = Cursor.new(@src, 0, @flags.extended, 0)
  @cap_count = 0
  @cap_names = Set.new
end

Instance Attribute Details

#curObject (readonly)

Returns the value of attribute cur.



87
88
89
# File 'lib/strling/core/parser.rb', line 87

def cur
  @cur
end

#flagsObject (readonly)

Returns the value of attribute flags.



87
88
89
# File 'lib/strling/core/parser.rb', line 87

def flags
  @flags
end

#srcObject (readonly)

Returns the value of attribute src.



87
88
89
# File 'lib/strling/core/parser.rb', line 87

def src
  @src
end

Instance Method Details

#parseObject

Main parsing entry point Returns the root AST node



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/strling/core/parser.rb', line 110

def parse
  node = parse_alt
  @cur.skip_ws_and_comments
  unless @cur.eof?
    if @cur.peek == ')'
      raise STRlingParseError.new(
        "Unmatched ')'",
        @cur.i,
        text: @src,
        hint: "This ')' character does not have a matching opening '('. Did you mean to escape it with '\\)'?"
      )
    end
    if @cur.peek == '|'
      raise_error('Alternation lacks right-hand side', @cur.i)
    else
      raise_error('Unexpected trailing input', @cur.i)
    end
  end
  [@flags, node]
end