Class: Repper::Token

Inherits:
Struct
  • Object
show all
Defined in:
lib/repper/token.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#idObject

Returns the value of attribute id

Returns:

  • (Object)

    the current value of id



2
3
4
# File 'lib/repper/token.rb', line 2

def id
  @id
end

#levelObject

Returns the value of attribute level

Returns:

  • (Object)

    the current value of level



2
3
4
# File 'lib/repper/token.rb', line 2

def level
  @level
end

#subtypeObject

Returns the value of attribute subtype

Returns:

  • (Object)

    the current value of subtype



2
3
4
# File 'lib/repper/token.rb', line 2

def subtype
  @subtype
end

#textObject

Returns the value of attribute text

Returns:

  • (Object)

    the current value of text



2
3
4
# File 'lib/repper/token.rb', line 2

def text
  @text
end

#typeObject

Returns the value of attribute type

Returns:

  • (Object)

    the current value of type



2
3
4
# File 'lib/repper/token.rb', line 2

def type
  @type
end

Instance Method Details

#annotationObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/repper/token.rb', line 25

def annotation
  case [type, subtype]
  in [_, :root]
    nil
  in [_, :alternation | :comment | :condition | :intersection | :range => subtype]
    subtype
  in [:conditional | :free_space => type, _]
    type
  in [:group, :capture | :named]
    "capture group #{id}"
  in [:meta, :dot]
    'match-all'
  in [:keep, :mark]
    'keep-mark lookbehind'
  in [type, subtype]
    [subtype, type].uniq.join(' ')
  end.to_s.tr('_', ' ')
end

#comment?Boolean

Returns:

  • (Boolean)


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

def comment?
  subtype == :comment
end

#indented_textObject



3
4
5
# File 'lib/repper/token.rb', line 3

def indented_text
  "#{'  ' * level}#{inlined_text}"
end

#inlined_textObject



7
8
9
10
11
12
13
14
15
# File 'lib/repper/token.rb', line 7

def inlined_text
  if comment?
    text.strip
  else
    text
      .gsub(/(?<!\\) /, '\\ ')
      .gsub(/[\n\r\t\v]/) { |ws| ws.inspect.delete(?") }
  end
end

#whitespace?Boolean

Returns:

  • (Boolean)


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

def whitespace?
  subtype == :whitespace
end