parent
064a612434
commit
9404719d3a
8
moduletests/expected/assoc.bash
Normal file
8
moduletests/expected/assoc.bash
Normal file
@ -0,0 +1,8 @@
|
||||
|
||||
declare -A assoc
|
||||
assoc[$1]=$3
|
||||
assoc[$1]+=_1
|
||||
assoc[$2]=$3
|
||||
assoc[$2]+=_2
|
||||
echo "«${assoc[$1]}»"
|
||||
echo "«${assoc[$2]}»"
|
8
moduletests/original/assoc.bash
Normal file
8
moduletests/original/assoc.bash
Normal file
@ -0,0 +1,8 @@
|
||||
|
||||
declare -A assoc
|
||||
assoc[$1]=$3
|
||||
assoc[$1]+=_1
|
||||
assoc[$2]=$3
|
||||
assoc[$2]+=_2
|
||||
echo "«${assoc[$1]}»"
|
||||
echo "«${assoc[$2]}»"
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2016 - 2019 Andreas Nordal
|
||||
* Copyright 2016 - 2020 Andreas Nordal
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
@ -48,22 +48,17 @@ pub fn keyword_or_command(
|
||||
})), pre: i, len: 1, alt: None
|
||||
};
|
||||
}
|
||||
let mut len = identifierlen(&horizon[i..]);
|
||||
if i + len == horizon.len() && (i > 0 || is_horizon_lengthenable) {
|
||||
let (found, len) = find_lvalue(&horizon[i..]);
|
||||
if found == Tri::Maybe && (i > 0 || is_horizon_lengthenable) {
|
||||
return flush(i);
|
||||
}
|
||||
if len > 0 && i + len < horizon.len() {
|
||||
if horizon[i + len] == b'+' && i + len + 1 < horizon.len() {
|
||||
len += 1;
|
||||
}
|
||||
if horizon[i + len] == b'=' {
|
||||
return WhatNow{
|
||||
tri: Transition::Push(Box::new(SitRvalue{end_trigger})),
|
||||
pre: i + len + 1, len: 0, alt: None
|
||||
};
|
||||
}
|
||||
if found == Tri::Yes {
|
||||
return WhatNow{
|
||||
tri: Transition::Push(Box::new(SitRvalue{end_trigger})),
|
||||
pre: i + len, len: 0, alt: None
|
||||
};
|
||||
}
|
||||
let len = len + predlen(is_word, &horizon[i+len..]);
|
||||
let len = predlen(is_word, &horizon[i..]);
|
||||
if i + len == horizon.len() && (i > 0 || is_horizon_lengthenable) {
|
||||
return flush(i);
|
||||
}
|
||||
@ -276,6 +271,51 @@ fn find_usual_suspects(
|
||||
None
|
||||
}
|
||||
|
||||
#[derive(PartialEq)]
|
||||
#[derive(Clone)]
|
||||
#[derive(Copy)]
|
||||
enum Tri {
|
||||
No,
|
||||
Maybe,
|
||||
Yes,
|
||||
}
|
||||
|
||||
fn find_lvalue(horizon: &[u8]) -> (Tri, usize) {
|
||||
let mut ate = identifierlen(&horizon);
|
||||
if ate == 0 {
|
||||
return (Tri::No, ate);
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
#[derive(Copy)]
|
||||
enum Lex {
|
||||
Ident,
|
||||
Brack,
|
||||
Pluss,
|
||||
}
|
||||
let mut state = Lex::Ident;
|
||||
|
||||
loop {
|
||||
if ate == horizon.len() {
|
||||
return (Tri::Maybe, ate);
|
||||
}
|
||||
let byte :u8 = horizon[ate];
|
||||
ate += 1;
|
||||
|
||||
// TODO: Recursion: Expression tracker
|
||||
match (state, byte) {
|
||||
(Lex::Ident, b'=') => return (Tri::Yes, ate),
|
||||
(Lex::Pluss, b'=') => return (Tri::Yes, ate),
|
||||
(Lex::Ident, b'[') => state = Lex::Brack,
|
||||
(Lex::Brack, b']') => state = Lex::Ident,
|
||||
(Lex::Ident, b'+') => state = Lex::Pluss,
|
||||
(Lex::Ident, _) => return (Tri::No, ate),
|
||||
(Lex::Pluss, _) => return (Tri::No, ate),
|
||||
(Lex::Brack, _) => {},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn find_heredoc(horizon: &[u8]) -> (usize, Vec<u8>) {
|
||||
let mut ate = predlen(|x| x == b'<', &horizon);
|
||||
let mut found = Vec::<u8>::new();
|
||||
@ -334,3 +374,20 @@ fn find_heredoc(horizon: &[u8]) -> (usize, Vec<u8>) {
|
||||
}
|
||||
(ate, found)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_find_lvalue() {
|
||||
assert!(find_lvalue(b"") == (Tri::No, 0));
|
||||
assert!(find_lvalue(b"=") == (Tri::No, 0));
|
||||
assert!(find_lvalue(b"[]") == (Tri::No, 0));
|
||||
assert!(find_lvalue(b"esa") == (Tri::Maybe, 3));
|
||||
assert!(find_lvalue(b"esa+") == (Tri::Maybe, 4));
|
||||
assert!(find_lvalue(b"esa[]") == (Tri::Maybe, 5));
|
||||
assert!(find_lvalue(b"esa[]+") == (Tri::Maybe, 6));
|
||||
assert!(find_lvalue(b"esa ") == (Tri::No, 4));
|
||||
assert!(find_lvalue(b"esa]") == (Tri::No, 4));
|
||||
assert!(find_lvalue(b"esa=") == (Tri::Yes, 4));
|
||||
assert!(find_lvalue(b"esa+=") == (Tri::Yes, 5));
|
||||
assert!(find_lvalue(b"esa[]=") == (Tri::Yes, 6));
|
||||
assert!(find_lvalue(b"esa[]+=") == (Tri::Yes, 7));
|
||||
}
|
||||
|
@ -97,10 +97,63 @@ impl Situation for SitArg {
|
||||
#[cfg(test)]
|
||||
use ::testhelpers::*;
|
||||
#[cfg(test)]
|
||||
use sitrvalue::SitRvalue;
|
||||
#[cfg(test)]
|
||||
use sitextent::SitExtent;
|
||||
#[cfg(test)]
|
||||
use sitvec::SitVec;
|
||||
#[cfg(test)]
|
||||
use situation::COLOR_KWD;
|
||||
#[cfg(test)]
|
||||
use situation::COLOR_HERE;
|
||||
|
||||
#[cfg(test)]
|
||||
fn mk_assignment(pre: usize) -> WhatNow {
|
||||
WhatNow{
|
||||
tri: Transition::Push(Box::new(SitRvalue{end_trigger: 0})),
|
||||
pre, len: 0, alt: None
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
fn mk_cmd(pre: usize) -> WhatNow {
|
||||
WhatNow{
|
||||
tri: Transition::Push(Box::new(SitCmd{end_trigger: 0})),
|
||||
pre, len: 0, alt: None
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
fn mk_kwd(pre: usize) -> WhatNow {
|
||||
WhatNow{
|
||||
tri: Transition::Push(Box::new(SitExtent{
|
||||
len: 0,
|
||||
color: COLOR_KWD,
|
||||
end_insert: None
|
||||
})), pre, len: 0, alt: None
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sit_normal() {
|
||||
let subj = || {
|
||||
SitNormal{end_trigger: 0, end_replace: None}
|
||||
};
|
||||
|
||||
sit_expect!(subj(), b"esa", &flush(0), &mk_cmd(0));
|
||||
sit_expect!(subj(), b"esa=", &mk_assignment(4));
|
||||
sit_expect!(subj(), b"esac", &flush(0), &mk_kwd(0));
|
||||
sit_expect!(subj(), b"esac=", &mk_assignment(5));
|
||||
sit_expect!(subj(), b"esack", &flush(0), &mk_cmd(0));
|
||||
sit_expect!(subj(), b"esack=", &mk_assignment(6));
|
||||
sit_expect!(subj(), b";esa", &flush(1));
|
||||
sit_expect!(subj(), b";esa=", &mk_assignment(5));
|
||||
sit_expect!(subj(), b";esac", &flush(1));
|
||||
sit_expect!(subj(), b";esac=", &mk_assignment(6));
|
||||
sit_expect!(subj(), b";esack", &flush(1));
|
||||
sit_expect!(subj(), b";esack=", &mk_assignment(7));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sit_arg() {
|
||||
let found_heredoc = WhatNow{
|
||||
@ -109,13 +162,17 @@ fn test_sit_arg() {
|
||||
)),
|
||||
pre: 0, len: 8, alt: None
|
||||
};
|
||||
sit_expect!(SitArg{end_trigger: 0}, b"", &flush_or_pop(0));
|
||||
sit_expect!(SitArg{end_trigger: 0}, b" ", &flush_or_pop(1));
|
||||
sit_expect!(SitArg{end_trigger: 0}, b"arg", &flush_or_pop(3));
|
||||
sit_expect!(SitArg{end_trigger: 0}, b"<<- \"\\\\\"\n", &found_heredoc);
|
||||
sit_expect!(SitArg{end_trigger: 0}, b"a <<- \"\\\\\"", &flush(2));
|
||||
sit_expect!(SitArg{end_trigger: 0}, b"a <<- \"\\", &flush(2));
|
||||
sit_expect!(SitArg{end_trigger: 0}, b"a <<- ", &flush(2));
|
||||
sit_expect!(SitArg{end_trigger: 0}, b"a <", &flush(2));
|
||||
sit_expect!(SitArg{end_trigger: 0}, b"a ", &flush_or_pop(2));
|
||||
let subj = || {
|
||||
SitArg{end_trigger: 0}
|
||||
};
|
||||
|
||||
sit_expect!(subj(), b"", &flush_or_pop(0));
|
||||
sit_expect!(subj(), b" ", &flush_or_pop(1));
|
||||
sit_expect!(subj(), b"arg", &flush_or_pop(3));
|
||||
sit_expect!(subj(), b"<<- \"\\\\\"\n", &found_heredoc);
|
||||
sit_expect!(subj(), b"a <<- \"\\\\\"", &flush(2));
|
||||
sit_expect!(subj(), b"a <<- \"\\", &flush(2));
|
||||
sit_expect!(subj(), b"a <<- ", &flush(2));
|
||||
sit_expect!(subj(), b"a <", &flush(2));
|
||||
sit_expect!(subj(), b"a ", &flush_or_pop(2));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user