Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 93 additions & 1 deletion src/parser/sections/tests/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
use super::super::components::*;

use crate::parser::KeywordParam;
use crate::types::{Layer, Mirror, RectangleRef, TextPar, XYRef};
use crate::types::{Attribute, Layer, Mirror, RectangleRef, TextPar, XYRef};

#[test]
fn test_example_components() {
Expand Down Expand Up @@ -226,3 +226,95 @@ fn test_component_with_improperly_formatted_strings() {
},]
);
}

#[test]
fn test_component_with_multiple_attributes() {
let params = vec![
KeywordParam {
keyword: "COMPONENT",
parameter: "Foo",
},
KeywordParam {
keyword: "PLACE",
parameter: "0.0 1.0",
},
KeywordParam {
keyword: "LAYER",
parameter: "TOP",
},
KeywordParam {
keyword: "ROTATION",
parameter: "0",
},
KeywordParam {
keyword: "SHAPE",
parameter: "Foo 0 0",
},
KeywordParam {
keyword: "DEVICE",
parameter: "Device Foo",
},
KeywordParam {
keyword: "ATTRIBUTE",
parameter: r#"COMPONENT_1 "OperatingTemp" "-40°C - +85°C""#,
},
KeywordParam {
keyword: "ATTRIBUTE",
parameter: r#"COMPONENT_2 "Technology" "SMD""#,
},
KeywordParam {
keyword: "ATTRIBUTE",
parameter: r#"COMPONENT_3 "Manufacturer" "Foobar Tech.""#,
},
KeywordParam {
keyword: "ATTRIBUTE",
parameter: r#"COMPONENT_4 "ComponentLinkUrl" "\\foobar.tech\unc\path\which\ends\with\a\backslash\""#,
},
];

let components = parse_components(&params).unwrap();

assert_eq!(
components,
vec![Component {
name: "Foo".to_string(),
device: "Device Foo".to_string(),
place: XYRef {
x: 0.0,
y: 1.0
},
layer: Layer::Top,
rotation: 0.0,
shape: Shape {
name: "Foo".to_string(),
mirror: Mirror::Not,
flip: false
},
subcomponents: vec![],
texts: vec![],
sheet: None,
attributes: vec![
Attribute {
category: "COMPONENT_1".to_string(),
name: "OperatingTemp".to_string(),
data: "-40°C - +85°C".to_string()
},
Attribute {
category: "COMPONENT_2".to_string(),
name: "Technology".to_string(),
data: "SMD".to_string()
},
Attribute {
category: "COMPONENT_3".to_string(),
name: "Manufacturer".to_string(),
data: "Foobar Tech.".to_string()
},
Attribute {
category: "COMPONENT_4".to_string(),
name: "ComponentLinkUrl".to_string(),
data: r#"\\foobar.tech\unc\path\which\ends\with\a\backslash\"#.to_string()
},
],
},]
);
}
30 changes: 23 additions & 7 deletions src/parser/types/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

use nom::branch::alt;
use nom::bytes::complete::{take_while, take_while1};
use nom::character::complete::char;
use nom::character::complete::{char, satisfy};
use nom::combinator::{map, not, peek, value};
use nom::multi::many0;
use nom::sequence::{delimited, preceded};
Expand All @@ -33,7 +33,8 @@ enum QuotedStringFragment<'a> {
}

fn is_valid_char(c: char) -> bool {
matches!(c, ' '..='~')
// Accept all Unicode except control characters
!c.is_control()
}

fn is_valid_char_but_not_backslash_or_quote(c: char) -> bool {
Expand All @@ -46,8 +47,17 @@ fn is_valid_char_but_not_space(c: char) -> bool {

fn backslash_sequence(s: &str) -> IResult<&str, char> {
alt((
// A backslash before a quote mark becomes a quote mark
preceded(char('\\'), value('"', char('"'))),
// A backslash before a quote mark becomes a quote mark, only if not at the end and followed by a printable character
preceded(
char('\\'),
preceded(
peek((
char('"'),
satisfy(is_valid_char),
)),
value('"', char('"')),
),
),
// Backslash before any other character is not an escape sequence--it's just a literal backslash
value('\\', char('\\')),
))
Expand Down Expand Up @@ -112,6 +122,8 @@ mod tests {
assert_eq!(backslash_sequence(r#"\;"#), Ok((";", '\\')));
assert_eq!(backslash_sequence(r#"\\;"#), Ok((r#"\;"#, '\\')));
assert_eq!(backslash_sequence(r#"\";"#), Ok((";", '"')));
assert_eq!(backslash_sequence(r#"\""#), Ok(("\"", '\\')));
assert_eq!(backslash_sequence(r#"\"#), Ok(("", '\\')));
}

#[test]
Expand Down Expand Up @@ -184,8 +196,12 @@ mod tests {
assert!(unquoted_string("\"qoY@M;").is_err());
assert!(unquoted_string(r#""A""#).is_err());

// No non-ASCII characters
assert_eq!(unquoted_string("V3\"'😀"), Ok(("😀", "V3\"'")));
assert_eq!(unquoted_string("A'😀A4%"), Ok(("😀A4%", "A'")));
// Unicode characters are allowed
assert_eq!(unquoted_string("V3\"'😀"), Ok(("", "V3\"'😀")));
assert_eq!(unquoted_string("A'😀A4%"), Ok(("", "A'😀A4%")));

// Control characters are not allowed
assert_eq!(unquoted_string("abc\u{0007}def"), Ok(("\u{0007}def", "abc")));
assert_eq!(unquoted_string("\u{0001}abc"), Ok(("\u{0001}abc", "")));
}
}
15 changes: 15 additions & 0 deletions src/parser/types/tests/attrib_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,18 @@ fn test_ok() {
))
);
}

#[test]
fn test_attrib_ref_quoted() {
assert_eq!(
attrib_ref(r#"COMPONENT "OperatingTemp" "-40°C - +85°C""#),
Ok((
"",
Attribute {
category: "COMPONENT".to_string(),
name: "OperatingTemp".to_string(),
data: "-40°C - +85°C".to_string()
}
))
);
}