Skip to content

Commit

Permalink
Add initial support for multiline parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Gekkio committed Dec 7, 2024
1 parent f324ef7 commit 7e7dd39
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 52 deletions.
29 changes: 29 additions & 0 deletions model/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ pub fn year1(text: &str) -> Result<Year, String> {

mod for_nom {
use nom::{
branch::alt,
bytes::streaming::{tag, take},
character::streaming::{anychar, char, satisfy},
combinator::{map_opt, recognize},
Expand Down Expand Up @@ -416,6 +417,34 @@ mod for_nom {
})
.parse(input)
}

fn line_sep<'a, E: ParseError<&'a str>>(input: &'a str) -> IResult<&'a str, char, E> {
alt((char(' '), char('\n'))).parse(input)
}

pub fn lines2<'a, O1, O2, O3, E: ParseError<&'a str>>(
a: impl Parser<&'a str, O1, E>,
b: impl Parser<&'a str, O2, E>,
) -> impl Parser<&'a str, (O1, O2), E> {
tuple((a, line_sep, b)).map(|(a, _, b)| (a, b))
}

pub fn lines3<'a, O1, O2, O3, E: ParseError<&'a str>>(
a: impl Parser<&'a str, O1, E>,
b: impl Parser<&'a str, O2, E>,
c: impl Parser<&'a str, O3, E>,
) -> impl Parser<&'a str, (O1, O2, O3), E> {
tuple((a, line_sep, b, line_sep, c)).map(|(a, _, b, _, c)| (a, b, c))
}

pub fn lines4<'a, O1, O2, O3, O4, E: ParseError<&'a str>>(
a: impl Parser<&'a str, O1, E>,
b: impl Parser<&'a str, O2, E>,
c: impl Parser<&'a str, O3, E>,
d: impl Parser<&'a str, O4, E>,
) -> impl Parser<&'a str, (O1, O2, O3, O4), E> {
tuple((a, line_sep, b, line_sep, c, line_sep, d)).map(|(a, _, b, _, c, _, d)| (a, b, c, d))
}
}

pub fn year2(text: &str) -> Result<Year, String> {
Expand Down
68 changes: 30 additions & 38 deletions model/src/parser/hynix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ use nom::{
character::streaming::{char, one_of},
combinator::{opt, recognize, value},
error::ParseError,
sequence::tuple,
sequence::{separated_pair, tuple},
IResult, Parser,
};

use super::{
for_nom::{agb_rom_code, digits, uppers, year2_week2},
for_nom::{agb_rom_code, digits, lines3, lines4, uppers, year2_week2},
sram::Ram,
GameMaskRom, GameRomType, PartDateCode,
};
Expand All @@ -31,21 +31,22 @@ use crate::parser::{Manufacturer, NomParser};
pub static HYNIX_HY62LF16206: NomParser<Ram> = NomParser {
name: "Hynix HY62LF16206",
f: |input| {
tuple((
tag("Hynix KOREA "),
lines3(
separated_pair(tag("Hynix"), char(' '), tag("KOREA")),
recognize(tag("HY62LF16206").and(opt(one_of("AB")))),
char(' '),
date_code.and(process_code),
char(' '),
tuple((
tag("L"), // power
tag("T"), // package
tag("12"), // speed
tag("C"), // temperature
)),
))
separated_pair(
date_code.and(process_code),
char(' '),
tuple((
tag("L"), // power
tag("T"), // package
tag("12"), // speed
tag("C"), // temperature
)),
),
)
.map(
|(_, kind, _, (date_code, _), _, (power, package, speed, temp))| Ram {
|(_, kind, ((date_code, _), (power, package, speed, temp)))| Ram {
kind: format!("{kind}-{power}{package}{speed}{temp}"),
manufacturer: Some(Manufacturer::Hynix),
date_code: Some(date_code),
Expand All @@ -67,25 +68,21 @@ pub static HYNIX_HY62LF16206: NomParser<Ram> = NomParser {
pub static HYNIX_HY62WT08081: NomParser<Ram> = NomParser {
name: "Hynix HY62WT08081",
f: |input| {
tuple((
tag("hynix "),
date_code.and(process_code),
char(' '),
lines3(
separated_pair(tag("hynix"), char(' '), date_code.and(process_code)),
tuple((
recognize(value("HY62WT08081", tag("HY62WT081")).and(opt(one_of("ABCDE")))),
alt((tag("L"), tag("D"))), // power
alt((tag("50"), tag("70"))), // speed
alt((tag("C"), tag("E"), tag("I"))), // temperature
)),
tag(" KOREA"),
))
.map(
|(_, (date_code, _), _, (kind, power, speed, temp), _)| Ram {
kind: format!("{kind}{power}{speed}{temp}"),
manufacturer: Some(Manufacturer::Hynix),
date_code: Some(date_code),
},
tag("KOREA"),
)
.map(|((_, (date_code, _)), (kind, power, speed, temp), _)| Ram {
kind: format!("{kind}{power}{speed}{temp}"),
manufacturer: Some(Manufacturer::Hynix),
date_code: Some(date_code),
})
.parse(input)
},
};
Expand All @@ -94,18 +91,13 @@ fn ac23v<'a, E: ParseError<&'a str>>(
chip_type: &'static str,
rom_type: GameRomType,
) -> impl Parser<&'a str, GameMaskRom, E> {
tuple((
tag("HYNIX "),
lines4(
tag("HYNIX"),
tag(chip_type),
char(' '),
agb_rom_code(),
char(' '),
tag(rom_type.as_str()),
char(' '),
alt((tag("NL"), tag("ZBR"))),
digits(4),
))
.map(move |(_, kind, _, rom_id, _, _, _, _, _)| GameMaskRom {
separated_pair(agb_rom_code(), char(' '), tag(rom_type.as_str())),
alt((tag("NL"), tag("ZBR"))).and(digits(4)),
)
.map(move |(_, kind, (rom_id, _), _)| GameMaskRom {
rom_id: String::from(rom_id),
rom_type,
manufacturer: Some(Manufacturer::Hynix),
Expand Down
26 changes: 12 additions & 14 deletions model/src/parser/magnachip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

use nom::{
branch::alt, bytes::streaming::tag, character::streaming::char, error::ParseError,
sequence::tuple, Parser,
sequence::separated_pair, Parser,
};

use super::{
for_nom::{agb_rom_code, digits},
for_nom::{agb_rom_code, digits, lines4},
GameMaskRom, GameRomType,
};
use crate::parser::{Manufacturer, NomParser};
Expand All @@ -17,19 +17,17 @@ fn ac23v<'a, E: ParseError<&'a str>>(
chip_type: &'static str,
rom_type: GameRomType,
) -> impl Parser<&'a str, GameMaskRom, E> {
tuple((
tag("MAGNACHIP "),
lines4(
tag("MAGNACHIP"),
tag(chip_type),
char(' '),
agb_rom_code(),
char(' '),
tag(rom_type.as_str()),
char(' '),
alt((tag("GB"), tag("SP"))),
digits(4),
tag(" PS"),
))
.map(move |(_, kind, _, rom_id, _, _, _, _, _, _)| GameMaskRom {
separated_pair(agb_rom_code(), char(' '), tag(rom_type.as_str())),
separated_pair(
alt((tag("GB"), tag("SP"))).and(digits(4)),
char(' '),
tag("PS"),
),
)
.map(move |(_, kind, (rom_id, _), _)| GameMaskRom {
rom_id: String::from(rom_id),
rom_type,
manufacturer: Some(Manufacturer::Magnachip),
Expand Down

0 comments on commit 7e7dd39

Please sign in to comment.