Documentation Index
Fetch the complete documentation index at: https://mintlify.com/smogon/pokemon-showdown-client/llms.txt
Use this file to discover all available pages before exploring further.
BattleTextParser turns raw Pokémon Showdown protocol lines into structured Args tuples and human-readable battle log HTML. It is used by both the full client (inside BattleLog) and the standalone replay viewer — the parser itself has no dependencies on battle state or the DOM, only an optional dependency on the BattleText locale table for message templates. Protocol lines are first split into Args by parseLine or parseBattleLine, then dispatched through parseArgs/parseArgsInner to produce localised text with player and Pokémon names filled in.
BattleTextParser is MIT-licensed and carries no Node.js runtime dependencies. It can be used in environments without a DOM as long as BattleText locale data is available.Type Exports
A non-empty tuple representing a parsed protocol line. The first element is always the command string (e.g.
'move', 'switch', '-damage'). Subsequent elements are the positional arguments extracted from the line.Keyword arguments extracted from the trailing
[keyword: value] suffix syntax on battle lines. The bracket-enclosed key maps to the trimmed value (or '.' when the value is empty, so the key evaluates as truthy).Identifies one of the four possible battle sides.
p1/p2 are the primary players; p3/p4 appear in multi-battles and are allies of p1/p2 respectively.BattleTextParser Class
Constructor
The side from whose perspective narrative text is generated. Defaults to
'p1'. Affects pronoun choices in templates — p1’s Pokémon are referred to as “your Pokémon” and p2’s as “the opposing Pokémon” when the perspective is 'p1'.Properties
Player display names. Set automatically when a
|player| protocol line is parsed. These strings are pre-escaped for use as the second argument to String.prototype.replace (dollar signs are doubled).The viewing side. Determines which player’s Pokémon are called “your Pokémon” vs “the opposing Pokémon” in log templates.
Current generation, updated when a
|gen| protocol line is parsed. Defaults to 9. Affects certain message templates that differ between generations.Current turn number, updated when a
|turn| protocol line is parsed.Tracks where in a turn’s message sequence the parser currently is. Used by
sectionBreak to insert blank-line separators at the right points in the formatted log.| Value | Meaning |
|---|---|
'break' | Between turns or at the start of the log |
'preMajor' | Before the main action of a turn (e.g. weather messages before a move) |
'major' | A primary action line (move, switch) |
'postMajor' | Consequential lines after the main action (damage, status, etc.) |
Lazily compiled regular expression used by
fixLowercase to capitalise the first letter of Pokémon/team template prefixes when they appear at the start of a log line. undefined means not yet initialised; null means no lowercasing is needed for the current locale.Static Methods
BattleTextParser.parseLine(line)
Parses a single raw protocol line and returns an Args tuple. This is the primary entry point for splitting protocol data.
A raw protocol line. Lines starting with
| are battle/room messages; lines without a leading | are treated as plain text with command ''.When
true, returns null for lines whose command is not in the known special-cased set (instead of falling back to a generic split). Use this to test whether a line needs custom handling.How parseLine Splits Lines
The method inspects the command token (the text between the first and second |) and applies one of three splitting strategies:
Single-argument commands (two parts total)
Single-argument commands (two parts total)
These commands have all their content in one trailing string — no further splitting is done.
Three-part commands
Three-part commands
Split at the first two
| separators so the third segment can contain embedded | characters.Four-part commands
Four-part commands
Split at the first three
| separators.Default split (battle lines)
Default split (battle lines)
For all other commands (and when
noDefault is falsy), the line is split on every |:BattleTextParser.parseBattleLine(line)
Like parseLine but also extracts keyword arguments from the trailing [key: value] suffix syntax used by battle messages. Calls upgradeArgs to normalise old protocol syntax.
BattleTextParser.upgradeArgs({ args, kwArgs })
Normalises legacy protocol syntax to the current format. Called by parseBattleLine. Handles deprecated commands such as -nothing (→ -activate||move:Splash), old -activate formats, Snow weather rename (Snow → Snowscape), and ability/move prefix corrections.
BattleTextParser.effectId(effect?)
Strips the item:, move:, or ability: prefix from an effect string, then converts to a bare ID. Used internally to identify effects in parseArgsInner.
BattleTextParser.allyID(sideid)
Returns the ally side ID in a multi-battle (p1 ↔ p3, p2 ↔ p4). Returns '' for unknown inputs.
BattleTextParser.escapeRegExp(input)
Escapes special regular-expression characters in a string so it can be embedded safely in a RegExp pattern.
BattleTextParser.escapeReplace(input)
Escapes $ characters in a string so it is safe to pass as the second argument to String.prototype.replace. Player names are pre-escaped with this method before being stored in this.p1–this.p4.
BattleTextParser.parseNameParts(text)
Splits a raw display name into its constituent parts: group symbol (e.g. +, @), base name, away flag, and status string (the part after @).
BattleTextParser.stat(stat)
Returns the human-readable display name for a stat ID (e.g. 'atk' → 'Attack'), sourced from the BattleText locale table.
Instance Methods
parseArgs(args, kwArgs, noSectionBreak?)
The main dispatch method. Inserts a section-break newline if sectionBreak returns true for this args/kwArgs combination, then calls parseArgsInner and passes the result through fixLowercase.
parseArgsInner(args, kwArgs)
The core switch statement that handles every recognised battle protocol command and returns a formatted string (or '' for commands that produce no log output such as |player| and |gen|).
| Command | Log output |
|---|---|
player | Sets this.p1–this.p4; returns '' |
gen | Sets this.gen; returns '' |
turn | Sets this.turn; returns formatted “Turn N” heading |
start | Returns “X sent out Y” opening line |
win / tie | Returns victory/tie message |
switch / drag | Returns switch-in description |
faint | Returns fainting message |
move | Returns “X used Y!” line |
cant | Returns move-prevention message |
-damage / -heal | Returns HP change description |
-status / -curestatus | Returns status-infliction/cure message |
-boost / -unboost | Returns stat change message |
-weather | Returns weather description |
c / c: | Returns chat message |
extractMessage(buf)
Parses a multi-line protocol buffer and returns the concatenated log output. Calls parseBattleLine and parseArgs for each line.
pokemon(pokemon) / pokemonName
Helper that resolve a Pokémon identifier string (e.g. "p1a: Pikachu") to a display string.
pokemonNameis an arrow-function property (not a regular method) that returns the bare nickname string, pre-escaped forString.prototype.replace.pokemonis a regular method that wraps the nickname in the appropriate near/far template fromBattleText.
pokemonFull(pokemon, details)
Returns a [side, displayName] tuple where side is 'p1'/'p2'/etc. and displayName combines the nickname and bold species name (e.g. "Sparky (**Pikachu**)"). Used to format switch-in log lines.
trainer(side)
Resolves a side string like "p1" to the player’s display name.
Protocol Line Format Reference
The Pokémon Showdown protocol uses|-delimited lines. Each line starts with |CMD where CMD is the command. Fields that contain | characters use the multi-part splitting strategies described under parseLine.
Player and game setup
Player and game setup
Pokémon actions
Pokémon actions
HP and status changes
HP and status changes
Stat changes and volatiles
Stat changes and volatiles
Field conditions and weather
Field conditions and weather
Chat messages
Chat messages
End-of-battle
End-of-battle
