2021-07-16 22:26:40 +02:00
|
|
|
use crate::{parser::Block, Declaration, Span};
|
2021-07-23 23:19:30 +02:00
|
|
|
use core::panic;
|
2021-07-22 09:48:45 +02:00
|
|
|
use std::collections::HashMap;
|
2021-06-30 03:42:56 +02:00
|
|
|
|
2021-07-16 08:24:46 +02:00
|
|
|
#[derive(Debug)]
|
2021-06-30 03:42:56 +02:00
|
|
|
pub struct ParserState {
|
2021-07-03 05:35:15 +02:00
|
|
|
files: Vec<(String, usize, usize)>,
|
|
|
|
file_contents: Vec<u8>,
|
2021-07-01 08:09:55 +02:00
|
|
|
vars: Vec<Type>,
|
2021-07-16 03:10:22 +02:00
|
|
|
decls: Vec<Declaration>,
|
2021-07-16 22:26:40 +02:00
|
|
|
blocks: Vec<Block>,
|
2021-07-17 08:31:34 +02:00
|
|
|
scope: Vec<ScopeFrame>,
|
2021-06-30 03:42:56 +02:00
|
|
|
}
|
|
|
|
|
2021-07-23 23:46:55 +02:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
2021-07-01 02:01:04 +02:00
|
|
|
pub enum Type {
|
|
|
|
Int,
|
2021-07-23 23:19:30 +02:00
|
|
|
Bool,
|
|
|
|
String,
|
|
|
|
Block,
|
|
|
|
ColumnPath,
|
|
|
|
Duration,
|
|
|
|
FilePath,
|
|
|
|
Filesize,
|
|
|
|
List(Box<Type>),
|
|
|
|
Number,
|
|
|
|
Table,
|
2021-07-01 02:01:04 +02:00
|
|
|
Unknown,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type VarId = usize;
|
2021-07-01 08:09:55 +02:00
|
|
|
pub type DeclId = usize;
|
2021-07-16 03:10:22 +02:00
|
|
|
pub type BlockId = usize;
|
2021-06-30 03:42:56 +02:00
|
|
|
|
2021-07-02 00:40:08 +02:00
|
|
|
#[derive(Debug)]
|
2021-06-30 03:42:56 +02:00
|
|
|
struct ScopeFrame {
|
2021-07-01 02:01:04 +02:00
|
|
|
vars: HashMap<Vec<u8>, VarId>,
|
2021-07-01 08:09:55 +02:00
|
|
|
decls: HashMap<Vec<u8>, DeclId>,
|
2021-06-30 03:42:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ScopeFrame {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
vars: HashMap::new(),
|
2021-07-01 08:09:55 +02:00
|
|
|
decls: HashMap::new(),
|
2021-06-30 03:42:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for ParserState {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ParserState {
|
|
|
|
pub fn new() -> Self {
|
2021-07-01 08:09:55 +02:00
|
|
|
Self {
|
|
|
|
files: vec![],
|
2021-07-03 05:35:15 +02:00
|
|
|
file_contents: vec![],
|
2021-07-01 08:09:55 +02:00
|
|
|
vars: vec![],
|
|
|
|
decls: vec![],
|
2021-07-16 03:10:22 +02:00
|
|
|
blocks: vec![],
|
2021-07-17 08:31:34 +02:00
|
|
|
scope: vec![ScopeFrame::new()],
|
2021-07-01 08:09:55 +02:00
|
|
|
}
|
2021-06-30 03:42:56 +02:00
|
|
|
}
|
|
|
|
|
2021-07-22 09:33:38 +02:00
|
|
|
pub fn merge_delta(this: &mut ParserState, mut delta: ParserDelta) {
|
2021-06-30 03:42:56 +02:00
|
|
|
// Take the mutable reference and extend the permanent state from the working set
|
2021-07-22 09:33:38 +02:00
|
|
|
this.files.extend(delta.files);
|
|
|
|
this.file_contents.extend(delta.file_contents);
|
|
|
|
this.decls.extend(delta.decls);
|
|
|
|
this.vars.extend(delta.vars);
|
|
|
|
this.blocks.extend(delta.blocks);
|
|
|
|
|
|
|
|
if let Some(last) = this.scope.last_mut() {
|
|
|
|
let first = delta.scope.remove(0);
|
|
|
|
for item in first.decls.into_iter() {
|
|
|
|
last.decls.insert(item.0, item.1);
|
|
|
|
}
|
|
|
|
for item in first.vars.into_iter() {
|
|
|
|
last.vars.insert(item.0, item.1);
|
2021-07-17 08:31:34 +02:00
|
|
|
}
|
2021-06-30 03:42:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn num_files(&self) -> usize {
|
|
|
|
self.files.len()
|
|
|
|
}
|
|
|
|
|
2021-07-01 08:09:55 +02:00
|
|
|
pub fn num_vars(&self) -> usize {
|
|
|
|
self.vars.len()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn num_decls(&self) -> usize {
|
|
|
|
self.decls.len()
|
|
|
|
}
|
|
|
|
|
2021-07-16 08:24:46 +02:00
|
|
|
pub fn num_blocks(&self) -> usize {
|
|
|
|
self.blocks.len()
|
|
|
|
}
|
|
|
|
|
2021-07-23 23:19:30 +02:00
|
|
|
pub fn print_vars(&self) {
|
|
|
|
for var in self.vars.iter().enumerate() {
|
|
|
|
println!("var{}: {:?}", var.0, var.1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn print_decls(&self) {
|
|
|
|
for decl in self.decls.iter().enumerate() {
|
|
|
|
println!("decl{}: {:?}", decl.0, decl.1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn print_blocks(&self) {
|
|
|
|
for block in self.blocks.iter().enumerate() {
|
|
|
|
println!("block{}: {:?}", block.0, block.1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-23 23:46:55 +02:00
|
|
|
pub fn find_decl(&self, name: &[u8]) -> Option<DeclId> {
|
|
|
|
for scope in self.scope.iter().rev() {
|
|
|
|
if let Some(decl_id) = scope.decls.get(name) {
|
|
|
|
return Some(*decl_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2021-07-23 07:14:49 +02:00
|
|
|
pub fn get_var(&self, var_id: VarId) -> &Type {
|
|
|
|
self.vars
|
|
|
|
.get(var_id)
|
|
|
|
.expect("internal error: missing variable")
|
2021-07-01 08:09:55 +02:00
|
|
|
}
|
|
|
|
|
2021-07-23 07:14:49 +02:00
|
|
|
pub fn get_decl(&self, decl_id: DeclId) -> &Declaration {
|
|
|
|
self.decls
|
|
|
|
.get(decl_id)
|
|
|
|
.expect("internal error: missing declaration")
|
2021-07-01 08:09:55 +02:00
|
|
|
}
|
|
|
|
|
2021-07-23 07:14:49 +02:00
|
|
|
pub fn get_block(&self, block_id: BlockId) -> &Block {
|
|
|
|
self.blocks
|
|
|
|
.get(block_id)
|
|
|
|
.expect("internal error: missing block")
|
2021-07-22 21:50:59 +02:00
|
|
|
}
|
|
|
|
|
2021-07-03 05:35:15 +02:00
|
|
|
pub fn next_span_start(&self) -> usize {
|
|
|
|
self.file_contents.len()
|
|
|
|
}
|
|
|
|
|
2021-07-02 00:54:04 +02:00
|
|
|
#[allow(unused)]
|
2021-06-30 03:42:56 +02:00
|
|
|
pub(crate) fn add_file(&mut self, filename: String, contents: Vec<u8>) -> usize {
|
2021-07-03 05:35:15 +02:00
|
|
|
let next_span_start = self.next_span_start();
|
2021-06-30 03:42:56 +02:00
|
|
|
|
2021-07-03 05:35:15 +02:00
|
|
|
self.file_contents.extend(&contents);
|
|
|
|
|
|
|
|
let next_span_end = self.next_span_start();
|
|
|
|
|
|
|
|
self.files.push((filename, next_span_start, next_span_end));
|
2021-07-03 05:11:24 +02:00
|
|
|
|
2021-07-03 05:35:15 +02:00
|
|
|
self.num_files() - 1
|
2021-07-03 05:11:24 +02:00
|
|
|
}
|
2021-06-30 03:42:56 +02:00
|
|
|
}
|
|
|
|
|
2021-07-16 08:24:46 +02:00
|
|
|
#[derive(Debug)]
|
2021-07-22 09:33:38 +02:00
|
|
|
pub struct ParserWorkingSet<'a> {
|
|
|
|
permanent_state: &'a ParserState,
|
|
|
|
pub delta: ParserDelta,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct ParserDelta {
|
2021-07-03 05:35:15 +02:00
|
|
|
files: Vec<(String, usize, usize)>,
|
|
|
|
pub(crate) file_contents: Vec<u8>,
|
2021-07-16 03:10:22 +02:00
|
|
|
vars: Vec<Type>, // indexed by VarId
|
|
|
|
decls: Vec<Declaration>, // indexed by DeclId
|
2021-07-16 22:26:40 +02:00
|
|
|
blocks: Vec<Block>, // indexed by BlockId
|
2021-07-01 08:09:55 +02:00
|
|
|
scope: Vec<ScopeFrame>,
|
|
|
|
}
|
|
|
|
|
2021-07-22 09:33:38 +02:00
|
|
|
impl ParserDelta {
|
|
|
|
pub fn num_files(&self) -> usize {
|
|
|
|
self.files.len()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn num_decls(&self) -> usize {
|
|
|
|
self.decls.len()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn num_blocks(&self) -> usize {
|
|
|
|
self.blocks.len()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn enter_scope(&mut self) {
|
|
|
|
self.scope.push(ScopeFrame::new());
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn exit_scope(&mut self) {
|
|
|
|
self.scope.pop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> ParserWorkingSet<'a> {
|
|
|
|
pub fn new(permanent_state: &'a ParserState) -> Self {
|
2021-06-30 03:42:56 +02:00
|
|
|
Self {
|
2021-07-22 09:33:38 +02:00
|
|
|
delta: ParserDelta {
|
|
|
|
files: vec![],
|
|
|
|
file_contents: vec![],
|
|
|
|
vars: vec![],
|
|
|
|
decls: vec![],
|
|
|
|
blocks: vec![],
|
|
|
|
scope: vec![ScopeFrame::new()],
|
|
|
|
},
|
2021-06-30 03:42:56 +02:00
|
|
|
permanent_state,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn num_files(&self) -> usize {
|
2021-07-22 09:33:38 +02:00
|
|
|
self.delta.num_files() + self.permanent_state.num_files()
|
2021-06-30 03:42:56 +02:00
|
|
|
}
|
|
|
|
|
2021-07-16 08:24:46 +02:00
|
|
|
pub fn num_decls(&self) -> usize {
|
2021-07-22 09:33:38 +02:00
|
|
|
self.delta.num_decls() + self.permanent_state.num_decls()
|
2021-07-16 08:24:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn num_blocks(&self) -> usize {
|
2021-07-22 09:33:38 +02:00
|
|
|
self.delta.num_blocks() + self.permanent_state.num_blocks()
|
2021-07-16 08:24:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_decl(&mut self, decl: Declaration) -> DeclId {
|
|
|
|
let name = decl.signature.name.as_bytes().to_vec();
|
|
|
|
|
2021-07-22 09:33:38 +02:00
|
|
|
self.delta.decls.push(decl);
|
2021-07-16 08:24:46 +02:00
|
|
|
let decl_id = self.num_decls() - 1;
|
|
|
|
|
2021-07-02 00:40:08 +02:00
|
|
|
let scope_frame = self
|
2021-07-22 09:33:38 +02:00
|
|
|
.delta
|
2021-07-02 00:40:08 +02:00
|
|
|
.scope
|
|
|
|
.last_mut()
|
|
|
|
.expect("internal error: missing required scope frame");
|
|
|
|
scope_frame.decls.insert(name, decl_id);
|
|
|
|
|
|
|
|
decl_id
|
|
|
|
}
|
|
|
|
|
2021-07-16 22:26:40 +02:00
|
|
|
pub fn add_block(&mut self, block: Block) -> BlockId {
|
2021-07-22 09:33:38 +02:00
|
|
|
self.delta.blocks.push(block);
|
2021-07-16 08:24:46 +02:00
|
|
|
|
|
|
|
self.num_blocks() - 1
|
|
|
|
}
|
|
|
|
|
2021-07-03 05:35:15 +02:00
|
|
|
pub fn next_span_start(&self) -> usize {
|
2021-07-22 09:33:38 +02:00
|
|
|
self.permanent_state.next_span_start() + self.delta.file_contents.len()
|
2021-07-03 05:35:15 +02:00
|
|
|
}
|
|
|
|
|
2021-07-22 22:45:23 +02:00
|
|
|
pub fn global_span_offset(&self) -> usize {
|
|
|
|
self.permanent_state.next_span_start()
|
|
|
|
}
|
|
|
|
|
2021-07-22 08:04:50 +02:00
|
|
|
pub fn add_file(&mut self, filename: String, contents: &[u8]) -> usize {
|
2021-07-03 05:35:15 +02:00
|
|
|
let next_span_start = self.next_span_start();
|
|
|
|
|
2021-07-22 09:33:38 +02:00
|
|
|
self.delta.file_contents.extend(contents);
|
2021-07-03 05:35:15 +02:00
|
|
|
|
|
|
|
let next_span_end = self.next_span_start();
|
|
|
|
|
2021-07-22 09:33:38 +02:00
|
|
|
self.delta
|
|
|
|
.files
|
|
|
|
.push((filename, next_span_start, next_span_end));
|
2021-06-30 03:42:56 +02:00
|
|
|
|
|
|
|
self.num_files() - 1
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_span_contents(&self, span: Span) -> &[u8] {
|
2021-07-22 09:33:38 +02:00
|
|
|
let permanent_end = self.permanent_state.next_span_start();
|
|
|
|
if permanent_end <= span.start {
|
|
|
|
&self.delta.file_contents[(span.start - permanent_end)..(span.end - permanent_end)]
|
2021-07-02 09:15:30 +02:00
|
|
|
} else {
|
2021-07-22 09:33:38 +02:00
|
|
|
&self.permanent_state.file_contents[span.start..span.end]
|
2021-07-02 09:15:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-30 03:42:56 +02:00
|
|
|
pub fn enter_scope(&mut self) {
|
2021-07-22 09:33:38 +02:00
|
|
|
self.delta.enter_scope();
|
2021-06-30 03:42:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn exit_scope(&mut self) {
|
2021-07-22 09:33:38 +02:00
|
|
|
self.delta.exit_scope();
|
2021-06-30 03:42:56 +02:00
|
|
|
}
|
|
|
|
|
2021-07-01 08:09:55 +02:00
|
|
|
pub fn find_decl(&self, name: &[u8]) -> Option<DeclId> {
|
2021-07-22 09:33:38 +02:00
|
|
|
for scope in self.delta.scope.iter().rev() {
|
2021-07-08 00:55:46 +02:00
|
|
|
if let Some(decl_id) = scope.decls.get(name) {
|
2021-07-01 08:09:55 +02:00
|
|
|
return Some(*decl_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-22 09:33:38 +02:00
|
|
|
for scope in self.permanent_state.scope.iter().rev() {
|
|
|
|
if let Some(decl_id) = scope.decls.get(name) {
|
|
|
|
return Some(*decl_id);
|
2021-07-17 08:31:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-01 08:09:55 +02:00
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2021-07-17 21:34:43 +02:00
|
|
|
pub fn contains_decl_partial_match(&self, name: &[u8]) -> bool {
|
2021-07-22 09:33:38 +02:00
|
|
|
for scope in self.delta.scope.iter().rev() {
|
2021-07-17 21:34:43 +02:00
|
|
|
for decl in &scope.decls {
|
|
|
|
if decl.0.starts_with(name) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-22 09:33:38 +02:00
|
|
|
for scope in self.permanent_state.scope.iter().rev() {
|
|
|
|
for decl in &scope.decls {
|
|
|
|
if decl.0.starts_with(name) {
|
|
|
|
return true;
|
2021-07-17 21:34:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2021-07-01 08:09:55 +02:00
|
|
|
pub fn next_var_id(&self) -> VarId {
|
2021-07-22 09:33:38 +02:00
|
|
|
let num_permanent_vars = self.permanent_state.num_vars();
|
|
|
|
num_permanent_vars + self.delta.vars.len()
|
2021-07-01 08:09:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn find_variable(&self, name: &[u8]) -> Option<VarId> {
|
2021-07-22 09:33:38 +02:00
|
|
|
for scope in self.delta.scope.iter().rev() {
|
2021-07-08 00:55:46 +02:00
|
|
|
if let Some(var_id) = scope.vars.get(name) {
|
2021-07-01 08:09:55 +02:00
|
|
|
return Some(*var_id);
|
2021-06-30 03:42:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-22 09:33:38 +02:00
|
|
|
for scope in self.permanent_state.scope.iter().rev() {
|
|
|
|
if let Some(var_id) = scope.vars.get(name) {
|
|
|
|
return Some(*var_id);
|
2021-07-17 08:31:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-30 03:42:56 +02:00
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2021-07-30 00:56:51 +02:00
|
|
|
pub fn add_variable(&mut self, mut name: Vec<u8>, ty: Type) -> VarId {
|
2021-07-01 08:09:55 +02:00
|
|
|
let next_id = self.next_var_id();
|
|
|
|
|
2021-07-30 00:56:51 +02:00
|
|
|
// correct name if necessary
|
|
|
|
if !name.starts_with(b"$") {
|
|
|
|
name.insert(0, b'$');
|
|
|
|
}
|
|
|
|
|
2021-07-01 02:01:04 +02:00
|
|
|
let last = self
|
2021-07-22 09:33:38 +02:00
|
|
|
.delta
|
2021-07-01 02:01:04 +02:00
|
|
|
.scope
|
|
|
|
.last_mut()
|
|
|
|
.expect("internal error: missing stack frame");
|
|
|
|
|
|
|
|
last.vars.insert(name, next_id);
|
|
|
|
|
2021-07-23 23:19:30 +02:00
|
|
|
self.delta.vars.push(ty);
|
2021-07-01 02:01:04 +02:00
|
|
|
|
|
|
|
next_id
|
|
|
|
}
|
2021-07-01 08:09:55 +02:00
|
|
|
|
2021-07-23 23:19:30 +02:00
|
|
|
pub fn set_variable_type(&mut self, var_id: VarId, ty: Type) {
|
|
|
|
let num_permanent_vars = self.permanent_state.num_vars();
|
|
|
|
if var_id < num_permanent_vars {
|
|
|
|
panic!("Internal error: attempted to set into permanent state from working set")
|
|
|
|
} else {
|
|
|
|
self.delta.vars[var_id - num_permanent_vars] = ty;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-23 07:14:49 +02:00
|
|
|
pub fn get_variable(&self, var_id: VarId) -> &Type {
|
2021-07-22 09:33:38 +02:00
|
|
|
let num_permanent_vars = self.permanent_state.num_vars();
|
|
|
|
if var_id < num_permanent_vars {
|
2021-07-22 21:50:59 +02:00
|
|
|
self.permanent_state.get_var(var_id)
|
2021-07-01 08:09:55 +02:00
|
|
|
} else {
|
2021-07-23 07:14:49 +02:00
|
|
|
self.delta
|
|
|
|
.vars
|
|
|
|
.get(var_id - num_permanent_vars)
|
|
|
|
.expect("internal error: missing variable")
|
2021-07-01 08:09:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-23 07:14:49 +02:00
|
|
|
pub fn get_decl(&self, decl_id: DeclId) -> &Declaration {
|
2021-07-22 09:33:38 +02:00
|
|
|
let num_permanent_decls = self.permanent_state.num_decls();
|
|
|
|
if decl_id < num_permanent_decls {
|
2021-07-22 21:50:59 +02:00
|
|
|
self.permanent_state.get_decl(decl_id)
|
|
|
|
} else {
|
2021-07-23 07:14:49 +02:00
|
|
|
self.delta
|
|
|
|
.decls
|
|
|
|
.get(decl_id - num_permanent_decls)
|
|
|
|
.expect("internal error: missing declaration")
|
2021-07-22 21:50:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-23 07:14:49 +02:00
|
|
|
pub fn get_block(&self, block_id: BlockId) -> &Block {
|
2021-07-22 21:50:59 +02:00
|
|
|
let num_permanent_blocks = self.permanent_state.num_blocks();
|
|
|
|
if block_id < num_permanent_blocks {
|
|
|
|
self.permanent_state.get_block(block_id)
|
2021-07-01 08:09:55 +02:00
|
|
|
} else {
|
2021-07-23 07:14:49 +02:00
|
|
|
self.delta
|
|
|
|
.blocks
|
|
|
|
.get(block_id - num_permanent_blocks)
|
|
|
|
.expect("internal error: missing block")
|
2021-07-01 08:09:55 +02:00
|
|
|
}
|
|
|
|
}
|
2021-07-22 09:48:45 +02:00
|
|
|
|
|
|
|
pub fn render(self) -> ParserDelta {
|
|
|
|
self.delta
|
|
|
|
}
|
2021-07-01 02:01:04 +02:00
|
|
|
}
|
2021-06-30 03:42:56 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod parser_state_tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_file_gives_id() {
|
2021-07-22 09:33:38 +02:00
|
|
|
let parser_state = ParserState::new();
|
|
|
|
let mut parser_state = ParserWorkingSet::new(&parser_state);
|
2021-07-22 08:04:50 +02:00
|
|
|
let id = parser_state.add_file("test.nu".into(), &[]);
|
2021-06-30 03:42:56 +02:00
|
|
|
|
|
|
|
assert_eq!(id, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_file_gives_id_including_parent() {
|
|
|
|
let mut parser_state = ParserState::new();
|
|
|
|
let parent_id = parser_state.add_file("test.nu".into(), vec![]);
|
|
|
|
|
2021-07-22 09:33:38 +02:00
|
|
|
let mut working_set = ParserWorkingSet::new(&parser_state);
|
2021-07-22 08:04:50 +02:00
|
|
|
let working_set_id = working_set.add_file("child.nu".into(), &[]);
|
2021-06-30 03:42:56 +02:00
|
|
|
|
|
|
|
assert_eq!(parent_id, 0);
|
|
|
|
assert_eq!(working_set_id, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn merge_states() {
|
|
|
|
let mut parser_state = ParserState::new();
|
2021-07-02 00:54:04 +02:00
|
|
|
parser_state.add_file("test.nu".into(), vec![]);
|
2021-06-30 03:42:56 +02:00
|
|
|
|
2021-07-22 09:33:38 +02:00
|
|
|
let delta = {
|
|
|
|
let mut working_set = ParserWorkingSet::new(&parser_state);
|
|
|
|
working_set.add_file("child.nu".into(), &[]);
|
2021-07-22 09:48:45 +02:00
|
|
|
working_set.render()
|
2021-07-22 09:33:38 +02:00
|
|
|
};
|
2021-06-30 03:42:56 +02:00
|
|
|
|
2021-07-22 09:33:38 +02:00
|
|
|
ParserState::merge_delta(&mut parser_state, delta);
|
2021-06-30 03:42:56 +02:00
|
|
|
|
|
|
|
assert_eq!(parser_state.num_files(), 2);
|
|
|
|
assert_eq!(&parser_state.files[0].0, "test.nu");
|
|
|
|
assert_eq!(&parser_state.files[1].0, "child.nu");
|
|
|
|
}
|
|
|
|
}
|