MCPcopy Create free account
hub / github.com/ControlCplusControlV/Scribe / parse_statement

Function parse_statement

crates/papyrus/src/parser.rs:54–195  ·  view source on GitHub ↗

Parses a Yul statement. This function matches a grammar rule and return an Expr struct which is later added into the Abstract Syntax Tree

(expression: Pair<Rule>)

Source from the content-addressed store, hash-verified

52//Parses a Yul statement. This function matches a grammar rule and return an Expr struct
53//which is later added into the Abstract Syntax Tree
54fn parse_statement(expression: Pair<Rule>) -> Expr {
55 let inner = expression.into_inner().next().unwrap();
56 match inner.as_rule() {
57 //Rule is expr
58 Rule::expr => parse_expression(inner),
59
60 //Rule is block
61 Rule::block => Expr::Block(parse_block(inner)),
62
63 // Rule is code
64 Rule::code => Expr::Block(parse_block(inner.into_inner().next().unwrap())),
65
66 //If the rule is a function definition, parse the function name, parameters, returns and then return an Expr
67 Rule::function_definition => {
68 let mut parts = inner.into_inner();
69 let function_name = parts.next().unwrap().as_str();
70
71 //get the typed identifiers from the function and parse each expression
72 let params: Vec<TypedIdentifier> = parse_typed_identifier_list(parts.next().unwrap());
73 let returns_rule = parts.next().unwrap();
74 let mut returns = vec![];
75 if let Some(inner) = returns_rule.into_inner().next() {
76 returns = parse_typed_identifier_list(inner);
77 }
78
79 let block = parts.next().unwrap();
80
81 Expr::FunctionDefinition(ExprFunctionDefinition {
82 function_name: function_name.to_string(),
83 params,
84 returns,
85 block: parse_block(block),
86 })
87 }
88
89 //Rule is assignment
90 Rule::assignment => {
91 let mut parts = inner.into_inner();
92 let identifiers = parse_identifier_list(parts.next().unwrap());
93 let rhs = parts.next().unwrap();
94 let rhs_expr = parse_expression(rhs);
95 Expr::Assignment(ExprAssignment {
96 identifiers,
97 inferred_types: vec![],
98 rhs: Box::new(rhs_expr),
99 })
100 }
101
102 //Rule is if statement
103 Rule::if_statement => {
104 let mut inners = inner.into_inner();
105 let first_arg = inners.next().unwrap();
106 let second_arg = inners.next().unwrap();
107 Expr::IfStatement(ExprIfStatement {
108 first_expr: Box::new(parse_expression(first_arg)),
109 second_expr: Box::new(parse_block(second_arg)),
110 })
111 }

Callers 2

parse_yul_syntaxFunction · 0.85
parse_blockFunction · 0.85

Calls 7

parse_expressionFunction · 0.85
parse_blockFunction · 0.85
parse_identifier_listFunction · 0.85
parse_caseFunction · 0.85
parse_literalFunction · 0.85
pushMethod · 0.80

Tested by

no test coverage detected