Skip to content

Commit

Permalink
Handle comments in tree walking
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniusnaumann committed Aug 8, 2024
1 parent c993eae commit 7a7d753
Show file tree
Hide file tree
Showing 14 changed files with 194 additions and 160 deletions.
33 changes: 21 additions & 12 deletions galvan-into-ast/src/cursor_macro.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
#[macro_export]
macro_rules! cursor_expect {
($cursor:ident, $rule:literal) => {
{
use crate::result::CursorUtil;
let node = $cursor.curr()?;
if node.kind() != $rule {
let kind = node.kind();
$cursor.goto_parent();
let parent = $cursor.curr()?;
unreachable!("Expected {} keyword, got: {}, in: {}", $rule, kind, parent.kind());
} else {
node
($cursor:ident, $rule:literal) => {{
use crate::result::CursorUtil;
while $cursor.kind()? == "comment" && $rule != "comment" {
if !$cursor.goto_next_sibling() {
break;
}
}
};

let node = $cursor.curr()?;
if node.kind() != $rule {
let kind = node.kind();
$cursor.goto_parent();
let parent = $cursor.curr()?;
unreachable!(
"Expected {} keyword, got: {}, in: {}",
$rule,
kind,
parent.kind()
);
} else {
node
}
}};
}
30 changes: 15 additions & 15 deletions galvan-into-ast/src/items/expression/collection_literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ impl ReadCursor for CollectionLiteral {
fn read_cursor(cursor: &mut TreeCursor<'_>, source: &str) -> Result<Self, AstError> {
cursor_expect!(cursor, "collection_literal");

cursor.goto_first_child();
cursor.child();
let inner = match cursor.kind()? {
"array_literal" => ArrayLiteral::read_cursor(cursor, source)?.into(),
"set_literal" => SetLiteral::read_cursor(cursor, source)?.into(),
Expand All @@ -29,14 +29,14 @@ impl ReadCursor for ArrayLiteral {
let node = cursor_expect!(cursor, "array_literal");
let span = Span::from_node(node);

cursor.goto_first_child();
cursor.child();
cursor_expect!(cursor, "bracket_open");

cursor.goto_next_sibling();
cursor.next();
let mut elements = Vec::new();
while cursor.kind()? == "expression" {
elements.push(Expression::read_cursor(cursor, source)?);
cursor.goto_next_sibling();
cursor.next();
}

cursor_expect!(cursor, "bracket_close");
Expand All @@ -51,14 +51,14 @@ impl ReadCursor for SetLiteral {
let node = cursor_expect!(cursor, "set_literal");
let span = Span::from_node(node);

cursor.goto_first_child();
cursor.child();
cursor_expect!(cursor, "brace_open");

cursor.goto_next_sibling();
cursor.next();
let mut elements = Vec::new();
while cursor.kind()? == "expression" {
elements.push(Expression::read_cursor(cursor, source)?);
cursor.goto_next_sibling();
cursor.next();
}

cursor_expect!(cursor, "brace_close");
Expand All @@ -73,14 +73,14 @@ impl ReadCursor for OrderedDictLiteral {
let node = cursor_expect!(cursor, "ordered_dict_literal");
let span = Span::from_node(node);

cursor.goto_first_child();
cursor.child();
cursor_expect!(cursor, "bracket_open");

cursor.goto_next_sibling();
cursor.next();
let mut elements = Vec::new();
while cursor.kind()? == "dict_element" {
elements.push(DictLiteralElement::read_cursor(cursor, source)?);
cursor.goto_next_sibling();
cursor.next();
}

cursor_expect!(cursor, "bracket_open");
Expand All @@ -96,14 +96,14 @@ impl ReadCursor for DictLiteral {
let node = cursor_expect!(cursor, "dict_literal");
let span = Span::from_node(node);

cursor.goto_first_child();
cursor.child();
cursor_expect!(cursor, "brace_open");

cursor.goto_next_sibling();
cursor.next();
let mut elements = Vec::new();
while cursor.kind()? == "dict_element" {
elements.push(DictLiteralElement::read_cursor(cursor, source)?);
cursor.goto_next_sibling();
cursor.next();
}

cursor_expect!(cursor, "brace_close");
Expand All @@ -118,10 +118,10 @@ impl ReadCursor for DictLiteralElement {
let node = cursor_expect!(cursor, "dict_element");
let span = Span::from_node(node);

cursor.goto_first_child();
cursor.child();
let key = Expression::read_cursor(cursor, source)?;

cursor.goto_next_sibling();
cursor.next();
let value = Expression::read_cursor(cursor, source)?;

Ok(DictLiteralElement { key, value, span })
Expand Down
54 changes: 27 additions & 27 deletions galvan-into-ast/src/items/expression/function_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ impl ReadCursor for FunctionCall {
let node = cursor_expect!(cursor, "function_call");
let span = Span::from_node(node);

cursor.goto_first_child();
cursor.child();
let identifier = Ident::read_cursor(cursor, source)?;

cursor.goto_next_sibling();
cursor.next();
cursor_expect!(cursor, "paren_open");

cursor.goto_next_sibling();
cursor.next();
let arguments = read_arguments(cursor, source)?;
cursor.goto_parent();

Expand All @@ -36,20 +36,20 @@ pub fn read_trailing_closure_call(
let node = cursor_expect!(cursor, "trailing_closure_expression");
let span = Span::from_node(node);

cursor.goto_first_child();
cursor.child();
let identifier = Ident::read_cursor(cursor, source)?;

cursor.goto_next_sibling();
cursor.next();
let mut arguments = read_arguments(cursor, source)?;

let mut closure_arguments = Vec::new();
if cursor.kind()? == "pipe" {
cursor.goto_next_sibling();
cursor.next();
while cursor.kind()? != "pipe" {
closure_arguments.push(ClosureArgument::read_cursor(cursor, source)?);
cursor.goto_next_sibling();
cursor.next();
}
cursor.goto_next_sibling();
cursor.next();
}

let body = Body::read_cursor(cursor, source)?;
Expand Down Expand Up @@ -87,9 +87,9 @@ pub fn read_free_function_call(
let node = cursor_expect!(cursor, "free_function");
let span = Span::from_node(node);

cursor.goto_first_child();
cursor.child();
let identifier = Ident::read_cursor(cursor, source)?;
cursor.goto_next_sibling();
cursor.next();
let arguments = read_arguments(cursor, source)?;
cursor.goto_parent();

Expand All @@ -109,7 +109,7 @@ fn read_arguments(

while cursor.kind()? == "function_call_arg" {
args.push(FunctionCallArg::read_cursor(cursor, source)?);
if !cursor.goto_next_sibling() {
if !cursor.next() {
break;
}
}
Expand All @@ -122,10 +122,10 @@ impl ReadCursor for FunctionCallArg {
let node = cursor_expect!(cursor, "function_call_arg");
let span = Span::from_node(node);

cursor.goto_first_child();
cursor.child();
let modifier = if cursor.kind()? == "declaration_modifier" {
let decl_mod = Some(DeclModifier::read_cursor(cursor, source)?);
cursor.goto_next_sibling();
cursor.next();

decl_mod
} else {
Expand All @@ -149,17 +149,17 @@ impl ReadCursor for Closure {
let node = cursor_expect!(cursor, "closure");
let span = Span::from_node(node);

cursor.goto_first_child();
cursor.child();
cursor_expect!(cursor, "pipe");
cursor.goto_next_sibling();
cursor.next();
let mut arguments = Vec::new();
while cursor.kind()? == "closure_argument" {
arguments.push(ClosureArgument::read_cursor(cursor, source)?);
cursor.goto_next_sibling();
cursor.next();
}

cursor_expect!(cursor, "pipe");
cursor.goto_next_sibling();
cursor.next();

let block = if cursor.kind()? == "expression" {
let expression = Expression::read_cursor(cursor, source)?;
Expand Down Expand Up @@ -196,12 +196,12 @@ impl ReadCursor for ClosureArgument {
let node = cursor_expect!(cursor, "closure_argument");
let span = Span::from_node(node);

cursor.goto_first_child();
cursor.child();
let ident = Ident::read_cursor(cursor, source)?;
cursor.goto_next_sibling();
cursor.next();

let ty = if cursor.kind()? == "colon" {
cursor.goto_next_sibling();
cursor.next();
Some(TypeElement::read_cursor(cursor, source)?)
} else {
None
Expand All @@ -217,18 +217,18 @@ impl ReadCursor for ConstructorCall {
let node = cursor_expect!(cursor, "constructor_call");
let span = Span::from_node(node);

cursor.goto_first_child();
cursor.child();
let identifier = TypeIdent::read_cursor(cursor, source)?;

cursor.goto_next_sibling();
cursor.next();
cursor_expect!(cursor, "paren_open");

let mut arguments = vec![];
cursor.goto_next_sibling();
cursor.next();
while cursor.kind()? != "paren_close" {
let arg = ConstructorCallArg::read_cursor(cursor, source)?;
arguments.push(arg);
cursor.goto_next_sibling();
cursor.next();
}

cursor.goto_parent();
Expand All @@ -247,13 +247,13 @@ impl ReadCursor for ConstructorCallArg {
let node = cursor_expect!(cursor, "constructor_call_arg");
let span = Span::from_node(node);

cursor.goto_first_child();
cursor.child();
let ident = Ident::read_cursor(cursor, source)?;

cursor.goto_next_sibling();
cursor.next();
cursor_expect!(cursor, "colon");

cursor.goto_next_sibling();
cursor.next();
let expression = Expression::read_cursor(cursor, source)?;

cursor.goto_parent();
Expand Down
6 changes: 3 additions & 3 deletions galvan-into-ast/src/items/expression/infix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ impl <T> ReadCursor for InfixOperation<T> where T: InfixOperator + ReadCursor {
let node = cursor.curr()?;
let span = Span::from_node(node);

cursor.goto_first_child();
cursor.child();
let lhs = Expression::read_cursor(cursor, source)?;
cursor.goto_next_sibling();
cursor.next();
let operator = T::read_cursor(cursor, source)?;
cursor.goto_next_sibling();
cursor.next();
let rhs = Expression::read_cursor(cursor, source)?;
cursor.goto_parent();

Expand Down
18 changes: 9 additions & 9 deletions galvan-into-ast/src/items/expression/postfix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ impl ReadCursor for PostfixExpression {
let node = cursor_expect!(cursor, "postfix_expression");
let span = Span::from_node(node);

cursor.goto_first_child();
cursor.child();
let inner = Expression::read_cursor(cursor, source)?;

cursor.goto_next_sibling();
cursor.next();
cursor_expect!(cursor, "postfix_operator");

cursor.goto_first_child();
cursor.child();
let res = match cursor.kind()? {
"yeet_operator" => YeetExpression { inner, span }.into(),
"access_operator" => {
cursor.goto_first_child();
cursor.child();
cursor_expect!(cursor, "bracket_open");

cursor.goto_next_sibling();
cursor.next();
let index = Expression::read_cursor(cursor, source)?;

cursor.goto_next_sibling();
cursor.next();
cursor_expect!(cursor, "bracket_close");

cursor.goto_parent();
Expand All @@ -51,13 +51,13 @@ impl ReadCursor for ElseExpression {
let node = cursor_expect!(cursor, "else_expression");
let span = Span::from_node(node);

cursor.goto_first_child();
cursor.child();
let receiver = Expression::read_cursor(cursor, source)?.into();

cursor.goto_next_sibling();
cursor.next();
cursor_expect!(cursor, "else_keyword");

cursor.goto_next_sibling();
cursor.next();
let body = Body::read_cursor(cursor, source)?;
let body_span = body.span;
let block = Block { body, span: body_span };
Expand Down
4 changes: 2 additions & 2 deletions galvan-into-ast/src/items/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ impl ReadCursor for Literal {
let node = cursor_expect!(cursor, "literal");
let span = Span::from_node(node);

cursor.goto_first_child();
cursor.child();
let inner = match cursor.kind()? {
"none_keyword" => Literal::NoneLiteral(NoneLiteral(span)),
"boolean_literal" => BooleanLiteral::read_cursor(cursor, source)?.into(),
Expand All @@ -29,7 +29,7 @@ impl ReadCursor for BooleanLiteral {
let node = cursor_expect!(cursor, "boolean_literal");
let span = Span::from_node(node);

cursor.goto_first_child();
cursor.child();
let lit = match cursor.kind()? {
"true_keyword" => BooleanLiteral { value: true, span },
"false_keyword" => BooleanLiteral { value: false, span },
Expand Down
Loading

0 comments on commit 7a7d753

Please sign in to comment.