Skip to content

Commit 1539732

Browse files
authored
cli: add back basic dump-ast support (#505)
usage: ```sh echo select | squawk --debug=ast ``` which gives: ```json { "stmts": [ { "type": "SELECT" } ], "version": "0.1" } ``` rel: #444
1 parent 70559be commit 1539732

File tree

5 files changed

+87
-24
lines changed

5 files changed

+87
-24
lines changed

crates/squawk/src/debug.rs

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ use std::{io, path::PathBuf};
22

33
use annotate_snippets::{Level, Message, Renderer, Snippet};
44
use anyhow::Result;
5-
use squawk_syntax::syntax_error::SyntaxError;
5+
use serde_json::json;
6+
use squawk_syntax::{ast::AstNode, syntax_error::SyntaxError};
67

78
use crate::{
89
file::{sql_from_path, sql_from_stdin},
@@ -13,11 +14,11 @@ pub(crate) fn debug<W: io::Write>(
1314
f: &mut W,
1415
paths: &[PathBuf],
1516
read_stdin: bool,
16-
dump_ast: &DebugOption,
17+
debug_option: &DebugOption,
1718
verbose: bool,
1819
) -> Result<()> {
1920
let process_dump_ast = |sql: &str, filename: &str, f: &mut W| -> Result<()> {
20-
match dump_ast {
21+
match debug_option {
2122
DebugOption::Lex => {
2223
let tokens = squawk_lexer::tokenize(sql);
2324
let mut start = 0;
@@ -61,6 +62,9 @@ pub(crate) fn debug<W: io::Write>(
6162
})?;
6263
}
6364
}
65+
DebugOption::Ast => {
66+
dump_ast(f, sql)?;
67+
}
6468
}
6569
Ok(())
6670
};
@@ -77,6 +81,30 @@ pub(crate) fn debug<W: io::Write>(
7781
Ok(())
7882
}
7983

84+
fn dump_ast<W: io::Write>(f: &mut W, sql: &str) -> Result<()> {
85+
let parse = squawk_syntax::SourceFile::parse(sql);
86+
let file = parse.tree();
87+
88+
let stmts = file
89+
.stmts()
90+
.map(|stmt| {
91+
// No api guarantees for now
92+
json!({
93+
"type": format!("{:?}", stmt.syntax().kind())
94+
})
95+
})
96+
.collect::<Vec<_>>();
97+
98+
let output = json!({
99+
"version": "0.1",
100+
"stmts": stmts,
101+
});
102+
103+
writeln!(f, "{}", serde_json::to_string_pretty(&output)?)?;
104+
105+
Ok(())
106+
}
107+
80108
fn render_syntax_errors(
81109
errors: &[SyntaxError],
82110
filename: &str,
@@ -96,3 +124,28 @@ fn render_syntax_errors(
96124
}
97125
Ok(())
98126
}
127+
128+
#[cfg(test)]
129+
mod test {
130+
use insta::assert_snapshot;
131+
132+
use super::dump_ast;
133+
134+
#[test]
135+
fn dump_ast_basic_output() {
136+
let mut buffer = vec![];
137+
dump_ast(
138+
&mut buffer,
139+
"
140+
select;
141+
insert into t values (1, 'a', true);
142+
update t set c = 10 where b = 5;
143+
delete from t;
144+
truncate t;
145+
",
146+
)
147+
.unwrap();
148+
let output = String::from_utf8(buffer).expect("Invalid UTF-8");
149+
assert_snapshot!(output);
150+
}
151+
}

crates/squawk/src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ arg_enum! {
6161
#[derive(Debug, StructOpt)]
6262
pub enum DebugOption {
6363
Lex,
64-
Parse
64+
Parse,
65+
Ast
6566
}
6667
}
6768

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
source: crates/squawk/src/debug.rs
3+
expression: output
4+
---
5+
{
6+
"stmts": [
7+
{
8+
"type": "SELECT"
9+
},
10+
{
11+
"type": "INSERT"
12+
},
13+
{
14+
"type": "UPDATE"
15+
},
16+
{
17+
"type": "DELETE"
18+
},
19+
{
20+
"type": "TRUNCATE"
21+
}
22+
],
23+
"version": "0.1"
24+
}

crates/squawk_parser/src/lib.rs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,6 @@
2424
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
2525
// DEALINGS IN THE SOFTWARE.
2626

27-
// https://www.scattered-thoughts.net/writing/babys-second-wasm-compiler/
28-
//
29-
// https://craftinginterpreters.com/parsing-expressions.html
30-
//
31-
// see: https://github.com/rust-lang/rust-analyzer/blob/master/crates/parser/src/parser.rs
32-
// https://rust-analyzer.github.io/blog/2020/09/16/challeging-LR-parsing.html
33-
// https://github.com/rust-lang/rust-analyzer/blob/master/docs/dev/syntax.md
34-
// https://ericlippert.com/2012/06/08/red-green-trees/
35-
// https://github.com/swiftlang/swift/tree/5e2c815edfd758f9b1309ce07bfc01c4bc20ec23/lib/Syntax
36-
// https://swift-ast-explorer.com
37-
// https://github.com/rust-lang/rust-analyzer/blob/cf156a7a43f822e71309e50470ac34363da26727/docs/dev/syntax.md
38-
// https://github.com/kaleidawave/ezno/blob/8ce921e39c3d4e947063f206347b2932cee456ec/parser/src/lib.rs#L177
39-
// https://kaleidawave.github.io/posts/sets-types-and-type-checking/
40-
// https://github.com/m-novikov/tree-sitter-sql/tree/main
41-
// https://github.com/withered-magic/starpls/tree/79f47e12dab8be650804ce7fa931ee5e1e116eae/crates/starpls_parser/src
42-
// https://github.com/apache/datafusion-sqlparser-rs -- removes comments and whitespace :/
43-
44-
// rust analyzer has a builtin doc test like thing where you generate snapshot
45-
// style tests from comments on top of grammar functions
46-
4727
use drop_bomb::DropBomb;
4828
use event::Event;
4929
use grammar::OPERATOR_FIRST;

crates/squawk_syntax/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# squawk_syntax
2+
3+
## related
4+
5+
- https://github.com/rust-lang/rust-analyzer/blob/d2f17873ff19786a121fb3302f91779c1a1b957f/docs/book/src/contributing/syntax.md

0 commit comments

Comments
 (0)