Skip to content

Commit

Permalink
Merge pull request #70 from supabase/fix/add-back-stmts
Browse files Browse the repository at this point in the history
fix: add ast back
  • Loading branch information
psteinroe authored Dec 13, 2023
2 parents 421e189 + c44da3f commit b0196f2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
37 changes: 26 additions & 11 deletions crates/parser/src/parse/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
7 changes: 6 additions & 1 deletion crates/parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit b0196f2

Please sign in to comment.