Gather comments in the preamble of a test file.

Comments preceding the first function are not associated with any
specific entity in the file. Put them in a TestFile::preamble_comments
field.
This commit is contained in:
Jakob Stoklund Olesen
2016-11-04 10:34:14 -07:00
parent a2b7769a51
commit 8d6d59cc7a
3 changed files with 20 additions and 2 deletions

View File

@@ -10,7 +10,7 @@ extern crate cretonne;
pub use error::{Location, Result, Error}; pub use error::{Location, Result, Error};
pub use parser::{parse_functions, parse_test}; pub use parser::{parse_functions, parse_test};
pub use testcommand::{TestCommand, TestOption}; pub use testcommand::{TestCommand, TestOption};
pub use testfile::{TestFile, Details}; pub use testfile::{TestFile, Details, Comment};
pub use isaspec::IsaSpec; pub use isaspec::IsaSpec;
pub use sourcemap::SourceMap; pub use sourcemap::SourceMap;

View File

@@ -38,9 +38,12 @@ pub fn parse_functions(text: &str) -> Result<Vec<Function>> {
/// The returned `TestFile` contains direct references to substrings of `text`. /// The returned `TestFile` contains direct references to substrings of `text`.
pub fn parse_test<'a>(text: &'a str) -> Result<TestFile<'a>> { pub fn parse_test<'a>(text: &'a str) -> Result<TestFile<'a>> {
let mut parser = Parser::new(text); let mut parser = Parser::new(text);
// Gather the preamble comments as 'Function'.
parser.gather_comments(AnyEntity::Function);
Ok(TestFile { Ok(TestFile {
commands: parser.parse_test_commands(), commands: parser.parse_test_commands(),
isa_spec: try!(parser.parse_isa_specs()), isa_spec: try!(parser.parse_isa_specs()),
preamble_comments: parser.take_comments(),
functions: try!(parser.parse_function_list()), functions: try!(parser.parse_function_list()),
}) })
} }
@@ -273,6 +276,11 @@ impl<'a> Parser<'a> {
self.comment_entity = Some(entity.into()); self.comment_entity = Some(entity.into());
} }
// Get the comments gathered so far, clearing out the internal list.
fn take_comments(&mut self) -> Vec<Comment<'a>> {
mem::replace(&mut self.comments, Vec::new())
}
// Rewrite the entity of the last added comments from `old` to `new`. // Rewrite the entity of the last added comments from `old` to `new`.
// Also switch to collecting future comments for `new`. // Also switch to collecting future comments for `new`.
fn rewrite_last_comment_entities<E: Into<AnyEntity>>(&mut self, old: E, new: E) { fn rewrite_last_comment_entities<E: Into<AnyEntity>>(&mut self, old: E, new: E) {
@@ -561,7 +569,7 @@ impl<'a> Parser<'a> {
let details = Details { let details = Details {
location: location, location: location,
comments: mem::replace(&mut self.comments, Vec::new()), comments: self.take_comments(),
map: ctx.map, map: ctx.map,
}; };
@@ -1482,6 +1490,7 @@ mod tests {
test cfg option=5 test cfg option=5
test verify test verify
set enable_float=false set enable_float=false
; still preamble
function comment() {}") function comment() {}")
.unwrap(); .unwrap();
assert_eq!(tf.commands.len(), 2); assert_eq!(tf.commands.len(), 2);
@@ -1491,6 +1500,9 @@ mod tests {
IsaSpec::None(s) => assert!(!s.enable_float()), IsaSpec::None(s) => assert!(!s.enable_float()),
_ => panic!("unexpected ISAs"), _ => panic!("unexpected ISAs"),
} }
assert_eq!(tf.preamble_comments.len(), 2);
assert_eq!(tf.preamble_comments[0].text, "; before");
assert_eq!(tf.preamble_comments[1].text, "; still preamble");
assert_eq!(tf.functions.len(), 1); assert_eq!(tf.functions.len(), 1);
assert_eq!(tf.functions[0].0.name.to_string(), "comment"); assert_eq!(tf.functions[0].0.name.to_string(), "comment");
} }

View File

@@ -20,6 +20,9 @@ pub struct TestFile<'a> {
pub commands: Vec<TestCommand<'a>>, pub commands: Vec<TestCommand<'a>>,
/// `isa bar ...` lines. /// `isa bar ...` lines.
pub isa_spec: IsaSpec, pub isa_spec: IsaSpec,
/// Comments appearing before the first function.
/// These are all tagged as 'Function' scope for lack of a better entity.
pub preamble_comments: Vec<Comment<'a>>,
/// Parsed functions and additional details about each function. /// Parsed functions and additional details about each function.
pub functions: Vec<(Function, Details<'a>)>, pub functions: Vec<(Function, Details<'a>)>,
} }
@@ -47,6 +50,9 @@ pub struct Details<'a> {
/// after the function are tagged as `AnyEntity::Function`. /// after the function are tagged as `AnyEntity::Function`.
#[derive(Clone, PartialEq, Eq, Debug)] #[derive(Clone, PartialEq, Eq, Debug)]
pub struct Comment<'a> { pub struct Comment<'a> {
/// The entity this comment is attached to.
/// Comments always follow their entity.
pub entity: AnyEntity, pub entity: AnyEntity,
/// Text of the comment, including the leading `;`.
pub text: &'a str, pub text: &'a str,
} }