Skip to content

Commit aa5e704

Browse files
committed
chore: case-insensitive antlr parsing
1 parent 6aa0e84 commit aa5e704

File tree

3 files changed

+115
-60
lines changed

3 files changed

+115
-60
lines changed

src/grammar/Ignition.g4

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -27,52 +27,52 @@ line:
2727
);
2828

2929
// lines of text
30-
lineMetaCash : site 'Hand' '#' handNumber fastFold? ('TBL#' | 'ID#') tableNumber variant bettingStructure '-' timestamp;
31-
lineMetaTournament: site 'Hand' '#' handNumber COLON variant 'Tournament' '#' tournamentNumber 'TBL#' tableNumber ',' tournamentSpeed? '-' 'Level' tournamentLevel '(' chipCount '/' chipCount ')' '-' timestamp;
32-
linePlayer : 'Seat' seatNumber COLON position ME? '(' chipCount 'in chips)';
33-
lineDealer : ('Dealer' ME? COLON)? 'Set dealer' ('[' INT ']')?;
34-
lineSmallBlind : ('Small Blind' | 'Dealer') ME? COLON ('Small Blind' | 'Small blind') chipCount;
35-
lineBigBlind : 'Big Blind' ME? COLON ('Big Blind' | 'Big blind') chipCount;
36-
linePost : position ME? COLON 'Posts' DEAD? 'chip' chipCount;
30+
lineMetaCash : site 'hand' '#' handNumber fastFold? ('tbl' | 'id') '#' tableNumber variant bettingStructure '-' timestamp;
31+
lineMetaTournament: site 'hand' '#' handNumber COLON variant 'tournament' '#' tournamentNumber 'tbl' '#' tableNumber ',' tournamentSpeed? '-' 'level' tournamentLevel '(' chipCount '/' chipCount ')' '-' timestamp;
32+
linePlayer : 'seat' seatNumber COLON position ME? '(' chipCount 'in chips)';
33+
lineDealer : ('dealer' ME? COLON)? 'set dealer' ('[' INT ']')?;
34+
lineSmallBlind : ('small blind' | 'dealer') ME? COLON 'small blind' chipCount;
35+
lineBigBlind : 'big blind' ME? COLON 'big blind' chipCount;
36+
linePost : position ME? COLON 'posts' DEAD? 'chip' chipCount;
3737
lineStreet : '***' STREET '***' boardSections?;
38-
lineHandsDealt : position ME? COLON 'Card dealt to a spot' hand;
39-
lineAwardBounty: position ME? COLON 'BOUNTY PRIZE' '[' chipCount ']';
40-
lineTournamentPlacement: position ME? COLON 'Ranking' tournamentPlacement;
41-
lineTournamentPrize: position ME? COLON 'Prize' (tournamentPrizeCash | tournamentPrizeTicket);
38+
lineHandsDealt : position ME? COLON 'card dealt to a spot' hand;
39+
lineAwardBounty: position ME? COLON 'bounty prize' '[' chipCount ']';
40+
lineTournamentPlacement: position ME? COLON 'ranking' tournamentPlacement;
41+
lineTournamentPrize: position ME? COLON 'prize' (tournamentPrizeCash | tournamentPrizeTicket);
4242
lineMisc :
4343
(position ME? COLON)?
4444
(
45-
'Table deposit' chipCount
46-
| 'Seat sit down'
47-
| 'Seat sit out'
48-
| 'Seat stand'
49-
| 'Seat re-join'
50-
| 'Re-join'
51-
| 'Table enter user'
52-
| 'Table leave user'
53-
| 'Stand'
54-
| 'Sit out'
55-
| 'Sitout' forcedActionReason
56-
| 'Enter' forcedActionReason
57-
| 'Leave' forcedActionReason
58-
| 'Draw for dealer' board
45+
'table deposit' chipCount
46+
| 'seat sit down'
47+
| 'seat sit out'
48+
| 'seat stand'
49+
| 'seat re-join'
50+
| 're-join'
51+
| 'table enter user'
52+
| 'table leave user'
53+
| 'stand'
54+
| 'sit out'
55+
| 'sitout' forcedActionReason
56+
| 'enter' forcedActionReason
57+
| 'leave' forcedActionReason
58+
| 'draw for dealer' board
5959
);
6060
lineAction : position ME? COLON action;
61-
lineMuck : position ME? COLON 'Does not show' hand '(' handStrength ')';
62-
lineUncalled : position ME? COLON 'Return uncalled portion of bet' chipCount;
61+
lineMuck : position ME? COLON 'does not show' hand '(' handStrength ')';
62+
lineUncalled : position ME? COLON 'return uncalled portion of bet' chipCount;
6363
lineShowdown : position ME? COLON showdownAction hand? '(' handStrength ')';
64-
lineResult : position ME? COLON ('Hand Result' | 'Hand result') ('-' SIDEPOT)? chipCount;
65-
lineTotalPot : 'Total Pot' '(' chipCount ')';
66-
lineBoard : 'Board' board;
64+
lineResult : position ME? COLON 'hand result' ('-' SIDEPOT)? chipCount;
65+
lineTotalPot : 'total pot' '(' chipCount ')';
66+
lineBoard : 'board' board;
6767
lineActionSummary:
68-
'Seat' '+'? INT COLON position
68+
'seat' '+'? INT COLON position
6969
(
70-
'Folded' ('before'|'on') 'the' STREET
71-
| 'HI'? chipCount? '[Does not show]'
72-
| '[Mucked]' hand
70+
'folded' ('before' | 'on') 'the' STREET
71+
| 'hi'? chipCount? '[does not show]'
72+
| '[mucked]' hand
7373
| winHighResult bountyAwardResult?
74-
| 'HI' winHighResult ('LO' winLowResult)? bountyAwardResult?
75-
| 'LO' winLowResult bountyAwardResult?
74+
| 'hi' winHighResult ('lo' winLowResult)? bountyAwardResult?
75+
| 'lo' winLowResult bountyAwardResult?
7676
| loseResult
7777
);
7878

@@ -82,16 +82,16 @@ seatNumber : INT;
8282
tableNumber : INT;
8383
tournamentNumber: INT;
8484
tournamentLevel: INT;
85-
tournamentSpeed: 'Normal' | 'Turbo';
85+
tournamentSpeed: 'normal' | 'turbo';
8686
tournamentPlacement: INT;
8787
tournamentPrizeCash: chipCount;
88-
tournamentPrizeTicket: 'Tournament Ticket' '[' WORD* chipCount WORD* ']';
88+
tournamentPrizeTicket: 'tournament ticket' '[' WORD* chipCount WORD* ']';
8989
timestamp : INT '-' INT '-' INT INT ':' INT ':' INT;
90-
site : 'Bodog' | 'Bovada' | 'Ignition';
91-
variant : 'HOLDEM' | 'OMAHA' | 'OMAHA HiLo' | 'HOLDEMZonePoker' | 'OMAHAZonePoker';
92-
bettingStructure : 'No Limit' | 'Pot Limit' | 'Limit';
90+
site : 'bodog' | 'bovada' | 'ignition';
91+
variant : 'holdem' | 'omaha' | 'omaha hilo' | 'holdemzonepoker' | 'omahazonepoker';
92+
bettingStructure : 'no limit' | 'pot limit' | 'limit';
9393
fastFold : FASTFOLD;
94-
position : 'Small Blind' | 'Big Blind' | 'UTG' | 'UTG+1' | 'UTG+2' | 'UTG+3' | 'UTG+4' | 'UTG+5' | 'Dealer';
94+
position : 'small blind' | 'big blind' | 'utg' | ('utg+' INT) | 'dealer';
9595
chipCount : '$'? value;
9696
value : (INT ',')* INT ('.' INT)?;
9797
board : '[' cards ']';
@@ -100,32 +100,32 @@ hand : '[' cards ']';
100100
handAndBoard : '[' cards '-' cards ']';
101101
cards : card+;
102102
card : CARD;
103-
handStrength : 'High Card' | 'One pair' | 'Two pair' | 'Three of a kind' | 'Straight' | 'Flush' | 'Full House' | 'Four of a kind' | 'Straight Flush' | 'Royal Straight Flush';
103+
handStrength : 'high card' | 'one pair' | 'two pair' | 'three of a kind' | 'straight' | 'flush' | 'full house' | 'four of a kind' | 'straight flush' | 'royal straight flush';
104104
action : actionFold | actionCheck | actionBet | actionCall | actionRaise | actionAllInRaise | actionAllIn | actionAnte;
105-
actionFold : ('Fold' | 'Folds') (forcedActionReason | '& shows' hand)?;
106-
actionCheck : 'Checks' forcedActionReason?;
107-
actionBet : 'Bets' forcedActionReason? chipCount;
108-
actionCall : ('Call' | 'Calls') forcedActionReason? chipCount;
109-
actionRaise : 'Raises' forcedActionReason? chipCount 'to' chipCount;
110-
actionAllIn : 'All-in' forcedActionReason? chipCount;
111-
actionAllInRaise : 'All-in' '(raise)' forcedActionReason? chipCount 'to' chipCount;
112-
actionAnte : 'Ante chip' chipCount;
105+
actionFold : ('fold' | 'folds') (forcedActionReason | '& shows' hand)?;
106+
actionCheck : 'checks' forcedActionReason?;
107+
actionBet : 'bets' forcedActionReason? chipCount;
108+
actionCall : ('call' | 'calls') forcedActionReason? chipCount;
109+
actionRaise : 'raises' forcedActionReason? chipCount 'to' chipCount;
110+
actionAllIn : 'all-in' forcedActionReason? chipCount;
111+
actionAllInRaise : 'all-in' '(raise)' forcedActionReason? chipCount 'to' chipCount;
112+
actionAnte : 'ante chip' chipCount;
113113
forcedActionReason : '(' ~')'* ')';
114-
showdownAction: 'Showdown' | 'Mucks';
114+
showdownAction: 'showdown' | 'mucks';
115115
winHighResult: chipCount 'with' handStrength (hand | handAndBoard);
116116
winLowResult : chipCount (hand | handAndBoard);
117117
loseResult : ('lose with' | 'lost with') handStrength (hand | handAndBoard);
118-
bountyAwardResult: 'BOUNTY awarded' COLON chipCount;
118+
bountyAwardResult: 'bounty awarded' COLON chipCount;
119119

120120
// lexer rules
121-
ME : '[ME]';
122-
STREET : 'HOLE CARDS' | 'FLOP' | 'TURN' | 'RIVER' | 'SUMMARY';
123-
FASTFOLD : 'Zone Poker';
121+
ME : '[me]';
122+
STREET : 'hole cards' | 'flop' | 'turn' | 'river' | 'summary';
123+
FASTFOLD : 'zone poker';
124124
DEAD : 'dead';
125-
SIDEPOT : 'Side pot' | 'Side Pot';
125+
SIDEPOT : 'side pot';
126126
COLON : ':';
127127
INT : [0-9]+;
128-
CARD : [2-9TJQKA][cdhs];
129-
WORD : [A-Za-z]+;
128+
CARD : [2-9tjqka][cdhs];
129+
WORD : [a-z]+;
130130
EOL : '\r' | '\n' | '\r\n';
131131
WS : [ \t]+ -> skip;

src/utils/LowerCaseCharStream.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { CharStream } from 'antlr4ts';
2+
import { Interval } from 'antlr4ts/misc/Interval';
3+
4+
export class LowerCaseCharStream implements CharStream {
5+
private readonly stream: CharStream;
6+
7+
/**
8+
* Constructs a new LowerCaseCharStream wrapping the given {@link CharStream} forcing all
9+
* characters to lower case.
10+
* @param {CharStream} stream The stream to wrap.
11+
*/
12+
constructor(stream: CharStream) {
13+
this.stream = stream;
14+
}
15+
16+
getText(interval: Interval): string {
17+
return this.stream.getText(interval);
18+
}
19+
20+
consume(): void {
21+
this.stream.consume();
22+
}
23+
24+
LA(i: number): number {
25+
const c = this.stream.LA(i);
26+
if (c <= 0) {
27+
return c;
28+
}
29+
return String.fromCharCode(c).toLowerCase().charCodeAt(0);
30+
}
31+
32+
mark(): number {
33+
return this.stream.mark();
34+
}
35+
36+
release(marker: number): void {
37+
this.stream.release(marker);
38+
}
39+
seek(index: number): void {
40+
this.stream.seek(index);
41+
}
42+
43+
get index(): number {
44+
return this.stream.index;
45+
}
46+
47+
get size(): number {
48+
return this.stream.size;
49+
}
50+
51+
get sourceName(): string {
52+
return this.stream.sourceName;
53+
}
54+
}

src/utils/getParser.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
Parser,
99
Token,
1010
} from 'antlr4ts';
11+
import { LowerCaseCharStream } from './LowerCaseCharStream';
1112

1213
interface Constructable<T> {
1314
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -30,7 +31,7 @@ export const getParser = <L extends Lexer, P extends Parser>(
3031
errorListener,
3132
}: Options<L, P>,
3233
) => {
33-
const inputStream = CharStreams.fromString(str);
34+
const inputStream = new LowerCaseCharStream(CharStreams.fromString(str));
3435
const lexer = new LexerClass(inputStream);
3536
const tokenStream = new CommonTokenStream(lexer);
3637
const parser = new ParserClass(tokenStream);

0 commit comments

Comments
 (0)