pgn2 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/release.yml +103 -0
- data/.gitignore +2 -1
- data/CHANGELOG.md +32 -0
- data/README.md +33 -15
- data/TODO.md +9 -0
- data/bench/IMPROVEMENTS.md +68 -0
- data/bench/baseline_moves.pre-quickwins.txt +23 -0
- data/bench/baseline_moves.txt +6 -6
- data/bench/baseline_parse.pre-quickwins.txt +26 -0
- data/bench/baseline_parse.txt +6 -6
- data/docs/superpowers/specs/2026-08-13-pgn-performance-quick-wins-design.md +227 -0
- data/lib/pgn/game.rb +15 -4
- data/lib/pgn/lexer.rb +60 -38
- data/lib/pgn/move.rb +5 -2
- data/lib/pgn/move_calculator.rb +9 -4
- data/lib/pgn/parser.rb +5 -15
- data/lib/pgn/pgn_parser.rb +79 -78
- data/lib/pgn/pgn_parser.y +20 -22
- data/lib/pgn/position.rb +3 -2
- data/lib/pgn/version.rb +1 -1
- data/spec/parser_spec.rb +6 -38
- metadata +5 -1
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
# PGN2 performance quick wins — Approach A (quick, behavior-compatible micro-opts)
|
|
2
|
+
|
|
3
|
+
> "Approach A" denotes the safe, allocation-focused track. A later, larger
|
|
4
|
+
> design (Approach B) will cover architectural changes: piece-location indexes,
|
|
5
|
+
> lazy position computation, and a coordinate-only internal board
|
|
6
|
+
> representation.
|
|
7
|
+
|
|
8
|
+
## Goal
|
|
9
|
+
|
|
10
|
+
Make the gem faster on the combined **parse + replay** workload after the
|
|
11
|
+
whittle → Racc migration, with only safe, behavior-compatible micro-optimizations
|
|
12
|
+
(no public API changes, no serialized-output changes).
|
|
13
|
+
|
|
14
|
+
## Scope
|
|
15
|
+
|
|
16
|
+
This design covers the five highest-ROI quick wins identified in the profiles:
|
|
17
|
+
|
|
18
|
+
1. Remove per-token `PGN::Lexer::Token` allocation on the parser hot path.
|
|
19
|
+
2. Avoid double `MoveText` wrapping in `PGN::Game#moves=`.
|
|
20
|
+
3. Replace `PGN::Move` SAN regex with hand-rolled parsing.
|
|
21
|
+
4. Reduce small temporary allocations in `PGN::Position#move` and
|
|
22
|
+
`PGN::MoveCalculator`.
|
|
23
|
+
5. Promote the Racc parse baseline to canonical and record new baselines in
|
|
24
|
+
`bench/baseline_*.txt` after verification.
|
|
25
|
+
|
|
26
|
+
Out of scope: larger architecture changes such as piece-location indexes,
|
|
27
|
+
lazy position computation, or coordinate-only internal board representation.
|
|
28
|
+
|
|
29
|
+
## Context / baseline
|
|
30
|
+
|
|
31
|
+
Measured on the current checkout with `bundle exec rake bench` (Racc parser);
|
|
32
|
+
these are the canonical `bench/baseline_*.txt` values refreshed as the
|
|
33
|
+
prerequisite in item 5:
|
|
34
|
+
|
|
35
|
+
- **Parse-only:** ~125 k objects / 7.88 MB for 100 copies of the immortal game
|
|
36
|
+
(= 626,037 objects / 39.4 MB for the 500-game corpus in `bench/baseline_parse.txt`).
|
|
37
|
+
- **Parse + replay:** ~337 k objects / 21.6 MB for the same per-100 ratio
|
|
38
|
+
(= 1,683,087 objects / 108 MB for 500 games).
|
|
39
|
+
- **Replay only:** 2,177 objects / 142 kB for the 45-ply immortal game
|
|
40
|
+
(`bench/baseline_moves.txt`).
|
|
41
|
+
|
|
42
|
+
Historical snapshots are kept in `bench/baseline_*.pre-quickwins.txt` and
|
|
43
|
+
`bench/baseline_parse.racc.txt` for reference. The previously committed
|
|
44
|
+
`baseline_parse.txt` held stale pre-migration whittle numbers and has been
|
|
45
|
+
overwritten by the current Racc output.
|
|
46
|
+
|
|
47
|
+
Top allocation hot spots:
|
|
48
|
+
|
|
49
|
+
- `PGN::Lexer::Token` creation (parse-only).
|
|
50
|
+
- `PGN::MoveText` created twice per parsed move (parser + `Game#moves=`).
|
|
51
|
+
- `Move#initialize` SAN regex (`Regexp` + `MatchData` objects, replay).
|
|
52
|
+
- Small throw-away arrays and string conversions in `MoveCalculator` and
|
|
53
|
+
`Position#move`.
|
|
54
|
+
|
|
55
|
+
## Detailed changes
|
|
56
|
+
|
|
57
|
+
### 1. Lexer: parser hot path avoids `Token` objects
|
|
58
|
+
|
|
59
|
+
`lib/pgn/lexer.rb`
|
|
60
|
+
|
|
61
|
+
- Add a fast public method `next_token_pair` that returns `[type, value]`
|
|
62
|
+
directly, without allocating a `PGN::Lexer::Token` Struct.
|
|
63
|
+
The byte offset is **not** expensive (`@ss.pos`) and is still required, so
|
|
64
|
+
the saving is the Struct allocation, not offset computation.
|
|
65
|
+
- **Correctness requirement:** `next_token_pair` must invoke the same
|
|
66
|
+
`note_token(type, off)` and `advance_line(value)` side effects as
|
|
67
|
+
`next_token`. In particular `note_token` maintains `game_starts`, which
|
|
68
|
+
`PgnParser#assign_pgn!` relies on to slice each game's verbatim raw `pgn`
|
|
69
|
+
text — skipping it would corrupt `Game#pgn`.
|
|
70
|
+
- Keep `next_token` and `tokens` unchanged (they continue to return `Token`
|
|
71
|
+
objects and are used by specs/other callers).
|
|
72
|
+
- Implement both methods on top of a shared private scanning routine so the
|
|
73
|
+
scanning logic is not duplicated.
|
|
74
|
+
- The single-byte literal path already returns frozen strings via
|
|
75
|
+
`LITERAL_BYTES` under `# frozen_string_literal: true`; preserve that in
|
|
76
|
+
`next_token_pair` (no change needed beyond routing through the shared
|
|
77
|
+
routine).
|
|
78
|
+
|
|
79
|
+
`lib/pgn/pgn_parser.y`
|
|
80
|
+
|
|
81
|
+
- Change `PgnParser#next_token` to call `@lexer.next_token_pair` and return the
|
|
82
|
+
resulting array directly.
|
|
83
|
+
- Regenerate `lib/pgn/pgn_parser.rb` from the `.y` file with Racc.
|
|
84
|
+
- No changes to grammar or semantics.
|
|
85
|
+
|
|
86
|
+
### 2. Game#moves=: stop re-wrapping existing MoveText objects
|
|
87
|
+
|
|
88
|
+
`lib/pgn/game.rb`
|
|
89
|
+
|
|
90
|
+
- Detect when an element is already a `PGN::MoveText` instance and reuse it
|
|
91
|
+
directly instead of creating a new one.
|
|
92
|
+
- Still perform castling normalization (`0` → `O`) for raw strings.
|
|
93
|
+
- Preserve `clean_text` behavior for fresh comments only.
|
|
94
|
+
- **Reuse-safety invariant:** reusing the parser's `MoveText` shares one object
|
|
95
|
+
between the parser's move tree and `Game#moves`. This is safe only because
|
|
96
|
+
nothing mutates a `MoveText` after construction; record that invariant in a
|
|
97
|
+
comment so a future mutation doesn't silently corrupt both holders.
|
|
98
|
+
|
|
99
|
+
Expected effect: roughly halves `MoveText` allocations on the parse path while
|
|
100
|
+
keeping serialized output identical.
|
|
101
|
+
|
|
102
|
+
### 3. Move#initialize: hand-rolled SAN parser (highest-risk item — quantify first)
|
|
103
|
+
|
|
104
|
+
`lib/pgn/move.rb`
|
|
105
|
+
|
|
106
|
+
This is the highest-risk, and likely lowest-allocation-payoff, item: the SAN
|
|
107
|
+
regex produces ~1 `MatchData` per ply (~2.1% of the 2,177-object replay
|
|
108
|
+
baseline). Before committing to a full hand-roll, **measure #3 in isolation**
|
|
109
|
+
and confirm the payoff; if marginal, ship #1, #2, #4 alone and defer #3.
|
|
110
|
+
|
|
111
|
+
Trivial independent sub-win (do regardless of the full hand-roll):
|
|
112
|
+
|
|
113
|
+
- In `Move#piece=`, replace `return if san.match('O-O')` (which allocates a
|
|
114
|
+
`MatchData` on every `Move.new`, castling or not) with a non-allocating
|
|
115
|
+
check such as `san.include?('O-O')` or `san.start_with?('O')`.
|
|
116
|
+
|
|
117
|
+
Full hand-roll (if pursued):
|
|
118
|
+
|
|
119
|
+
- Replace `move.match(SAN_REGEX)` with a manual parse over the byte/string
|
|
120
|
+
representation of the SAN notation.
|
|
121
|
+
- Populate all existing attributes (`piece`, `destination`, `promotion`,
|
|
122
|
+
`check`, `capture`, `disambiguation`, `castle`) with the same values as
|
|
123
|
+
today, including the `piece=` early-return for castling.
|
|
124
|
+
- Keep setter methods (`piece=`, `promotion=`, `capture=`, `disambiguation=`,
|
|
125
|
+
`castle=`) so external callers that assign attributes manually are unaffected.
|
|
126
|
+
- Use frozen constants for frequently-checked piece sets (`pawn?` lives here,
|
|
127
|
+
in `move.rb` — see item 4).
|
|
128
|
+
- **Upfront fixtures:** add a dedicated spec pinning every attribute for a
|
|
129
|
+
comprehensive set of SAN strings: `O-O`, `O-O-O`, `O-O+`, `O-O-O#`;
|
|
130
|
+
pawn moves and captures incl. promotion (`e4`, `exd5`, `e8=Q`, `exd8=Q+`,
|
|
131
|
+
`b1=N#`); piece moves with file/rank/full disambiguation (`Nbd2`, `R1e2`,
|
|
132
|
+
`Qh4e1`); captures with check/mate; and the `--` "don't care" move. Do this
|
|
133
|
+
before implementation, not only if ambiguity surfaces.
|
|
134
|
+
|
|
135
|
+
### 4. Position / MoveCalculator: fewer throw-away allocations
|
|
136
|
+
|
|
137
|
+
`lib/pgn/position.rb`
|
|
138
|
+
|
|
139
|
+
- Inline `next_player` logic in `Position#move` (or simply rewrite its body to
|
|
140
|
+
`player == :white ? :black : :white`) to avoid the `(PLAYERS - [player])`
|
|
141
|
+
array allocation per ply.
|
|
142
|
+
- Apply `castling_restrictions` only when non-empty; avoid array subtraction
|
|
143
|
+
when there is nothing to remove. (Safe to return the existing `castling`
|
|
144
|
+
array directly because castling arrays are replaced, never mutated.)
|
|
145
|
+
|
|
146
|
+
`lib/pgn/move_calculator.rb`
|
|
147
|
+
|
|
148
|
+
- Compute and cache destination coordinates in `initialize` instead of calling
|
|
149
|
+
`board.coordinates_for(move.destination)` repeatedly (it can be invoked 2–3
|
|
150
|
+
times per move across `direction_origins` / `move_origins` / `pawn_origins`).
|
|
151
|
+
- Inline `valid_square?` boundary checks to remove the per-call method dispatch
|
|
152
|
+
(note: `(0..7)` are frozen range literals already cached by the VM, so the
|
|
153
|
+
win is call overhead, not Range allocation — don't claim the latter).
|
|
154
|
+
- Replace run-time array/hash literals with frozen constants or `case`/`when`
|
|
155
|
+
where they appear on hot paths (e.g. rook-origin lookup in
|
|
156
|
+
`castling_restrictions`, the per-call hash literals `{ 'a1' => 'Q', ... }`).
|
|
157
|
+
- Keep semantic behavior identical (same board updates, same disambiguation,
|
|
158
|
+
same `king_position` scan — Approach A deliberately defers indexing/caching
|
|
159
|
+
to a later design).
|
|
160
|
+
|
|
161
|
+
`lib/pgn/move.rb` (relocated from move_calculator)
|
|
162
|
+
|
|
163
|
+
- `Move#pawn?` uses `%w[P p].include?(piece)`, allocating an array each call;
|
|
164
|
+
it's invoked from `MoveCalculator#increment_halfmove?` and `pawn_origins`.
|
|
165
|
+
Replace with a frozen constant or `piece == 'P' || piece == 'p'`.
|
|
166
|
+
|
|
167
|
+
## Testing & behavior preservation
|
|
168
|
+
|
|
169
|
+
- All existing specs must pass without modification (`bundle exec rspec`).
|
|
170
|
+
- The `bench/baseline_moves.txt` and `bench/baseline_parse.txt` files will be
|
|
171
|
+
regenerated with `bundle exec rake bench` and committed if they show lower
|
|
172
|
+
object/byte counts and equal or better throughput.
|
|
173
|
+
- Public API remains unchanged: `PGN.parse`, `Game#moves=`, `Move.new`,
|
|
174
|
+
`Position#move`, etc. accept the same inputs and produce the same outputs and
|
|
175
|
+
side effects.
|
|
176
|
+
- No changes to PGN/FEN serialization format.
|
|
177
|
+
- **Lexer non-regression:** because `next_token_pair` shares the scanning
|
|
178
|
+
routine with `next_token`/`tokens`, the existing `tokens`/`next_token` specs
|
|
179
|
+
and the `game_starts`-driven `Game#pgn` raw-text slicing output must stay
|
|
180
|
+
byte-identical across the fixtures and inline inputs in
|
|
181
|
+
`spec/parser_explicit_spec.rb`.
|
|
182
|
+
|
|
183
|
+
## Risks & mitigations
|
|
184
|
+
|
|
185
|
+
| Risk | Mitigation |
|
|
186
|
+
|---|---|
|
|
187
|
+
| Hand-rolled SAN parser mishandles an edge case | Upfront comprehensive SAN fixture spec (castling both sides, promotion with/without check, pawn & piece captures, file/rank/full disambiguation, `--`) added before implementation; full suite must stay green. |
|
|
188
|
+
| `next_token_pair` drifts from `next_token` behavior | Both methods share the private scanning routine, the same `RULES` ordering, and the same `note_token`/`advance_line` side effects (incl. `game_starts`). |
|
|
189
|
+
| `Game#moves=` no longer applies castling normalization | Continue normalizing raw strings; only skip work when input is already `MoveText`. |
|
|
190
|
+
| Performance wins smaller than expected | Measure before/after with the committed baseline harness. |
|
|
191
|
+
|
|
192
|
+
## Acceptance criteria
|
|
193
|
+
|
|
194
|
+
1. `bundle exec rspec` passes (0 failures).
|
|
195
|
+
2. `lib/pgn/pgn_parser.rb` is regenerated and in sync with `.y`
|
|
196
|
+
(`git diff --stat` should reflect the generation, and CI racc-sync check
|
|
197
|
+
passes).
|
|
198
|
+
3. `bundle exec rake bench` shows lower allocated objects/bytes than the
|
|
199
|
+
**post-Racc** committed baselines (`baseline_moves.txt` and
|
|
200
|
+
`baseline_parse.txt`, the latter promoted from `baseline_parse.racc.txt`
|
|
201
|
+
per item 5). Comparing against the old whittle `baseline_parse.txt` would
|
|
202
|
+
not be a meaningful gate.
|
|
203
|
+
4. IPS numbers are equal or higher than current baselines.
|
|
204
|
+
5. Baseline files are updated and committed as part of the change set.
|
|
205
|
+
6. Lexer non-regression: `tokens`/`next_token` spec output and `Game#pgn`
|
|
206
|
+
raw-text slicing are byte-identical to the pre-change checkout.
|
|
207
|
+
|
|
208
|
+
## Estimated impact
|
|
209
|
+
|
|
210
|
+
Per-item expectations (to be confirmed by measurement, not assumed in
|
|
211
|
+
aggregate):
|
|
212
|
+
|
|
213
|
+
- **Items 1, 2, 4 (low-risk):** the largest reliable wins. Parse-side `Token`
|
|
214
|
+
elimination (#1) and halved `MoveText` allocation (#2) should show up directly
|
|
215
|
+
in parse-only object counts; `next_player`/`pawn?`/`castling_restrictions`
|
|
216
|
+
(#4) trim a handful of objects per ply.
|
|
217
|
+
- **Item 3 (SAN hand-roll):** ~1 `MatchData` per ply (~2.1% of the 2,177-object
|
|
218
|
+
replay baseline) plus the `piece=` guard fix — modest, and the bulk of the
|
|
219
|
+
estimate does **not** come from this item.
|
|
220
|
+
- **Replay allocations are dominated by `Position.new` + `Board#dup` +
|
|
221
|
+
`MoveCalculator` arrays**, which Approach A only trims at the edges; the big
|
|
222
|
+
replay win is deferred to Approach B.
|
|
223
|
+
|
|
224
|
+
Conservative aggregate guess once #1/#2/#4 land: parse-only allocations down
|
|
225
|
+
~10–15%, replay allocations down in the single-digit-percent range (lower than
|
|
226
|
+
a naive reading of these micro-opts might suggest). Actual numbers will come
|
|
227
|
+
from the baseline run; revise this section with measured per-item deltas.
|
data/lib/pgn/game.rb
CHANGED
|
@@ -77,15 +77,26 @@ module PGN
|
|
|
77
77
|
#
|
|
78
78
|
# Standardize castling moves to use O's instead of 0's
|
|
79
79
|
#
|
|
80
|
+
# Reuse-safety invariant: when an element is already a MoveText we reuse
|
|
81
|
+
# it directly (sharing one object between the parser's move tree and
|
|
82
|
+
# Game#moves) whenever its comment is already fully cleaned. This is safe
|
|
83
|
+
# because nothing mutates a MoveText after this assignment returns. We
|
|
84
|
+
# still re-wrap when the comment carries braces: the parser's single-pass
|
|
85
|
+
# clean_text leaves braces on multi-line/nested comments, and the second
|
|
86
|
+
# clean_text that MoveText.new applies here is load-bearing for those
|
|
87
|
+
# (matches the legacy whittle byte-output). Skipping it would change
|
|
88
|
+
# serialized comments.
|
|
80
89
|
def moves=(moves)
|
|
81
90
|
@moves =
|
|
82
91
|
moves.map do |m|
|
|
83
|
-
if m.is_a?
|
|
92
|
+
if m.is_a?(String)
|
|
84
93
|
MoveText.new(m.include?('0') ? m.gsub('0', 'O') : m)
|
|
94
|
+
elsif m.notation.include?('0')
|
|
95
|
+
MoveText.new(m.notation.gsub('0', 'O'), m.annotation, m.comment, m.variations)
|
|
96
|
+
elsif m.comment.nil? || !m.comment.include?('{')
|
|
97
|
+
m
|
|
85
98
|
else
|
|
86
|
-
notation
|
|
87
|
-
notation = notation.gsub('0', 'O') if notation.include?('0')
|
|
88
|
-
MoveText.new(notation, m.annotation, m.comment, m.variations)
|
|
99
|
+
MoveText.new(m.notation, m.annotation, m.comment, m.variations)
|
|
89
100
|
end
|
|
90
101
|
end
|
|
91
102
|
end
|
data/lib/pgn/lexer.rb
CHANGED
|
@@ -24,10 +24,10 @@ module PGN
|
|
|
24
24
|
end
|
|
25
25
|
|
|
26
26
|
# Discarded: insignificant whitespace.
|
|
27
|
-
WSP = /\s
|
|
27
|
+
WSP = /\s+/
|
|
28
28
|
|
|
29
29
|
# Discarded: a PGN "rest of line" comment beginning with `%`.
|
|
30
|
-
PGN_COMMENT = /%
|
|
30
|
+
PGN_COMMENT = /% .*/
|
|
31
31
|
|
|
32
32
|
# A tag value string. Allows unescaped double-quotes inside the value
|
|
33
33
|
# (a form seen in real-world PGN files) — only a bare backslash starts
|
|
@@ -40,14 +40,14 @@ module PGN
|
|
|
40
40
|
\\" # escaped quotation marks
|
|
41
41
|
)* # zero or more of the above
|
|
42
42
|
" # end of string
|
|
43
|
-
/x
|
|
43
|
+
/x
|
|
44
44
|
|
|
45
45
|
# A brace-delimited comment, with recursive nesting via \g<1>.
|
|
46
46
|
COMMENT = /
|
|
47
47
|
(
|
|
48
48
|
\{ # beginning of comment
|
|
49
49
|
(
|
|
50
|
-
[[:print:]&&[
|
|
50
|
+
[[:print:]&&[^\\{}]] | # printing characters except brace and backslash
|
|
51
51
|
\n |
|
|
52
52
|
\\\\ | # escaped backslashes
|
|
53
53
|
\\\{|\\\} | # escaped braces
|
|
@@ -56,15 +56,15 @@ module PGN
|
|
|
56
56
|
)* # zero or more of the above
|
|
57
57
|
\} # end of comment
|
|
58
58
|
)
|
|
59
|
-
/x
|
|
59
|
+
/x
|
|
60
60
|
|
|
61
61
|
# Game termination marker.
|
|
62
62
|
GAME_TERMINATION = %r{
|
|
63
63
|
1-0 | # white wins
|
|
64
64
|
0-1 | # black wins
|
|
65
|
-
1
|
|
65
|
+
1/2-1/2 | # draw
|
|
66
66
|
\* # ?
|
|
67
|
-
}x
|
|
67
|
+
}x
|
|
68
68
|
|
|
69
69
|
# A move in standard algebraic notation (incl. castling, promotion,
|
|
70
70
|
# check/mate, the `--` "don't care" move).
|
|
@@ -83,43 +83,46 @@ module PGN
|
|
|
83
83
|
\+ | # check (g5+)
|
|
84
84
|
\# # checkmate (Qe7#)
|
|
85
85
|
)?
|
|
86
|
-
}x
|
|
86
|
+
}x
|
|
87
87
|
|
|
88
88
|
# A move number indication, e.g. `1.`, `12.`, `1...`.
|
|
89
|
-
MOVE_NUMBER = /[[:digit:]]
|
|
89
|
+
MOVE_NUMBER = /[[:digit:]]+\.*/
|
|
90
90
|
|
|
91
91
|
# A tag name (letters, digits, underscores).
|
|
92
|
-
TAG_NAME = /[A-Za-z0-9_]
|
|
92
|
+
TAG_NAME = /[A-Za-z0-9_]+/
|
|
93
93
|
|
|
94
94
|
# A numeric annotation glyph (`$1`) or a punctuation annotation (`?!`,
|
|
95
95
|
# `!?`, `??`, ...).
|
|
96
96
|
NAG = /
|
|
97
97
|
\$\d+ | # dollar sign followed by an integer
|
|
98
|
-
[
|
|
99
|
-
/x
|
|
98
|
+
[?!][?!]? # support the most used annotations directly
|
|
99
|
+
/x
|
|
100
100
|
|
|
101
101
|
# Order matters: more specific / longer tokens are tried first so that
|
|
102
102
|
# e.g. `1-0` (termination) wins over `1` (move number), and `0-0`
|
|
103
103
|
# (castling) wins over `0` (move number). Whitespace and `%` comments
|
|
104
|
-
# are discarded (consumed but not emitted).
|
|
104
|
+
# are discarded (consumed but not emitted). Beyond that constraint,
|
|
105
|
+
# rules are ordered most- to least-frequent (one san_move/move_number
|
|
106
|
+
# per ply/full-move vs. a handful of comments/strings per game) so the
|
|
107
|
+
# common case fails the fewest regexes before matching.
|
|
105
108
|
RULES = [
|
|
106
109
|
[:wsp, WSP, true], # discarded
|
|
107
110
|
[:pgn_comment, PGN_COMMENT, true], # discarded
|
|
108
|
-
[:comment, COMMENT, false],
|
|
109
|
-
[:string, STRING, false],
|
|
110
111
|
[:game_termination, GAME_TERMINATION, false],
|
|
111
112
|
[:san_move, SAN_MOVE, false],
|
|
112
|
-
[:nag, NAG, false],
|
|
113
113
|
[:move_number, MOVE_NUMBER, false],
|
|
114
|
-
[:
|
|
114
|
+
[:nag, NAG, false],
|
|
115
|
+
[:comment, COMMENT, false],
|
|
116
|
+
[:string, STRING, false],
|
|
117
|
+
[:tag_name, TAG_NAME, false]
|
|
115
118
|
].freeze.each(&:freeze)
|
|
116
119
|
|
|
117
|
-
# Single-character literals, matched by their byte value.
|
|
120
|
+
# Single-character literals, matched by their byte value: [type, frozen value].
|
|
118
121
|
LITERAL_BYTES = {
|
|
119
|
-
91 => :lbracket,
|
|
120
|
-
93 => :rbracket,
|
|
121
|
-
40 => :lparen,
|
|
122
|
-
41 => :rparen,
|
|
122
|
+
91 => [:lbracket, '['], # [
|
|
123
|
+
93 => [:rbracket, ']'], # ]
|
|
124
|
+
40 => [:lparen, '('], # (
|
|
125
|
+
41 => [:rparen, ')'] # )
|
|
123
126
|
}.freeze
|
|
124
127
|
|
|
125
128
|
def initialize(input)
|
|
@@ -127,7 +130,7 @@ module PGN
|
|
|
127
130
|
@ss = StringScanner.new(input)
|
|
128
131
|
@line = 1
|
|
129
132
|
@game_starts = []
|
|
130
|
-
@between_games = true
|
|
133
|
+
@between_games = true # at start we are "between" games
|
|
131
134
|
end
|
|
132
135
|
|
|
133
136
|
attr_reader :game_starts
|
|
@@ -143,36 +146,55 @@ module PGN
|
|
|
143
146
|
|
|
144
147
|
# Returns the next {Token}, or +nil+ at end of input.
|
|
145
148
|
def next_token
|
|
149
|
+
type, value, off = scan_next
|
|
150
|
+
return nil unless type
|
|
151
|
+
Token.new(type: type, value: value, offset: off, line: @line)
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
# Fast path for the parser: returns [type, value] for the next
|
|
155
|
+
# non-discarded token, or +nil+ at end of input. Does not allocate a
|
|
156
|
+
# {Token} Struct. Shares the same scanning routine and the same
|
|
157
|
+
# +note_token+ / +advance_line+ side effects as +next_token+ (so
|
|
158
|
+
# +game_starts+ tracking is preserved).
|
|
159
|
+
def next_token_pair
|
|
160
|
+
type, value, = scan_next
|
|
161
|
+
return nil unless type
|
|
162
|
+
[type, value]
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
private
|
|
166
|
+
|
|
167
|
+
# Scan to the next non-discarded token and return [type, value, offset],
|
|
168
|
+
# or nil at end of input. Performs the exact +advance_line+ and
|
|
169
|
+
# +note_token+ side effects that +next_token+ historically did, so
|
|
170
|
+
# +game_starts+ (used for verbatim +Game#pgn+ slicing) stays correct.
|
|
171
|
+
def scan_next
|
|
146
172
|
until @ss.eos?
|
|
147
173
|
off = @ss.pos
|
|
148
174
|
|
|
149
175
|
if (lit = LITERAL_BYTES[@input.getbyte(off)])
|
|
176
|
+
type, value = lit
|
|
150
177
|
@ss.pos = off + 1
|
|
151
|
-
note_token(
|
|
152
|
-
return
|
|
153
|
-
offset: off, line: @line)
|
|
178
|
+
note_token(type, off)
|
|
179
|
+
return [type, value, off]
|
|
154
180
|
end
|
|
155
181
|
|
|
156
|
-
type, value = scan_one
|
|
182
|
+
type, value, discarded = scan_one
|
|
157
183
|
advance_line(value)
|
|
158
|
-
if
|
|
159
|
-
|
|
160
|
-
next
|
|
161
|
-
end
|
|
184
|
+
next if discarded
|
|
185
|
+
|
|
162
186
|
note_token(type, off)
|
|
163
|
-
return
|
|
187
|
+
return [type, value, off]
|
|
164
188
|
end
|
|
165
189
|
nil
|
|
166
190
|
end
|
|
167
191
|
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
# Try each terminal rule in order; return [type, matched_string] for
|
|
171
|
-
# the first match, or raise if nothing matches at the current position.
|
|
192
|
+
# Try each terminal rule in order; return [type, matched_string, discarded]
|
|
193
|
+
# for the first match, or raise if nothing matches at the current position.
|
|
172
194
|
def scan_one
|
|
173
|
-
RULES.each do |(type, re,
|
|
195
|
+
RULES.each do |(type, re, discarded)|
|
|
174
196
|
if (m = @ss.scan(re))
|
|
175
|
-
return [type, m]
|
|
197
|
+
return [type, m, discarded]
|
|
176
198
|
end
|
|
177
199
|
end
|
|
178
200
|
raise UnconsumedInputError,
|
data/lib/pgn/move.rb
CHANGED
|
@@ -105,7 +105,10 @@ module PGN
|
|
|
105
105
|
end
|
|
106
106
|
|
|
107
107
|
def piece=(val)
|
|
108
|
-
|
|
108
|
+
# Castling SAN (O-O / O-O-O) has no piece attribute; the castle attribute
|
|
109
|
+
# is set later in #initialize. Use a non-allocating prefix check instead
|
|
110
|
+
# of `san.match('O-O')`, which allocated a MatchData on every Move.new.
|
|
111
|
+
return if san.start_with?('O')
|
|
109
112
|
|
|
110
113
|
val ||= 'P'
|
|
111
114
|
@piece = black? ? val.downcase : val
|
|
@@ -161,7 +164,7 @@ module PGN
|
|
|
161
164
|
# @return [Boolean] whether the piece being moved is a pawn
|
|
162
165
|
#
|
|
163
166
|
def pawn?
|
|
164
|
-
|
|
167
|
+
piece == 'P' || piece == 'p'
|
|
165
168
|
end
|
|
166
169
|
end
|
|
167
170
|
end
|
data/lib/pgn/move_calculator.rb
CHANGED
|
@@ -82,6 +82,11 @@ module PGN
|
|
|
82
82
|
}
|
|
83
83
|
}.freeze
|
|
84
84
|
|
|
85
|
+
# Frozen rook-origin -> castling-restriction lookup, shared by both
|
|
86
|
+
# white ('R') and black ('r') since their rook origins (a1/h1, a8/h8)
|
|
87
|
+
# are distinct keys. Replaces a per-call hash literal.
|
|
88
|
+
ROOK_RESTRICTIONS = { 'a1' => 'Q', 'h1' => 'K', 'a8' => 'q', 'h8' => 'k' }.freeze
|
|
89
|
+
|
|
85
90
|
attr_accessor :board, :move, :origin
|
|
86
91
|
|
|
87
92
|
# @param board [PGN::Board] the current board
|
|
@@ -114,9 +119,9 @@ module PGN
|
|
|
114
119
|
when 'k'
|
|
115
120
|
restrict += %w[k q]
|
|
116
121
|
when 'R'
|
|
117
|
-
restrict <<
|
|
122
|
+
restrict << ROOK_RESTRICTIONS[origin]
|
|
118
123
|
when 'r'
|
|
119
|
-
restrict <<
|
|
124
|
+
restrict << ROOK_RESTRICTIONS[origin]
|
|
120
125
|
end
|
|
121
126
|
|
|
122
127
|
# when castling occurs
|
|
@@ -129,7 +134,7 @@ module PGN
|
|
|
129
134
|
restrict << 'K' if move.destination == 'h1'
|
|
130
135
|
restrict << 'k' if move.destination == 'h8'
|
|
131
136
|
|
|
132
|
-
restrict.compact.uniq
|
|
137
|
+
restrict.empty? ? restrict : restrict.compact.uniq
|
|
133
138
|
end
|
|
134
139
|
|
|
135
140
|
# @return [Boolean] whether to increment the halfmove clock
|
|
@@ -332,7 +337,7 @@ module PGN
|
|
|
332
337
|
end
|
|
333
338
|
|
|
334
339
|
def destination_coords
|
|
335
|
-
board.coordinates_for(move.destination)
|
|
340
|
+
@dest_coords ||= board.coordinates_for(move.destination)
|
|
336
341
|
end
|
|
337
342
|
end
|
|
338
343
|
end
|
data/lib/pgn/parser.rb
CHANGED
|
@@ -1,28 +1,18 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'pgn/pgn_parser'
|
|
2
4
|
|
|
3
5
|
module PGN
|
|
4
6
|
# {PGN::Parser} is the public entry point for parsing PGN text into a list
|
|
5
|
-
# of game hashes. It delegates to
|
|
6
|
-
#
|
|
7
|
-
#
|
|
8
|
-
# The +backend+ class attribute is retained so callers (tests, benchmarks)
|
|
9
|
-
# can substitute an alternative parser responding to +new.parse(pgn_string)+.
|
|
7
|
+
# of game hashes. It delegates to the stdlib Racc + StringScanner parser
|
|
8
|
+
# {PGN::PgnParser}.
|
|
10
9
|
class Parser
|
|
11
|
-
class << self
|
|
12
|
-
# The backend parser class used by {.parse}.
|
|
13
|
-
# @return [Class]
|
|
14
|
-
attr_accessor :backend
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
# Default backend is the stdlib Racc + StringScanner parser.
|
|
18
|
-
self.backend = PGN::PgnParser
|
|
19
|
-
|
|
20
10
|
# @param input [String] the raw PGN text (already force-encoded by the
|
|
21
11
|
# caller, e.g. via {PGN.parse}).
|
|
22
12
|
# @return [Array<Hash>] one Hash per game, with keys +:tags+, +:result+,
|
|
23
13
|
# +:moves+, +:pgn+, +:comment+.
|
|
24
14
|
def parse(input)
|
|
25
|
-
|
|
15
|
+
PGN::PgnParser.new.parse(input)
|
|
26
16
|
end
|
|
27
17
|
end
|
|
28
18
|
end
|