Skip to content

Commit

Permalink
echo parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
giacomocavalieri committed Oct 7, 2024
1 parent c8fe1c2 commit 9e947f5
Show file tree
Hide file tree
Showing 11 changed files with 355 additions and 10 deletions.
21 changes: 11 additions & 10 deletions compiler-core/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,15 +465,6 @@ where
Ok(())
}

fn parse_expression_unit_collapsing_single_value_blocks(
&mut self,
) -> Result<Option<UntypedExpr>, ParseError> {
match self.parse_expression_unit()? {
Some(expression) => Ok(Some(expression)),
None => Ok(None),
}
}

// examples:
// 1
// "one"
Expand Down Expand Up @@ -545,6 +536,16 @@ where
}
}

Some((start, Token::Echo, end)) => {
self.advance();
let expression = self.parse_expression_unit()?;
let end = expression.as_ref().map_or(end, |e| e.location().end);
UntypedExpr::Echo {
location: SrcSpan { start, end },
expression: expression.map(Box::new),
}
}

Some((start, Token::Hash, _)) => {
self.advance();
let _ = self.expect_one(&Token::LeftParen)?;
Expand Down Expand Up @@ -646,7 +647,7 @@ where
&|s| {
Parser::parse_bit_array_segment(
s,
&Parser::parse_expression_unit_collapsing_single_value_blocks,
&Parser::parse_expression_unit,
&Parser::expect_expression,
&bit_array_expr_int,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
source: compiler-core/src/parse/tests.rs
expression: echo wibble
---
[
Expression(
Echo {
location: SrcSpan {
start: 0,
end: 11,
},
expression: Some(
Var {
location: SrcSpan {
start: 5,
end: 11,
},
name: "wibble",
},
),
},
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
source: compiler-core/src/parse/tests.rs
expression: echo 1 + 1
---
[
Expression(
BinOp {
location: SrcSpan {
start: 0,
end: 10,
},
name: AddInt,
left: Echo {
location: SrcSpan {
start: 0,
end: 6,
},
expression: Some(
Int {
location: SrcSpan {
start: 5,
end: 6,
},
value: "1",
},
),
},
right: Int {
location: SrcSpan {
start: 9,
end: 10,
},
value: "1",
},
},
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
source: compiler-core/src/parse/tests.rs
expression: "[] |> echo |> wibble"
---
[
Expression(
PipeLine {
expressions: [
List {
location: SrcSpan {
start: 0,
end: 2,
},
elements: [],
tail: None,
},
Echo {
location: SrcSpan {
start: 6,
end: 10,
},
expression: None,
},
Var {
location: SrcSpan {
start: 14,
end: 20,
},
name: "wibble",
},
],
},
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
source: compiler-core/src/parse/tests.rs
expression: "[] |> echo fun |> wibble"
---
[
Expression(
PipeLine {
expressions: [
List {
location: SrcSpan {
start: 0,
end: 2,
},
elements: [],
tail: None,
},
Echo {
location: SrcSpan {
start: 6,
end: 14,
},
expression: Some(
Var {
location: SrcSpan {
start: 11,
end: 14,
},
name: "fun",
},
),
},
Var {
location: SrcSpan {
start: 18,
end: 24,
},
name: "wibble",
},
],
},
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
source: compiler-core/src/parse/tests.rs
expression: "echo { 1 + 1 }"
---
[
Expression(
Echo {
location: SrcSpan {
start: 0,
end: 14,
},
expression: Some(
Block {
location: SrcSpan {
start: 5,
end: 14,
},
statements: [
Expression(
BinOp {
location: SrcSpan {
start: 7,
end: 12,
},
name: AddInt,
left: Int {
location: SrcSpan {
start: 7,
end: 8,
},
value: "1",
},
right: Int {
location: SrcSpan {
start: 11,
end: 12,
},
value: "1",
},
},
),
],
},
),
},
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
source: compiler-core/src/parse/tests.rs
expression: echo
---
[
Expression(
Echo {
location: SrcSpan {
start: 0,
end: 4,
},
expression: None,
},
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
source: compiler-core/src/parse/tests.rs
expression: "echo panic as \"a\""
---
[
Expression(
Echo {
location: SrcSpan {
start: 0,
end: 17,
},
expression: Some(
Panic {
location: SrcSpan {
start: 5,
end: 17,
},
message: Some(
String {
location: SrcSpan {
start: 14,
end: 17,
},
value: "a",
},
),
},
),
},
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
source: compiler-core/src/parse/tests.rs
expression: "panic as echo \"string\""
---
[
Expression(
Panic {
location: SrcSpan {
start: 0,
end: 22,
},
message: Some(
Echo {
location: SrcSpan {
start: 9,
end: 22,
},
expression: Some(
String {
location: SrcSpan {
start: 14,
end: 22,
},
value: "string",
},
),
},
),
},
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
source: compiler-core/src/parse/tests.rs
expression: echo echo echo 1
---
[
Expression(
Echo {
location: SrcSpan {
start: 0,
end: 16,
},
expression: Some(
Echo {
location: SrcSpan {
start: 5,
end: 16,
},
expression: Some(
Echo {
location: SrcSpan {
start: 10,
end: 16,
},
expression: Some(
Int {
location: SrcSpan {
start: 15,
end: 16,
},
value: "1",
},
),
},
),
},
),
},
),
]
Loading

0 comments on commit 9e947f5

Please sign in to comment.