Skip to content

Commit 1bb1994

Browse files
committed
feat(parser): handle missing rparen error
1 parent 093bf73 commit 1bb1994

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

crates/erg_parser/error.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,23 @@ impl LexError {
175175
))
176176
}
177177

178+
pub fn parenthesize_error(errno: usize, loc: Location, got: &str) -> Self {
179+
let msg = switch_lang!(
180+
"japanese" => "不適切な閉じ括弧があります",
181+
"simplified_chinese" => "存在不匹配的右括号",
182+
"traditional_chinese" => "存在不匹配的右括號",
183+
"english" => "unmatched closing parenthesis found",
184+
);
185+
let got = StyledStr::new(got, Some(ERR), Some(ATTR));
186+
let hint = switch_lang!(
187+
"japanese" => format!("この{}は対応する開き括弧がないため、削除するか開き括弧を追加してください", got),
188+
"simplified_chinese" => format!("{}缺少对应的左括号,请删除或添加左括号", got),
189+
"traditional_chinese" => format!("{}缺少對應的左括號,請刪除或添加左括號", got),
190+
"english" => format!("{} has no matching opening parenthesis - either remove it or add an opening parenthesis", got),
191+
);
192+
Self::syntax_error(errno, loc, msg, Some(hint))
193+
}
194+
178195
pub fn expect_next_line_error(errno: usize, loc: Location, caused: &str) -> Self {
179196
Self::new(ErrorCore::new(
180197
vec![SubMessage::ambiguous_new(

crates/erg_parser/parse.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2050,6 +2050,13 @@ impl Parser {
20502050
debug_exit_info!(self);
20512051
return Err(());
20522052
}
2053+
Some(t) if t.is(RParen) => {
2054+
let err = ParseError::parenthesize_error(line!() as usize, t.loc(), ")");
2055+
self.errs.push(err);
2056+
debug_exit_info!(self);
2057+
return Err(());
2058+
}
2059+
20532060
_ => {
20542061
if stack.len() <= 1 {
20552062
break;

0 commit comments

Comments
 (0)