Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@ dir-test = "0.4"
drop_bomb = "0.1.5"
camino = "1.1.9"
pg_query = "6.1.0"
rowan = "0.15.15"
smol_str = "0.3.2"

# local
squawk-parser = { version = "0.0.0", path = "./crates/parser" }
squawk-linter = { version = "0.0.0", path = "./crates/linter" }
squawk-github = { version = "0.0.0", path = "./crates/github" }
squawk_lexer = { version = "0.0.0", path = "./crates/squawk_lexer" }
squawk_parser = { version = "0.0.0", path = "./crates/squawk_parser" }

[workspace.lints.clippy]
collapsible_else_if = "allow"
Expand All @@ -53,3 +56,6 @@ debug = 0
[profile.dev.package]
insta.opt-level = 3
similar.opt-level = 3
# These speed up local tests.
rowan.opt-level = 3
text-size.opt-level = 3
6 changes: 3 additions & 3 deletions crates/squawk_parser/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ fn sqltest(fixture: Fixture<&str>) {

let parent_dir = input_file.parent().and_then(|x| x.file_name()).unwrap();

let (parsed, has_errors) = parse_text(&content);
let (parsed, has_errors) = parse_text(content);

with_settings!({
omit_expression => true,
Expand All @@ -97,12 +97,12 @@ fn sqltest(fixture: Fixture<&str>) {
);
// skipping pg17 specific stuff since our parser isn't using the latest parser
if !test_name.ends_with("pg17") {
let pg_result = pg_query::parse(&content);
let pg_result = pg_query::parse(content);
if let Err(e) = &pg_result {
assert!(
&pg_result.is_ok(),
"tests defined in the `ok` can't have Postgres parser errors. Found {}",
e.to_string()
e
);
}
}
Expand Down
20 changes: 20 additions & 0 deletions crates/squawk_syntax/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "squawk_syntax"
version = "0.0.0"
edition.workspace = true
rust-version.workspace = true
authors.workspace = true
license.workspace = true

[dependencies]
squawk_parser.workspace = true
rowan.workspace = true
smol_str.workspace = true

[dev-dependencies]
insta.workspace = true
dir-test.workspace = true
camino.workspace = true

[lints]
workspace = true
126 changes: 126 additions & 0 deletions crates/squawk_syntax/src/ast.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// via https://github.com/rust-lang/rust-analyzer/blob/d8887c0758bbd2d5f752d5bd405d4491e90e7ed6/crates/syntax/src/ast.rs
//
// Permission is hereby granted, free of charge, to any
// person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the
// Software without restriction, including without
// limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software
// is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice
// shall be included in all copies or substantial portions
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
// SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

mod nodes;
mod support;
mod traits;

mod node_ext;

use std::marker::PhantomData;

use crate::syntax_node::{SyntaxNode, SyntaxNodeChildren, SyntaxToken};
use squawk_parser::SyntaxKind;

pub use self::{
nodes::*,
// generated::{nodes::*, tokens::*},
// node_ext::{
// AttrKind, FieldKind, Macro, NameLike, NameOrNameRef, PathSegmentKind, SelfParamKind,
// SlicePatComponents, StructKind, TraitOrAlias, TypeBoundKind, TypeOrConstParam,
// VisibilityKind,
// },
// operators::{ArithOp, BinaryOp, CmpOp, LogicOp, Ordering, RangeOp, UnaryOp},
// token_ext::{CommentKind, CommentPlacement, CommentShape, IsString, QuoteOffsets, Radix},
traits::{
// AttrDocCommentIter, DocCommentIter,
HasArgList, // HasAttrs, HasDocComments, HasGenericArgs,
HasIfExists,
HasIfNotExists, // HasTypeBounds,
// HasVisibility,
// HasGenericParams, HasLoopBody,
HasModuleItem,
HasName,
},
};

/// The main trait to go from untyped `SyntaxNode` to a typed ast. The
/// conversion itself has zero runtime cost: ast and syntax nodes have exactly
/// the same representation: a pointer to the tree root and a pointer to the
/// node itself.
pub trait AstNode {
fn can_cast(kind: SyntaxKind) -> bool
where
Self: Sized;

fn cast(syntax: SyntaxNode) -> Option<Self>
where
Self: Sized;

fn syntax(&self) -> &SyntaxNode;
fn clone_for_update(&self) -> Self
where
Self: Sized,
{
Self::cast(self.syntax().clone_for_update()).unwrap()
}
fn clone_subtree(&self) -> Self
where
Self: Sized,
{
Self::cast(self.syntax().clone_subtree()).unwrap()
}
}

/// Like `AstNode`, but wraps tokens rather than interior nodes.
pub trait AstToken {
fn can_cast(token: SyntaxKind) -> bool
where
Self: Sized;

fn cast(syntax: SyntaxToken) -> Option<Self>
where
Self: Sized;

fn syntax(&self) -> &SyntaxToken;

fn text(&self) -> &str {
self.syntax().text()
}
}

/// An iterator over `SyntaxNode` children of a particular AST type.
#[derive(Debug, Clone)]
pub struct AstChildren<N> {
inner: SyntaxNodeChildren,
ph: PhantomData<N>,
}

impl<N> AstChildren<N> {
fn new(parent: &SyntaxNode) -> Self {
AstChildren {
inner: parent.children(),
ph: PhantomData,
}
}
}

impl<N: AstNode> Iterator for AstChildren<N> {
type Item = N;
fn next(&mut self) -> Option<N> {
self.inner.find_map(N::cast)
}
}
52 changes: 52 additions & 0 deletions crates/squawk_syntax/src/ast/node_ext.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// via https://github.com/rust-lang/rust-analyzer/blob/d8887c0758bbd2d5f752d5bd405d4491e90e7ed6/crates/syntax/src/ast/node_ext.rs
//
// Permission is hereby granted, free of charge, to any
// person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the
// Software without restriction, including without
// limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software
// is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice
// shall be included in all copies or substantial portions
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
// SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

use std::borrow::Cow;

use rowan::{GreenNodeData, GreenTokenData, NodeOrToken};

use crate::{SyntaxNode, TokenText};

// impl ast::Name {
// pub fn text(&self) -> TokenText<'_> {
// text_of_first_token(self.syntax())
// }
// }

pub(crate) fn text_of_first_token(node: &SyntaxNode) -> TokenText<'_> {
fn first_token(green_ref: &GreenNodeData) -> &GreenTokenData {
green_ref
.children()
.next()
.and_then(NodeOrToken::into_token)
.unwrap()
}

match node.green() {
Cow::Borrowed(green_ref) => TokenText::borrowed(first_token(green_ref).text()),
Cow::Owned(green) => TokenText::owned(first_token(&green).to_owned()),
}
}
Loading
Loading