@@ -17,6 +17,7 @@ pub trait ExpressionParser {
1717 fn parse_primary_expression ( & mut self ) -> Result < Expression , String > ;
1818 fn parse_expression_type ( & mut self ) -> Result < Type , String > ;
1919 fn is_lambda_parameter_list ( & self ) -> bool ;
20+ fn is_likely_generic_call ( & self ) -> bool ;
2021 fn peek_ahead ( & self , offset : usize ) -> Option < & String > ;
2122}
2223
@@ -220,6 +221,15 @@ impl<'a> ExpressionParser for ParserBase<'a> {
220221 self . consume ( ) ; // 消费 "throw"
221222 let exception_expr = self . parse_primary_expression ( ) ?;
222223 return Ok ( Expression :: Throw ( Box :: new ( exception_expr) ) ) ;
224+ } else if op == "-" {
225+ // 一元负号
226+ self . consume ( ) ; // 消费 "-"
227+ let expr = self . parse_unary_expression ( ) ?;
228+ return Ok ( Expression :: BinaryOp ( Box :: new ( Expression :: IntLiteral ( 0 ) ) , BinaryOperator :: Subtract , Box :: new ( expr) ) ) ;
229+ } else if op == "+" {
230+ // 一元正号(通常可以忽略)
231+ self . consume ( ) ; // 消费 "+"
232+ return self . parse_unary_expression ( ) ;
223233 }
224234 }
225235
@@ -567,7 +577,7 @@ impl<'a> ExpressionParser for ParserBase<'a> {
567577 return Ok ( Expression :: This ) ;
568578 }
569579
570- if self . peek ( ) == Some ( & "<" . to_string ( ) ) {
580+ if self . peek ( ) == Some ( & "<" . to_string ( ) ) && self . is_likely_generic_call ( ) {
571581 // 泛型函数调用
572582 return self . parse_generic_function_call ( name) ;
573583 } else if self . peek ( ) == Some ( & "(" . to_string ( ) ) {
@@ -1135,11 +1145,11 @@ impl<'a> ExpressionParser for ParserBase<'a> {
11351145 // 我们需要向前查看,找到匹配的右括号,然后检查是否有 "=>"
11361146 let mut depth = 0 ;
11371147 let mut pos = 0 ;
1138-
1148+
11391149 // 跳过当前的 "("
11401150 pos += 1 ;
11411151 depth += 1 ;
1142-
1152+
11431153 while let Some ( token) = self . peek_ahead ( pos) {
11441154 match token. as_str ( ) {
11451155 "(" => depth += 1 ,
@@ -1154,9 +1164,36 @@ impl<'a> ExpressionParser for ParserBase<'a> {
11541164 }
11551165 pos += 1 ;
11561166 }
1157-
1167+
11581168 false
11591169 }
1170+
1171+ fn is_likely_generic_call ( & self ) -> bool {
1172+ // 检查 < 后面是否像是泛型参数
1173+ // 如果 < 后面跟着类型名(如 int, string),则可能是泛型
1174+ // 如果 < 后面跟着数字、变量名或其他表达式,则可能是比较操作符
1175+ if let Some ( next_token) = self . peek_ahead ( 1 ) {
1176+ // 检查是否是明确的类型名
1177+ match next_token. as_str ( ) {
1178+ "int" | "float" | "bool" | "string" | "long" | "void" | "auto" => true ,
1179+ _ => {
1180+ // 对于其他标识符,我们需要更保守的判断
1181+ // 如果是数字,肯定是比较操作
1182+ if next_token. chars ( ) . next ( ) . unwrap_or ( '0' ) . is_ascii_digit ( ) {
1183+ return false ;
1184+ }
1185+
1186+ // 对于标识符,检查是否看起来像类型名
1187+ // 类型名通常以大写字母开头,但变量名和常量名也可能如此
1188+ // 为了安全起见,我们假设大多数情况下 < 是比较操作符
1189+ // 只有在明确的类型名情况下才认为是泛型
1190+ false
1191+ }
1192+ }
1193+ } else {
1194+ false
1195+ }
1196+ }
11601197
11611198 fn peek_ahead ( & self , offset : usize ) -> Option < & String > {
11621199 self . tokens . get ( self . position + offset)
0 commit comments