Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 2 #13

Draft
wants to merge 26 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add dates
Mubelotix committed Jan 14, 2023
commit deac165e51633d8f3552d68e898dea45b8c60fd7
28 changes: 23 additions & 5 deletions src/address.pest
Original file line number Diff line number Diff line change
@@ -3,13 +3,15 @@
WSP = _{ " " | "\t" } // todo: such alias slow down the parser
CRLF = _{ "\r\n" }

// 3.2.1. Quoted characters
// 3.2. Lexical Tokens

// 3.2.1. Quoted characters

VCHAR = _{ '\x21'..'\x7E' }
vchar_seq = { VCHAR+ }
quoted_pair = { "\\" ~ (VCHAR | WSP) }

// 3.2.2. Folding White Space and Comments
// 3.2.2. Folding White Space and Comments

FWS = _{ (WSP* ~ CRLF)? ~ WSP+ }
CTEXT = _{ '!'..'\'' | '*'..'[' | ']'..'~' }
@@ -18,7 +20,7 @@ ccontent = _{ ctext_seq | quoted_pair | comment }
comment = { "(" ~ (FWS? ~ ccontent)* ~ FWS? ~ ")" }
CFWS = _{ ((FWS? ~ comment)+ ~ FWS?) | FWS }

// 3.2.3. Atom
// 3.2.3. Atom

ATEXT = _{ ASCII_ALPHANUMERIC | "!" | "#" | "$" | "%" | "&" | "'" | "*" | "+" | "-" | "/" | "=" | "?" | "^" | "_" | "`" | "{" | "|" | "}" | "~" }
atext_seq = { ATEXT+ }
@@ -27,14 +29,30 @@ dot_atom_text = { atext_seq ~ ("." ~ atext_seq)* }
dot_atom = _{ CFWS? ~ dot_atom_text ~ CFWS? }
specials = _{ "(" | ")" | "<" | ">" | "[" | "]" | ":" | ";" | "@" | "\\" | "," | "." | "\"" }

// 3.2.4. Quoted Strings
// 3.2.4. Quoted Strings

QTEXT = _{ "!" | '#'..'[' | ']'..'~' }
qtext_seq = { QTEXT+ }
quoted_string = { CFWS? ~ "\"" ~ (FWS? ~ (qtext_seq | quoted_pair))* ~ FWS? ~ "\"" ~ CFWS? }

// 3.2.5. Miscellaneous Tokens
// 3.2.5. Miscellaneous Tokens

word = { atom | quoted_string }
phrase = { word+ }
unstructured = { (FWS? ~ vchar_seq)* ~ WSP*}

// 3.3. Date and Time Specification

date_time = { (day_of_week ~ ",")? ~ date ~ time ~ CFWS? }
day_of_week = _{ FWS? ~ day_name }
day_name = { "Mon" | "Tue" | "Wed" | "Thu" | "Fri" | "Sat" | "Sun" }
date = _{ (FWS? ~ day ~ FWS) ~ month ~ (FWS ~ year ~ FWS) }
day = { ASCII_DIGIT{1,2} } // In the spec FWS are included in day, year and zone but I moved them one level up
month = { "Jan" | "Feb" | "Mar" | "Apr" | "May" | "Jun" | "Jul" | "Aug" | "Sep" | "Oct" | "Nov" | "Dec" }
year = { ASCII_DIGIT{4} }
time = _{ time_of_day ~ (FWS ~ zone) }
time_of_day = _{ hour ~ ":" ~ minute ~ (":" ~ second)? }
hour = { ASCII_DIGIT{2} }
minute = { ASCII_DIGIT{2} }
second = { ASCII_DIGIT{2} }
zone = { ("+" | "-") ~ ASCII_DIGIT{4} }
21 changes: 21 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -103,3 +103,24 @@ fn test_quoted_string() {
let qtext_seqs = quoted_string.children().map(|c| c.as_str()).collect::<Vec<_>>();
assert_eq!(qtext_seqs, vec!["quoted", "\\ ", "test"]);
}

#[test]
fn test_date() {
let input = "Mon, 5 May 2003 18:58:55 +0100 trail";
let output = Parser::parse_date_time(input).map_err(|e| e.print(input)).unwrap();
let date_time = output.into_iter().next().unwrap();
let children = date_time.children().map(|c| c.as_str()).collect::<Vec<_>>();
assert_eq!(children, vec!["Mon", "5", "May", "2003", "18", "58", "55", "+0100"]);

let input = "3 Jan 2009 18:15:05 +0000";
let output = Parser::parse_date_time(input).map_err(|e| e.print(input)).unwrap();
let date_time = output.into_iter().next().unwrap();
let children = date_time.children().map(|c| c.as_str()).collect::<Vec<_>>();
assert_eq!(children, vec!["3", "Jan", "2009", "18", "15", "05", "+0000"]);

let input = "14 Jul 1789 14:00:00 +0100";
let output = Parser::parse_date_time(input).map_err(|e| e.print(input)).unwrap();
let date_time = output.into_iter().next().unwrap();
let children = date_time.children().map(|c| c.as_str()).collect::<Vec<_>>();
assert_eq!(children, vec!["14", "Jul", "1789", "14", "00", "00", "+0100"]);
}