Class: RPrec::Grammar

Inherits:
Object
  • Object
show all
Defined in:
lib/rprec/grammar.rb

Overview

Grammar is a grammar defined by operator precedences.

Instance Method Summary collapse

Constructor Details

#initialize(main, precs) ⇒ Grammar

Returns a new instance of Grammar.

Parameters:



8
9
10
11
# File 'lib/rprec/grammar.rb', line 8

def initialize(main, precs)
  @main = main
  @precs = precs
end

Instance Method Details

#[](name) ⇒ RPrec::Prec

Parameters:

  • name (Symbol)

Returns:

Raises:

  • (KeyError)


15
16
17
18
19
20
# File 'lib/rprec/grammar.rb', line 15

def [](name)
  prec = @precs[name]
  raise KeyError, "A prec '#{name}' is undefined" unless prec

  prec
end

#inspectString

Returns:

  • (String)


91
92
93
94
95
96
97
98
99
100
# File 'lib/rprec/grammar.rb', line 91

def inspect
  result = "DSL.build #{@main.inspect} do\n"
  @precs.each do |(_, prec)|
    prec.inspect.lines(chomp: true).each do |line|
      result << "  #{line}\n"
    end
  end
  result << 'end'
  result
end

#parse(stream, check_eof: true) ⇒ Object

Parameters:

Returns:

  • (Object)


35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rprec/grammar.rb', line 35

def parse(stream, check_eof: true)
  result = parse_prec(@main, stream)
  stream.unexpected unless result

  if check_eof
    stream.expected(['EOF'])
    stream.unexpected unless stream.eof?
  end

  result
end

#parse_parts(parts, stream) ⇒ Array<RPrec::Token, Object>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

Returns:



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/rprec/grammar.rb', line 72

def parse_parts(parts, stream)
  parts.map do |part|
    if part.is_a?(Symbol)
      result = parse_prec(part, stream)
      stream.unexpected unless result
      result
    else
      token = stream.current
      unless token.type == part
        stream.expected([part])
        stream.unexpected
      end
      stream.next
      token
    end
  end
end

#parse_prec(name, stream) ⇒ Object?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

Returns:

  • (Object, nil)


63
64
65
66
# File 'lib/rprec/grammar.rb', line 63

def parse_prec(name, stream)
  prec = self[name]
  prec.parse(self, stream)
end

#parse_precs(names, stream) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

Returns:

  • (Object)


51
52
53
54
55
56
57
# File 'lib/rprec/grammar.rb', line 51

def parse_precs(names, stream)
  names.each do |name|
    result = parse_prec(name, stream)
    return result if result
  end
  stream.unexpected
end

#setup(names = [@main]) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Parameters:

  • names (Array<Symbol>) (defaults to: [@main])


25
26
27
28
29
30
# File 'lib/rprec/grammar.rb', line 25

def setup(names = [@main])
  names.each do |name|
    prec = self[name]
    prec.setup(self)
  end
end