Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: runtime literal union type check. #600

Merged
merged 1 commit into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 23 additions & 17 deletions kclvm/runtime/src/value/val_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,13 +508,19 @@ pub fn is_literal_type(tpe: &str) -> bool {
/// is_dict_type returns the type string whether is a dict type
#[inline]
pub fn is_dict_type(tpe: &str) -> bool {
tpe.len() >= 2 && &tpe[0..1] == "{" && &tpe[tpe.len() - 1..] == "}"
let count = tpe.chars().count();
count >= 2
&& matches!(tpe.chars().nth(0), Some('{'))
&& matches!(tpe.chars().nth(count - 1), Some('}'))
}

/// is_list_type returns the type string whether is a list type
#[inline]
pub fn is_list_type(tpe: &str) -> bool {
tpe.len() >= 2 && &tpe[0..1] == "[" && &tpe[tpe.len() - 1..] == "]"
let count = tpe.chars().count();
count >= 2
&& matches!(tpe.chars().nth(0), Some('['))
&& matches!(tpe.chars().nth(count - 1), Some(']'))
}

#[inline]
Expand Down Expand Up @@ -546,16 +552,16 @@ pub fn is_type_union(tpe: &str) -> bool {
} else if c == ']' || c == '}' {
stack.pop();
} else if c == '\"' {
let t = &tpe[i..];
let t: String = tpe.chars().skip(i).collect();
let re = fancy_regex::Regex::new(r#""(?!"").*?(?<!\\)(\\\\)*?""#).unwrap();
if let Ok(Some(v)) = re.find(t) {
i += v.range().end - 1;
if let Ok(Some(v)) = re.find(&t) {
i += v.as_str().chars().count() - 1;
}
} else if c == '\'' {
let t = &tpe[i..];
let t: String = tpe.chars().skip(i).collect();
let re = fancy_regex::Regex::new(r#"'(?!'').*?(?<!\\)(\\\\)*?'"#).unwrap();
if let Ok(Some(v)) = re.find(t) {
i += v.range().end - 1;
if let Ok(Some(v)) = re.find(&t) {
i += v.as_str().chars().count() - 1;
}
}
i += 1;
Expand All @@ -580,10 +586,10 @@ pub fn split_type_union(tpe: &str) -> Vec<&str> {
let mut stack = String::new();
let mut types: Vec<&str> = vec![];
while i < tpe.chars().count() {
let c = tpe.chars().nth(i).unwrap();
let (c_idx, c) = tpe.char_indices().nth(i).unwrap();
if c == '|' && stack.is_empty() {
types.push(&tpe[s_index..i]);
s_index = i + 1;
types.push(&tpe[s_index..c_idx]);
s_index = c_idx + 1;
}
// List/Dict type
else if c == '[' || c == '{' {
Expand All @@ -595,18 +601,18 @@ pub fn split_type_union(tpe: &str) -> Vec<&str> {
}
// String literal type
else if c == '\"' {
let t = &tpe[i..];
let t: String = tpe.chars().skip(i).collect();
let re = fancy_regex::Regex::new(r#""(?!"").*?(?<!\\)(\\\\)*?""#).unwrap();
if let Ok(Some(v)) = re.find(t) {
i += v.range().end - 1;
if let Ok(Some(v)) = re.find(&t) {
i += v.as_str().chars().count() - 1;
}
}
// String literal type
else if c == '\'' {
let t = &tpe[i..];
let t: String = tpe.chars().skip(i).collect();
let re = fancy_regex::Regex::new(r#"'(?!'').*?(?<!\\)(\\\\)*?'"#).unwrap();
if let Ok(Some(v)) = re.find(t) {
i += v.range().end - 1;
if let Ok(Some(v)) = re.find(&t) {
i += v.as_str().chars().count() - 1;
}
}
i += 1;
Expand Down
1 change: 1 addition & 0 deletions test/grammar/types/literal/lit_07_uf8_str_union/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
msg: "无需容灾" | "标准型" | "流水型" = "流水型"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
msg: 流水型
Loading