Class: Gly::Parser

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

Overview

parses gly source

Instance Method Summary collapse

Constructor Details

#initialize(syllable_separator = nil) ⇒ Parser

Returns a new instance of Parser.



6
7
8
# File 'lib/gly/parser.rb', line 6

def initialize(syllable_separator=nil)
  @syllable_separator = syllable_separator || '--'
end

Instance Method Details

#parse(source) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/gly/parser.rb', line 10

def parse(source)
  if source.is_a? String
    if File.file? source
      parse_fname source
    elsif source == '-'
      parse_io STDIN
    else
      parse_str source
    end
  else
    parse_io source
  end
end

#parse_fname(str) ⇒ Object



24
25
26
27
28
# File 'lib/gly/parser.rb', line 24

def parse_fname(str)
  File.open(str) do |fr|
    parse_io fr
  end
end

#parse_io(io) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/gly/parser.rb', line 34

def parse_io(io)
  @doc = Document.new
  @score = ParsedScore.new

  if io.respond_to? :path
    @doc.path = io.path
  end

  io.each do |line|
    line = strip_comments(line)

    if empty? line
      next
    elsif new_score? line
      push_score
      @score = ParsedScore.new
    elsif header_start? line
      push_score
      @score = @doc.header
    elsif header_line? line
      parse_header line
    elsif explicit_lyrics? line
      parse_lyrics line
    elsif explicit_music? line
      parse_music line
    elsif lyrics_line? line
      parse_lyrics line
    else
      parse_music line
    end
  end

  push_score

  return @doc
end

#parse_str(str) ⇒ Object



30
31
32
# File 'lib/gly/parser.rb', line 30

def parse_str(str)
  parse_io(StringIO.new(str))
end