Skip to content

Commit

Permalink
Worked on fastn_section::Document::parse
Browse files Browse the repository at this point in the history
  • Loading branch information
Arpita-Jaiswal committed Nov 4, 2024
1 parent 2365e17 commit 8184687
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 4 deletions.
4 changes: 4 additions & 0 deletions v0.5/fastn-section/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ impl JDebug for fastn_section::Document {
if !self.comments.is_empty() {
o.insert("comments".to_string(), self.comments.debug(source));
}

if !self.sections.is_empty() {
o.insert("sections".to_string(), self.sections.debug(source));
}
if o.is_empty() {
return "<empty-document>".into();
}
Expand Down
1 change: 1 addition & 0 deletions v0.5/fastn-section/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub use fastn_section::parser::kinded_name::kinded_name;
pub use fastn_section::parser::module_name::module_name;
pub use fastn_section::parser::package_name::package_name;
pub use fastn_section::parser::qualified_identifier::qualified_identifier;
pub use fastn_section::parser::section::section;
pub use fastn_section::parser::section_init::section_init;
pub use fastn_section::parser::tes::tes;
pub use fastn_section::warning::Warning;
Expand Down
58 changes: 55 additions & 3 deletions v0.5/fastn-section/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,31 @@ pub(super) mod kinded_name;
pub(super) mod module_name;
pub(super) mod package_name;
pub(super) mod qualified_identifier;
mod section;
pub(super) mod section;
pub(super) mod section_init;
pub(super) mod tes;
pub(super) mod visibility;

impl fastn_section::Document {
pub fn parse(source: &str) -> fastn_section::Document {
let _scanner = fastn_section::Scanner::new(
let mut scanner = fastn_section::Scanner::new(
source,
Default::default(),
fastn_section::Document::default(),
);
todo!()
document(&mut scanner);
scanner.output
}
}

pub fn document(scanner: &mut fastn_section::Scanner<fastn_section::Document>) {
// TODO: parse module_doc, comments etc
scanner.skip_spaces();
while let Some(section) = fastn_section::section(scanner) {
scanner.skip_spaces();
scanner.skip_new_lines();
scanner.skip_spaces();
scanner.output.sections.push(section);
}
}

Expand Down Expand Up @@ -61,3 +73,43 @@ macro_rules! tt {
}
};
}

#[cfg(test)]
mod test {
fn doc(
scanner: &mut fastn_section::Scanner<fastn_section::Document>,
) -> fastn_section::Document {
fastn_section::parser::document(scanner);
scanner.output.clone()
}

fastn_section::tt!(doc);
#[test]
fn document() {
t!(
"-- foo: Hello World",
{
"sections": [{
"init": {"name": "foo"},
"caption": ["Hello World"]
}]
}
);

t!(
"-- foo: Hello World from foo\n-- bar: Hello World from bar",
{
"sections": [
{
"init": {"name": "foo"},
"caption": ["Hello World from foo"]
},
{
"init": {"name": "bar"},
"caption": ["Hello World from bar"]
}
]
}
);
}
}
1 change: 0 additions & 1 deletion v0.5/fastn-section/src/parser/section.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#[allow(dead_code)]
pub fn section(
scanner: &mut fastn_section::Scanner<fastn_section::Document>,
) -> Option<fastn_section::Section> {
Expand Down
10 changes: 10 additions & 0 deletions v0.5/fastn-section/src/scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ impl<'input, T: Scannable> Scanner<'input, T> {
}
}

pub fn skip_new_lines(&mut self) {
while let Some(c) = self.peek() {
if c == '\n' {
self.pop();
continue;
}
break;
}
}

pub fn take_till_char_or_end_of_line(&mut self, t: char) -> Option<fastn_section::Span> {
self.take_while(|c| c != t && c != '\n')
}
Expand Down

0 comments on commit 8184687

Please sign in to comment.