Class: Kumi::Parser::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/kumi/parser/token.rb

Overview

Token with embedded metadata for smart parsing

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, value, location, metadata = {}) ⇒ Token

Returns a new instance of Token.



9
10
11
12
13
14
# File 'lib/kumi/parser/token.rb', line 9

def initialize(type, value, location,  = {})
  @type = type
  @value = value
  @location = location
  @metadata = 
end

Instance Attribute Details

#locationObject (readonly)

Returns the value of attribute location.



7
8
9
# File 'lib/kumi/parser/token.rb', line 7

def location
  @location
end

#metadataObject (readonly)

Returns the value of attribute metadata.



7
8
9
# File 'lib/kumi/parser/token.rb', line 7

def 
  @metadata
end

#typeObject (readonly)

Returns the value of attribute type.



7
8
9
# File 'lib/kumi/parser/token.rb', line 7

def type
  @type
end

#valueObject (readonly)

Returns the value of attribute value.



7
8
9
# File 'lib/kumi/parser/token.rb', line 7

def value
  @value
end

Instance Method Details

#==(other) ⇒ Object



76
77
78
79
80
81
# File 'lib/kumi/parser/token.rb', line 76

def ==(other)
  other.is_a?(Token) &&
    @type == other.type &&
    @value == other.value &&
    @location == other.location
end

#ast_classObject

Direct AST construction hint



64
65
66
# File 'lib/kumi/parser/token.rb', line 64

def ast_class
  @metadata[:ast_class]
end

#expects_block?Boolean

Parser hints embedded in token

Returns:

  • (Boolean)


51
52
53
# File 'lib/kumi/parser/token.rb', line 51

def expects_block?
  @metadata[:expects_block] == true
end

#identifier?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/kumi/parser/token.rb', line 29

def identifier?
  @metadata[:category] == :identifier
end

#inspectObject



72
73
74
# File 'lib/kumi/parser/token.rb', line 72

def inspect
  to_s
end

#keyword?Boolean

Semantic predicates embedded in token

Returns:

  • (Boolean)


17
18
19
# File 'lib/kumi/parser/token.rb', line 17

def keyword?
  @metadata[:category] == :keyword
end

#left_associative?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/kumi/parser/token.rb', line 42

def left_associative?
  @metadata[:associativity] == :left
end

#literal?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/kumi/parser/token.rb', line 25

def literal?
  @metadata[:category] == :literal
end

#operator?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/kumi/parser/token.rb', line 21

def operator?
  @metadata[:category] == :operator
end

#precedenceObject

Operator precedence embedded in token



38
39
40
# File 'lib/kumi/parser/token.rb', line 38

def precedence
  @metadata[:precedence] || 0
end

#punctuation?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/kumi/parser/token.rb', line 33

def punctuation?
  @metadata[:category] == :punctuation
end

#right_associative?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/kumi/parser/token.rb', line 46

def right_associative?
  @metadata[:associativity] == :right
end

#starts_expression?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/kumi/parser/token.rb', line 59

def starts_expression?
  @metadata[:starts_expression] == true
end

#terminates_expression?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/kumi/parser/token.rb', line 55

def terminates_expression?
  @metadata[:terminates_expression] == true
end

#to_sObject



68
69
70
# File 'lib/kumi/parser/token.rb', line 68

def to_s
  "#{@type}(#{@value.inspect}) at #{@location}"
end