Class: Tailor::Lexer::Token

Inherits:
String
  • Object
show all
Includes:
Tailor::LexerConstants, Tailor::Logger::Mixin
Defined in:
lib/tailor/lexer/token.rb

Overview

Helper methods for tokens that are parsed by Tailor::Lexer.

Constant Summary

Constants included from Tailor::LexerConstants

Tailor::LexerConstants::CONTINUATION_KEYWORDS, Tailor::LexerConstants::KEYWORDS_AND_MODIFIERS, Tailor::LexerConstants::KEYWORDS_TO_INDENT, Tailor::LexerConstants::LOOP_KEYWORDS, Tailor::LexerConstants::MODIFIERS, Tailor::LexerConstants::MULTILINE_OPERATORS

Instance Method Summary collapse

Methods included from Tailor::Logger::Mixin

included

Methods inherited from String

#underscore

Constructor Details

#initialize(the_token, options = {}) ⇒ Token

Returns a new instance of Token.

Parameters:

  • the_token (String)
  • options (Hash) (defaults to: {})


15
16
17
18
# File 'lib/tailor/lexer/token.rb', line 15

def initialize(the_token, options={})
  super(the_token)
  @options = options
end

Instance Method Details

#contains_capital_letter?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/tailor/lexer/token.rb', line 52

def contains_capital_letter?
  self =~ /[A-Z]/
end

#contains_hard_tab?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/tailor/lexer/token.rb', line 57

def contains_hard_tab?
  self =~ /\t/
end

#continuation_keyword?Boolean

Checks if self is in {CONTINUATION_KEYWORDS}.

Returns:

  • (Boolean)


30
31
32
# File 'lib/tailor/lexer/token.rb', line 30

def continuation_keyword?
  CONTINUATION_KEYWORDS.include? self
end

#do_is_for_a_loop?Boolean

Checks if self is “do” and +@options is true.

Returns:

  • (Boolean)


42
43
44
# File 'lib/tailor/lexer/token.rb', line 42

def do_is_for_a_loop?
  self == 'do' && @options[:loop_with_do]
end

#ends_with_newline?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/tailor/lexer/token.rb', line 35

def ends_with_newline?
  self =~ /\n$/
end

#fake_backslash_line_end?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/tailor/lexer/token.rb', line 105

def fake_backslash_line_end?
  self =~ /^# TAILOR REMOVED BACKSLASH\n?$/
end

#keyword_to_indent?Boolean

Checks if self is in {KEYWORDS_TO_INDENT}.

Returns:

  • (Boolean)


23
24
25
# File 'lib/tailor/lexer/token.rb', line 23

def keyword_to_indent?
  KEYWORDS_TO_INDENT.include? self
end

#modifier_keyword?Boolean

Checks the current line to see if self is being used as a modifier.

Returns:

  • (Boolean)

    True if there’s a modifier in the current line that is the same type as token.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/tailor/lexer/token.rb', line 65

def modifier_keyword?
  return false unless keyword_to_indent?

  line_of_text = @options[:full_line_of_text]
  log "Line of text: #{line_of_text}"

  catch(:result) do
    sexp_line = Ripper.sexp(line_of_text)

    if sexp_line.nil?
      msg = 'sexp line was nil.  '
      msg << 'Perhaps that line is part of a multi-line statement?'
      log msg
      log 'Trying again with the last char removed from the line...'
      line_of_text.chop!
      sexp_line = Ripper.sexp(line_of_text)
    end

    if sexp_line.nil?
      log 'sexp line was nil again.'
      log 'Trying 1 more time with the last char removed from the line...'
      line_of_text.chop!
      sexp_line = Ripper.sexp(line_of_text)
    end

    if sexp_line.is_a? Array
      log "sexp_line.flatten: #{sexp_line.flatten}"
      log "sexp_line.last.first: #{sexp_line.last.first}"

      begin
        throw(:result, sexp_line.flatten.compact.any? do |s|
          s == MODIFIERS[self]
        end)
      rescue NoMethodError
      end
    end
  end
end

#screaming_snake_case?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/tailor/lexer/token.rb', line 47

def screaming_snake_case?
  self =~ /[A-Z].*_/
end