Skip to content

Commit

Permalink
Add support for (ignored) comment lines in source documents
Browse files Browse the repository at this point in the history
  • Loading branch information
althonos committed Jul 19, 2020
1 parent ad4264d commit 8665e70
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 11 deletions.
27 changes: 18 additions & 9 deletions src/grammar.pest
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,12 @@ NonWsChar = @{ !(WhitespaceChar) ~ !(NewlineChar) ~ OboChar }

// 2.3 Line Termination

EOL = { QualifierList? ~ HiddenComment? ~ nl }
EOL = { QualifierList? ~ Comment? ~ nl }

HiddenComment = ${ "!" ~ ( !NewlineChar ~ UniCodeChar )* }
Comment = { CommentPrefix ~ CommentText }
CommentPrefix = _{ WhitespaceChar* ~ "!" }
CommentText = ${ ( !NewlineChar ~ UniCodeChar )* }
CommentSilent = _{ Comment }

QualifierChar = @{ !("=" | "," | "}" | "{" | "\"") ~ NonWsChar }
QualifierId = @{ QualifierChar+ }
Expand Down Expand Up @@ -166,7 +169,7 @@ EntitySingle = _{ EntityFrame ~ EOI } // NB(@althonos): for iterative parsers.

// 3.2 Obo Headers

HeaderFrame = { (HeaderClause? ~ nl)* ~ HeaderClause? }
HeaderFrame = { ((HeaderClause | CommentSilent)? ~ nl)* ~ HeaderClause? ~ (nl ~ CommentSilent?)* }

NaiveDateTime = { NaiveDate ~ NaiveTime }
NaiveDate = ${ NaiveDay ~ ":" ~ NaiveMonth ~ ":" ~ NaiveYear }
Expand Down Expand Up @@ -232,9 +235,11 @@ Unreserved = @{
// 3.3 Term Frames

TermFrame = {
WhitespaceChar* ~ "[Term]" ~ nl
(CommentSilent? ~ nl)*
~ WhitespaceChar* ~ "[Term]" ~ nl
~ (CommentSilent? ~ nl)*
~ WhitespaceChar* ~ "id:" ~ ClassId ~ EOL
~ (TermClauseLine | nl)*
~ (TermClauseLine | CommentSilent? ~ nl)*
}
TermClauseLine = {
TermClause ~ EOL
Expand Down Expand Up @@ -268,9 +273,11 @@ TermClause = { WhitespaceChar* ~ (
// 3.4 Typedef Frames

TypedefFrame = {
WhitespaceChar* ~ "[Typedef]" ~ nl
(CommentSilent? ~ nl)*
~ WhitespaceChar* ~ "[Typedef]" ~ nl
~ (CommentSilent? ~ nl)*
~ WhitespaceChar* ~ "id:" ~ ClassId ~ EOL
~ (TypedefClauseLine | nl)*
~ (TypedefClauseLine | CommentSilent? ~ nl)*
}
TypedefClauseLine = {
TypedefClause ~ EOL
Expand Down Expand Up @@ -323,9 +330,11 @@ TypedefClause = { WhitespaceChar* ~ (
// 3.5 Instance Frames

InstanceFrame = {
WhitespaceChar* ~ "[Instance]" ~ nl
(CommentSilent? ~ nl)*
~ WhitespaceChar* ~ "[Instance]" ~ nl
~ (CommentSilent? ~ nl)*
~ WhitespaceChar* ~"id:" ~ InstanceId ~ EOL
~ (InstanceClauseLine | nl)*
~ (InstanceClauseLine | CommentSilent? ~ nl)*
}
InstanceClauseLine = {
InstanceClause ~ EOL
Expand Down
45 changes: 45 additions & 0 deletions tests/comments.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

extern crate fastobo_syntax;
use fastobo_syntax::OboParser;
use fastobo_syntax::Rule;

macro_rules! test_parse {
($rule:ident, $input:literal) => ({
match OboParser::parse(Rule::$rule, $input) {
Ok(mut pairs) => assert_eq!(pairs.next().unwrap().as_str(), $input),
Err(e) => panic!("could not parse {:?}:\n{}", $input, e),
}
})
}

#[test]
fn term_frame() {
test_parse!(EntityFrame, "[Term]\nid: TST:001\n");
test_parse!(EntityFrame, "! irrelevant\n[Term]\nid: TST:001\n");
test_parse!(EntityFrame, "[Term]\n! irrelevant\nid: TST:001\n");
test_parse!(EntityFrame, "[Term]\nid: TST:001\n! irrelevant\n");
}

#[test]
fn header_frame() {
test_parse!(HeaderFrame, "format-version: 1,4\n! this is a comment\n");
test_parse!(HeaderFrame, "! this is a comment\nformat-version: 1,4\n");
test_parse!(OboDoc, "format-version: 1,4\n! this is a comment\n");
test_parse!(OboDoc, "! this is a comment\nformat-version: 1,4\n");
}

#[test]
fn typedef_frame() {
test_parse!(EntityFrame, "[Typedef]\nid: TST:001\n");
test_parse!(EntityFrame, "! irrelevant\n[Typedef]\nid: TST:001\n");
test_parse!(EntityFrame, "[Typedef]\n! irrelevant\nid: TST:001\n");
test_parse!(EntityFrame, "[Typedef]\nid: TST:001\n! irrelevant\n");
}

#[test]
fn instance_frame() {
test_parse!(EntityFrame, "[Instance]\nid: TST:001\n");
test_parse!(EntityFrame, "! irrelevant\n[Term]\nid: TST:001\n");
test_parse!(EntityFrame, "[Instance]\n! irrelevant\nid: TST:001\n");
test_parse!(EntityFrame, "[Instance]\nid: TST:001\n! irrelevant\n");
}
4 changes: 2 additions & 2 deletions tests/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ fn xreflist() {
#[test]
fn header_frame() {
// with Unix endlines
test_parse!(HeaderFrame, r#"format-version: 1.4\ndata-version: 1\n"#);
test_parse!(HeaderFrame, "format-version: 1.4\ndata-version: 1\n");
// with Windows endlines
test_parse!(HeaderFrame, r#"format-version: 1.4\r\ndata-version: 1\r\n"#);
test_parse!(HeaderFrame, "format-version: 1.4\r\ndata-version: 1\r\n");
}

0 comments on commit 8665e70

Please sign in to comment.