Class: Cucumber::Parser::NaturalLanguage

Inherits:
Object
  • Object
show all
Defined in:
lib/cucumber/parser/natural_language.rb

Constant Summary collapse

KEYWORD_KEYS =
%w{name native feature background scenario scenario_outline examples given when then and but}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(step_mother, lang) ⇒ NaturalLanguage

Returns a new instance of NaturalLanguage.



23
24
25
26
27
28
29
# File 'lib/cucumber/parser/natural_language.rb', line 23

def initialize(step_mother, lang)
  @keywords = Cucumber::LANGUAGES[lang]
  raise "Language not supported: #{lang.inspect}" if @keywords.nil?
  @keywords['grammar_name'] = @keywords['name'].gsub(/\s/, '')
  register_adverbs(step_mother) if step_mother
  @parser = nil
end

Class Method Details

.all(step_mother = nil) ⇒ Object

Used by code generators for other lexer tools like pygments lexer and textmate bundle



18
19
20
# File 'lib/cucumber/parser/natural_language.rb', line 18

def all(step_mother=nil)
  Cucumber::LANGUAGES.keys.sort.map{|lang| get(step_mother, lang)}
end

.get(step_mother, lang) ⇒ Object



9
10
11
# File 'lib/cucumber/parser/natural_language.rb', line 9

def get(step_mother, lang)
  languages[lang] ||= new(step_mother, lang)
end

.inspectObject



44
45
46
# File 'lib/cucumber/parser/natural_language.rb', line 44

def @parser.inspect
  "#<#{self.class.name}>"
end

.languagesObject



13
14
15
# File 'lib/cucumber/parser/natural_language.rb', line 13

def languages
  @languages ||= {}
end

Instance Method Details

#and_keywords(space = true) ⇒ Object



84
85
86
# File 'lib/cucumber/parser/natural_language.rb', line 84

def and_keywords(space=true)
  keywords('and', space)
end

#background_keywordsObject



72
73
74
# File 'lib/cucumber/parser/natural_language.rb', line 72

def background_keywords
  keywords('background')
end

#but_keywords(space = true) ⇒ Object



80
81
82
# File 'lib/cucumber/parser/natural_language.rb', line 80

def but_keywords(space=true)
  keywords('but', space)
end

#examples_keywordsObject



76
77
78
# File 'lib/cucumber/parser/natural_language.rb', line 76

def examples_keywords
  keywords('examples')
end

#feature_keywordsObject



60
61
62
# File 'lib/cucumber/parser/natural_language.rb', line 60

def feature_keywords
  keywords('feature')
end

#incomplete?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/cucumber/parser/natural_language.rb', line 56

def incomplete?
  KEYWORD_KEYS.detect{|key| @keywords[key].nil?}
end

#keywords(key, space = false) ⇒ Object



92
93
94
95
# File 'lib/cucumber/parser/natural_language.rb', line 92

def keywords(key, space=false)
  raise "No #{key} in #{@keywords.inspect}" if @keywords[key].nil?
  @keywords[key].split('|').map{|kw| space ? keyword_space(kw) : kw}
end

#parse(source, path, filter) ⇒ Object



50
51
52
53
54
# File 'lib/cucumber/parser/natural_language.rb', line 50

def parse(source, path, filter)
  feature = parser.parse_or_fail(source, path, filter)
  feature.language = self if feature
  feature
end

#parserObject



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cucumber/parser/natural_language.rb', line 36

def parser
  return @parser if @parser
  i18n_tt = File.expand_path(File.dirname(__FILE__) + '/i18n.tt')
  template = File.open(i18n_tt, Cucumber.file_mode('r')).read
  erb = ERB.new(template)
  grammar = erb.result(binding)
  Treetop.load_from_string(grammar)
  @parser = Parser::I18n.const_get("#{@keywords['grammar_name']}Parser").new
  def @parser.inspect
    "#<#{self.class.name}>"
  end
  @parser
end

#register_adverbs(step_mother) ⇒ Object



31
32
33
34
# File 'lib/cucumber/parser/natural_language.rb', line 31

def register_adverbs(step_mother)
  adverbs = %w{given when then and but}.map{|keyword| @keywords[keyword].split('|').map{|w| w.gsub(/[\s<']/, '')}}.flatten
  step_mother.register_adverbs(adverbs) if step_mother
end

#scenario_keywordsObject



64
65
66
# File 'lib/cucumber/parser/natural_language.rb', line 64

def scenario_keywords
  keywords('scenario')
end

#scenario_outline_keywordsObject



68
69
70
# File 'lib/cucumber/parser/natural_language.rb', line 68

def scenario_outline_keywords
  keywords('scenario_outline')
end

#step_keywordsObject



88
89
90
# File 'lib/cucumber/parser/natural_language.rb', line 88

def step_keywords
  %w{given when then and but}.map{|key| keywords(key, true)}.flatten.uniq
end