forked from facebook/hermes
-
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.
Summary: SourceManager owns a collection of source buffers with names (so they can be parsed and so it can provide context when printing errors). This is a very simple implementation - it directly prints error messages to stderr without any buffering, etc. The error() method takes `&self` by design - the implementation will use interior mutability. A SourceManager is intended to be easily shareable. Reviewed By: avp Differential Revision: D31311326 fbshipit-source-id: 47d48d2efb3bcf2978d73b524cfd7e8b4122a8be
- Loading branch information
1 parent
765df80
commit 0dc8937
Showing
9 changed files
with
137 additions
and
18 deletions.
There are no files selected for viewing
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
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
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
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
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 |
---|---|---|
|
@@ -8,4 +8,5 @@ | |
pub mod ast; | ||
pub mod gen_js; | ||
pub mod hparser; | ||
pub mod source_manager; | ||
pub mod sourcemap; |
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 |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
use crate::ast::SourceRange; | ||
use std::rc::Rc; | ||
use support::NullTerminatedBuf; | ||
|
||
/// An opaque value identifying a source registered with SourceManager. | ||
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] | ||
pub struct SourceId(pub u32); | ||
|
||
impl SourceId { | ||
pub const INVALID: SourceId = SourceId(u32::MAX); | ||
|
||
pub fn is_valid(self) -> bool { | ||
self.0 != Self::INVALID.0 | ||
} | ||
|
||
fn as_usize(self) -> usize { | ||
self.0 as usize | ||
} | ||
} | ||
|
||
/// SourceManager owns a collection of source buffers and their names and handles | ||
/// reporting errors. | ||
#[derive(Debug)] | ||
pub struct SourceManager { | ||
sources: Vec<(String, Rc<NullTerminatedBuf>)>, | ||
} | ||
|
||
impl Default for SourceManager { | ||
fn default() -> Self { | ||
SourceManager { | ||
sources: Default::default(), | ||
} | ||
} | ||
} | ||
|
||
impl SourceManager { | ||
pub fn new() -> SourceManager { | ||
Default::default() | ||
} | ||
|
||
/// Register a source buffer with its name. | ||
pub fn add_source<S: Into<String>>(&mut self, name: S, buf: NullTerminatedBuf) -> SourceId { | ||
assert!( | ||
self.sources.len() < SourceId::INVALID.0 as usize, | ||
"Too many sources", | ||
); | ||
self.sources.push((name.into(), Rc::new(buf))); | ||
SourceId(self.sources.len() as u32 - 1) | ||
} | ||
|
||
/// Obtain the name of a previously registered source buffer. | ||
pub fn source_name(&self, source_id: SourceId) -> &str { | ||
self.sources[source_id.as_usize()].0.as_str() | ||
} | ||
|
||
/// Obtain a reference to a previously registered source buffer. | ||
pub fn source_buffer(&self, source_id: SourceId) -> &NullTerminatedBuf { | ||
&self.sources[source_id.as_usize()].1 | ||
} | ||
|
||
/// Obtain a Rc of a previously registered source buffer. | ||
pub fn source_buffer_rc(&self, source_id: SourceId) -> Rc<NullTerminatedBuf> { | ||
Rc::clone(&self.sources[source_id.as_usize()].1) | ||
} | ||
|
||
/// Report an error at the specified range in the specified source buffer. | ||
pub fn error<S: Into<String>>(&self, range: SourceRange, msg: S) { | ||
// NOTE: this method deliberately takes immutable `self`. A SourceManager | ||
// should be easily shareable. It will be implemented using interior | ||
// mutability. | ||
eprintln!( | ||
"{}:{}:{}: error: {}", | ||
self.source_name(range.file), | ||
range.start.line, | ||
range.start.col, | ||
msg.into() | ||
); | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn smoke_test() { | ||
let mut sm = SourceManager::new(); | ||
|
||
let id1 = sm.add_source("buf1", NullTerminatedBuf::from_str_copy("a")); | ||
let id2 = sm.add_source("buf2", NullTerminatedBuf::from_str_copy("bb")); | ||
|
||
assert_eq!("buf1", sm.source_name(id1)); | ||
assert_eq!("buf2", sm.source_name(id2)); | ||
|
||
assert_eq!(2, sm.source_buffer(id1).len()); | ||
assert_eq!(3, sm.source_buffer(id2).len()); | ||
|
||
let buf1 = sm.source_buffer_rc(id1); | ||
assert_eq!(2, buf1.len()); | ||
assert_eq!(b"a\0", buf1.as_bytes()); | ||
} | ||
} |
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
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
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