From bc09159ce8d9376131124cbbb9aed6c9125598cd Mon Sep 17 00:00:00 2001 From: Glyphack Date: Sat, 30 Sep 2023 17:50:47 +0200 Subject: [PATCH] Add single else statement to the ast --- parser/src/parser/parser.rs | 14 +- ...ser__tests__complete@input_program.py.snap | 645 +++++++++++++++++- 2 files changed, 648 insertions(+), 11 deletions(-) diff --git a/parser/src/parser/parser.rs b/parser/src/parser/parser.rs index 1b884702..0b1d7181 100644 --- a/parser/src/parser/parser.rs +++ b/parser/src/parser/parser.rs @@ -400,22 +400,26 @@ impl Parser { orelse = Some(if_value); } } - - if self.at(Kind::Else) { - self.bump(Kind::Else); + let mut single_else_body: Option> = None; + if self.eat(Kind::Else) { self.expect(Kind::Colon)?; let else_body = self.parse_suite()?; if let Some(val) = &mut orelse { val.update_orelse(else_body); + } else { + single_else_body = Some(else_body); } - } + }; // if we had any else or elif statements, we need to wrap them in a vec // otherwise we just return an empty vec as the else block of the if statement let or_else_vec = if let Some(val) = orelse { vec![Statement::IfStatement(val)] } else { - vec![] + match single_else_body { + Some(val) => val, + None => vec![], + } }; // There can be a dedent after the if block diff --git a/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__complete@input_program.py.snap b/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__complete@input_program.py.snap index 8936a8cf..3a5d77b1 100644 --- a/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__complete@input_program.py.snap +++ b/parser/src/parser/snapshots/enderpy_python_parser__parser__parser__tests__complete@input_program.py.snap @@ -2648,7 +2648,275 @@ Module { }, ), ], - orelse: [], + orelse: [ + AssignStatement( + Assign { + node: Node { + start: 2187, + end: 2230, + }, + targets: [ + Name( + Name { + node: Node { + start: 2187, + end: 2199, + }, + id: "ticker_index", + }, + ), + ], + value: Call( + Call { + node: Node { + start: 2202, + end: 2230, + }, + func: Name( + Name { + node: Node { + start: 2202, + end: 2222, + }, + id: "_handle_ticker_index", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 2223, + end: 2229, + }, + id: "symbol", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + }, + ), + IfStatement( + If { + node: Node { + start: 2247, + end: 2357, + }, + test: Compare( + Compare { + node: Node { + start: 2250, + end: 2270, + }, + left: Name( + Name { + node: Node { + start: 2250, + end: 2262, + }, + id: "ticker_index", + }, + ), + ops: [ + Is, + ], + comparators: [ + Constant( + Constant { + node: Node { + start: 2266, + end: 2270, + }, + value: None, + }, + ), + ], + }, + ), + body: [ + Raise( + Raise { + node: Node { + start: 2292, + end: 2340, + }, + exc: Some( + Call( + Call { + node: Node { + start: 2298, + end: 2340, + }, + func: Name( + Name { + node: Node { + start: 2298, + end: 2307, + }, + id: "Exception", + }, + ), + args: [ + JoinedStr( + JoinedStr { + node: Node { + start: 2308, + end: 2339, + }, + values: [ + Constant( + Constant { + node: Node { + start: 2330, + end: 0, + }, + value: Str( + "Cannot find symbol: ", + ), + }, + ), + Name( + Name { + node: Node { + start: 2331, + end: 2337, + }, + id: "symbol", + }, + ), + ], + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + ), + cause: None, + }, + ), + ], + orelse: [], + }, + ), + AssignStatement( + Assign { + node: Node { + start: 2357, + end: 2415, + }, + targets: [ + Name( + Name { + node: Node { + start: 2357, + end: 2371, + }, + id: "ticker_indexes", + }, + ), + ], + value: Call( + Call { + node: Node { + start: 2407, + end: 2415, + }, + func: Attribute( + Attribute { + node: Node { + start: 2374, + end: 2407, + }, + value: Name( + Name { + node: Node { + start: 2374, + end: 2386, + }, + id: "symbols_data", + }, + ), + attr: "get_ticker_old_index", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 2408, + end: 2414, + }, + id: "symbol", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + }, + ), + ExpressionStatement( + Call( + Call { + node: Node { + start: 2453, + end: 2470, + }, + func: Attribute( + Attribute { + node: Node { + start: 2432, + end: 2453, + }, + value: Name( + Name { + node: Node { + start: 2432, + end: 2446, + }, + id: "ticker_indexes", + }, + ), + attr: "insert", + }, + ), + args: [ + Constant( + Constant { + node: Node { + start: 2454, + end: 2455, + }, + value: Int( + "0", + ), + }, + ), + Name( + Name { + node: Node { + start: 2457, + end: 2469, + }, + id: "ticker_index", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + ), + ], }, ), ForStatement( @@ -3734,7 +4002,53 @@ Module { }, ), ], - orelse: [], + orelse: [ + AssignStatement( + Assign { + node: Node { + start: 3678, + end: 3698, + }, + targets: [ + Subscript( + Subscript { + node: Node { + start: 3678, + end: 3693, + }, + value: Name( + Name { + node: Node { + start: 3678, + end: 3685, + }, + id: "df_list", + }, + ), + slice: Name( + Name { + node: Node { + start: 3686, + end: 3692, + }, + id: "symbol", + }, + ), + }, + ), + ], + value: Name( + Name { + node: Node { + start: 3696, + end: 3698, + }, + id: "df", + }, + ), + }, + ), + ], }, ), IfStatement( @@ -4090,7 +4404,129 @@ Module { ), ), ], - orelse: [], + orelse: [ + ExpressionStatement( + Call( + Call { + node: Node { + start: 4109, + end: 4197, + }, + func: Attribute( + Attribute { + node: Node { + start: 4102, + end: 4109, + }, + value: Subscript( + Subscript { + node: Node { + start: 4087, + end: 4102, + }, + value: Name( + Name { + node: Node { + start: 4087, + end: 4094, + }, + id: "df_list", + }, + ), + slice: Name( + Name { + node: Node { + start: 4095, + end: 4101, + }, + id: "symbol", + }, + ), + }, + ), + attr: "to_csv", + }, + ), + args: [ + JoinedStr( + JoinedStr { + node: Node { + start: 4135, + end: 4162, + }, + values: [ + Name( + Name { + node: Node { + start: 4138, + end: 4147, + }, + id: "base_path", + }, + ), + Constant( + Constant { + node: Node { + start: 4149, + end: 0, + }, + value: Str( + "/", + ), + }, + ), + Name( + Name { + node: Node { + start: 4150, + end: 4156, + }, + id: "symbol", + }, + ), + Constant( + Constant { + node: Node { + start: 4161, + end: 0, + }, + value: Str( + ".csv", + ), + }, + ), + ], + }, + ), + ], + keywords: [ + Keyword { + node: Node { + start: 4164, + end: 4175, + }, + arg: Some( + "index", + ), + value: Constant( + Constant { + node: Node { + start: 4170, + end: 4175, + }, + value: Bool( + false, + ), + }, + ), + }, + ], + starargs: None, + kwargs: None, + }, + ), + ), + ], }, ), IfStatement( @@ -5343,7 +5779,73 @@ Module { }, ), ], - orelse: [], + orelse: [ + AssignStatement( + Assign { + node: Node { + start: 6174, + end: 6193, + }, + targets: [ + Name( + Name { + node: Node { + start: 6174, + end: 6179, + }, + id: "start", + }, + ), + ], + value: Subscript( + Subscript { + node: Node { + start: 6182, + end: 6193, + }, + value: Name( + Name { + node: Node { + start: 6182, + end: 6186, + }, + id: "diff", + }, + ), + slice: BinOp( + BinOp { + node: Node { + start: 6187, + end: 6192, + }, + op: Sub, + left: Name( + Name { + node: Node { + start: 6187, + end: 6188, + }, + id: "i", + }, + ), + right: Constant( + Constant { + node: Node { + start: 6191, + end: 6192, + }, + value: Int( + "1", + ), + }, + ), + }, + ), + }, + ), + }, + ), + ], }, ), AssignStatement( @@ -8256,7 +8758,67 @@ Module { }, ), ], - orelse: [], + orelse: [ + AssignStatement( + Assign { + node: Node { + start: 9159, + end: 9217, + }, + targets: [ + Name( + Name { + node: Node { + start: 9159, + end: 9174, + }, + id: "financial_index", + }, + ), + ], + value: Call( + Call { + node: Node { + start: 9209, + end: 9217, + }, + func: Attribute( + Attribute { + node: Node { + start: 9177, + end: 9209, + }, + value: Name( + Name { + node: Node { + start: 9177, + end: 9189, + }, + id: "symbols_data", + }, + ), + attr: "get_financial_index", + }, + ), + args: [ + Name( + Name { + node: Node { + start: 9210, + end: 9216, + }, + id: "symbol", + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + }, + ), + ], }, ), IfStatement( @@ -13693,7 +14255,78 @@ Module { }, ), ], - orelse: [], + orelse: [ + ExpressionStatement( + Call( + Call { + node: Node { + start: 15151, + end: 15172, + }, + func: Attribute( + Attribute { + node: Node { + start: 15127, + end: 15151, + }, + value: Attribute( + Attribute { + node: Node { + start: 15127, + end: 15144, + }, + value: Name( + Name { + node: Node { + start: 15127, + end: 15140, + }, + id: "market_symbol", + }, + ), + attr: "old", + }, + ), + attr: "append", + }, + ), + args: [ + Subscript( + Subscript { + node: Node { + start: 15152, + end: 15171, + }, + value: Name( + Name { + node: Node { + start: 15152, + end: 15168, + }, + id: "symbol_full_info", + }, + ), + slice: Constant( + Constant { + node: Node { + start: 15169, + end: 15170, + }, + value: Int( + "2", + ), + }, + ), + }, + ), + ], + keywords: [], + starargs: None, + kwargs: None, + }, + ), + ), + ], }, ), IfStatement(