Class: Language::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/language/parser.rb,
lib/language/parser/str.rb,
lib/language/parser/absent.rb,
lib/language/parser/interuption.rb,
lib/language/parser/end_of_input.rb,
lib/language/parser/str/not_found.rb,
lib/language/parser/absent/present.rb,
lib/language/parser/not_end_of_input.rb

Defined Under Namespace

Classes: Absent, EndOfInput, Interuption, NotEndOfInput, Str

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root:, input:, cursor: 0, buffer: "", output: Output.new) ⇒ Parser

Returns a new instance of Parser.



7
8
9
10
11
12
13
# File 'lib/language/parser.rb', line 7

def initialize(root:, input:, cursor: 0, buffer: "", output: Output.new)
  @root = root
  @input = input
  @cursor = cursor
  @buffer = buffer
  @output = output
end

Instance Attribute Details

#bufferObject

Returns the value of attribute buffer.



5
6
7
# File 'lib/language/parser.rb', line 5

def buffer
  @buffer
end

#cursorObject

Returns the value of attribute cursor.



5
6
7
# File 'lib/language/parser.rb', line 5

def cursor
  @cursor
end

#inputObject

Returns the value of attribute input.



5
6
7
# File 'lib/language/parser.rb', line 5

def input
  @input
end

#outputObject

Returns the value of attribute output.



5
6
7
# File 'lib/language/parser.rb', line 5

def output
  @output
end

#rootObject

Returns the value of attribute root.



5
6
7
# File 'lib/language/parser.rb', line 5

def root
  @root
end

Instance Method Details

#aka(name) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/language/parser.rb', line 32

def aka(name)
  if @buffer.empty?
    @output = Output.new({ name => @output })
  else
    @output[name] = Output.new(@buffer)
    @buffer = ""
  end
end

#buffer?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/language/parser.rb', line 45

def buffer?
  @buffer != ""
end

#consume(n) ⇒ Object

Raises:



25
26
27
28
29
30
# File 'lib/language/parser.rb', line 25

def consume(n)
  raise EndOfInput, self unless @cursor + n <= @input.size

  @buffer += @input[@cursor, n]
  @cursor += n
end

#next?(string) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/language/parser.rb', line 41

def next?(string)
  @input[@cursor...(@cursor + string.size)] == string
end

#output?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/language/parser.rb', line 49

def output?
  @output.present?
end

#parse(check_end_of_input: true) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/language/parser.rb', line 15

def parse(check_end_of_input: true)
  @root.parse(self)

  unless @cursor == @input.size || !check_end_of_input
    raise NotEndOfInput, self
  end

  @output.present? ? @output : Output.new(@buffer.empty? ? nil : @buffer)
end