Permalink
Browse files

Moving sgf tools to github

  • Loading branch information...
0 parents commit 7051ce31ac316f2a2cf2274a1971ea161f9f534f @ps2 committed Oct 13, 2010
Showing with 581 additions and 0 deletions.
  1. +4 −0 CHANGES
  2. +5 −0 README
  3. +31 −0 Rakefile
  4. +6 −0 lib/sgf_tools.rb
  5. +261 −0 lib/sgf_tools/parser.rb
  6. +76 −0 lib/sgf_tools/parser.y
  7. +87 −0 lib/sgf_tools/tokenizer.rb
  8. +31 −0 lib/sgf_tools/tokenizer.rex
  9. +2 −0 lib/sgftools.rb
  10. +31 −0 sgftools.gemspec
  11. +21 −0 spec/parser_spec.rb
  12. +26 −0 test/test_sgf_parse.rb
@@ -0,0 +1,4 @@
+== Version 0.0.1 (sgf_parser) - 15-Dec-2007
+
+* Initial revision
+
5 README
@@ -0,0 +1,5 @@
+= SGF Tools - Ruby library to parse SGF files
+
+For more information about the SGF format, see http://www.red-bean.com/sgf/
+
+
@@ -0,0 +1,31 @@
+# Available options:
+#
+# rake test - Runs all test cases.
+# rake package - Runs test cases and builds packages for distribution.
+# rake rdoc - Builds API documentation in doc dir.
+
+require 'rake'
+require 'rspec/core/rake_task'
+require 'rake/gempackagetask'
+
+task :default => :spec
+
+RSpec::Core::RakeTask.new do |rspec|
+ #rspec.ruby_opts="-w"
+end
+
+load(File.join(File.dirname(__FILE__), "sgftools.gemspec"))
+Rake::GemPackageTask.new(SPEC) do |package|
+ # do nothing: I just need a gem but this block is required
+end
+
+file "lib/sgf_tools/parser.rb" => "lib/sgf_tools/parser.y" do
+ sh "racc -o lib/sgf_tools/parser.rb lib/sgf_tools/parser.y"
+end
+
+file "lib/sgf_tools/tokenizer.rb" => "lib/sgf_tools/tokenizer.rex" do
+ sh "rex -o lib/sgf_tools/tokenizer.rb lib/sgf_tools/tokenizer.rex"
+end
+
+task :build => ["lib/sgf_tools/parser.rb", "lib/sgf_tools/tokenizer.rb"]
+
@@ -0,0 +1,6 @@
+module SgfTools
+ VERSION = "0.0.1"
+end
+
+require 'sgf_tools/parser'
+

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -0,0 +1,76 @@
+
+ class Parser
+rule
+ collection: game_tree { result = val }
+ | collection game_tree { result.push val[1] }
+
+ game_tree: '(' sequence ')' { result = val[1] }
+ | '(' sequence collection ')'
+ {
+ val[2][0].first.branches = val[2][1..-1]
+ result = val[1] + val[2][0]
+ }
+
+ sequence: node { result = val }
+ | sequence node { result.push val[1] }
+
+ node: ';' { result = {} }
+ | ';' properties { result = Node.new(val[1], nil) }
+
+ properties: property { result = val[0] }
+ | properties property { result.merge!(val[1]) }
+
+ property: prop_ident prop_list { result = {val[0] => val[1]} }
+
+ prop_list: prop_value { result = val }
+ | prop_list prop_value { result.push val[1] }
+
+ end
+
+---- header
+require 'sgf_tools/tokenizer'
+
+module SgfTools
+ class Node
+ attr_accessor :properties
+ attr_accessor :branches
+
+ def initialize(prop, branches)
+ @properties = prop
+ @branches = branches
+ end
+
+ def inspect
+ str = ""
+ str << @properties.inspect
+ if !@branches.nil? && @branches.size > 0
+ str << " (#{@branches.size} variation(s))"
+ end
+ str
+ end
+
+ def [](key)
+ @properties[key]
+ end
+ end
+
+---- inner
+
+ def parse_file(filename)
+ @tokenizer = SgfTokenizer.new
+ @tokenizer.load_file(filename)
+ do_parse
+ end
+
+ def parse_string(str)
+ @tokenizer = SgfTokenizer.new
+ @tokenizer.scan_evaluate(str)
+ do_parse
+ end
+
+ def next_token
+ @tokenizer.next_token
+ end
+
+---- footer
+end
Oops, something went wrong.

0 comments on commit 7051ce3

Please sign in to comment.