Skip to content

Add Position.parseLan for Long Algebraic Notation - #62

Open
gd-evil wants to merge 6 commits into
lichess-org:mainfrom
gd-evil:feature/LanParsing
Open

gd-evil wants to merge 6 commits into
lichess-org:mainfrom
gd-evil:feature/LanParsing

Conversation

@gd-evil

@gd-evil gd-evil commented Oct 30, 2026

Copy link
Copy Markdown

Refs lichess-org/mobile#3855

Summary

Adds Position.parseLan(String), which parses a move in Long Algebraic
Notation (LAN) and returns a legal Move, or null if the input is
invalid or illegal in the position. It is a separate method from
parseSan, so SAN behavior is unchanged.

Accepted input

Input Result
e2-e4, Ng1-f3 normal moves, with or without the piece letter
Nf3xe5, d5xc6 captures, including en passant
h7-h8=Q, h7-h8Q promotions, with or without =
O-O, 0-0, O-O-O, 0-0-0 castling
e1-g1 written-out castling, normalized to king-to-rook (e1h1)
N@f3, @e4 Crazyhouse drops

Validation rules

  • Origin square, destination square and separator (- or x) are required,
    so UCI-style input such as e2e4 returns null.
  • If a piece letter is given, it must match the piece on the origin square.
  • The separator must agree with the position: x only for captures
    (including en passant), - otherwise.
  • A pawn reaching the last rank must specify a promotion.
  • Trailing +, #, !, ? and surrounding whitespace are ignored.
  • Castling and drops are delegated to parseSan, and the result goes through
    isLegal, so pins, checks and castling rights are enforced.

Related fix: PGN draw result

While reviewing the PGN tokenizer I found that the regex was ported from a
JavaScript regex literal and kept its closing /. In Dart the draw token
therefore only matched 1/2-1/2/, so a normal 1/2-1/2 in the movetext was
skipped and headers['Result'] was not updated. This PR removes the trailing
/ (and the unneeded \/ escapes). It is a separate fix and can be split into
its own PR if you prefer.

Tests

  • New LAN parsing group in test/position_test.dart covering normal moves,
    captures, en passant, promotions, castling (including Chess960), suffixes,
    whitespace, pins, a full game, Crazyhouse drops, Antichess king promotion,
    and malformed input.
  • New test for the 1/2-1/2 result token in test/pgn_test.dart.

Design decisions to review

  • Strict separator. - vs x is enforced because that is what LAN
    specifies. If lenient input is preferred, it is a small change to the regex
    and the separator check.
  • No UCI fallback. UCI-style input stays out of parseLan so each parser
    has one clear format.
  • Normalized castling. The result matches what parseSan returns, so
    callers can treat both methods the same way.

Checklist

  • [ X] dart test passes

AI assistance

Tool: Claude Sonnet 5.5.

What the AI did:

  • Reviewed my initial parseLan implementation and the Square.parse helper,
    and proposed a stricter version (piece-letter check, -/x separator check,
    mandatory promotion, annotation and whitespace handling, delegation of
    castling and drops to parseSan, normalizeMove on the result).
  • Rewrote the existing "LAN Parsing Tests" group to match the stricter behavior.
  • Reviewed pgn.dart and spotted the stray / in the draw token of
    tokenRegex (a leftover from the JavaScript regex literal).
  • Drafted the commit message and this PR description.

What I did myself:

  • Wrote the initial parseLan and the original tests, and chose to implement
    LAN as a separate method.
  • Applied the changes to the codebase, ran the test suite, and fixed the
    failures that appeared (for example the e1-g1 castling normalization).

Prompts used (summarized):

  1. Review whether parseLan and Square.parse are correct and cover all
    chess scenarios.
  2. Modify the existing tests for the new parseLan method.
  3. Explain the parseLan code and the tests step by step.
  4. Check whether pgn.dart is correct, especially the token regex.
  5. Write the commit message and improve the PR title and description.

Add `Position.parseLan` to parse moves in Long Algebraic Notation
(e.g. `e2-e4`, `Ng1-f3`, `e5xd6`, `e7-e8=Q`, `O-O`, `N@f3`) and return a
legal `Move`, or `null` if the input is invalid or illegal.

- The origin square, destination square and separator (`-` or `x`) are
  required, so UCI-style input like `e2e4` is rejected.
- The optional piece letter must match the piece on the origin square.
- The separator must agree with the position: `x` only for captures
  (including en passant), `-` otherwise.
- Promotion is accepted with or without `=` and is required when a
  pawn reaches the last rank.
- Trailing `+`, `#`, `!` and `?` and surrounding whitespace are ignored.
- Castling (`O-O`, `0-0`, `O-O-O`, `0-0-0`) and Crazyhouse drops are
  delegated to `parseSan`. Written-out castling (`e1-g1`) is normalized
  to the king-to-rook form, consistent with `parseSan`.
JavaScript regex literal and kept its closing `/` delimiter, so the
draw token only matched `1/2-1/2/`. A normal `1/2-1/2` was skipped and
`Result` was not set from the movetext. Remove the trailing `/` (and
the unneeded `\/` escapes).

Add unit tests for `parseLan` and for the draw result in movetext.

@veloce veloce left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll review asap when the formatting is back to normal. Thanks.

Comment thread lib/src/models.dart Resolved
@veloce

veloce commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator

@gd-evil I reformatted the code on main, please re-apply your PR on top of it so it avoids any formatting issue.

@gd-evil

gd-evil commented Nov 5, 2026

Copy link
Copy Markdown
Author

@veloce thank you, apologies for not replying earlier, I'll do it as soon as possible

# Conflicts:
#	lib/src/pgn.dart
@gd-evil

gd-evil commented Nov 13, 2026

Copy link
Copy Markdown
Author

@veloce I'mn having trouble with the formatting, am I doing something wrong? I only formatted position.dart and pgn.dart files.

@gd-evil
gd-evil requested a review from veloce November 13, 2026 17:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants