Class: Gherkin::TokenMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/gherkin/token_matcher.rb

Constant Summary collapse

LANGUAGE_PATTERN =
/^\s*#\s*language\s*:\s*([a-zA-Z\-_]+)\s*$/

Instance Method Summary collapse

Constructor Details

#initialize(dialect_name = 'en') ⇒ TokenMatcher

Returns a new instance of TokenMatcher.



11
12
13
14
15
# File 'lib/gherkin/token_matcher.rb', line 11

def initialize(dialect_name = 'en')
  @default_dialect_name = dialect_name
  change_dialect(dialect_name, nil)
  reset
end

Instance Method Details

#_match_DocStringSeparator(token, separator, is_open) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/gherkin/token_matcher.rb', line 95

def _match_DocStringSeparator(token, separator, is_open)
  return false unless token.line.start_with?(separator)

  media_type = nil
  if is_open
    media_type = token.line.get_rest_trimmed(separator.length)
    @active_doc_string_separator = separator
    @indent_to_remove = token.line.indent
  else
    @active_doc_string_separator = nil
    @indent_to_remove = 0
  end

  set_token_matched(token, :DocStringSeparator, media_type, separator)
  true
end

#match_BackgroundLine(token) ⇒ Object



43
44
45
# File 'lib/gherkin/token_matcher.rb', line 43

def match_BackgroundLine(token)
  match_title_line(token, :BackgroundLine, @dialect.background_keywords)
end

#match_Comment(token) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/gherkin/token_matcher.rb', line 65

def match_Comment(token)
  return false unless token.line.start_with?('#')

  text = token.line.get_line_text(0) # take the entire line, including leading space
  set_token_matched(token, :Comment, text, nil, 0)
  true
end

#match_DocStringSeparator(token) ⇒ Object



84
85
86
87
88
89
90
91
92
93
# File 'lib/gherkin/token_matcher.rb', line 84

def match_DocStringSeparator(token)
  if @active_doc_string_separator.nil?
    # open
    _match_DocStringSeparator(token, '"""', true) ||
      _match_DocStringSeparator(token, '```', true)
  else
    # close
    _match_DocStringSeparator(token, @active_doc_string_separator, false)
  end
end

#match_Empty(token) ⇒ Object



58
59
60
61
62
63
# File 'lib/gherkin/token_matcher.rb', line 58

def match_Empty(token)
  return false unless token.line.empty?

  set_token_matched(token, :Empty, nil, nil, 0)
  true
end

#match_EOF(token) ⇒ Object



112
113
114
115
116
117
# File 'lib/gherkin/token_matcher.rb', line 112

def match_EOF(token)
  return false unless token.eof?

  set_token_matched(token, :EOF)
  true
end

#match_ExamplesLine(token) ⇒ Object



47
48
49
# File 'lib/gherkin/token_matcher.rb', line 47

def match_ExamplesLine(token)
  match_title_line(token, :ExamplesLine, @dialect.examples_keywords)
end

#match_FeatureLine(token) ⇒ Object



30
31
32
# File 'lib/gherkin/token_matcher.rb', line 30

def match_FeatureLine(token)
  match_title_line(token, :FeatureLine, @dialect.feature_keywords)
end

#match_Language(token) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/gherkin/token_matcher.rb', line 73

def match_Language(token)
  return false unless token.line.trimmed_line_text =~ LANGUAGE_PATTERN

  dialect_name = Regexp.last_match(1)
  set_token_matched(token, :Language, dialect_name)

  change_dialect(dialect_name, token.location)

  true
end

#match_Other(token) ⇒ Object



119
120
121
122
123
# File 'lib/gherkin/token_matcher.rb', line 119

def match_Other(token)
  text = token.line.get_line_text(@indent_to_remove) # take the entire line, except removing DocString indents
  set_token_matched(token, :Other, unescape_docstring(text), nil, 0)
  true
end

#match_RuleLine(token) ⇒ Object



34
35
36
# File 'lib/gherkin/token_matcher.rb', line 34

def match_RuleLine(token)
  match_title_line(token, :RuleLine, @dialect.rule_keywords)
end

#match_ScenarioLine(token) ⇒ Object



38
39
40
41
# File 'lib/gherkin/token_matcher.rb', line 38

def match_ScenarioLine(token)
  match_title_line(token, :ScenarioLine, @dialect.scenario_keywords) ||
    match_title_line(token, :ScenarioLine, @dialect.scenario_outline_keywords)
end

#match_StepLine(token) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/gherkin/token_matcher.rb', line 125

def match_StepLine(token)
  keywords =
    @dialect.given_keywords +
    @dialect.when_keywords +
    @dialect.then_keywords +
    @dialect.and_keywords +
    @dialect.but_keywords

  keyword = keywords.detect { |k| token.line.start_with?(k) }

  return false unless keyword

  title = token.line.get_rest_trimmed(keyword.length)
  keyword_types = @keyword_types[keyword]
  keyword_type = keyword_types[0]
  keyword_type = Cucumber::Messages::StepKeywordType::UNKNOWN if keyword_types.length > 1

  set_token_matched(token,
                    :StepLine, title, keyword, nil, keyword_type)
  true
end

#match_TableRow(token) ⇒ Object



51
52
53
54
55
56
# File 'lib/gherkin/token_matcher.rb', line 51

def match_TableRow(token)
  return false unless token.line.start_with?('|')

  set_token_matched(token, :TableRow, nil, nil, nil, nil, token.line.table_cells)
  true
end

#match_TagLine(token) ⇒ Object



23
24
25
26
27
28
# File 'lib/gherkin/token_matcher.rb', line 23

def match_TagLine(token)
  return false unless token.line.start_with?('@')

  set_token_matched(token, :TagLine, nil, nil, nil, nil, token.line.tags)
  true
end

#resetObject



17
18
19
20
21
# File 'lib/gherkin/token_matcher.rb', line 17

def reset
  change_dialect(@default_dialect_name, nil) unless @dialect_name == @default_dialect_name
  @active_doc_string_separator = nil
  @indent_to_remove = 0
end