diff --git a/crates/parser/src/parse/statement.rs b/crates/parser/src/parse/statement.rs index 90368af7..1ec50ad0 100644 --- a/crates/parser/src/parse/statement.rs +++ b/crates/parser/src/parse/statement.rs @@ -18,18 +18,33 @@ pub fn statement(parser: &mut Parser, kind: SyntaxKind) { .as_str(), ) { Ok(result) => { - libpg_query_node( - parser, - result - .protobuf - .nodes() - .iter() - .find(|n| n.1 == 1) - .unwrap() - .0 - .to_enum(), - &token_range, + let root = result + .protobuf + .nodes() + .iter() + .find(|n| n.1 == 1) + .unwrap() + .0 + .to_enum(); + + // FIXME: if have no idea why the subtraction is needed + let start = if parser.tokens[token_range.start].span.start() == TextSize::from(0) { + TextSize::from(0) + } else { + parser.tokens[token_range.start].span.start() - TextSize::from(1) + }; + let end = if token_range.end == parser.tokens.len() { + parser.tokens[token_range.end - 1].span.end() - TextSize::from(1) + } else { + parser.tokens[token_range.end - 1].span.end() + }; + let text_range = TextRange::new( + TextSize::from(u32::try_from(start).unwrap()), + TextSize::from(u32::try_from(end).unwrap()), ); + + parser.stmt(root.clone(), text_range); + libpg_query_node(parser, root, &token_range); } Err(err) => { parser.error( diff --git a/crates/parser/src/parser.rs b/crates/parser/src/parser.rs index dcfb7adb..9b071193 100644 --- a/crates/parser/src/parser.rs +++ b/crates/parser/src/parser.rs @@ -346,13 +346,18 @@ mod tests { fn test_parser_simple() { init(); - let input = "alter table x rename to y \n alter table x alter column z set default 1"; + let input = "alter table x rename to y \n\n alter table x alter column z set default 1"; let mut p = Parser::new(lex(input)); source(&mut p); let result = p.finish(); dbg!(&result.cst); + assert_eq!(result.stmts.len(), 2); + result.stmts.iter().for_each(|x| { + dbg!(&x.range); + dbg!(&x.stmt); + }); println!("{:#?}", result.errors); }