Class: Tailor::Lexer::Token
- 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
- #contains_capital_letter? ⇒ Boolean
- #contains_hard_tab? ⇒ Boolean
-
#continuation_keyword? ⇒ Boolean
Checks if
self
is in {CONTINUATION_KEYWORDS}. -
#do_is_for_a_loop? ⇒ Boolean
Checks if
self
is “do” and +@options is true. - #ends_with_newline? ⇒ Boolean
- #fake_backslash_line_end? ⇒ Boolean
-
#initialize(the_token, options = {}) ⇒ Token
constructor
A new instance of Token.
-
#keyword_to_indent? ⇒ Boolean
Checks if
self
is in {KEYWORDS_TO_INDENT}. -
#modifier_keyword? ⇒ Boolean
Checks the current line to see if
self
is being used as a modifier. - #screaming_snake_case? ⇒ Boolean
Methods included from Tailor::Logger::Mixin
Methods inherited from String
Constructor Details
#initialize(the_token, options = {}) ⇒ Token
Returns a new instance of Token.
15 16 17 18 |
# File 'lib/tailor/lexer/token.rb', line 15 def initialize(the_token, ={}) super(the_token) @options = end |
Instance Method Details
#contains_capital_letter? ⇒ Boolean
52 53 54 |
# File 'lib/tailor/lexer/token.rb', line 52 def contains_capital_letter? self =~ /[A-Z]/ end |
#contains_hard_tab? ⇒ 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}.
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.
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
35 36 37 |
# File 'lib/tailor/lexer/token.rb', line 35 def ends_with_newline? self =~ /\n$/ end |
#fake_backslash_line_end? ⇒ Boolean
107 108 109 |
# File 'lib/tailor/lexer/token.rb', line 107 def fake_backslash_line_end? self =~ /^# TAILOR REMOVED BACKSLASH\n?$/ end |
#keyword_to_indent? ⇒ Boolean
Checks if self
is in {KEYWORDS_TO_INDENT}.
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.
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 103 104 |
# File 'lib/tailor/lexer/token.rb', line 65 def modifier_keyword? return false if not keyword_to_indent? line_of_text = @options[:full_line_of_text] log "Line of text: #{line_of_text}" result = 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 result end |
#screaming_snake_case? ⇒ Boolean
47 48 49 |
# File 'lib/tailor/lexer/token.rb', line 47 def screaming_snake_case? self =~ /[A-Z].*_/ end |