Start interpreting parsing into property types

This commit is contained in:
Savanni D'Gerinel 2023-09-07 22:05:05 -04:00
parent 0384d789a4
commit a1a5fbe048
2 changed files with 152 additions and 129 deletions

View File

@ -1,12 +1,12 @@
mod date;
pub use date::Date;
pub mod go;
// pub mod go;
mod tree;
use tree::parse_collection;
mod game;
// mod game;
use thiserror::Error;
@ -25,10 +25,7 @@ pub enum Color {
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Position {
row: char,
column: char,
}
pub struct Position(String);
#[derive(Debug)]
pub struct VerboseNomError(nom::error::VerboseError<String>);
@ -70,6 +67,7 @@ impl From<nom::error::Error<&str>> for ParseError {
}
}
/*
pub enum Game {
Go(go::Game),
Unsupported(tree::Tree),
@ -87,6 +85,7 @@ pub fn parse_sgf(input: &str) -> Result<Vec<Game>, Error> {
})
.collect::<Vec<Game>>())
}
*/
/*
impl From<(&str, VerboseErrorKind)> for

View File

@ -1,4 +1,4 @@
use crate::Error;
use crate::{Color, Error, Position};
use nom::{
branch::alt,
bytes::complete::{escaped_transform, tag},
@ -93,6 +93,7 @@ impl ToString for Node {
}
}
/*
impl Node {
pub fn find_prop(&self, ident: &str) -> Option<Property> {
self.properties
@ -105,21 +106,123 @@ impl Node {
self.next.get(0)
}
}
*/
// KO
// MN
// AB
// AE
// AW
// PL
// DM
// GB
// GW
// HO
// N
// UC
// V
// BM
// DO
// IT
// TE
// AR
// CR
// DD
// LB
// LN
// MA
// SL
// SQ
// TR
// AP
// CA
// FF
// GM
// ST
// SZ
// AN
// BR
// BT
// CP
// DT
// EV
// GN
// GC
// ON
// OT
// PB
// PC
// PW
// RE
// RO
// RU
// SO
// TM
// US
// WR
// WT
// BL
// OB
// OW
// WL
// FG
// PM
// VW
#[derive(Clone, Debug, PartialEq)]
pub enum Property {
// B, W
Move((Color, Position)),
// C
Comment(Vec<String>),
Unknown(UnknownProperty),
}
#[derive(Clone, Debug, PartialEq)]
pub struct UnknownProperty {
ident: String,
values: Vec<String>,
}
/*
#[derive(Clone, Debug, PartialEq)]
pub struct Property {
pub ident: String,
pub values: Vec<String>,
}
*/
impl ToString for Property {
fn to_string(&self) -> String {
let values = self
.values
match self {
Property::Move((color, position)) => format!(
"{}[{}]",
match color {
Color::White => "W",
Color::Black => "B",
},
position.0
),
Property::Comment(values) => format!(
"C{}",
values
.iter()
.map(|v| format!("[{}]", v))
.collect::<String>()
),
Property::Unknown(UnknownProperty { ident, values }) => {
let values = values
.iter()
.map(|val| format!("[{}]", val))
.collect::<String>();
format!("{}{}", self.ident, values)
format!("{}{}", ident, values)
}
}
/*
let values = self
.collect::<String>();
*/
}
}
@ -174,13 +277,18 @@ fn parse_property<'a, E: nom::error::ParseError<&'a str>>(
.into_iter()
.map(|v| v.to_owned())
.collect::<Vec<String>>();
Ok((
input,
Property {
let prop = match ident {
"W" => Property::Move((Color::White, Position(values[0].clone()))),
"B" => Property::Move((Color::Black, Position(values[0].clone()))),
"C" => Property::Comment(values),
_ => Property::Unknown(UnknownProperty {
ident: ident.to_owned(),
values,
},
))
}),
};
Ok((input, prop))
}
fn parse_propval<'a, E: nom::error::ParseError<&'a str>>(
@ -221,21 +329,12 @@ mod test {
#[test]
fn it_can_parse_properties() {
let (_, prop) = parse_property::<nom::error::VerboseError<&str>>("C[a]").unwrap();
assert_eq!(
prop,
Property {
ident: "C".to_owned(),
values: vec!["a".to_owned()]
}
);
assert_eq!(prop, Property::Comment(vec!["a".to_owned()]));
let (_, prop) = parse_property::<nom::error::VerboseError<&str>>("C[a][b][c]").unwrap();
assert_eq!(
prop,
Property {
ident: "C".to_owned(),
values: vec!["a".to_owned(), "b".to_owned(), "c".to_owned()]
}
Property::Comment(vec!["a".to_owned(), "b".to_owned(), "c".to_owned()])
);
}
@ -246,10 +345,7 @@ mod test {
assert_eq!(
node,
Node {
properties: vec![Property {
ident: "B".to_owned(),
values: vec!["ab".to_owned()]
}],
properties: vec![Property::Move((Color::Black, Position("ab".to_owned())))],
next: vec![]
}
);
@ -261,25 +357,13 @@ mod test {
assert_eq!(
node,
Node {
properties: vec![Property {
ident: "B".to_owned(),
values: vec!["ab".to_owned()]
}],
properties: vec![Property::Move((Color::Black, Position("ab".to_owned())))],
next: vec![Node {
properties: vec![Property {
ident: "W".to_owned(),
values: vec!["dp".to_owned()]
}],
properties: vec![Property::Move((Color::White, Position("dp".to_owned())))],
next: vec![Node {
properties: vec![
Property {
ident: "B".to_owned(),
values: vec!["pq".to_owned()]
},
Property {
ident: "C".to_owned(),
values: vec!["some comments".to_owned()]
}
Property::Move((Color::Black, Position("pq".to_owned()))),
Property::Comment(vec!["some comments".to_owned()])
],
next: vec![],
}]
@ -297,25 +381,13 @@ mod test {
assert_eq!(
sequence,
Node {
properties: vec![Property {
ident: "B".to_owned(),
values: vec!["ab".to_owned()]
}],
properties: vec![Property::Move((Color::Black, Position("ab".to_owned())))],
next: vec![Node {
properties: vec![Property {
ident: "W".to_owned(),
values: vec!["dp".to_owned()]
}],
properties: vec![Property::Move((Color::White, Position("dp".to_owned())))],
next: vec![Node {
properties: vec![
Property {
ident: "B".to_owned(),
values: vec!["pq".to_owned()]
},
Property {
ident: "C".to_owned(),
values: vec!["some comments".to_owned()]
}
Property::Move((Color::Black, Position("pq".to_owned()))),
Property::Comment(vec!["some comments".to_owned()])
],
next: vec![],
}]
@ -330,33 +402,18 @@ mod test {
let (_, tree) = parse_tree::<nom::error::VerboseError<&str>>(text).unwrap();
let expected = Node {
properties: vec![Property {
ident: "C".to_owned(),
values: vec!["a".to_owned()],
}],
properties: vec![Property::Comment(vec!["a".to_owned()])],
next: vec![Node {
properties: vec![Property {
ident: "C".to_owned(),
values: vec!["b".to_owned()],
}],
properties: vec![Property::Comment(vec!["b".to_owned()])],
next: vec![
Node {
properties: vec![Property {
ident: "C".to_owned(),
values: vec!["c".to_owned()],
}],
properties: vec![Property::Comment(vec!["c".to_owned()])],
next: vec![],
},
Node {
properties: vec![Property {
ident: "C".to_owned(),
values: vec!["d".to_owned()],
}],
properties: vec![Property::Comment(vec!["d".to_owned()])],
next: vec![Node {
properties: vec![Property {
ident: "C".to_owned(),
values: vec!["e".to_owned()],
}],
properties: vec![Property::Comment(vec!["e".to_owned()])],
next: vec![],
}],
},
@ -372,85 +429,52 @@ mod test {
let (_, tree) = parse_tree::<nom::error::VerboseError<&str>>(EXAMPLE).unwrap();
let j = Node {
properties: vec![Property {
ident: "C".to_owned(),
values: vec!["j".to_owned()],
}],
properties: vec![Property::Comment(vec!["j".to_owned()])],
next: vec![],
};
let i = Node {
properties: vec![Property {
ident: "C".to_owned(),
values: vec!["i".to_owned()],
}],
properties: vec![Property::Comment(vec!["i".to_owned()])],
next: vec![],
};
let h = Node {
properties: vec![Property {
ident: "C".to_owned(),
values: vec!["h".to_owned()],
}],
properties: vec![Property::Comment(vec!["h".to_owned()])],
next: vec![i],
};
let g = Node {
properties: vec![Property {
ident: "C".to_owned(),
values: vec!["g".to_owned()],
}],
properties: vec![Property::Comment(vec!["g".to_owned()])],
next: vec![h],
};
let f = Node {
properties: vec![Property {
ident: "C".to_owned(),
values: vec!["f".to_owned()],
}],
properties: vec![Property::Comment(vec!["f".to_owned()])],
next: vec![g, j],
};
let e = Node {
properties: vec![Property {
ident: "C".to_owned(),
values: vec!["e".to_owned()],
}],
properties: vec![Property::Comment(vec!["e".to_owned()])],
next: vec![],
};
let d = Node {
properties: vec![Property {
ident: "C".to_owned(),
values: vec!["d".to_owned()],
}],
properties: vec![Property::Comment(vec!["d".to_owned()])],
next: vec![e],
};
let c = Node {
properties: vec![Property {
ident: "C".to_owned(),
values: vec!["c".to_owned()],
}],
properties: vec![Property::Comment(vec!["c".to_owned()])],
next: vec![],
};
let b = Node {
properties: vec![Property {
ident: "C".to_owned(),
values: vec!["b".to_owned()],
}],
properties: vec![Property::Comment(vec!["b".to_owned()])],
next: vec![c, d],
};
let a = Node {
properties: vec![Property {
ident: "C".to_owned(),
values: vec!["a".to_owned()],
}],
properties: vec![Property::Comment(vec!["a".to_owned()])],
next: vec![b],
};
let expected = Node {
properties: vec![
Property {
Property::Unknown(UnknownProperty {
ident: "FF".to_owned(),
values: vec!["4".to_owned()],
},
Property {
ident: "C".to_owned(),
values: vec!["root".to_owned()],
},
}),
Property::Comment(vec!["root".to_owned()]),
],
next: vec![a, f],
};