Skip to content

Commit

Permalink
figuring out git
Browse files Browse the repository at this point in the history
  • Loading branch information
mjy committed Mar 18, 2010
1 parent e98fe4a commit 5f3110f
Show file tree
Hide file tree
Showing 8 changed files with 606 additions and 609 deletions.
4 changes: 2 additions & 2 deletions README.rdoc
@@ -1,6 +1,6 @@
= nexus_parser

Description goes here.
See the test files for usage for now, lots of examples there.

== Note on Patches/Pull Requests

Expand All @@ -14,4 +14,4 @@ Description goes here.

== Copyright

Copyright (c) 2010 mjy. See LICENSE for details.
Copyright (c) 2010 Matt Yoder. See LICENSE for details.
8 changes: 4 additions & 4 deletions lib/lexer.rb
@@ -1,6 +1,6 @@


class NexusFile::Lexer
class NexusParser::Lexer

def initialize(input)
@input = input
Expand All @@ -21,7 +21,7 @@ def pop(token_class)
token = read_next_token(token_class)
@next_token = nil
if token.class != token_class
raise(NexusFile::ParseError,"expected #{token_class.to_s} but received #{token.class.to_s} at #{@input[0..10]}...", caller)
raise(NexusParser::ParseError,"expected #{token_class.to_s} but received #{token.class.to_s} at #{@input[0..10]}...", caller)
else
return token
end
Expand All @@ -38,13 +38,13 @@ def read_next_token(token_class)
return @next_token
else
# now check all the tokens for a match
NexusFile::Tokens.nexus_file_token_list.each {|t|
NexusParser::Tokens.nexus_file_token_list.each {|t|
return @next_token if match(t)
}
end
# no match, either end of string or lex-error
if @input != ''
raise( NexusFile::ParseError, "Lex Error, unknown token at #{@input[0..10]}...", caller)
raise( NexusParser::ParseError, "Lex Error, unknown token at #{@input[0..10]}...", caller)
else
return nil
end
Expand Down
41 changes: 20 additions & 21 deletions lib/nexus_parser.rb
Expand Up @@ -7,13 +7,13 @@
# outstanding issues:
## need to resolve Tokens Labels, ValuePair, IDs

module NexusFile
module NexusParser

require File.expand_path(File.join(File.dirname(__FILE__), 'tokens'))
require File.expand_path(File.join(File.dirname(__FILE__), 'parser'))
require File.expand_path(File.join(File.dirname(__FILE__), 'lexer'))

class NexusFile
class NexusParser

attr_accessor :taxa, :characters, :sets, :codings, :vars, :notes

Expand Down Expand Up @@ -117,28 +117,28 @@ def note
end


# constructs the NexusFile
# constructs the NexusParser
class Builder

def initialize
@nf = NexusFile.new
@nf = NexusParser.new
end

def stub_taxon
@nf.taxa.push(NexusFile::Taxon.new)
@nf.taxa.push(NexusParser::Taxon.new)
return @nf.taxa.size
end

def stub_chr
@nf.characters.push(NexusFile::Character.new)
@nf.characters.push(NexusParser::Character.new)
return @nf.characters.size
end

def code_row(taxon_index, rowvector)

@nf.characters.each_with_index do |c, i|
@nf.codings[taxon_index.to_i] = [] if !@nf.codings[taxon_index.to_i]
@nf.codings[taxon_index.to_i][i] = NexusFile::Coding.new(:states => rowvector[i])
@nf.codings[taxon_index.to_i][i] = NexusParser::Coding.new(:states => rowvector[i])

# !! we must update states for a given character if the state isn't found (not all states are referenced in description !!

Expand Down Expand Up @@ -183,7 +183,7 @@ def update_chr(options = {} )

# need to create the characters

raise(NexusFile::ParseError, "Can't update character of index #{@index}, it doesn't exist! This is a problem parsing the character state labels. Check the indices. It may be for this character \"#{@opt[:name]}\".") if !@nf.characters[@index]
raise(NexusParser::ParseError, "Can't update character of index #{@index}, it doesn't exist! This is a problem parsing the character state labels. Check the indices. It may be for this character \"#{@opt[:name]}\".") if !@nf.characters[@index]

(@nf.characters[@index].name = @opt[:name]) if @opt[:name]

Expand Down Expand Up @@ -223,28 +223,28 @@ def add_note(options = {})
# Why does mesquite differentiate b/w footnotes and annotations?!, apparently same data structure?
when 'TEXT' # a footnote
if @opt[:file]
@nf.notes << NexusFile::Note.new(@opt)
@nf.notes << NexusParser::Note.new(@opt)

elsif @opt[:taxon] && @opt[:character] # its a cell, parse this case
@nf.codings[@opt[:taxon].to_i - 1][@opt[:character].to_i - 1].notes = [] if !@nf.codings[@opt[:taxon].to_i - 1][@opt[:character].to_i - 1].notes
@nf.codings[@opt[:taxon].to_i - 1][@opt[:character].to_i - 1].notes << NexusFile::Note.new(@opt)
@nf.codings[@opt[:taxon].to_i - 1][@opt[:character].to_i - 1].notes << NexusParser::Note.new(@opt)

elsif @opt[:taxon] && !@opt[:character]
@nf.taxa[@opt[:taxon].to_i - 1].notes << NexusFile::Note.new(@opt)
@nf.taxa[@opt[:taxon].to_i - 1].notes << NexusParser::Note.new(@opt)

elsif @opt[:character] && !@opt[:taxon]

@nf.characters[@opt[:character].to_i - 1].notes << NexusFile::Note.new(@opt)
@nf.characters[@opt[:character].to_i - 1].notes << NexusParser::Note.new(@opt)
end

when 'AN' # an annotation, rather than a footnote, same dif
if @opt[:t] && @opt[:c]
@nf.codings[@opt[:t].to_i - 1][@opt[:c].to_i - 1].notes = [] if !@nf.codings[@opt[:t].to_i - 1][@opt[:c].to_i - 1].notes
@nf.codings[@opt[:t].to_i - 1][@opt[:c].to_i - 1].notes << NexusFile::Note.new(@opt)
@nf.codings[@opt[:t].to_i - 1][@opt[:c].to_i - 1].notes << NexusParser::Note.new(@opt)
elsif @opt[:t]
@nf.taxa[@opt[:t].to_i - 1].notes << NexusFile::Note.new(@opt)
@nf.taxa[@opt[:t].to_i - 1].notes << NexusParser::Note.new(@opt)
elsif @opt[:c]
@nf.characters[@opt[:c].to_i - 1].notes << NexusFile::Note.new(@opt)
@nf.characters[@opt[:c].to_i - 1].notes << NexusParser::Note.new(@opt)
end
end

Expand All @@ -256,7 +256,7 @@ def nexus_file

end # end file

# NexusFile::ParseError
# NexusParser::ParseError
class ParseError < StandardError
end

Expand All @@ -267,15 +267,14 @@ class ParseError < StandardError
def parse_nexus_file(input)
@input = input
@input.gsub!(/\[[^\]]*\]/,'') # strip out all comments BEFORE we parse the file

# quickly peek at the input, does this look like a Nexus file?
if !(@input =~ /\#Nexus/i) || !(@input =~ /Begin/i) || !(@input =~ /Matrix/i) || !(@input =~ /end\;/i)
raise(NexusFile::ParseError, "File is missing at least some required headers, check formatting.", caller)
raise(NexusParser::ParseError, "File is missing at least some required headers, check formatting.", caller)
end

builder = NexusFile::Builder.new
lexer = NexusFile::Lexer.new(@input)
NexusFile::Parser.new(lexer, builder).parse_file
builder = NexusParser::Builder.new
lexer = NexusParser::Lexer.new(@input)
NexusParser::Parser.new(lexer, builder).parse_file

return builder.nexus_file
end
Expand Down

0 comments on commit 5f3110f

Please sign in to comment.