diff --git a/galvan-into-ast/src/cursor_macro.rs b/galvan-into-ast/src/cursor_macro.rs index 60e0b14..d9c6194 100644 --- a/galvan-into-ast/src/cursor_macro.rs +++ b/galvan-into-ast/src/cursor_macro.rs @@ -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 + } + }}; } diff --git a/galvan-into-ast/src/items/expression/collection_literal.rs b/galvan-into-ast/src/items/expression/collection_literal.rs index 370366d..d494a5d 100644 --- a/galvan-into-ast/src/items/expression/collection_literal.rs +++ b/galvan-into-ast/src/items/expression/collection_literal.rs @@ -10,7 +10,7 @@ impl ReadCursor for CollectionLiteral { fn read_cursor(cursor: &mut TreeCursor<'_>, source: &str) -> Result { 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(), @@ -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"); @@ -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"); @@ -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"); @@ -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"); @@ -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 }) diff --git a/galvan-into-ast/src/items/expression/function_call.rs b/galvan-into-ast/src/items/expression/function_call.rs index e635922..dd6a123 100644 --- a/galvan-into-ast/src/items/expression/function_call.rs +++ b/galvan-into-ast/src/items/expression/function_call.rs @@ -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(); @@ -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)?; @@ -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(); @@ -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; } } @@ -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 { @@ -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)?; @@ -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 @@ -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(); @@ -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(); diff --git a/galvan-into-ast/src/items/expression/infix.rs b/galvan-into-ast/src/items/expression/infix.rs index 6f2674e..a64e3d2 100644 --- a/galvan-into-ast/src/items/expression/infix.rs +++ b/galvan-into-ast/src/items/expression/infix.rs @@ -44,11 +44,11 @@ impl ReadCursor for InfixOperation 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(); diff --git a/galvan-into-ast/src/items/expression/postfix.rs b/galvan-into-ast/src/items/expression/postfix.rs index 6f00e04..be2e8e2 100644 --- a/galvan-into-ast/src/items/expression/postfix.rs +++ b/galvan-into-ast/src/items/expression/postfix.rs @@ -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(); @@ -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 }; diff --git a/galvan-into-ast/src/items/literal.rs b/galvan-into-ast/src/items/literal.rs index 9c291fa..50fae94 100644 --- a/galvan-into-ast/src/items/literal.rs +++ b/galvan-into-ast/src/items/literal.rs @@ -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(), @@ -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 }, diff --git a/galvan-into-ast/src/items/statement.rs b/galvan-into-ast/src/items/statement.rs index 3de91ed..2ecf1de 100644 --- a/galvan-into-ast/src/items/statement.rs +++ b/galvan-into-ast/src/items/statement.rs @@ -12,7 +12,7 @@ impl ReadCursor for Statement { fn read_cursor(cursor: &mut TreeCursor<'_>, source: &str) -> Result { cursor_expect!(cursor, "statement"); - cursor.goto_first_child(); + cursor.child(); let inner = match cursor.kind()? { "assignment" => Statement::Assignment(Assignment::read_cursor(cursor, source)?), "declaration" => Statement::Declaration(Declaration::read_cursor(cursor, source)?), @@ -33,18 +33,18 @@ impl ReadCursor for Declaration { let node = cursor_expect!(cursor, "declaration"); let span = Span::from_node(node); - cursor.goto_first_child(); + cursor.child(); let decl_modifier = DeclModifier::read_cursor(cursor, source)?; - cursor.goto_next_sibling(); + cursor.next(); let identifier = Ident::read_cursor(cursor, source)?; - cursor.goto_next_sibling(); + cursor.next(); let type_annotation = match cursor.kind()? { "colon" => { - cursor.goto_next_sibling(); + cursor.next(); let elem = Some(TypeElement::read_cursor(cursor, source)?); - cursor.goto_next_sibling(); + cursor.next(); elem } _ => None, @@ -52,9 +52,9 @@ impl ReadCursor for Declaration { let assignment = match cursor.kind()? { "assign" => { - cursor.goto_next_sibling(); + cursor.next(); let expr = Some(Expression::read_cursor(cursor, source)?); - cursor.goto_next_sibling(); + cursor.next(); expr } _ => None, @@ -79,13 +79,13 @@ impl ReadCursor for Assignment { let node = cursor_expect!(cursor, "assignment"); 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 = AssignmentOperator::read_cursor(cursor, source)?; - cursor.goto_next_sibling(); + cursor.next(); let rhs = Expression::read_cursor(cursor, source)?; cursor.goto_parent(); @@ -119,7 +119,7 @@ impl ReadCursor for Expression { fn read_cursor(cursor: &mut TreeCursor<'_>, source: &str) -> Result { cursor_expect!(cursor, "expression"); - cursor.goto_first_child(); + cursor.child(); let inner: Expression = match cursor.kind()? { "else_expression" => ElseExpression::read_cursor(cursor, source)?.into(), diff --git a/galvan-into-ast/src/items/struct_declaration.rs b/galvan-into-ast/src/items/struct_declaration.rs index 2945827..c9a60b8 100644 --- a/galvan-into-ast/src/items/struct_declaration.rs +++ b/galvan-into-ast/src/items/struct_declaration.rs @@ -11,24 +11,24 @@ impl ReadCursor for StructTypeDecl { let struct_decl = cursor_expect!(cursor, "struct"); let span = Span::from_node(struct_decl); - cursor.goto_first_child(); + cursor.child(); let visibility = Visibility::read_cursor(cursor, source)?; cursor_expect!(cursor, "type_keyword"); - cursor.goto_next_sibling(); + cursor.next(); let ident = TypeIdent::read_cursor(cursor, source)?; - cursor.goto_next_sibling(); + cursor.next(); cursor_expect!(cursor, "brace_open"); - cursor.goto_next_sibling(); + cursor.next(); let mut members = vec![]; while cursor.kind()? == "struct_field" { let field = StructTypeMember::read_cursor(cursor, source)?; members.push(field); - cursor.goto_next_sibling(); + cursor.next(); } cursor_expect!(cursor, "brace_close"); @@ -49,12 +49,12 @@ impl ReadCursor for StructTypeMember { let struct_field = cursor_expect!(cursor, "struct_field"); let span = Span::from_node(struct_field); - cursor.goto_first_child(); + cursor.child(); let visibility = Visibility::read_cursor(cursor, source)?; let decl_modifier = if cursor.kind()? == "declaration_modifier" { let modifier = Some(DeclModifier::read_cursor(cursor, source)?); - cursor.goto_next_sibling(); + cursor.next(); modifier } else { None @@ -62,10 +62,10 @@ impl ReadCursor for StructTypeMember { let ident = Ident::read_cursor(cursor, source)?; - cursor.goto_next_sibling(); + cursor.next(); cursor_expect!(cursor, "colon"); - cursor.goto_next_sibling(); + cursor.next(); let r#type = TypeElement::read_cursor(cursor, source)?; cursor.goto_parent(); @@ -84,24 +84,24 @@ impl ReadCursor for TupleTypeDecl { let node = cursor_expect!(cursor, "tuple_struct"); let span = Span::from_node(node); - cursor.goto_first_child(); + cursor.child(); let visibility = Visibility::read_cursor(cursor, source)?; cursor_expect!(cursor, "type_keyword"); - cursor.goto_next_sibling(); + cursor.next(); let ident = TypeIdent::read_cursor(cursor, source)?; - cursor.goto_next_sibling(); + cursor.next(); cursor_expect!(cursor, "paren_open"); - cursor.goto_next_sibling(); + cursor.next(); let mut members = vec![]; while cursor.kind()? == "tuple_field" { let field = TupleTypeMember::read_cursor(cursor, source)?; members.push(field); - cursor.goto_next_sibling(); + cursor.next(); } cursor_expect!(cursor, "paren_close"); @@ -122,20 +122,20 @@ impl ReadCursor for TupleTypeMember { let node = cursor_expect!(cursor, "tuple_field"); let span = Span::from_node(node); - cursor.goto_first_child(); + cursor.child(); // TODO: Implement field visibility and declaration modifier let visibility = Visibility::read_cursor(cursor, source)?; let decl_modifier = if cursor.kind()? == "declaration_modifier" { let modifier = Some(DeclModifier::read_cursor(cursor, source)?); - cursor.goto_next_sibling(); + cursor.next(); modifier } else { None }; - cursor.goto_next_sibling(); + cursor.next(); let r#type = TypeElement::read_cursor(cursor, source)?; cursor.goto_parent(); @@ -153,19 +153,19 @@ impl ReadCursor for AliasTypeDecl { let alias = cursor_expect!(cursor, "alias"); let span = Span::from_node(alias); - cursor.goto_first_child(); + cursor.child(); let visibility = Visibility::read_cursor(cursor, source)?; cursor_expect!(cursor, "type_keyword"); - cursor.goto_next_sibling(); + cursor.next(); let ident = TypeIdent::read_cursor(cursor, source)?; - cursor.goto_next_sibling(); + cursor.next(); cursor_expect!(cursor, "assign"); - cursor.goto_next_sibling(); + cursor.next(); let r#type = TypeElement::read_cursor(cursor, source)?; cursor.goto_parent(); @@ -183,10 +183,10 @@ impl ReadCursor for EmptyTypeDecl { let empty_type = cursor_expect!(cursor, "empty_struct"); let span = Span::from_node(empty_type); - cursor.goto_first_child(); + cursor.child(); let visibility = Visibility::read_cursor(cursor, source)?; - cursor.goto_next_sibling(); + cursor.next(); let ident = TypeIdent::read_cursor(cursor, source)?; cursor.goto_parent(); diff --git a/galvan-into-ast/src/items/toplevel.rs b/galvan-into-ast/src/items/toplevel.rs index 883b441..b8dee51 100644 --- a/galvan-into-ast/src/items/toplevel.rs +++ b/galvan-into-ast/src/items/toplevel.rs @@ -25,13 +25,13 @@ impl ReadCursor for MainDecl { fn read_cursor(cursor: &mut TreeCursor<'_>, source: &str) -> Result { let main = cursor_expect!(cursor, "main"); let span = Span::from_node(main); - cursor.goto_first_child(); + cursor.child(); cursor_expect!(cursor, "main_keyword"); - cursor.goto_next_sibling(); + cursor.next(); let body = Body::read_cursor(cursor, source)?; - assert!(!cursor.goto_next_sibling(), "Unexpected token in main"); + assert!(!cursor.next(), "Unexpected token in main"); cursor.goto_parent(); @@ -43,19 +43,19 @@ impl ReadCursor for TestDecl { fn read_cursor(cursor: &mut TreeCursor<'_>, source: &str) -> Result { let test = cursor_expect!(cursor, "test"); let span = Span::from_node(test); - cursor.goto_first_child(); + cursor.child(); cursor_expect!(cursor, "test_keyword"); - cursor.goto_next_sibling(); + cursor.next(); let name = if cursor.kind()? == "string_literal" { let lit = Some(StringLiteral::read_cursor(cursor, source)?); - cursor.goto_next_sibling(); + cursor.next(); lit } else { None }; - cursor.goto_next_sibling(); + cursor.next(); let body = Body::read_cursor(cursor, source)?; cursor.goto_parent(); @@ -67,7 +67,7 @@ impl ReadCursor for TestDecl { impl ReadCursor for TypeDecl { fn read_cursor(cursor: &mut TreeCursor<'_>, source: &str) -> Result { let ty = cursor_expect!(cursor, "type_declaration"); - cursor.goto_first_child(); + cursor.child(); // TODO Parse type declaration variatns let decl = match cursor.kind()? { @@ -89,11 +89,11 @@ impl ReadCursor for FnDecl { fn read_cursor(cursor: &mut TreeCursor<'_>, source: &str) -> Result { let function = cursor_expect!(cursor, "function"); let span = Span::from_node(function); - cursor.goto_first_child(); + cursor.child(); let signature = FnSignature::read_cursor(cursor, source)?; - cursor.goto_next_sibling(); + cursor.next(); let body = Body::read_cursor(cursor, source)?; cursor.goto_parent(); @@ -110,16 +110,16 @@ impl ReadCursor for Body { fn read_cursor(cursor: &mut TreeCursor<'_>, source: &str) -> Result { let body = cursor_expect!(cursor, "body"); let span = Span::from_node(body); - cursor.goto_first_child(); + cursor.child(); cursor_expect!(cursor, "brace_open"); - cursor.goto_next_sibling(); + cursor.next(); let mut statements = vec![]; while cursor.kind()? == "statement" { let stmt = Statement::read_cursor(cursor, source)?; statements.push(stmt); - cursor.goto_next_sibling(); + cursor.next(); } cursor_expect!(cursor, "brace_close"); @@ -133,23 +133,23 @@ impl ReadCursor for FnSignature { fn read_cursor(cursor: &mut TreeCursor<'_>, source: &str) -> Result { let signature = cursor_expect!(cursor, "fn_signature"); let span = Span::from_node(signature); - cursor.goto_first_child(); + cursor.child(); let visibility = Visibility::read_cursor(cursor, source)?; cursor_expect!(cursor, "fn_keyword"); - cursor.goto_next_sibling(); + cursor.next(); let identifier = Ident::read_cursor(cursor, source)?; - cursor.goto_next_sibling(); + cursor.next(); let parameters = ParamList::read_cursor(cursor, source)?; - cursor.goto_next_sibling(); + cursor.next(); let return_type = if cursor.kind()? == "return_type" { - cursor.goto_first_child(); + cursor.child(); cursor_expect!(cursor, "single_arrow"); - cursor.goto_next_sibling(); + cursor.next(); let ty = Some(TypeElement::read_cursor(cursor, source)?); cursor.goto_parent(); ty @@ -173,14 +173,14 @@ impl ReadCursor for ParamList { fn read_cursor(cursor: &mut TreeCursor<'_>, source: &str) -> Result { let node = cursor_expect!(cursor, "param_list"); let span = Span::from_node(node); - cursor.goto_first_child(); + cursor.child(); cursor_expect!(cursor, "paren_open"); - cursor.goto_next_sibling(); + cursor.next(); let mut params = Vec::new(); while cursor.kind()? != "paren_close" { params.push(Param::read_cursor(cursor, source)?); - cursor.goto_next_sibling(); + cursor.next(); } cursor.goto_parent(); @@ -192,21 +192,21 @@ impl ReadCursor for Param { fn read_cursor(cursor: &mut TreeCursor<'_>, source: &str) -> Result { let node = cursor_expect!(cursor, "param"); let span = Span::from_node(node); - cursor.goto_first_child(); + cursor.child(); let decl_modifier = if cursor.kind()? == "declaration_modifier" { let modifier = Some(DeclModifier::read_cursor(cursor, source)?); - cursor.goto_next_sibling(); + cursor.next(); modifier } else { None }; let identifier = Ident::read_cursor(cursor, source)?; - cursor.goto_next_sibling(); + cursor.next(); cursor_expect!(cursor, "colon"); - cursor.goto_next_sibling(); + cursor.next(); let param_type = TypeElement::read_cursor(cursor, source)?; diff --git a/galvan-into-ast/src/items/type_element.rs b/galvan-into-ast/src/items/type_element.rs index 0ad922d..b130264 100644 --- a/galvan-into-ast/src/items/type_element.rs +++ b/galvan-into-ast/src/items/type_element.rs @@ -12,7 +12,7 @@ impl ReadCursor for TypeElement { let type_item = cursor_expect!(cursor, "type_item"); let span = Span::from_node(type_item); - cursor.goto_first_child(); + cursor.child(); let inner = match cursor.kind()? { "result_type" => { @@ -50,13 +50,13 @@ impl ReadCursor for ResultTypeItem { let node = cursor_expect!(cursor, "result_type"); let span = Span::from_node(node); - cursor.goto_first_child(); + cursor.child(); let success = TypeElement::read_cursor(cursor, source)?; - cursor.goto_next_sibling(); + cursor.next(); cursor_expect!(cursor, "exclamation_mark"); - cursor.goto_next_sibling(); + cursor.next(); let error = if cursor.kind()? == "type_item" { Some(TypeElement::read_cursor(cursor, source)?) } else { @@ -77,10 +77,10 @@ impl ReadCursor for OptionalTypeItem { let optional = cursor_expect!(cursor, "optional_type"); let span = Span::from_node(optional); - cursor.goto_first_child(); + cursor.child(); let some = TypeElement::read_cursor(cursor, source)?; - cursor.goto_next_sibling(); + cursor.next(); let _question_mark = cursor_expect!(cursor, "question_mark"); cursor.goto_parent(); @@ -94,13 +94,13 @@ impl ReadCursor for ArrayTypeItem { let array = cursor_expect!(cursor, "array_type"); let span = Span::from_node(array); - cursor.goto_first_child(); + cursor.child(); let _bracket = cursor_expect!(cursor, "bracket_open"); - cursor.goto_next_sibling(); + cursor.next(); let inner = TypeElement::read_cursor(cursor, source)?; - cursor.goto_next_sibling(); + cursor.next(); let _bracket = cursor_expect!(cursor, "bracket_close"); cursor.goto_parent(); @@ -117,19 +117,19 @@ impl ReadCursor for DictionaryTypeItem { let dict = cursor_expect!(cursor, "dict_type"); let span = Span::from_node(dict); - cursor.goto_first_child(); + cursor.child(); let _brace = cursor_expect!(cursor, "brace_open"); - cursor.goto_next_sibling(); + cursor.next(); let key = TypeElement::read_cursor(cursor, source)?; - cursor.goto_next_sibling(); + cursor.next(); let _colon = cursor_expect!(cursor, "colon"); - cursor.goto_next_sibling(); + cursor.next(); let value = TypeElement::read_cursor(cursor, source)?; - cursor.goto_next_sibling(); + cursor.next(); let _brace = cursor_expect!(cursor, "brace_close"); cursor.goto_parent(); @@ -143,19 +143,19 @@ impl ReadCursor for OrderedDictionaryTypeItem { let dict = cursor_expect!(cursor, "ordered_dict_type"); let span = Span::from_node(dict); - cursor.goto_first_child(); + cursor.child(); let _bracket = cursor_expect!(cursor, "bracket_open"); - cursor.goto_next_sibling(); + cursor.next(); let key = TypeElement::read_cursor(cursor, source)?; - cursor.goto_next_sibling(); + cursor.next(); let _colon = cursor_expect!(cursor, "colon"); - cursor.goto_next_sibling(); + cursor.next(); let value = TypeElement::read_cursor(cursor, source)?; - cursor.goto_next_sibling(); + cursor.next(); let _bracket = cursor_expect!(cursor, "bracket_close"); cursor.goto_parent(); @@ -169,13 +169,13 @@ impl ReadCursor for SetTypeItem { let set = cursor_expect!(cursor, "set_type"); let span = Span::from_node(set); - cursor.goto_first_child(); + cursor.child(); let _brace = cursor_expect!(cursor, "brace_close"); - cursor.goto_next_sibling(); + cursor.next(); let inner = TypeElement::read_cursor(cursor, source)?; - cursor.goto_next_sibling(); + cursor.next(); let _brace = cursor_expect!(cursor, "brace_close"); cursor.goto_parent(); @@ -204,7 +204,7 @@ impl ReadCursor for BasicTypeItem { let type_item = cursor_expect!(cursor, "basic_type"); let span = Span::from_node(type_item); - cursor.goto_first_child(); + cursor.child(); let ident = TypeIdent::read_cursor(cursor, source)?; diff --git a/galvan-into-ast/src/lib.rs b/galvan-into-ast/src/lib.rs index 86c2d1f..eea1ef2 100644 --- a/galvan-into-ast/src/lib.rs +++ b/galvan-into-ast/src/lib.rs @@ -3,6 +3,7 @@ use galvan_files::Source; use galvan_parse::*; mod result; +use result::CursorUtil; pub use result::{AstError, AstResult}; mod modifiers; @@ -42,14 +43,12 @@ impl IntoAst for ParseTree { source, }; - if cursor.goto_first_child() { - let node = cursor.node(); + if cursor.child() { let item = RootItem::read_cursor(&mut cursor, &ast.source)?; ast.toplevel.push(item); - while cursor.goto_next_sibling() { - let node = cursor.node(); + while cursor.next() { let item = RootItem::read_cursor(&mut cursor, &ast.source)?; ast.toplevel.push(item); diff --git a/galvan-into-ast/src/modifiers.rs b/galvan-into-ast/src/modifiers.rs index 7782633..6dc1e3c 100644 --- a/galvan-into-ast/src/modifiers.rs +++ b/galvan-into-ast/src/modifiers.rs @@ -7,14 +7,14 @@ use crate::{cursor_expect, AstError, ReadCursor}; impl ReadCursor for Visibility { fn read_cursor(cursor: &mut TreeCursor, source: &str) -> Result { let kind = if cursor.kind()? == "visibility" { - cursor.goto_first_child(); + cursor.child(); let vis = match cursor.kind()? { "pub_keyword" => VisibilityKind::Public, unknown => unreachable!("Unknown visibility modifier: {unknown}"), }; cursor.goto_parent(); - cursor.goto_next_sibling(); + cursor.next(); vis } else { VisibilityKind::Inherited @@ -32,7 +32,7 @@ impl ReadCursor for DeclModifier { fn read_cursor(cursor: &mut TreeCursor<'_>, source: &str) -> Result { cursor_expect!(cursor, "declaration_modifier"); - cursor.goto_first_child(); + cursor.child(); let modifier = match cursor.kind()? { "ref_keyword" => Self::Ref, diff --git a/galvan-into-ast/src/result.rs b/galvan-into-ast/src/result.rs index deb76ce..8415a74 100644 --- a/galvan-into-ast/src/result.rs +++ b/galvan-into-ast/src/result.rs @@ -36,6 +36,10 @@ impl TreeSitterError for Node<'_> { pub trait CursorUtil { fn kind(&self) -> Result<&str, AstError>; fn curr(&self) -> Result, AstError>; + /// Goes to the next sibling, skipping comments if possible + fn next(&mut self) -> bool; + /// Goes to the first child, skipping comments if possible + fn child(&mut self) -> bool; } impl CursorUtil for TreeCursor<'_> { @@ -46,4 +50,26 @@ impl CursorUtil for TreeCursor<'_> { fn curr(&self) -> Result, AstError> { Ok(self.node().err()?) } + + fn next(&mut self) -> bool { + let mut res = self.goto_next_sibling(); + + while let Ok("comment") = self.kind() { + if !res { break; } + res = self.goto_next_sibling(); + } + + res + } + + fn child(&mut self) -> bool { + let mut res = self.goto_first_child(); + + while let Ok("comment") = self.kind() { + if !res { break; } + res = self.goto_next_sibling(); + } + + res + } } diff --git a/tree-sitter-galvan b/tree-sitter-galvan index c1f2d48..9c77c8f 160000 --- a/tree-sitter-galvan +++ b/tree-sitter-galvan @@ -1 +1 @@ -Subproject commit c1f2d489164f999a561cf31a2557077a1e20105d +Subproject commit 9c77c8f497e9dfcbc667f94a1b20075a039fbde2