Class: Taxonifi::Splitter::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/taxonifi/splitter/parser.rb

Overview

Parser pattern taken from OboParser and other mjy gems.

The parser takes a builder and a lexer and does the actual breakdown.

Instance Method Summary collapse

Constructor Details

#initialize(lexer, builder) ⇒ Parser

Returns a new instance of Parser.



7
8
9
10
# File 'lib/taxonifi/splitter/parser.rb', line 7

def initialize(lexer, builder )
  @lexer = lexer
  @builder = builder
end

Instance Method Details

#parse_author_yearObject

parse out an author year combination. TODO: This is only indirectly tested in lumper code



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/taxonifi/splitter/parser.rb', line 14

def parse_author_year
  t = @lexer.pop(Taxonifi::Splitter::Tokens::AuthorYear)

  lexer = Taxonifi::Splitter::Lexer.new(t.authors)
  authors = lexer.pop(Taxonifi::Splitter::Tokens::Authors)

  # TODO: A people collection?
  authors.names.each do |a|
    n = Taxonifi::Model::Person.new()
    n.last_name = a[:last_name]
    n.initials = a[:initials]
    @builder.people.push n 
  end

  @builder.year   = t.year.to_i
  @builder.parens = t.parens
end

#parse_species_nameObject

Parse a species name



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/taxonifi/splitter/parser.rb', line 33

def parse_species_name
  t = @lexer.pop(Taxonifi::Splitter::Tokens::Quadrinomial)
  ranks = %w{genus subgenus species subspecies}
  names = {} 
  ranks.each do |r|
    names.merge!(r: nil)
    @builder.send("#{r}=", Taxonifi::Model::Name.new(:name => t.send(r), rank: r) ) if t.send(r)
  end

  if @lexer.peek(Taxonifi::Splitter::Tokens::Variety)
    t = @lexer.pop(Taxonifi::Splitter::Tokens::Variety)
    @builder.variety = Taxonifi::Model::Name.new(:name => t.variety, rank: 'variety')  
  end

  if @lexer.peek(Taxonifi::Splitter::Tokens::AuthorYear)
    t = @lexer.pop(Taxonifi::Splitter::Tokens::AuthorYear)
    @builder.names.last.author = t.authors
    @builder.names.last.year = t.year
    @builder.names.last.parens = t.parens
    @builder.names.last.derive_authors_year
  end

  @builder
end