bchess 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +45 -0
- data/LICENSE.txt +21 -0
- data/README.md +65 -0
- data/Rakefile +6 -0
- data/bchess.gemspec +29 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/full_test.sh +1 -0
- data/lib/bchess.rb +33 -0
- data/lib/bchess/bishop.rb +17 -0
- data/lib/bchess/board.rb +131 -0
- data/lib/bchess/game.rb +21 -0
- data/lib/bchess/helpers/board_helpers.rb +43 -0
- data/lib/bchess/helpers/castle_helpers.rb +58 -0
- data/lib/bchess/helpers/en_passant_helpers.rb +22 -0
- data/lib/bchess/helpers/fen_helpers.rb +141 -0
- data/lib/bchess/helpers/field_between_helpers.rb +50 -0
- data/lib/bchess/helpers/validations.rb +16 -0
- data/lib/bchess/king.rb +30 -0
- data/lib/bchess/knight.rb +27 -0
- data/lib/bchess/pawn.rb +54 -0
- data/lib/bchess/piece.rb +133 -0
- data/lib/bchess/queen.rb +21 -0
- data/lib/bchess/rook.rb +25 -0
- data/lib/bchess/version.rb +3 -0
- data/lib/pgn/game.rb +18 -0
- data/lib/pgn/game_body.rb +111 -0
- data/lib/pgn/game_header.rb +55 -0
- data/lib/pgn/move_info_parser.rb +106 -0
- data/lib/pgn/parser.rb +42 -0
- data/lib/pgn/pgn_file.rb +16 -0
- data/lib/pgn/pgn_nodes.rb +84 -0
- data/lib/pgn/pgn_rules.treetop +127 -0
- data/test.sh +1 -0
- metadata +153 -0
data/lib/pgn/parser.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'treetop'
|
2
|
+
|
3
|
+
module Bchess
|
4
|
+
module PGN
|
5
|
+
class ParserException < Exception ;end
|
6
|
+
|
7
|
+
class Parser
|
8
|
+
attr_reader :input, :output, :error, :tree
|
9
|
+
|
10
|
+
Treetop.load('./lib/pgn/pgn_rules.treetop')
|
11
|
+
@@parser = SexpParser.new
|
12
|
+
|
13
|
+
def initialize(input)
|
14
|
+
@input = input
|
15
|
+
end
|
16
|
+
|
17
|
+
def parse
|
18
|
+
if input.kind_of?(String)
|
19
|
+
@tree = @@parser.parse(input)
|
20
|
+
elsif input.kind_of?(Bchess::PGN::PGNFile)
|
21
|
+
@tree = @@parser.parse(input.load_games)
|
22
|
+
end
|
23
|
+
|
24
|
+
if !tree
|
25
|
+
raise PGN::ParserException, "Parse error at offset: #{@@parser.index}"
|
26
|
+
end
|
27
|
+
|
28
|
+
sanitize_tree(tree)
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def sanitize_tree(root_node)
|
34
|
+
root_elements = root_node.elements
|
35
|
+
return unless root_elements
|
36
|
+
|
37
|
+
root_elements.delete_if{ |node| node.class.name == "Treetop::Runtime::SyntaxNode" }
|
38
|
+
root_elements.each{ |node| sanitize_tree(node) }
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/lib/pgn/pgn_file.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
module Bchess
|
2
|
+
module PGN
|
3
|
+
class PGNFile
|
4
|
+
attr_accessor :filepath, :games, :content
|
5
|
+
|
6
|
+
def initialize(filepath)
|
7
|
+
@filepath = filepath
|
8
|
+
end
|
9
|
+
|
10
|
+
def load_games
|
11
|
+
file = File.open(filepath, "rt")
|
12
|
+
@content = file.read
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
module Sexp
|
2
|
+
class PAllGames < Treetop::Runtime::SyntaxNode
|
3
|
+
end
|
4
|
+
|
5
|
+
class PGame < Treetop::Runtime::SyntaxNode
|
6
|
+
end
|
7
|
+
|
8
|
+
class PHeaderBody < Treetop::Runtime::SyntaxNode
|
9
|
+
end
|
10
|
+
|
11
|
+
class PHeader < Treetop::Runtime::SyntaxNode
|
12
|
+
def parse(value)
|
13
|
+
val = value.split(" ", 2)
|
14
|
+
|
15
|
+
[val[0].delete('['), val[1].delete('"]').rstrip]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class PAllMoves < Treetop::Runtime::SyntaxNode
|
20
|
+
end
|
21
|
+
|
22
|
+
class PAllMovesWithResult < Treetop::Runtime::SyntaxNode
|
23
|
+
end
|
24
|
+
|
25
|
+
class POneMove < Treetop::Runtime::SyntaxNode
|
26
|
+
end
|
27
|
+
|
28
|
+
class PMove < Treetop::Runtime::SyntaxNode
|
29
|
+
end
|
30
|
+
|
31
|
+
class PMoveNumber < Treetop::Runtime::SyntaxNode
|
32
|
+
end
|
33
|
+
|
34
|
+
class PComment < Treetop::Runtime::SyntaxNode
|
35
|
+
end
|
36
|
+
|
37
|
+
class PCommentWithBracket < Treetop::Runtime::SyntaxNode
|
38
|
+
end
|
39
|
+
|
40
|
+
class PVariation < Treetop::Runtime::SyntaxNode
|
41
|
+
end
|
42
|
+
|
43
|
+
class PResult < Treetop::Runtime::SyntaxNode
|
44
|
+
end
|
45
|
+
|
46
|
+
class PTakes < Treetop::Runtime::SyntaxNode
|
47
|
+
end
|
48
|
+
|
49
|
+
class PInteger < Treetop::Runtime::SyntaxNode
|
50
|
+
end
|
51
|
+
|
52
|
+
class PFloat < Treetop::Runtime::SyntaxNode
|
53
|
+
end
|
54
|
+
|
55
|
+
class PString < Treetop::Runtime::SyntaxNode
|
56
|
+
end
|
57
|
+
|
58
|
+
class PIdentifier < Treetop::Runtime::SyntaxNode
|
59
|
+
end
|
60
|
+
|
61
|
+
class PCastle < Treetop::Runtime::SyntaxNode
|
62
|
+
end
|
63
|
+
|
64
|
+
class PTime < Treetop::Runtime::SyntaxNode
|
65
|
+
end
|
66
|
+
|
67
|
+
class PTimeIdentifier < Treetop::Runtime::SyntaxNode
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
class Treetop::Runtime::SyntaxNode
|
72
|
+
def create_value_hash
|
73
|
+
hash = {}
|
74
|
+
|
75
|
+
self.elements.each do |element|
|
76
|
+
key = element.elements.first.text_value
|
77
|
+
value = element.elements.last.text_value
|
78
|
+
|
79
|
+
hash[key] = value.gsub('"','')
|
80
|
+
end
|
81
|
+
|
82
|
+
hash
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,127 @@
|
|
1
|
+
grammar Sexp
|
2
|
+
rule all_games
|
3
|
+
( game )* <PAllGames>
|
4
|
+
end
|
5
|
+
|
6
|
+
rule game
|
7
|
+
header_body new_line all_moves? new_line? result? multiple_line? <PGame>
|
8
|
+
end
|
9
|
+
|
10
|
+
## HEADER PART
|
11
|
+
|
12
|
+
rule header_body
|
13
|
+
(header)* <PHeaderBody>
|
14
|
+
end
|
15
|
+
|
16
|
+
rule header
|
17
|
+
"[" identifier space string "]" new_line <PHeader>
|
18
|
+
end
|
19
|
+
|
20
|
+
## BODY PART
|
21
|
+
|
22
|
+
rule all_moves
|
23
|
+
( move / comment_with_brackets / variation / castle / special_sign )* <PAllMovesWithResult>
|
24
|
+
end
|
25
|
+
|
26
|
+
rule move
|
27
|
+
( move_number? piecechar? chessline? takes? chesscolumn takes? chesscolumn? chessline promotion? check?) space? <PMove>
|
28
|
+
end
|
29
|
+
|
30
|
+
rule variation
|
31
|
+
"(" space? all_moves space? ")" space? <PVariation>
|
32
|
+
end
|
33
|
+
|
34
|
+
rule move_number
|
35
|
+
( integer ( '...' / '.' ) space? ) <PMoveNumber>
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
rule comment_with_brackets
|
40
|
+
"{" comment "}" space? <PCommentWithBracket>
|
41
|
+
end
|
42
|
+
|
43
|
+
rule comment
|
44
|
+
space? new_line? float? time? ( identifier space? )* <PComment>
|
45
|
+
end
|
46
|
+
|
47
|
+
rule time
|
48
|
+
"[" time_identifier "]" <PTime>
|
49
|
+
end
|
50
|
+
|
51
|
+
rule time_identifier
|
52
|
+
"%clk" space [0-9]+ ":" [0-9]* ":" [0-9]* <PTimeIdentifier>
|
53
|
+
end
|
54
|
+
|
55
|
+
rule move_with_number
|
56
|
+
integer ( '...' / '.' ) space? move <PMoveWithNumber>
|
57
|
+
end
|
58
|
+
|
59
|
+
rule takes
|
60
|
+
[x] / "ax" / "bx" / "cx" / "dx" / "ex" / "fx" / "gx" / "hx" <PTakes>
|
61
|
+
end
|
62
|
+
|
63
|
+
|
64
|
+
rule result
|
65
|
+
"1-0" / "0-1" / "1/2-1/2" / "1/2" / "*" <PResult>
|
66
|
+
end
|
67
|
+
|
68
|
+
## DATATYPES
|
69
|
+
|
70
|
+
rule integer
|
71
|
+
('+' / '-')? [0-9]+ <PInteger>
|
72
|
+
end
|
73
|
+
|
74
|
+
rule float
|
75
|
+
('+' / '-')? [0-9]+ (('.' [0-9]+) / ('e' [0-9]+)) <PFloat>
|
76
|
+
end
|
77
|
+
|
78
|
+
rule string
|
79
|
+
'"' ([^"\\] / "\\" . )* '"' <PString>
|
80
|
+
end
|
81
|
+
|
82
|
+
rule identifier
|
83
|
+
[a-zA-Z0-9\=\*\+\-\%\'\"\`\;\/\$\!\?\,\:\(\)\.\<\>] [a-zA-Z0-9_\=\*\+\-\%\'\"\`\;\/\$\!\?\,\:\(\)\.\<\>]* <PIdentifier>
|
84
|
+
end
|
85
|
+
|
86
|
+
rule space
|
87
|
+
[\s]+
|
88
|
+
end
|
89
|
+
|
90
|
+
rule new_line
|
91
|
+
[\n]
|
92
|
+
end
|
93
|
+
|
94
|
+
rule multiple_line
|
95
|
+
(new_line)*
|
96
|
+
end
|
97
|
+
|
98
|
+
## CHESS RULES
|
99
|
+
|
100
|
+
rule chesscolumn
|
101
|
+
[a-h]
|
102
|
+
end
|
103
|
+
|
104
|
+
rule chessline
|
105
|
+
[1-8]
|
106
|
+
end
|
107
|
+
|
108
|
+
rule piecechar
|
109
|
+
[A-Z]
|
110
|
+
end
|
111
|
+
|
112
|
+
rule check
|
113
|
+
"+" / "#"
|
114
|
+
end
|
115
|
+
|
116
|
+
rule castle
|
117
|
+
move_number? ( "O-O-O" / "O-O" ) check? space? <PCastle>
|
118
|
+
end
|
119
|
+
|
120
|
+
rule promotion
|
121
|
+
"="? [A-Z]
|
122
|
+
end
|
123
|
+
|
124
|
+
rule special_sign
|
125
|
+
"$" integer space?
|
126
|
+
end
|
127
|
+
end
|
data/test.sh
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rspec --tag ~slow spec/
|
metadata
ADDED
@@ -0,0 +1,153 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bchess
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Staszek Zawadzki
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-11-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: treetop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.6'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.6'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Parsing and validating a chess games and returning as chess game objects
|
84
|
+
email:
|
85
|
+
- s.zawadzki@visuality.pl
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".travis.yml"
|
93
|
+
- CODE_OF_CONDUCT.md
|
94
|
+
- Gemfile
|
95
|
+
- Gemfile.lock
|
96
|
+
- LICENSE.txt
|
97
|
+
- README.md
|
98
|
+
- Rakefile
|
99
|
+
- bchess.gemspec
|
100
|
+
- bin/console
|
101
|
+
- bin/setup
|
102
|
+
- full_test.sh
|
103
|
+
- lib/bchess.rb
|
104
|
+
- lib/bchess/bishop.rb
|
105
|
+
- lib/bchess/board.rb
|
106
|
+
- lib/bchess/game.rb
|
107
|
+
- lib/bchess/helpers/board_helpers.rb
|
108
|
+
- lib/bchess/helpers/castle_helpers.rb
|
109
|
+
- lib/bchess/helpers/en_passant_helpers.rb
|
110
|
+
- lib/bchess/helpers/fen_helpers.rb
|
111
|
+
- lib/bchess/helpers/field_between_helpers.rb
|
112
|
+
- lib/bchess/helpers/validations.rb
|
113
|
+
- lib/bchess/king.rb
|
114
|
+
- lib/bchess/knight.rb
|
115
|
+
- lib/bchess/pawn.rb
|
116
|
+
- lib/bchess/piece.rb
|
117
|
+
- lib/bchess/queen.rb
|
118
|
+
- lib/bchess/rook.rb
|
119
|
+
- lib/bchess/version.rb
|
120
|
+
- lib/pgn/game.rb
|
121
|
+
- lib/pgn/game_body.rb
|
122
|
+
- lib/pgn/game_header.rb
|
123
|
+
- lib/pgn/move_info_parser.rb
|
124
|
+
- lib/pgn/parser.rb
|
125
|
+
- lib/pgn/pgn_file.rb
|
126
|
+
- lib/pgn/pgn_nodes.rb
|
127
|
+
- lib/pgn/pgn_rules.treetop
|
128
|
+
- test.sh
|
129
|
+
homepage: ''
|
130
|
+
licenses:
|
131
|
+
- MIT
|
132
|
+
metadata: {}
|
133
|
+
post_install_message:
|
134
|
+
rdoc_options: []
|
135
|
+
require_paths:
|
136
|
+
- lib
|
137
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
138
|
+
requirements:
|
139
|
+
- - ">="
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
version: '0'
|
142
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
143
|
+
requirements:
|
144
|
+
- - ">="
|
145
|
+
- !ruby/object:Gem::Version
|
146
|
+
version: '0'
|
147
|
+
requirements: []
|
148
|
+
rubyforge_project:
|
149
|
+
rubygems_version: 2.6.14
|
150
|
+
signing_key:
|
151
|
+
specification_version: 4
|
152
|
+
summary: Parsing chess PGN games.
|
153
|
+
test_files: []
|