Skip to content

Commit

Permalink
Use more nom parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
Gekkio committed Nov 10, 2024
1 parent cd5fbb5 commit 9149e8a
Show file tree
Hide file tree
Showing 13 changed files with 532 additions and 386 deletions.
33 changes: 28 additions & 5 deletions backend/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,12 @@ pub use self::{
coil::Coil,
dmg_stamp::DmgStamp,
eeprom::Eeprom,
gbs_dol::GbsDol,
gen1_soc::{Gen1Soc, Gen1SocKind},
gen2_soc::{Gen2Soc, Gen2SocKind},
icd2::Icd2,
lcd_chip::LcdChip,
lcd_screen::LcdScreen,
mapper::{Huc1Version, Mapper, MapperType, Mbc1Version, Mbc2Version, Mbc3Version},
mask_rom::MaskRom,
sgb_rom::SgbRom,
tama::{Tama, TamaType},
};

Expand All @@ -53,12 +50,10 @@ pub mod crystal_8mihz;
pub mod dmg_stamp;
pub mod eeprom;
pub mod fujitsu;
pub mod gbs_dol;
pub mod gen1_soc;
pub mod gen2_soc;
pub mod hynix;
pub mod hyundai;
pub mod icd2;
pub mod lcd_chip;
pub mod lcd_screen;
pub mod lgs;
Expand All @@ -68,6 +63,7 @@ pub mod mask_rom;
pub mod mitsubishi;
pub mod mitsumi;
pub mod nec;
pub mod oki;
pub mod oxy_u4;
pub mod oxy_u5;
pub mod rohm;
Expand Down Expand Up @@ -313,6 +309,20 @@ mod for_nom {
recognize(fold_many_m_n(min, max, satisfy(f), || (), |_, _| ()))
}

pub fn satisfy_m_n_complete<'a, E: ParseError<&'a str>>(
min: usize,
max: usize,
f: impl Fn(char) -> bool,
) -> impl Parser<&'a str, &'a str, E> {
recognize(fold_many_m_n(
min,
max,
nom::character::complete::satisfy(f),
|| (),
|_, _| (),
))
}

pub fn alnum_uppers<'a, E: ParseError<&'a str>>(
count: usize,
) -> impl Parser<&'a str, &'a str, E> {
Expand Down Expand Up @@ -655,3 +665,16 @@ pub fn ags_pmic_old() -> &'static impl LabelParser<GenericPart> {
pub fn fram_sop_28_3v3() -> &'static impl LabelParser<GenericPart> {
multi_parser!(GenericPart, &fujitsu::FUJITSU_MB85R256,)
}

pub fn gbs_dol() -> &'static impl LabelParser<GenericPart> {
&nec::NEC_GBS_DOL
}

pub fn icd2() -> &'static impl LabelParser<GenericPart> {
multi_parser!(
GenericPart,
&rohm::ROHM_ICD2_R,
&nec::NEC_ICD2_N,
&nec::NEC_ICD2_R
)
}
12 changes: 7 additions & 5 deletions backend/src/parser/agb_soc_bga.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
//
// SPDX-License-Identifier: MIT

use super::{week2, year2, ChipYearWeek, LabelParser};
use crate::macros::single_parser;
use super::{week2, year2, GenericPart, LabelParser};
use crate::{macros::single_parser, parser::PartDateCode};

pub type AgbSoc = ChipYearWeek;
pub type AgbSoc = GenericPart;

/// ```
/// use gbhwdb_backend::parser::{self, LabelParser};
Expand All @@ -19,8 +19,10 @@ pub fn agb_soc_bga() -> &'static impl LabelParser<AgbSoc> {
Ok(AgbSoc {
kind: c[3].to_owned(),
manufacturer: None,
year: Some(year2(&c[1])?),
week: Some(week2(&c[2])?),
date_code: Some(PartDateCode::YearWeek {
year: year2(&c[1])?,
week: week2(&c[2])?,
}),
})
},
)
Expand Down
27 changes: 26 additions & 1 deletion backend/src/parser/fujitsu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use nom::{
bytes::streaming::tag,
character::{complete::one_of, streaming::char},
combinator::{opt, recognize},
sequence::tuple,
sequence::{terminated, tuple},
Parser as _,
};

Expand Down Expand Up @@ -110,3 +110,28 @@ pub static FUJITSU_MASK_ROM: NomParser<MaskRom> = NomParser {
.parse(input)
},
};

/// Fujitsu SGB mask ROM
///
/// ```
/// use gbhwdb_backend::parser::{self, LabelParser};
/// assert!(parser::fujitsu::FUJITSU_SGB_ROM.parse("SYS-SGB-2 © 1994 Nintendo 9429 R77").is_ok());
/// ```
pub static FUJITSU_SGB_ROM: NomParser<MaskRom> = NomParser {
name: "Fujitsu SGB ROM",
f: |input| {
tuple((
terminated(tag("SYS-SGB-2"), tag(" © 1994 Nintendo ")),
year2_week2,
char(' '),
uppers(1).and(digits(2)),
))
.map(|(rom_id, date_code, _, _)| MaskRom {
rom_id: String::from(rom_id),
manufacturer: Some(Manufacturer::Fujitsu),
chip_type: None,
date_code: Some(date_code),
})
.parse(input)
},
};
31 changes: 0 additions & 31 deletions backend/src/parser/gbs_dol.rs

This file was deleted.

50 changes: 0 additions & 50 deletions backend/src/parser/icd2.rs

This file was deleted.

Loading

0 comments on commit 9149e8a

Please sign in to comment.