Class: Pubid::Ieee::Parser

Inherits:
Parslet::Parser
  • Object
show all
Defined in:
lib/pubid/ieee/parser.rb

Overview

Parser class for IEEE identifiers Single Responsibility: Parsing IEEE identifier syntax Note: IEEE is extremely complex with many edge cases

Class Method Summary collapse

Class Method Details

.normalize_relaton_suffixes(cleaned) ⇒ Object

Rewrite relaton's historical IEEE serialization into canonical pubid spellings. relaton's own formatter (Relaton::Ieee::PubId::Id#to_s) emits suffix tokens that differ from pubid's grammar:

/D-N-YYYY[-MM]  draft + trailing numeric date  (the dominant form)
/E-N[-YYYY[-MM]] edition
/R-N[-YYYY]      revision (pubid has no revision suffix)
" Redline"       redline suffix without the " - " pubid expects

The draft/edition trailing date is repositioned onto the document number as a base year/month (a form pubid already parses), which also keeps the draft component clean so it round-trips through to_hash/from_hash.



1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
# File 'lib/pubid/ieee/parser.rb', line 1073

def self.normalize_relaton_suffixes(cleaned)
  # NOTE: the trailing " Redline"/" - Redline" suffix is NO LONGER stripped
  # here — the grammar's `redline` rule captures it into a redline flag so
  # a redline id stays distinct from its base standard.

  # Combined draft + corrigendum: relaton emits "…/D-N/CorM-YYYY" (draft
  # then corrigendum), but pubid's grammar accepts the corrigendum first.
  # Swap them so the corrigendum keeps its own year and the draft trails.
  # The hyphen after "D" is mandatory here: relaton's formatter always
  # emits "/D-<draft>", whereas pubid's own canonical joint-development
  # form is "/D<draft>-<year>" (no hyphen, year kept on the draft) — which
  # already parses and must not be repositioned. A trailing corrigendum
  # month (the "-MM" in "/CorM-YYYY-MM") is intentionally dropped: pubid's
  # corrigendum model carries only a year.
  cleaned = cleaned.sub(
    %r{\A(.*)/D-([0-9A-Za-z][0-9A-Za-z.+]*?)/Cor\.?[ ]?(\d+)(?:-((?:19|20)\d\d))?(?:-\d\d)?\z},
  ) do
    base, draft, cor, year = Regexp.last_match.captures
    "#{base}/Cor #{cor}#{year ? "-#{year}" : ''}/D#{draft}"
  end

  # Combined draft + revision, and the empty-draft revision-only form:
  #   "…/D-<d>/R-<x>-YYYY[-MM]"  and  "…/D-/R-<x>-YYYY"   (nil-residue #2).
  # Reposition the base publication date onto the number (pubid's
  # "-YYYY[-MM]" shape), keep the draft as "/D<d>" (dropped when the draft
  # is empty), and leave a trailing "/R-<x>" the grammar captures as the
  # revision. Runs before the plain "/D-…" reposition, which the embedded
  # "/R-" would otherwise defeat.
  cleaned = cleaned.sub(
    %r{\A(.*?)/D-([0-9A-Za-z.+]*)/R-([0-9A-Za-z]+)(?:-((?:19|20)\d\d)(?:-(0[1-9]|1[0-2]))?)?\z},
  ) do
    base, draft, rev, year, month = Regexp.last_match.captures
    date = year ? "-#{year}#{month ? "-#{month}" : ''}" : ""
    draft_part = draft.to_s.empty? ? "" : "/D#{draft}"
    "#{base}#{date}#{draft_part}/R-#{rev}"
  end

  # /D-N drafts with a trailing numeric date, when the draft is the last
  # suffix: reposition the -YYYY[-MM] date onto the number. A following
  # /Cor, /Amd, /R or /E suffix carries its own year, so the `\z` anchor
  # keeps this from firing on those combined forms.
  cleaned = cleaned.sub(
    %r{\A(.*)/D-([0-9A-Za-z][0-9A-Za-z.+]*?)-((?:19|20)\d\d)(?:-(0[1-9]|1[0-2]))?\z},
  ) do
    base, draft, year, month = Regexp.last_match.captures
    "#{base}-#{year}#{month ? "-#{month}" : ''}/D#{draft}"
  end

  # /E-N editions: relaton's "/E-2-2023-02" → pubid's "Edition 2.0 2023-02".
  cleaned = cleaned.sub(
    %r{\A(.*?)/E-(\d+)(?:-((?:19|20)\d\d)(?:-(0[1-9]|1[0-2]))?)?\z},
  ) do
    base, edition, year, month = Regexp.last_match.captures
    date = year ? " #{year}#{month ? "-#{month}" : ''}" : ""
    "#{base} Edition #{edition}.0#{date}"
  end

  # /R-N revisions: PRESERVE them (the grammar's revision_suffix rule now
  # captures a trailing "/R-<x>" into the `revision` attribute). Just
  # reposition any trailing publication year onto the number, keeping the
  # "/R-<x>" in place for the grammar.
  cleaned.sub(
    %r{\A(.*?)/R-([0-9A-Za-z]+)(?:-((?:19|20)\d\d))?\z},
  ) do
    base, rev, year = Regexp.last_match.captures
    "#{year ? "#{base}-#{year}" : base}/R-#{rev}"
  end
end

.normalize_revision_notation(cleaned) ⇒ Object

Strip the IEEE rawbib revision-notation dialects. REV/Rev (case-insensitive) + a trailing revision id [A-Za-z0-9]+, glued to the number or separated by -, /, _, ., or a space, and preceding the draft. pubid's canonical "/D/R-" form already drops the revision on render (normalize_relaton_suffixes strips a trailing /R-x), so the revision-less result is the same identifier — and stripping (rather than reordering) leaves any trailing date/parenthetical intact, which is why forms that already parse (Draft P…-REVmb/D3.0, Mar 2010) are NOT disturbed. Examples:

"P802.16.2-REVa/D8" -> "P802.16.2/D8"
"P802.16/REVd/D5"   -> "P802.16/D5"
"P802.15.1REVa/D5"  -> "P802.15.1/D5"
"P802.11REVmb"      -> "P802.11"   (no draft)


1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
# File 'lib/pubid/ieee/parser.rb', line 1155

def self.normalize_revision_notation(cleaned)
  # NUMBERED revisions ("Rev<digits>") are PRESERVED — repositioned to a
  # trailing "/R-<n>" suffix the grammar captures as the `revision`
  # attribute (IEEE's native inline spelling; numbered-revision hand-off).
  # A "\d+" right after "Rev" both selects the numbered subset and keeps
  # these off the English word "Revision". Three source positions:
  #   after a draft : "PC37.30.2/D043 Rev 18" -> ".../D043/R-18"
  cleaned = cleaned.sub(
    %r{(/D[0-9A-Za-z.]*)\s+[Rr][Ee][Vv]\s*(\d+)}, '\1/R-\2'
  )
  #   before a draft: "P802.16Rev2/D3" -> "P802.16/D3/R-2"
  cleaned = cleaned.sub(
    %r{[-/_.]?\s?[Rr][Ee][Vv][-\s]?(\d+)(/D[0-9A-Za-z.]*)}, '\2/R-\1'
  )
  #   no draft, trailing: "P1722-rev1" -> "P1722/R-1"
  cleaned = cleaned.sub(
    %r{(\d)[-._]?\s?[Rr][Ee][Vv]\s*(\d+)\s*\z}, '\1/R-\2'
  )

  # LETTERED inline revisions ("REVa", "REVmb") have no pubid model and are
  # still STRIPPED (unchanged behaviour). The numbered forms above already
  # became "/R-<n>", so these regexes only see the lettered residue.
  # Revision token that PRECEDES a draft: drop it (keep the /D…).
  cleaned = cleaned.sub(
    %r{[-/_.]?\s?[Rr][Ee][Vv][-\s]?[A-Za-z0-9]+(?=/D[0-9])},
    "",
  )
  # Trailing revision glued to the number with no draft ("P802.11REVmb");
  # a digit must immediately precede REV so a trailing English word like
  # "…Revision" can't match.
  cleaned.sub(%r{(\d)[Rr][Ee][Vv][A-Za-z0-9]+\s*\z}, '\1')
end

.parse(string) ⇒ Object



1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
# File 'lib/pubid/ieee/parser.rb', line 1188

def self.parse(string)
  # Strip .pdf extension if present (Pattern 3: File Extensions)
  cleaned = string.sub(/\.pdf$/i, "")

  # Note: IEC and ANSI identifiers are NOT filtered here because they can have
  # IEEE co-publication or adoption. The Base.parse method handles determining
  # which standards are actually IEEE-related.
  # ISO-only standards are still filtered as they have separate handling.

  # Pattern 3: Replace underscore before ISO stage codes with slash
  # These are joint development drafts that use underscore instead of slash
  cleaned = cleaned.gsub(/_(FDIS|CDV|CD|DIS|WD|PWI|NP)/, '/\1')

  # NEW: Normalize multiple spaces to single space
  # No valid IEEE identifier pattern needs more than 1 space
  cleaned = cleaned.gsub(/\s+/, " ")

  # A joint ISO-led publisher list is sometimes crawled with a stray slash
  # (or slash+space) before the ISO stage code — "ISO/IEC/IEEE/ FDIS …" or
  # "ISO/IEC/IEEE/FDIS …". Restore the space separator so the stage parses
  # (bucket 7).
  cleaned = cleaned.gsub(
    %r{\b(ISO/IEC/IEEE|IEEE/ISO/IEC|IEEE/IEC/ISO|ISO/IEEE|IEC/IEEE|IEEE/IEC|ISO/IEC)/ ?(FDIS|FCD|CDV|DIS\d?|CD\d?|WD|PWI|NP)\b},
    '\1 \2',
  )

  # Rewrite the rawbib revision-notation dialects (REVa/REVd/glued) into
  # the canonical /R-<x> form before the suffix normalization below.
  cleaned = normalize_revision_notation(cleaned)

  # Normalize relaton's bespoke historical serialization (the spellings
  # emitted by Relaton::Ieee::PubId::Id#to_s) into canonical pubid forms
  # so `relaton-data-ieee` parses. See #normalize_relaton_suffixes.
  cleaned = normalize_relaton_suffixes(cleaned)

  # NEW Session 171: CONSERVATIVE data quality fixes for TODO.IEEE-MUST-DO.txt
  # Only fix clear typos: space before dash + 4-digit year, OR dash + space + 4-digit year
  # Do NOT touch " - " (space-dash-space) which is valid formatting
  cleaned = cleaned.gsub(/(\d)\s+-(\d{4})\b/, '\1-\2')  # "C37.101 -2006" → "C37.101-2006"
  cleaned = cleaned.gsub(/(\d)-\s+(\d{4})\b/, '\1-\2')  # "C62.35- 2010" → "C62.35-2010"

  # NEW Session 171: HTML entity for en dash (&#x2013;)
  # ONLY convert if not already followed by a dash (avoid creating --)
  cleaned = cleaned.gsub(/&#x2013;(?!-)/, "-")  # En dash → regular hyphen (if not followed by dash)
  cleaned = cleaned.gsub("&#x2013;-", "-")      # En-dash-dash → single dash

  # NEW Session 171: Remove wrong ! prefix
  cleaned = cleaned.gsub(/^!IEEE /, "IEEE ")

  # NEW Session 171: Fix "IEEE/ ASTM" spacing (extra space after slash)
  cleaned = cleaned.gsub("IEEE/ ASTM", "IEEE/ASTM")

  # NEW Phase 1: Handle HTML entities comprehensively
  cleaned = cleaned.gsub("&#x2122;", "") # Trademark symbol
  cleaned = cleaned.gsub("&#x2019;", "'") # Smart apostrophe
  cleaned = cleaned.gsub("&amp;amp;", "&")   # Double-encoded ampersand
  cleaned = cleaned.gsub("&amp;", "&")       # Single-encoded ampersand

  # NEW: Wrap P&V notation in parentheses (Paper & Video, etc.)
  # Pattern: "IEEE Std 500-1984 P&V" → "IEEE Std 500-1984 (P&V)"
  cleaned = cleaned.gsub(/\s+(P&V)\s*$/, ' (\1)')

  # NEW Phase 1: Fix number spacing issues (e.g., "C57.1 2.25" → "C57.12.25")
  # This handles cases where a space appears in the middle of a number
  cleaned = cleaned.gsub(/(\d+\.\d+)\s+(\d+\.)/, '\1\2')

  # NEW Phase 1: Fix year spacing issues (e.g., "1 996" → "1996")
  # Remove spaces within 4-digit years
  cleaned = cleaned.gsub(/\b(1|2)\s+(\d{3})\b/, '\1\2')

  # NEW: Fix month+year spacing (e.g., "March2016" → "March 2016")
  # Add space between month name and 4-digit year when they're concatenated
  cleaned = cleaned.gsub(
    /\b(January|February|March|April|May|June|July|August|September|October|November|December)(\d{4})\b/, '\1 \2'
  )
  # Also handle abbreviated months
  cleaned = cleaned.gsub(
    /\b(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Sept|Oct|Nov|Dec)(\d{4})\b/, '\1 \2'
  )

  # NEW: Convert IEC/IEEE space-separated to semicolon format
  # Pattern: "IEC 61523-3 First edition 2004-09; IEEE 1497" → already semicolon
  # Pattern: "IEC 62539 First Edition 2007-07 IEEE 930" → needs semicolon
  # Pattern: "IEC 60076-21:2011 Edition 1.0 2011-12 IEEE Std C57.15" → needs semicolon (issue #202)
  # Match: IEC identifier (with optional colon-year, optional "First"/numeric
  # Edition + YYYY-MM) + space + IEEE identifier.
  cleaned = cleaned.gsub(
    /(IEC\s+\d+(?:-\d+)?(?::\d{4})?(?:\s+(?:First\s+)?[Ee]dition\s+\d+(?:\.\d+)?\s+\d{4}-\d{2})?)\s+(IEEE\s+Std\s+\S+|IEEE\s+\S+)/,
    '\1; \2'
  )

  # Strip ":YYYY" from IEC numbers when an Edition clause follows — the
  # IEEE parser's number rule doesn't accept the colon-year form, but
  # the year is preserved in the "Edition N.M YYYY-MM" suffix.
  # (issue #202)
  cleaned = cleaned.gsub(
    /^(IEC\s+\d+(?:-\d+)?):\d{4}(\s+(?:First\s+)?[Ee]dition\s+\d+(?:\.\d+)?\s+\d{4}-\d{2})/,
    '\1\2'
  )

  # NEW Phase 1 (Session 141): Remove literal trademark symbol
  # "C57.110™-2018" → "C57.110-2018"
  cleaned = cleaned.gsub(//, "")

  # NEW Phase 1 (Session 141): Fix specific year typo
  # "19969" → "1969" (very specific pattern, won't affect other text)
  cleaned = cleaned.gsub(/\b19969\b/, "1969")

  # NEW Session 169: Fix comma typo in 802.3 series numbers
  # "802.3ch-2020,802.3ca-2020" → "802.3ch-2020, 802.3ca-2020"
  # Very specific: 4 digits, comma, 3 digits (likely 802.3xx typo)
  cleaned = cleaned.gsub(/(\d{4}),(\d{3})/, '\1, \2')

  # NEW Session 169: Fix /lNT typo (lowercase L as 1)
  # "1003.1/2003.l/lNT" → "1003.1/2003.1/INT"
  cleaned = cleaned.gsub(/\/lNT\b/, "/INT")
  cleaned = cleaned.gsub(".l/", ".1/") # Also fix .l/ -> .1/

  # NEW Session 169: Fix I99O typo (letter I and O instead of digits)
  # "IEEE 1076-CONC-I99O" → "IEEE 1076-CONC-1990"
  cleaned = cleaned.gsub(/\bI99O\b/, "1990")

  # NEW: Fix common typos (Category 9)
  cleaned = cleaned.gsub(/^EEE /, "IEEE ")

  # NEW Session 170: Additional safe typo fixes
  # Fix "I EEE" (space between I and EEE)
  cleaned = cleaned.gsub(/^I EEE /, "IEEE ")

  # Fix "lEEE" (lowercase L instead of I)
  cleaned = cleaned.gsub(/^lEEE /, "IEEE ")

  # Fix missing closing parenthesis at end only (very conservative)
  # Only if there's exactly one more opening than closing paren
  open_count = cleaned.count("(")
  close_count = cleaned.count(")")
  if open_count == close_count + 1 && !cleaned.end_with?(")")
    cleaned = "#{cleaned})"
  end

  # NEW Phase 1: Remove trailing commas/colons and text
  cleaned = cleaned.gsub(/,\s*Standard\s*$/, "") # ", Standard" at end
  cleaned = cleaned.gsub(/[,:]\s*$/, "") # Trailing comma/colon
  cleaned = cleaned.gsub(/,\s+and\s+IEEE\s+Std\s/, " and ") # Handle "IEEE Std and Std" case

  # Enhanced: Fix unbalanced parentheses comprehensively
  # Handle three cases: missing closing, extra opening, nested unbalanced
  open_count = cleaned.count("(")
  close_count = cleaned.count(")")

  if open_count > close_count
    # More opening than closing - add closing parens at end
    # This handles both simple missing and nested unbalanced cases
    missing = open_count - close_count
    cleaned = cleaned + (")" * missing)
  elsif close_count > open_count
    # More closing than opening - remove extra closing from end
    # Very conservative: only remove trailing excess closing parens
    extra = close_count - open_count
    cleaned = cleaned.sub(/\){#{extra}}$/, "")
  end

  # === SESSION 173: TODO.IEEE-MUST-DO.txt Preprocessing Enhancements ===

  # Part A: Simple Normalizations (Lines 13, 16, 32-35, 36, 39-41 from TODO)

  # 1. Missing dash before year: "802.16g 2007" → "802.16g-2007"
  # But be careful not to affect month names (already have space)
  # Only apply if: digit + space + 4-digit year (and not after a month name)
  cleaned = cleaned.gsub(/(\d)\s+(\d{4})(?=\s*\(|\s*$)/, '\1-\2')

  # 2. Space-dash-space before year: "802.1ag - 2007" → "802.1ag-2007"
  # This is distinct from " - " in titles, targets space-dash-space-year pattern
  cleaned = cleaned.gsub(/\s+-\s+(\d{4})\b/, '-\1')

  # 3. Add missing "Std" after IEEE: "IEEE 1070-1995" → "IEEE Std 1070-1995"
  # Only at start of string, IEEE + space + digit
  cleaned = cleaned.gsub(/^IEEE\s+(?!Std\b)(\d)/, 'IEEE Std \1')

  # 3.5. Convert "IEEE No." to "IEEE Std": "IEEE No. 264-1968" → "IEEE Std 264-1968"
  # NOTE: Do NOT convert AIEE No - AIEE uses "No" as standard format
  cleaned = cleaned.gsub(/^IEEE\s+No\.\s*/, "IEEE Std ")
  cleaned = cleaned.gsub(/^IEEE\s+No\s/, "IEEE Std ")
  # Skip AIEE No conversion - AIEE preserves "No" format

  # 4. Space before slash in dual published: "262-1973 /ANSI" → "262-1973/ANSI"
  cleaned = cleaned.gsub(/\s+\//, "/")

  # 5. Comma before Edition: ", 1998 Edition" → "-1998"
  # Normalize to standard year format for parser
  cleaned = cleaned.gsub(/,\s+(\d{4})\s+Edition/, '-\1')

  # 6. ISO/IEC spacing: "ISO/IEC15802" → "ISO/IEC 15802"
  # Add space between publisher prefix and number
  cleaned = cleaned.gsub(/(ISO\/IEC)(\d)/, '\1 \2')

  # Part B: Publisher Order (Line 38 from TODO)

  # Fix wrong publisher order: "IEEE Std ANSI/IEEE" → "ANSI/IEEE Std"
  # This handles cases where IEEE Std appears before ANSI/IEEE publisher
  cleaned = cleaned.gsub(/^IEEE\s+Std\s+(ANSI\/IEEE)/, '\1 Std')

  # Part C: Dual Published Formats (Lines 8, 19 from TODO)

  # 1. Semicolon to parenthetical for dual published (MultiLabeledIdentifier)
  # "IEEE Std 120-1955; ASME PTC 19.6-1955" → "IEEE Std 120-1955 (ASME PTC 19.6-1955)"
  # Only if semicolon + space + organization abbreviation (capital letters)
  if cleaned.match?(/;\s+[A-Z]{2,}/)
    cleaned = cleaned.sub(/;\s+([A-Z][^;]+)$/, ' (\1)')
  end

  # === SESSION 174: Additional TODO.IEEE-MUST-DO.txt Preprocessing ===

  # Part A: Edition Abbreviation Normalization (Lines 10-11)
  # Pattern: ", 1999 Edn. (Reaff 2003)" → "-1999 (R2003)"
  # Normalize both the Edition abbreviation and the Reaffirmed format
  cleaned = cleaned.gsub(/,\s+(\d{4})\s+Edn\.\s+\(Reaff\s+(\d{4})\)/,
                         '-\1 (R\2)')
  # Also handle without initial comma (might occur in relationships)
  cleaned = cleaned.gsub(/(\d{4})\s+Edn\.\s+\(Reaff\s+(\d{4})\)/,
                         '\1 (R\2)')

  # Part B: IRE Parenthetical Split (Line 9)
  # Pattern: "(Reaffirmed 1980, 56 IRE 28.S2)" → "(R1980) (56 IRE 28.S2)"
  # Split nested reaffirmation + IRE reference into two parentheticals
  cleaned = cleaned.gsub(/\(Reaffirmed\s+(\d{4}),\s+(\d+\s+IRE[^)]+)\)/,
                         '(R\1) (\2)')

  # Part C: Slash to Parenthetical (Line 37)
  # Pattern: "number-year/ANSI identifier" → "number-year (ANSI identifier)"
  # Only convert if slash is followed by ANSI and NOT a relationship keyword
  # Look ahead to ensure we're at end of main identifier (before paren or end of string)
  cleaned = cleaned.gsub(%r{(\d{4})/ANSI\s+([^(]+)(?=\s*\(|$)},
                         '\1 (ANSI \2)')

  # Part D: ISO/IEC TR Spacing (Line 40)
  # Pattern: "ISO/IEC TR11802" → "ISO/IEC TR 11802"
  # Add space after TR when directly followed by digit
  cleaned = cleaned.gsub(/(ISO\/IEC\s+TR)(\d)/, '\1 \2')
  # === SESSION 178: AIEE Dual Numbers Expansion (Line 45) ===

  # Part E: AIEE "Nos X and Y" Expansion
  # Pattern: "AIEE Nos 72 and 73 - 1932" → "AIEE No 72-1932 and AIEE No 73-1932"
  # Expands dual AIEE numbers to separate identifiers with shared year
  if cleaned.match?(/AIEE\s+Nos\s+(\d+)\s+and\s+(\d+)\s+-\s+(\d{4})/)
    cleaned = cleaned.sub(/AIEE\s+Nos\s+(\d+)\s+and\s+(\d+)\s+-\s+(\d{4})/) do
      first_num = $1
      second_num = $2
      year = $3
      "AIEE No #{first_num}-#{year} and AIEE No #{second_num}-#{year}"
    end
  end

  # === SESSION 222: TODO.IEEE-MUST-FIX-IDs.txt Comprehensive Fixes ===

  # Part A: Typo Fixes
  # 1. "Stad" -> "Std" (typo)
  cleaned = cleaned.gsub(/\bStad\b/, "Std")

  # 2. Lowercase "std" -> "Std" when after IEEE/ANSI publishers
  cleaned = cleaned.gsub(/\b(IEEE|ANSI|AIEE)\s+std\b/, '\1 Std')

  # Part B: Symbol Normalization
  # 3. Additional (TM) patterns - strip them out
  cleaned = cleaned.gsub("(TM)", "")

  # Part C: Year-first format normalization
  # 4. Pattern "62704-4/D4, 2020" -> "IEEE P62704-4/D4, 2020"
  # Only if starts with digits-dash-digits/D pattern
  if cleaned.match?(/^(\d+[-.]\d+)\/D\d+/)
    cleaned = "IEEE P#{cleaned}"
  end

  # Part D: Suffix Normalization
  # 5. "/Preprint" -> remove (data quality - not standard suffix)
  cleaned = cleaned.gsub(/\/Preprint\b/, "")

  # Part E: Relationship Text Normalization
  # 6. "Proposed Revision of" -> "Revision of"
  cleaned = cleaned.gsub("Proposed Revision of", "Revision of")

  # 7. "ammended" typo -> "amended"
  cleaned = cleaned.gsub(/\bammended\b/i, "amended")

  # Part F: Trailing Characters After Special Patterns
  # 8. Remove trailing periods after /INT, /Cor, etc.
  cleaned = cleaned.gsub(/(\/INT|\/Cor\s+\d+-\d{4})\./, '\1')

  # Part G: Conformance Pattern Spacing
  # 9. Fix spacing in "/Conformance" patterns WITHOUT year (malformed only)
  # "1904.1(TM)/Conformance02" -> "1904.1 /Conformance02" (space before slash)
  # BUT: DO NOT touch valid patterns like "802.16/Conformance01-2003" (with year)
  # Use positive check for year suffix to exclude valid patterns
  # Actually, this preprocessing is breaking valid patterns - just remove it entirely
  # The parser can handle both "6/Conformance01-2003" and "6 /Conformance02" formats

  # Part H: Edition Text After /INT
  # 10. Handle ", Month YYYY Edition" after /INT by converting to month-year format
  # "1003.1/INT, March 1994 Edition" -> "1003.1/INT, March 1994"
  cleaned = cleaned.gsub(/(\/INT),\s+([A-Z][a-z]+)\s+(\d{4})\s+Edition/,
                         '\1, \2 \3')

  # Part I: Handle "Ed." abbreviation
  # 11. "Dec. 1994 Ed." -> "Dec. 1994"
  cleaned = cleaned.gsub(/\s+Ed\.\s*$/, "")

  # === PHASE 2: High-impact preprocessing for fixture failures ===

  # Quick wins from SESSION 224 (must come before more complex fixes)

  # Remove period after "Std": "IEEE Std." -> "IEEE Std"
  cleaned = cleaned.gsub(/\bStd\.\s+/, "Std ")


  # Title portion removal after year: "YYYY - IEEE Standard for..."
  cleaned = cleaned.gsub(
    /(\d{4})(\s+\([^)]+\))?\s+-\s+IEEE\s+Standard\s+for.*$/, '\1\2'
  )

  # Fix 2A: "IEEE PC" prefix -> "IEEE Std PC" or "IEEE P" treatment
  # "IEEE PC37.20.9/D7.3A" -> needs to parse as IEEE project draft
  # Strategy: Add "Std" after "IEEE" when followed by "PC" to route to standard pattern
  # Actually, the issue is the number rule consumes "PC37" as P + C37.
  # Better: normalize "IEEE PC" to "IEEE Std PC" so it hits the standard identifier path
  cleaned = cleaned.gsub(/^IEEE\s+PC(\d)/, 'IEEE Std PC\1')
  cleaned = cleaned.gsub(/^IEEE\s+Unapproved\s+Draft\s+Std\s+PC(\d)/,
                         'IEEE Unapproved Draft Std PC\1')

  # Fix 2B: "IEEE P" without "Std"/"Draft" prefix
  # ieee_p_identifier rule handles these directly - no preprocessing needed
  # Only handle "IEEE P" followed by "and ASHRAE" (copub case)
  cleaned = cleaned.gsub(/^IEEE\s+P(\d+)\s+and\s+ASHRAE/,
                         'IEEE Std P\1 and ASHRAE')

  # Fix 2C: "ISO/IEC XXXX-YYYY: Title" -> strip title after colon for ISO/IEC published standards
  # These are ISO-format identifiers with IEEE adoption, strip the title
  cleaned = cleaned.gsub(/^(ISO\/IEC \d+[-.]\d+-\d{4}):.*$/, '\1')
  cleaned = cleaned.gsub(/^(ISO\/IEC \d+-\d{4}):.*$/, '\1')

  # Fix 2D: "ISO/IEC XXXX : YYYY" -> normalize spacing around colon
  cleaned = cleaned.gsub(/^(ISO\/IEC \d+[-.]\d*)\s*:\s*(\d{4})/, '\1:\2')
  cleaned = cleaned.gsub(/^(ISO\/IEC \d+)\s*:\s*(\d{4})/, '\1:\2')

  # Fix 2G: "IEC/IEEE PXXX_D5" -> underscore to slash
  cleaned = cleaned.gsub(/^(IEC\/IEEE P[\w.-]+)_D/, '\1/D')

  # Fix 2H: "IEC XXXX First edition YYYY-MM; IEEE NNNN" -> normalize semicolon
  # Already handled by earlier semicolon normalization

  # Fix 2I: "IEEE/ISO/IEC PXXX/DIS" -> normalize to "ISO/IEC/IEEE PXXX/DIS"
  cleaned = cleaned.gsub(/^IEEE\/ISO\/IEC\s+(P[\w.-]+)/,
                         'ISO/IEC/IEEE \1')
  cleaned = cleaned.gsub(/^IEEE\/IEC\/ISO\s+(P[\w.-]+)/,
                         'IEC/ISO/IEEE \1')

  # Fix 2J: "IEEE/IEC PXXX D5" -> normalize space to slash before D
  cleaned = cleaned.gsub(/^(IEEE\/IEC P[\w.-]+)\s+D(\d)/, '\1/D\2')
  cleaned = cleaned.gsub(
    /^(IEEE\/IEC P[\w.-]+)\s+(CDV|FDIS|CD|DIS|ED\d)/, '\1/\2'
  )

  # Fix 2K: "ISO /IEC/IEEE" -> fix space before slash
  cleaned = cleaned.gsub(/^ISO\s+\/IEC\/IEEE/, "ISO/IEC/IEEE")
  cleaned = cleaned.gsub(/^ISO\s+\/IEC/, "ISO/IEC")

  # Fix 2L: "IS0" typo (letter O instead of digit 0)
  cleaned = cleaned.gsub(/^IS0\//, "ISO/")

  # Fix 2M: "IEEE-P15026-3-DIS-January 2015" -> dash-separated format
  # Normalize to "ISO/IEC/IEEE P15026-3/DIS, January 2015"
  cleaned = cleaned.gsub(/^IEEE-P(\d+)-(\d+)-DIS-(.*)/,
                         'ISO/IEC/IEEE P\1-\2/DIS, \3')

  # Fix 2N: "IEEE/CSA P844.1/293.1/D2" -> normalize CSA dual numbering
  cleaned = cleaned.gsub(/^IEEE\/CSA\s+(P[\d.]+)\/([\d.]+)\/D(\d+)/,
                         'IEEE/CSA \1/D\3')

  # Fix 2O: "IEEE Approved Draft Std P" -> normalize spacing
  cleaned = cleaned.gsub(/^IEEE\s+Approved\s+Draft\s+Std\s+(P\d)/,
                         'IEEE Approved Draft Std \1')
  # Fix: "IEEE Approved Draft Std P1234 / D12" -> remove space before slash
  cleaned = cleaned.gsub(/^(IEEE Approved Draft Std P[\w.-]+)\s+\/\s*D/,
                         '\1/D')

  # Fix 2P: "IEEE/EIA" -> normalize (parser handles IEEE/EIA via copublisher)
  # Already works - no fix needed

  # Fix 2Q: AIEE format variations
  # "AIEE No.1C-1954" -> "AIEE No. 1C-1954" (add space after No.)
  cleaned = cleaned.gsub(/^AIEE\s+No\.\s*(\d)/, 'AIEE No. \1')
  # "AIEE no 700-1945" -> "AIEE No 700-1945" (capitalize)
  cleaned = cleaned.gsub(/^AIEE\s+no\s/, "AIEE No ")
  # "AIEE Std No. 800" -> "AIEE Standard No 800" (normalize type word)
  cleaned = cleaned.gsub(/^AIEE\s+Std\s+No\.\s*/, "AIEE Standard No ")
  # "AIEE No 750.1-1960" -> handled by AIEE parser if decimal support added

  # Fix 2R: "IEEE PSI 10/D2" -> normalize to "IEEE/ASTM PSI 10/D2"
  cleaned = cleaned.gsub(/^IEEE\s+PSI\s+(\d)/, 'IEEE/ASTM PSI \1')

  # Fix 2S: "IEEE/IEC P62271-111/PC37.60_D5" -> normalize
  cleaned = cleaned.gsub(/^(IEEE\/IEC P[\d.-]+\/PC[\d.]+)_D/, '\1/D')

  # Fix 2T: "IEC P62271-111/IEEE PC37.60_D5" -> normalize to IEC/IEEE format
  cleaned = cleaned.gsub(/^IEC\s+(P[\d.-]+)\/IEEE\s+(PC[\d.]+)_D/,
                         'IEC/IEEE \2/D')

  # Fix 2U: "IEC/IEC P" -> "IEC/IEEE P" (typo)
  cleaned = cleaned.gsub(/^IEC\/IEC\s+(P\d)/, 'IEC/IEEE \1')

  # Fix 2V: "NACE SPXXXX-YYYY/IEEE Std NNNN-YYYY" -> normalize slash to parenthetical
  cleaned = cleaned.gsub(/^(NACE\s+SP\d+-\d+)\/(IEEE\s+Std\s+\d+-\d+)$/,
                         '\1 (\2)')

  # Fix 2W: "IEEE Std 802.11g-2003 (Amendment to IEEE Std 802.11, 1999 Edn. (Reaff 2003) as amended by"
  # This is a complex relationship - strip the parenthetical if too complex
  # Let the parser handle it but fix "Edn." to "Edition"
  cleaned = cleaned.gsub("Edn.", "Edition")

  # Fix 2X: "IEEE-P15026-3-DIS" format -> normalize
  # Already handled by Fix 2M

  # Fix 2Y: "P1635/D10/ASHARE 21/D10" -> fix ASHARE typo to ASHRAE
  cleaned = cleaned.gsub("ASHARE", "ASHRAE")

  # Fix 2Z: "PC37.30.2/D043 Rev 18" -> normalize draft version with Rev
  # "PC57-15 D2.0" -> normalize to "P57-15/D2.0"
  cleaned = cleaned.gsub(/^PC(\d)/, 'P\1')

  # Fix 2AA: "IEEE/ISO/IEC 8802-1Q-2020/Amd31-2021" -> normalize
  cleaned = cleaned.gsub(/^IEEE\/ISO\/IEC\s+(8802[\w.-]+)/,
                         'ISO/IEC/IEEE \1')

  # Fix 2AB: "IEEE C57.139/D14June 2010" -> add missing space
  cleaned = cleaned.gsub(
    /^(IEEE\s+C?\d[\d.]*\/D\d+)([A-Z][a-z]+\s+\d{4})/, '\1, \2'
  )

  # Fix 2AC: "IEEE Std: Title" -> strip colon and title (ANSI/IEEE Std: )
  cleaned = cleaned.gsub(/^(ANSI\/IEEE Std):\s+.*$/, '\1')

  # Fix 2AD: "IEEE 1076 IEC 61691-1-1 First edition 2004-10" -> semicolon format
  cleaned = cleaned.gsub(
    /^(IEEE\s+[\d.]+)\s+(IEC\s+\d+[-\d]*\s+.*edition\s+\d{4}-\d{2})$/i, '\1; \2'
  )

  # Fix 2AE: "IEEE No 29-1941 / ASA C77.1-1943" -> normalize to IEEE Std format
  cleaned = cleaned.gsub(/^IEEE\s+No\s+(\d+-\d+)\s+\/\s+ASA\s+(.*)/,
                         'IEEE Std \1 (ASA \2)')

  # Fix 2AF: "IEEE Std 1003.1/2003.l/lNT" -> fix typos
  # .l -> .1 and lNT -> INT handled by existing fixes

  new.parse(cleaned)
end