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

View File

@ -1,4 +1,4 @@
use crate::Error; use crate::{Color, Error, Position};
use nom::{ use nom::{
branch::alt, branch::alt,
bytes::complete::{escaped_transform, tag}, bytes::complete::{escaped_transform, tag},
@ -93,6 +93,7 @@ impl ToString for Node {
} }
} }
/*
impl Node { impl Node {
pub fn find_prop(&self, ident: &str) -> Option<Property> { pub fn find_prop(&self, ident: &str) -> Option<Property> {
self.properties self.properties
@ -105,21 +106,123 @@ impl Node {
self.next.get(0) 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)] #[derive(Clone, Debug, PartialEq)]
pub struct Property { pub struct Property {
pub ident: String, pub ident: String,
pub values: Vec<String>, pub values: Vec<String>,
} }
*/
impl ToString for Property { impl ToString for Property {
fn to_string(&self) -> String { fn to_string(&self) -> String {
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!("{}{}", ident, values)
}
}
/*
let values = self let values = self
.values
.iter()
.map(|val| format!("[{}]", val))
.collect::<String>(); .collect::<String>();
format!("{}{}", self.ident, values) */
} }
} }
@ -174,13 +277,18 @@ fn parse_property<'a, E: nom::error::ParseError<&'a str>>(
.into_iter() .into_iter()
.map(|v| v.to_owned()) .map(|v| v.to_owned())
.collect::<Vec<String>>(); .collect::<Vec<String>>();
Ok((
input, let prop = match ident {
Property { "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(), ident: ident.to_owned(),
values, values,
}, }),
)) };
Ok((input, prop))
} }
fn parse_propval<'a, E: nom::error::ParseError<&'a str>>( fn parse_propval<'a, E: nom::error::ParseError<&'a str>>(
@ -221,21 +329,12 @@ mod test {
#[test] #[test]
fn it_can_parse_properties() { fn it_can_parse_properties() {
let (_, prop) = parse_property::<nom::error::VerboseError<&str>>("C[a]").unwrap(); let (_, prop) = parse_property::<nom::error::VerboseError<&str>>("C[a]").unwrap();
assert_eq!( assert_eq!(prop, Property::Comment(vec!["a".to_owned()]));
prop,
Property {
ident: "C".to_owned(),
values: vec!["a".to_owned()]
}
);
let (_, prop) = parse_property::<nom::error::VerboseError<&str>>("C[a][b][c]").unwrap(); let (_, prop) = parse_property::<nom::error::VerboseError<&str>>("C[a][b][c]").unwrap();
assert_eq!( assert_eq!(
prop, prop,
Property { Property::Comment(vec!["a".to_owned(), "b".to_owned(), "c".to_owned()])
ident: "C".to_owned(),
values: vec!["a".to_owned(), "b".to_owned(), "c".to_owned()]
}
); );
} }
@ -246,10 +345,7 @@ mod test {
assert_eq!( assert_eq!(
node, node,
Node { Node {
properties: vec![Property { properties: vec![Property::Move((Color::Black, Position("ab".to_owned())))],
ident: "B".to_owned(),
values: vec!["ab".to_owned()]
}],
next: vec![] next: vec![]
} }
); );
@ -261,25 +357,13 @@ mod test {
assert_eq!( assert_eq!(
node, node,
Node { Node {
properties: vec![Property { properties: vec![Property::Move((Color::Black, Position("ab".to_owned())))],
ident: "B".to_owned(),
values: vec!["ab".to_owned()]
}],
next: vec![Node { next: vec![Node {
properties: vec![Property { properties: vec![Property::Move((Color::White, Position("dp".to_owned())))],
ident: "W".to_owned(),
values: vec!["dp".to_owned()]
}],
next: vec![Node { next: vec![Node {
properties: vec![ properties: vec![
Property { Property::Move((Color::Black, Position("pq".to_owned()))),
ident: "B".to_owned(), Property::Comment(vec!["some comments".to_owned()])
values: vec!["pq".to_owned()]
},
Property {
ident: "C".to_owned(),
values: vec!["some comments".to_owned()]
}
], ],
next: vec![], next: vec![],
}] }]
@ -297,25 +381,13 @@ mod test {
assert_eq!( assert_eq!(
sequence, sequence,
Node { Node {
properties: vec![Property { properties: vec![Property::Move((Color::Black, Position("ab".to_owned())))],
ident: "B".to_owned(),
values: vec!["ab".to_owned()]
}],
next: vec![Node { next: vec![Node {
properties: vec![Property { properties: vec![Property::Move((Color::White, Position("dp".to_owned())))],
ident: "W".to_owned(),
values: vec!["dp".to_owned()]
}],
next: vec![Node { next: vec![Node {
properties: vec![ properties: vec![
Property { Property::Move((Color::Black, Position("pq".to_owned()))),
ident: "B".to_owned(), Property::Comment(vec!["some comments".to_owned()])
values: vec!["pq".to_owned()]
},
Property {
ident: "C".to_owned(),
values: vec!["some comments".to_owned()]
}
], ],
next: vec![], next: vec![],
}] }]
@ -330,33 +402,18 @@ mod test {
let (_, tree) = parse_tree::<nom::error::VerboseError<&str>>(text).unwrap(); let (_, tree) = parse_tree::<nom::error::VerboseError<&str>>(text).unwrap();
let expected = Node { let expected = Node {
properties: vec![Property { properties: vec![Property::Comment(vec!["a".to_owned()])],
ident: "C".to_owned(),
values: vec!["a".to_owned()],
}],
next: vec![Node { next: vec![Node {
properties: vec![Property { properties: vec![Property::Comment(vec!["b".to_owned()])],
ident: "C".to_owned(),
values: vec!["b".to_owned()],
}],
next: vec![ next: vec![
Node { Node {
properties: vec![Property { properties: vec![Property::Comment(vec!["c".to_owned()])],
ident: "C".to_owned(),
values: vec!["c".to_owned()],
}],
next: vec![], next: vec![],
}, },
Node { Node {
properties: vec![Property { properties: vec![Property::Comment(vec!["d".to_owned()])],
ident: "C".to_owned(),
values: vec!["d".to_owned()],
}],
next: vec![Node { next: vec![Node {
properties: vec![Property { properties: vec![Property::Comment(vec!["e".to_owned()])],
ident: "C".to_owned(),
values: vec!["e".to_owned()],
}],
next: vec![], next: vec![],
}], }],
}, },
@ -372,85 +429,52 @@ mod test {
let (_, tree) = parse_tree::<nom::error::VerboseError<&str>>(EXAMPLE).unwrap(); let (_, tree) = parse_tree::<nom::error::VerboseError<&str>>(EXAMPLE).unwrap();
let j = Node { let j = Node {
properties: vec![Property { properties: vec![Property::Comment(vec!["j".to_owned()])],
ident: "C".to_owned(),
values: vec!["j".to_owned()],
}],
next: vec![], next: vec![],
}; };
let i = Node { let i = Node {
properties: vec![Property { properties: vec![Property::Comment(vec!["i".to_owned()])],
ident: "C".to_owned(),
values: vec!["i".to_owned()],
}],
next: vec![], next: vec![],
}; };
let h = Node { let h = Node {
properties: vec![Property { properties: vec![Property::Comment(vec!["h".to_owned()])],
ident: "C".to_owned(),
values: vec!["h".to_owned()],
}],
next: vec![i], next: vec![i],
}; };
let g = Node { let g = Node {
properties: vec![Property { properties: vec![Property::Comment(vec!["g".to_owned()])],
ident: "C".to_owned(),
values: vec!["g".to_owned()],
}],
next: vec![h], next: vec![h],
}; };
let f = Node { let f = Node {
properties: vec![Property { properties: vec![Property::Comment(vec!["f".to_owned()])],
ident: "C".to_owned(),
values: vec!["f".to_owned()],
}],
next: vec![g, j], next: vec![g, j],
}; };
let e = Node { let e = Node {
properties: vec![Property { properties: vec![Property::Comment(vec!["e".to_owned()])],
ident: "C".to_owned(),
values: vec!["e".to_owned()],
}],
next: vec![], next: vec![],
}; };
let d = Node { let d = Node {
properties: vec![Property { properties: vec![Property::Comment(vec!["d".to_owned()])],
ident: "C".to_owned(),
values: vec!["d".to_owned()],
}],
next: vec![e], next: vec![e],
}; };
let c = Node { let c = Node {
properties: vec![Property { properties: vec![Property::Comment(vec!["c".to_owned()])],
ident: "C".to_owned(),
values: vec!["c".to_owned()],
}],
next: vec![], next: vec![],
}; };
let b = Node { let b = Node {
properties: vec![Property { properties: vec![Property::Comment(vec!["b".to_owned()])],
ident: "C".to_owned(),
values: vec!["b".to_owned()],
}],
next: vec![c, d], next: vec![c, d],
}; };
let a = Node { let a = Node {
properties: vec![Property { properties: vec![Property::Comment(vec!["a".to_owned()])],
ident: "C".to_owned(),
values: vec!["a".to_owned()],
}],
next: vec![b], next: vec![b],
}; };
let expected = Node { let expected = Node {
properties: vec![ properties: vec![
Property { Property::Unknown(UnknownProperty {
ident: "FF".to_owned(), ident: "FF".to_owned(),
values: vec!["4".to_owned()], values: vec!["4".to_owned()],
}, }),
Property { Property::Comment(vec!["root".to_owned()]),
ident: "C".to_owned(),
values: vec!["root".to_owned()],
},
], ],
next: vec![a, f], next: vec![a, f],
}; };