Skip to content

Commit

Permalink
fix: joined string format spec parse (#699)
Browse files Browse the repository at this point in the history
  • Loading branch information
Peefy authored Sep 5, 2023
1 parent 5323b36 commit 67ee863
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 4 deletions.
7 changes: 3 additions & 4 deletions kclvm/parser/src/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![allow(unused_macros)]

use compiler_base_span::{span::new_byte_pos, BytePos, Span};
use kclvm_ast::token::{DelimToken, LitKind, Token, TokenKind};
use kclvm_ast::token::{CommentKind, DelimToken, LitKind, Token, TokenKind};
use kclvm_ast::{ast::*, expr_as, node_ref};
use kclvm_span::symbol::kw;

Expand Down Expand Up @@ -1506,9 +1506,8 @@ impl<'a> Parser<'a> {
if let TokenKind::Colon = parser.token.kind {
// bump the format spec interval token `:`.
parser.bump();
if let TokenKind::DocComment(_) = parser.token.kind {
let format_spec = parser.sess.span_to_snippet(parser.token.span);
formatted_value.format_spec = Some(format_spec);
if let TokenKind::DocComment(CommentKind::Line(symbol)) = parser.token.kind {
formatted_value.format_spec = Some(symbol.as_str());
} else {
this.sess.struct_span_error(
"invalid joined string spec without #",
Expand Down
2 changes: 2 additions & 0 deletions kclvm/parser/src/tests/error_recovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ parse_expr_snapshot! { joined_string_recovery_2, r#"'${(a +}'"# }
parse_expr_snapshot! { joined_string_recovery_3, r#"'${a'"# }
parse_expr_snapshot! { joined_string_recovery_5, r#"'${a + 1 = }'"# }
parse_expr_snapshot! { joined_string_recovery_6, r#"'${a: json}'"# }
parse_expr_snapshot! { joined_string_recovery_7, r#"'\n${a: #json}'"# }
parse_expr_snapshot! { joined_string_recovery_8, r#"'a\nb${a: #json}\n'"# }
parse_expr_snapshot! { lambda_recovery_0, r#"lambda"# }
parse_expr_snapshot! { lambda_recovery_1, r#"lambda {"# }
parse_expr_snapshot! { lambda_recovery_2, r#"lambda {}"# }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
---
source: parser/src/tests/error_recovery.rs
assertion_line: 158
expression: "crate::tests::parsing_expr_string(r#\"'\\n${a: #json}'\"#)"
---
Node {
node: JoinedString(
JoinedString {
is_long_string: false,
values: [
Node {
node: StringLit(
StringLit {
is_long_string: false,
raw_value: "\n",
value: "\n",
},
),
filename: "",
line: 1,
column: 1,
end_line: 1,
end_column: 1,
},
Node {
node: FormattedValue(
FormattedValue {
is_long_string: false,
value: Node {
node: Identifier(
Identifier {
names: [
Node {
node: "a",
filename: "",
line: 1,
column: 4,
end_line: 1,
end_column: 5,
},
],
pkgpath: "",
ctx: Load,
},
),
filename: "",
line: 1,
column: 4,
end_line: 1,
end_column: 5,
},
format_spec: Some(
"#json",
),
},
),
filename: "",
line: 1,
column: 4,
end_line: 1,
end_column: 12,
},
],
raw_value: "'\\n${a: #json}'",
},
),
filename: "",
line: 1,
column: 0,
end_line: 1,
end_column: 15,
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
---
source: parser/src/tests/error_recovery.rs
assertion_line: 159
expression: "crate::tests::parsing_expr_string(r#\"'a\\nb${a: #json}\\n'\"#)"
---
Node {
node: JoinedString(
JoinedString {
is_long_string: false,
values: [
Node {
node: StringLit(
StringLit {
is_long_string: false,
raw_value: "a\nb",
value: "a\nb",
},
),
filename: "",
line: 1,
column: 1,
end_line: 1,
end_column: 1,
},
Node {
node: FormattedValue(
FormattedValue {
is_long_string: false,
value: Node {
node: Identifier(
Identifier {
names: [
Node {
node: "a",
filename: "",
line: 1,
column: 6,
end_line: 1,
end_column: 7,
},
],
pkgpath: "",
ctx: Load,
},
),
filename: "",
line: 1,
column: 6,
end_line: 1,
end_column: 7,
},
format_spec: Some(
"#json",
),
},
),
filename: "",
line: 1,
column: 6,
end_line: 1,
end_column: 14,
},
Node {
node: StringLit(
StringLit {
is_long_string: false,
raw_value: "\n",
value: "\n",
},
),
filename: "",
line: 1,
column: 1,
end_line: 1,
end_column: 1,
},
],
raw_value: "'a\\nb${a: #json}\\n'",
},
),
filename: "",
line: 1,
column: 0,
end_line: 1,
end_column: 19,
}

0 comments on commit 67ee863

Please sign in to comment.