Class: Antelope::Grammar::Precedence

Inherits:
Struct
  • Object
show all
Includes:
Comparable
Defined in:
lib/antelope/grammar/precedence.rb

Overview

Defines a precedence. A precedence has a type, tokens, and a level.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#levelObject

Returns the value of attribute level

Returns:

  • (Object)

    the current value of level



7
8
9
# File 'lib/antelope/grammar/precedence.rb', line 7

def level
  @level
end

#tokensObject

Returns the value of attribute tokens

Returns:

  • (Object)

    the current value of tokens



7
8
9
# File 'lib/antelope/grammar/precedence.rb', line 7

def tokens
  @tokens
end

#typeObject

Returns the value of attribute type

Returns:

  • (Object)

    the current value of type



7
8
9
# File 'lib/antelope/grammar/precedence.rb', line 7

def type
  @type
end

Instance Method Details

#<=>(other) ⇒ Numeric?

Compares the other object to this object. If the other object isn't a Antelope::Grammar::Precedence, it returns nil. If the other precedence isn't on the same level as this one, then the levels are compared and the result of that is returned. If it is, however, the type is checked; if this precedence is left associative, then it returns 1 (it is greater than the other); if this precedence is right associative, then it returns -1 (it is less than the other); if this precedence is nonassociative, it returns 0 (it is equal to the other).

Parameters:

  • other (Object)

    the object to compare to this one.

Returns:

  • (Numeric?)


37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/antelope/grammar/precedence.rb', line 37

def <=>(other)
  return nil unless other.is_a? Precedence
  if level != other.level
    level <=> other.level
  elsif type == :left
    1
  elsif type == :right
    -1
  else
    0
  end
end

#to_sString

Converts the precedence into a representative string, denoting the type and the level.

Returns:

  • (String)


54
55
56
# File 'lib/antelope/grammar/precedence.rb', line 54

def to_s
  "#{type.to_s[0]}#{level}"
end