-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.rs
126 lines (115 loc) · 3.43 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
mod declarations;
mod document;
mod expressions;
mod meta;
mod node;
mod primitives;
mod task;
mod workflow;
use crate::{
model::{Comments, Document, DocumentSource, Position, Span},
parsers::{pest::node::PestNode, WdlParser, WdlParserError},
};
use error_stack::{IntoReport, Result, ResultExt};
use pest::error::{Error as PestError, InputLocation, LineColLocation};
use pest_wdl_1 as wdl;
use pest_wdl_1::Rule;
use std::{cell::RefCell, rc::Rc};
pub struct PestParser;
impl PestParser {
pub fn new() -> Self {
PestParser {}
}
}
impl WdlParser for PestParser {
fn parse_text<Text: AsRef<str>>(
&mut self,
text: Text,
source: DocumentSource,
) -> Result<Document, WdlParserError> {
let text: &str = text.as_ref();
let root_pair = wdl::parse_document(text)
.into_report()
.change_context(WdlParserError::Syntax(source.clone()))?;
let root_node = PestNode::new(root_pair, Rc::new(RefCell::new(Comments::default())));
let mut doc: Document = root_node
.try_into()
.change_context(WdlParserError::Model(source.clone()))?;
doc.source = source.clone();
doc.validate()
.change_context(WdlParserError::Model(source))?;
Ok(doc)
}
}
impl<'a> From<pest::Position<'a>> for Position {
fn from(value: pest::Position<'a>) -> Self {
let (line, column) = value.line_col();
Self {
line: line - 1,
column: column - 1,
offset: value.pos(),
}
}
}
impl<'a> From<&pest::Span<'a>> for Span {
fn from(value: &pest::Span<'a>) -> Self {
Self {
start: value.start_pos().into(),
end: value.end_pos().into(),
}
}
}
impl From<PestError<Rule>> for Span {
fn from(error: PestError<Rule>) -> Self {
let ((start_line, start_column), (end_line, end_column)) = match error.line_col {
LineColLocation::Pos(pos) => (pos, pos),
LineColLocation::Span(start, end) => (start, end),
};
let (start_offset, end_offset) = match error.location {
InputLocation::Pos(pos) => (pos, pos),
InputLocation::Span(span) => span,
};
Self {
start: Position {
line: start_line - 1,
column: start_column - 1,
offset: start_offset - 1,
},
end: Position {
line: end_line - 1,
column: end_column - 1,
offset: end_offset - 1,
},
}
}
}
#[cfg(test)]
mod tests {
use crate::{
model::tests,
parsers::{pest::PestParser, WdlParser, WdlParserError},
};
use error_stack::Result;
use std::path::PathBuf;
fn test_path(filename: &str) -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("resources")
.join("test")
.join(filename)
}
#[test]
fn test_comprehensive() -> Result<(), WdlParserError> {
let mut parser = PestParser::new();
let wdl_file = test_path("comprehensive.wdl");
let doc = parser.parse_file(wdl_file)?;
tests::test_comprehensive(doc);
Ok(())
}
#[test]
fn test_expressions() -> Result<(), WdlParserError> {
let mut parser = PestParser::new();
let wdl_file = test_path("expressions.wdl");
let _ = parser.parse_file(wdl_file)?;
Ok(())
}
}