Original commit: 1de3c3413a
This commit is contained in:
Danilo Guanabara 2020-03-27 09:25:55 -03:00 committed by GitHub
parent 4af19c5745
commit 6552176247
6 changed files with 38 additions and 20 deletions

View File

@ -26,7 +26,10 @@ function run(cmd,args) {
return new Promise((resolve, reject) => {
console.log(`Calling '${cmd} ${args.join(' ')}'`)
let proc = spawn(cmd,args,{stdio:'inherit', shell:true})
proc.on('exit', () => resolve(out))
proc.on('exit', (code) => {
if (code) process.exit(code)
resolve(out)
})
})
}
@ -36,7 +39,10 @@ function run_read(cmd,args) {
let proc = spawn(cmd,args,{shell:true})
proc.stderr.pipe(process.stderr)
proc.stdout.on('data', (data) => { out += data })
proc.on('exit', () => resolve(out))
proc.on('exit', (code) => {
if (code) process.exit(code);
resolve(out)
})
})
}

View File

@ -22,7 +22,8 @@ pub mod test_utils;
use prelude::*;
use ast_macros::*;
use data::text::*;
use data::text::Index;
use data::text::Span;
use serde::de::Deserializer;
use serde::de::Visitor;
@ -762,7 +763,7 @@ impl TokenConsumer for IdMapBuilder {
let begin = self.offset;
val.shape().feed_to(self);
if let Some(id) = val.id {
let span = Span::from((begin, self.offset));
let span = Span::from_indices(Index::new(begin), Index::new(self.offset));
self.id_map.insert(span, id);
}
}
@ -1101,6 +1102,8 @@ impl<T> From<EscapeUnicode32> for SegmentFmt<T> {
#[cfg(test)]
mod tests {
use super::*;
use data::text::Size;
use serde::de::DeserializeOwned;
/// Assert that given value round trips JSON serialization.
@ -1148,9 +1151,9 @@ mod tests {
#[test]
fn ast_id_map() {
let span = |ix,length| Span::from((ix,length));
let span = |ix,length| Span::new(Index::new(ix),Size::new(length));
let uid = default();
let ids = vec![(span(0,2),uid), (span(3,5),uid), (span(0,5),uid)];
let ids = vec![(span(0,2),uid), (span(3,2),uid), (span(0,5),uid)];
let func = Ast::new(Var {name:"XX".into()}, Some(uid));
let arg = Ast::new(Var {name:"YY".into()}, Some(uid));
let ast = Ast::new(Prefix {func,off:1,arg }, Some(uid));

View File

@ -22,7 +22,7 @@ fn web_test() {
let mut parser = Parser::new_or_panic();
let mut parse = |input:&str| {
let span = Span::from((0,input.len()));
let span = Span::from_beginning(Size::new(input.len()));
let ids = IdMap::new(vec![(span,uuid)]);
let ast = parser.parse(String::from(input), ids).unwrap().wrapped;

View File

@ -199,7 +199,7 @@ impl Handle {
/// If ID doesn't have metadata, empty (default) metadata is inserted.
pub fn with_node_metadata(&self, id:ast::ID, fun:impl FnOnce(&mut NodeMetadata)) {
let module = self.module();
let mut data = module.pop_node_metadata(id).unwrap_or(default());
let mut data = module.pop_node_metadata(id).unwrap_or_default();
fun(&mut data);
module.set_node_metadata(id, data);
}

View File

@ -16,7 +16,6 @@ use parser::api::SourceFile;
use ast;
use ast::Ast;
use ast::HasRepr;
use ast::IdMap;
use ast::HasIdMap;
use ast::known;
use data::text::*;
@ -282,7 +281,7 @@ impl Handle {
pub fn new_mock
( location : Location
, code : &str
, id_map : IdMap
, id_map : ast::IdMap
, file_manager : fmc::Handle
, mut parser : Parser
) -> FallibleResult<Self> {
@ -353,10 +352,10 @@ mod test {
let uuid2 = Uuid::new_v4();
let uuid3 = Uuid::new_v4();
let module = "2+2";
let id_map = IdMap::new(vec!
[ (Span::from((0,1)),uuid1.clone())
, (Span::from((2,1)),uuid2)
, (Span::from((0,3)),uuid3)
let id_map = ast::IdMap::new(vec!
[ (Span::new(Index::new(0),Size::new(1)),uuid1.clone())
, (Span::new(Index::new(2),Size::new(1)),uuid2)
, (Span::new(Index::new(0),Size::new(3)),uuid3)
]);
let controller = Handle::new_mock

View File

@ -79,18 +79,28 @@ impl SubAssign for Size {
#[derive(Clone,Copy,Debug,Default,PartialEq,Eq,PartialOrd,Ord,Serialize,Deserialize)]
pub struct Span { pub index:Index, pub size:Size }
impl From<(usize,usize)> for Span {
fn from(val:(usize, usize)) -> Span {
Span::new(Index::new(val.0), Size::new(val.1))
}
}
impl Span {
/// Initializes Span with given values.
pub fn new(index:Index, size:Size) -> Self {
Span {index,size}
}
/// Creates a span describing a range between two indices.
pub fn from_indices(begin:Index, end:Index) -> Self {
if end < begin {
Self::from_indices(end,begin)
} else {
let index = begin;
let size = end - begin;
Span {index,size}
}
}
/// Creates a span from zero index with given length.
pub fn from_beginning(size:Size) -> Self {
Span {index:Index::new(0), size}
}
/// Get the character after last character of this span.
///
/// If span has size 0, it returns the `index` field.