Skip to content

Commit

Permalink
fix: fix CR comments and add more test case
Browse files Browse the repository at this point in the history
Signed-off-by: zongz <[email protected]>
  • Loading branch information
zong-zhe committed Nov 2, 2023
1 parent ba03b31 commit f9a11fe
Show file tree
Hide file tree
Showing 53 changed files with 3,611 additions and 40 deletions.
17 changes: 15 additions & 2 deletions kclvm/ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1639,8 +1639,21 @@ impl ToString for Type {
w.push_str(&format!("\"{}\"", v.replace('"', "\\\"")));
}
},
Type::Function(_) => {
w.push_str("function");
Type::Function(v) => {
w.push_str("(");
if let Some(params) = &v.params_ty {
for (i, param) in params.iter().enumerate() {
if i > 0 {
w.push_str(", ");
}
to_str(&param.node, w);
}
}
w.push_str(")");
if let Some(ret) = &v.ret_ty {
w.push_str(" -> ");
to_str(&ret.node, w);
}
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions kclvm/parser/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ impl<'a> Parser<'a> {
self.cursor.next().is_some()
}

#[inline]
/// Whether the parser has the next token in the token stream.
pub(crate) fn peek_has_next(&mut self) -> bool {
self.cursor.peek().is_some()
}

pub(crate) fn bump_keyword(&mut self, kw: Symbol) {
if !self.token.is_keyword(kw) {
self.sess.struct_token_error(&[kw.into()], self.token);
Expand Down
35 changes: 34 additions & 1 deletion kclvm/parser/src/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,42 @@ impl<'a> Parser<'a> {

Some(t)
} else {
let miss_expr = self.missing_expr();
self.skip_newlines();
self.sess
.struct_token_error(&[TokenKind::Assign.into()], self.token);
None
if type_annotation.is_some() && !targets.is_empty() && !is_in_schema_stmt {
let mut pos = targets[0].pos();
pos.3 = targets.last().unwrap().end_line;
pos.4 = targets.last().unwrap().end_column;

let targets: Vec<_> = targets
.iter()
.map(|expr| match &expr.node {
Expr::Identifier(x) => {
let mut x = x.clone();
x.ctx = ExprContext::Store;
Box::new(Node::node_with_pos(x, expr.pos()))
}
_ => {
self.sess
.struct_token_error(&[TokenKind::ident_value()], self.token);
Box::new(expr.into_missing_identifier())
}
})
.collect();
Some(Box::new(Node::node_with_pos(
Stmt::Assign(AssignStmt {
targets: targets.clone(),
value: miss_expr,
type_annotation,
ty,
}),
pos,
)))
} else {
None
}
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions kclvm/parser/src/parser/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,12 @@ impl<'a> Parser<'a> {
}
// (type) -> type
else if let TokenKind::OpenDelim(DelimToken::Paren) = self.token.kind {
// Parse function type start with '('
self.bump_token(TokenKind::OpenDelim(DelimToken::Paren));

let mut params_type = vec![];
// Parse all the params type until the params list end ')'
while self.token.kind != TokenKind::CloseDelim(DelimToken::Paren) {
while self.token.kind != TokenKind::CloseDelim(DelimToken::Paren)
&& self.peek_has_next()
{
params_type.push(self.parse_type_annotation());
// All the params type should be separated by ','
if let TokenKind::Comma = self.token.kind {
Expand Down Expand Up @@ -271,9 +271,11 @@ impl<'a> Parser<'a> {
TokenKind::literal_value(),
TokenKind::OpenDelim(DelimToken::Bracket).into(),
TokenKind::OpenDelim(DelimToken::Brace).into(),
TokenKind::CloseDelim(DelimToken::Paren).into(),
],
self.token,
);
self.bump();
Box::new(Node::node(
Type::Any,
self.sess.struct_token_loc(token, self.prev_token),
Expand Down
28 changes: 28 additions & 0 deletions kclvm/parser/src/tests/error_recovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,31 @@ parse_module_snapshot! { rule_stmt_recovery_7, r#"
rule A:
@
"#}
parse_module_snapshot! { fn_ty_annotation_recovery_0, r#"a:("#}
parse_module_snapshot! { fn_ty_annotation_recovery_1, r#"a:(i"#}
parse_module_snapshot! { fn_ty_annotation_recovery_2, r#"a:(int"#}
parse_module_snapshot! { fn_ty_annotation_recovery_3, r#"a:i)"#}
parse_module_snapshot! { fn_ty_annotation_recovery_4, r#"a:([i"#}
parse_module_snapshot! { fn_ty_annotation_recovery_5, r#"a:([i:"#}
parse_module_snapshot! { fn_ty_annotation_recovery_6, r#"a:([i]"#}
parse_module_snapshot! { fn_ty_annotation_recovery_7, r#"a:([int]"#}
parse_module_snapshot! { fn_ty_annotation_recovery_8, r#"a:([int"#}
parse_module_snapshot! { fn_ty_annotation_recovery_9, r#"a:({}"#}
parse_module_snapshot! { fn_ty_annotation_recovery_10, r#"a:({"#}
parse_module_snapshot! { fn_ty_annotation_recovery_11, r#"a:({i"#}
parse_module_snapshot! { fn_ty_annotation_recovery_12, r#"a:({i:"#}
parse_module_snapshot! { fn_ty_annotation_recovery_13, r#"a:({i:i"#}
parse_module_snapshot! { fn_ty_annotation_recovery_14, r#"a:({i:int"#}
parse_module_snapshot! { fn_ty_annotation_recovery_15, r#"a:({i:int]"#}
parse_module_snapshot! { fn_ty_annotation_recovery_16, r#"a:({str:int]"#}
parse_module_snapshot! { fn_ty_annotation_recovery_17, r#"a:({str:int}"#}
parse_module_snapshot! { fn_ty_annotation_recovery_18, r#"a:({str:int} ->"#}
parse_module_snapshot! { fn_ty_annotation_recovery_19, r#"a:({str:int}) -> i"#}

parse_module_snapshot! { fn_ty_annotation_recovery_20, r#"a:(str|int) -> i"#}

parse_module_snapshot! { fn_ty_annotation_recovery_21, r#"a:(str|int, int) -> i"#}

parse_module_snapshot! { fn_ty_annotation_recovery_22, r#"a:(str|int, int|"#}

parse_module_snapshot! { fn_ty_annotation_recovery_23, r#"a:(str|int, int|) ->"#}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
source: parser/src/tests/error_recovery.rs
expression: "crate::tests::parsing_module_string(r#\"a:(\"#)"
---
Module {
filename: "",
pkg: "",
doc: "",
name: "",
body: [],
comments: [],
}

Original file line number Diff line number Diff line change
@@ -1,14 +1,78 @@
---
source: parser/src/tests/error_recovery.rs
assertion_line: 149
expression: "crate::tests::parsing_module_string(r#\"a:\"#)"
---
Module {
filename: "",
pkg: "",
doc: "",
name: "",
body: [],
body: [
Node {
node: Assign(
AssignStmt {
targets: [
Node {
node: Identifier {
names: [
Node {
node: "a",
filename: "",
line: 1,
column: 0,
end_line: 1,
end_column: 1,
},
],
pkgpath: "",
ctx: Store,
},
filename: "",
line: 1,
column: 0,
end_line: 1,
end_column: 1,
},
],
value: Node {
node: Missing(
MissingExpr,
),
filename: "",
line: 1,
column: 2,
end_line: 1,
end_column: 2,
},
type_annotation: Some(
Node {
node: "any",
filename: "",
line: 1,
column: 2,
end_line: 1,
end_column: 2,
},
),
ty: Some(
Node {
node: Any,
filename: "",
line: 1,
column: 2,
end_line: 1,
end_column: 2,
},
),
},
),
filename: "",
line: 1,
column: 0,
end_line: 1,
end_column: 1,
},
],
comments: [],
}

Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Module {
},
type_annotation: Some(
Node {
node: "function",
node: "()",
filename: "",
line: 1,
column: 3,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Module {
},
type_annotation: Some(
Node {
node: "function",
node: "()",
filename: "",
line: 1,
column: 3,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
---
source: parser/src/tests/error_recovery.rs
expression: "crate::tests::parsing_module_string(r#\"a:(\"#)"
---
Module {
filename: "",
pkg: "",
doc: "",
name: "",
body: [
Node {
node: Assign(
AssignStmt {
targets: [
Node {
node: Identifier {
names: [
Node {
node: "a",
filename: "",
line: 1,
column: 0,
end_line: 1,
end_column: 1,
},
],
pkgpath: "",
ctx: Store,
},
filename: "",
line: 1,
column: 0,
end_line: 1,
end_column: 1,
},
],
value: Node {
node: Missing(
MissingExpr,
),
filename: "",
line: 1,
column: 3,
end_line: 1,
end_column: 3,
},
type_annotation: Some(
Node {
node: "(any)",
filename: "",
line: 1,
column: 2,
end_line: 1,
end_column: 3,
},
),
ty: Some(
Node {
node: Function(
FunctionType {
params_ty: Some(
[
Node {
node: Any,
filename: "",
line: 1,
column: 3,
end_line: 1,
end_column: 3,
},
],
),
ret_ty: None,
},
),
filename: "",
line: 1,
column: 2,
end_line: 1,
end_column: 3,
},
),
},
),
filename: "",
line: 1,
column: 0,
end_line: 1,
end_column: 1,
},
],
comments: [],
}

Loading

0 comments on commit f9a11fe

Please sign in to comment.