fix: collections are handled similarly to multi-line str

This commit is contained in:
GreasySlug 2024-03-04 00:30:08 +09:00
parent 87cb7c3af5
commit 045e5c8064
2 changed files with 55 additions and 6 deletions

View file

@ -600,7 +600,7 @@ impl VirtualMachine {
if bk == BlockKind::AtMark || bk == BlockKind::ClassDef {
return;
}
if bk == BlockKind::MultiLineStr {
if bk == BlockKind::MultiLineStr || bk == BlockKind::Collections {
self.length = 1;
return;
}
@ -625,7 +625,7 @@ impl VirtualMachine {
}
pub fn indent(&mut self) -> String {
if self.now == BlockKind::MultiLineStr {
if self.now == BlockKind::MultiLineStr || self.now == BlockKind::Collections {
String::new()
} else if self.length == 0 {
self.length = 1;
@ -797,7 +797,7 @@ pub trait Runnable: Sized + Default + New {
instance.clear();
continue;
}
"" | "}" | ")" => {
"" | "}" | ")" | "]" => {
// eval after the end of the block
if vm.now == BlockKind::Collections && line == "}" {
vm.push_code("}");
@ -808,7 +808,14 @@ pub trait Runnable: Sized + Default + New {
} else if vm.now == BlockKind::Collections && line == "]" {
vm.push_code("]");
vm.push_code("\n");
} else if vm.now == BlockKind::MultiLineStr
|| vm.now == BlockKind::Collections
{
vm.push_code(line);
vm.push_code("\n");
continue;
}
if vm.now_block.len() == 2 {
vm.remove_block_kind();
} else if vm.now_block.len() > 1 {
@ -819,7 +826,6 @@ pub trait Runnable: Sized + Default + New {
match instance.eval(mem::take(&mut vm.codes)) {
Ok(out) if out.is_empty() => {
instance.input().set_block_begin();
continue;
}
Ok(out) => {
output.write_all((out + "\n").as_bytes()).unwrap();
@ -850,6 +856,14 @@ pub trait Runnable: Sized + Default + New {
line
};
let bk = instance.expect_block(line);
let bk = if bk == BlockKind::Error
&& (vm.now == BlockKind::MultiLineStr || vm.now == BlockKind::Collections)
{
BlockKind::None
} else {
bk
};
// let bk = instance.expect_block(line);
match bk {
BlockKind::None if vm.now == BlockKind::AtMark => {
if let Some(eq) = line.find('=') {
@ -888,7 +902,10 @@ pub trait Runnable: Sized + Default + New {
vm.push_code("\n");
vm.now = BlockKind::Main;
}
BlockKind::None if vm.now == BlockKind::MultiLineStr => {
BlockKind::None
if vm.now == BlockKind::MultiLineStr
|| vm.now == BlockKind::Collections =>
{
vm.push_code(line);
vm.push_code("\n");
continue;
@ -914,6 +931,13 @@ pub trait Runnable: Sized + Default + New {
vm.push_code(line);
vm.push_code("\n");
}
// end of Collection
BlockKind::Collections if vm.now == BlockKind::Collections => {
vm.remove_block_kind();
vm.length = vm.now_block.len();
vm.push_code(line);
vm.push_code("\n");
}
// start of MultiLineStr
BlockKind::MultiLineStr => {
vm.push_block_kind(BlockKind::MultiLineStr);
@ -923,6 +947,14 @@ pub trait Runnable: Sized + Default + New {
vm.push_code("\n");
continue;
}
BlockKind::Collections => {
vm.push_block_kind(BlockKind::Collections);
vm.push_code(indent.as_str());
instance.input().insert_whitespace(indent.as_str());
vm.push_code(line);
vm.push_code("\n");
continue;
}
// block is expected but string
_ if vm.now == BlockKind::MultiLineStr => {
vm.push_code(line);

View file

@ -706,6 +706,12 @@ impl Parser {
.opt_reduce_decorator()
.map_err(|_| self.stack_dec(fn_name!()))?
{
if self.cur_is(EOF) {
let err =
ParseError::expect_next_line_error(line!() as usize, deco.0.loc(), "AtMark");
self.errs.push(err);
return Err(());
}
decs.insert(deco);
expect_pop!(self, fail_next Newline);
}
@ -853,6 +859,12 @@ impl Parser {
fn try_reduce_array_elems(&mut self) -> ParseResult<ArrayInner> {
debug_call_info!(self);
if self.cur_is(EOF) {
let tk = self.tokens.last().unwrap();
let err = ParseError::expect_next_line_error(line!() as usize, tk.loc(), "Collections");
self.errs.push(err);
return Err(());
}
if self.cur_category_is(TC::REnclosure) {
let args = Args::empty();
debug_exit_info!(self);
@ -2866,7 +2878,12 @@ impl Parser {
fn try_reduce_brace_container(&mut self) -> ParseResult<BraceContainer> {
debug_call_info!(self);
let l_brace = expect_pop!(self, fail_next LBrace);
if self.cur_is(EOF) {
let err =
ParseError::expect_next_line_error(line!() as usize, l_brace.loc(), "Collections");
self.errs.push(err);
return Err(());
}
// Empty brace literals
match self.peek_kind() {
Some(RBrace) => {