-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
162 additions
and
165 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,64 @@ | ||
//! Describes the top-level document structure. | ||
mod document; | ||
pub use document::Document; | ||
use std::slice::Iter; | ||
|
||
use nom::IResult; | ||
|
||
use crate::{blocks::Block, primitives::consume_empty_lines, Error, Span}; | ||
|
||
/// A document represents the top-level block element in AsciiDoc. It consists | ||
/// of an optional document header and either a) one or more sections preceded | ||
/// by an optional preamble or b) a sequence of top-level blocks only. | ||
/// | ||
/// The document can be configured using a document header. The header is not a | ||
/// block itself, but contributes metadata to the document, such as the document | ||
/// title and document attributes. | ||
#[allow(dead_code)] // TEMPORARY while building | ||
#[derive(Clone, Debug, Eq, PartialEq)] | ||
pub struct Document<'a> { | ||
blocks: Vec<Block<'a>>, | ||
source: Span<'a>, | ||
} | ||
|
||
impl<'a> Document<'a> { | ||
/// Parse a UTF-8 string as an AsciiDoc document. | ||
/// | ||
/// Note that the document references the underlying source string and | ||
/// necessarily has the same lifetime as the source. | ||
pub fn parse(source: &'a str) -> Result<Self, Error> { | ||
let source = Span::new(source, true); | ||
let i = source; | ||
|
||
// TO DO: Look for document header. | ||
// TO DO: Add option for best-guess parsing? | ||
|
||
let (_rem, blocks) = parse_blocks(i)?; | ||
|
||
// let blocks: Vec<Block<'a>> = vec![]; // TEMPORARY | ||
Ok(Self { source, blocks }) | ||
} | ||
|
||
/// Return a [`Span`] describing the entire document as parsed. | ||
pub fn span(&'a self) -> &'a Span<'a> { | ||
&self.source | ||
} | ||
|
||
/// Return an iterator over the blocks in this document. | ||
pub fn blocks(&'a self) -> Iter<'a, Block<'a>> { | ||
self.blocks.iter() | ||
} | ||
} | ||
|
||
fn parse_blocks<'a>(mut i: Span<'a>) -> IResult<Span, Vec<Block<'a>>> { | ||
let mut blocks: Vec<Block<'a>> = vec![]; | ||
i = consume_empty_lines(i); | ||
|
||
while !i.data().is_empty() { | ||
// TO DO: Handle other kinds of blocks. | ||
let (i2, block) = Block::parse(i)?; | ||
i = i2; | ||
blocks.push(block); | ||
} | ||
|
||
Ok((i, blocks)) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,98 @@ | ||
mod document; | ||
use pretty_assertions_sorted::assert_eq; | ||
|
||
use crate::{ | ||
document::Document, | ||
tests::fixtures::{ | ||
blocks::{TBlock, TSimpleBlock}, | ||
document::TDocument, | ||
TSpan, | ||
}, | ||
}; | ||
|
||
#[test] | ||
fn empty_source() { | ||
assert_eq!( | ||
Document::parse("").unwrap(), | ||
TDocument { | ||
source: TSpan { | ||
data: "", | ||
line: 1, | ||
col: 1, | ||
offset: 0 | ||
}, | ||
blocks: vec![], | ||
} | ||
); | ||
} | ||
|
||
#[test] | ||
fn only_spaces() { | ||
assert_eq!( | ||
Document::parse(" ").unwrap(), | ||
TDocument { | ||
source: TSpan { | ||
data: " ", | ||
line: 1, | ||
col: 1, | ||
offset: 0 | ||
}, | ||
blocks: vec![], | ||
} | ||
); | ||
} | ||
|
||
#[test] | ||
fn one_simple_block() { | ||
assert_eq!( | ||
Document::parse("abc").unwrap(), | ||
TDocument { | ||
source: TSpan { | ||
data: "abc", | ||
line: 1, | ||
col: 1, | ||
offset: 0 | ||
}, | ||
blocks: vec![TBlock::Simple(TSimpleBlock { | ||
inlines: vec![TSpan { | ||
data: "abc", | ||
line: 1, | ||
col: 1, | ||
offset: 0, | ||
},], | ||
})], | ||
} | ||
); | ||
} | ||
|
||
#[test] | ||
fn two_simple_blocks() { | ||
assert_eq!( | ||
Document::parse("abc\n\ndef").unwrap(), | ||
TDocument { | ||
source: TSpan { | ||
data: "abc\n\ndef", | ||
line: 1, | ||
col: 1, | ||
offset: 0 | ||
}, | ||
blocks: vec![ | ||
TBlock::Simple(TSimpleBlock { | ||
inlines: vec![TSpan { | ||
data: "abc", | ||
line: 1, | ||
col: 1, | ||
offset: 0, | ||
},], | ||
}), | ||
TBlock::Simple(TSimpleBlock { | ||
inlines: vec![TSpan { | ||
data: "def", | ||
line: 3, | ||
col: 1, | ||
offset: 5, | ||
},], | ||
}), | ||
], | ||
} | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
mod document; | ||
pub(crate) use document::TDocument; | ||
mod tdocument; | ||
pub(crate) use tdocument::TDocument; |
File renamed without changes.