From e5b8ce7497f515bc998a31cd8b45fb2f3e9287a2 Mon Sep 17 00:00:00 2001 From: Arash Maymandi <27716912+am357@users.noreply.github.com> Date: Thu, 8 Aug 2024 18:39:40 -0700 Subject: [PATCH] Move NodeId Generation to a separate crate (#484) Moves the `NodeId` and its Id generation to a new crate, `partiql-common` so that it can get consumed by multiple crates. --------- Co-authored-by: Josh Pschorr --- Cargo.toml | 2 +- partiql-ast-passes/Cargo.toml | 1 + partiql-ast-passes/src/name_resolver.rs | 31 ++++----- partiql-ast/Cargo.toml | 2 + partiql-ast/src/ast.rs | 8 +-- partiql-ast/src/builder.rs | 64 ++++--------------- partiql-ast/src/visit.rs | 5 +- .../Cargo.toml | 24 +++---- .../LICENSE | 0 partiql-common/src/lib.rs | 4 ++ partiql-common/src/node.rs | 47 ++++++++++++++ .../src/syntax}/line_offset_tracker.rs | 6 +- .../src/syntax}/location.rs | 10 +-- partiql-common/src/syntax/metadata.rs | 5 ++ partiql-common/src/syntax/mod.rs | 3 + partiql-logical-planner/Cargo.toml | 11 ++-- partiql-logical-planner/src/lower.rs | 5 +- partiql-parser/Cargo.toml | 4 +- partiql-parser/src/error.rs | 4 +- partiql-parser/src/lexer.rs | 8 +-- partiql-parser/src/lib.rs | 6 +- partiql-parser/src/parse/mod.rs | 15 ++--- partiql-parser/src/parse/parse_util.rs | 6 +- partiql-parser/src/parse/parser_state.rs | 17 ++--- partiql-parser/src/parse/partiql.lalrpop | 6 +- partiql-parser/src/preprocessor.rs | 6 +- partiql-parser/src/token_parser.rs | 2 +- partiql-source-map/src/lib.rs | 8 --- partiql-source-map/src/metadata.rs | 5 -- 29 files changed, 164 insertions(+), 151 deletions(-) rename {partiql-source-map => partiql-common}/Cargo.toml (62%) rename {partiql-source-map => partiql-common}/LICENSE (100%) create mode 100644 partiql-common/src/lib.rs create mode 100644 partiql-common/src/node.rs rename {partiql-source-map/src => partiql-common/src/syntax}/line_offset_tracker.rs (97%) rename {partiql-source-map/src => partiql-common/src/syntax}/location.rs (97%) create mode 100644 partiql-common/src/syntax/metadata.rs create mode 100644 partiql-common/src/syntax/mod.rs delete mode 100644 partiql-source-map/src/lib.rs delete mode 100644 partiql-source-map/src/metadata.rs diff --git a/Cargo.toml b/Cargo.toml index 705df987..acd82026 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,7 +16,7 @@ members = [ "partiql-catalog", "partiql-conformance-tests", "partiql-conformance-test-generator", - "partiql-source-map", + "partiql-common", "partiql-logical-planner", "partiql-logical", "partiql-eval", diff --git a/partiql-ast-passes/Cargo.toml b/partiql-ast-passes/Cargo.toml index b67fdc1a..6ff1dba2 100644 --- a/partiql-ast-passes/Cargo.toml +++ b/partiql-ast-passes/Cargo.toml @@ -22,6 +22,7 @@ bench = false [dependencies] partiql-ast = { path = "../partiql-ast", version = "0.10.*" } partiql-catalog = { path = "../partiql-catalog", version = "0.10.*" } +partiql-common = { path = "../partiql-common", version = "0.10.*" } partiql-types = { path = "../partiql-types", version = "0.10.*" } assert_matches = "1.5.*" diff --git a/partiql-ast-passes/src/name_resolver.rs b/partiql-ast-passes/src/name_resolver.rs index e6994668..52147ba9 100644 --- a/partiql-ast-passes/src/name_resolver.rs +++ b/partiql-ast-passes/src/name_resolver.rs @@ -5,6 +5,7 @@ use partiql_ast::ast; use partiql_ast::ast::{GroupByExpr, GroupKey}; use partiql_ast::visit::{Traverse, Visit, Visitor}; use partiql_catalog::Catalog; +use partiql_common::node::NodeId; use std::sync::atomic::{AtomicU32, Ordering}; type FnvIndexSet = IndexSet; @@ -40,9 +41,9 @@ pub struct KeySchema { #[derive(Default, Debug, Clone)] pub struct KeyRegistry { - pub in_scope: FnvIndexMap>, - pub schema: FnvIndexMap, - pub aliases: FnvIndexMap, + pub in_scope: FnvIndexMap>, + pub schema: FnvIndexMap, + pub aliases: FnvIndexMap, } #[derive(Debug)] @@ -87,17 +88,17 @@ enum EnclosingClause { #[allow(dead_code)] pub struct NameResolver<'c> { // environment stack tracking - id_path_to_root: Vec, - id_child_stack: Vec>, + id_path_to_root: Vec, + id_child_stack: Vec>, keyref_stack: Vec, - lateral_stack: Vec>, + lateral_stack: Vec>, id_gen: IdGenerator, // data flow tracking - enclosing_clause: FnvIndexMap>, - in_scope: FnvIndexMap>, - schema: FnvIndexMap, - aliases: FnvIndexMap, + enclosing_clause: FnvIndexMap>, + in_scope: FnvIndexMap>, + schema: FnvIndexMap, + aliases: FnvIndexMap, // errors that occur during name resolution errors: Vec, @@ -148,7 +149,7 @@ impl<'c> NameResolver<'c> { } #[inline] - fn current_node(&self) -> &ast::NodeId { + fn current_node(&self) -> &NodeId { self.id_path_to_root.last().unwrap() } @@ -175,7 +176,7 @@ impl<'c> NameResolver<'c> { } #[inline] - fn exit_lateral(&mut self) -> Result, AstTransformError> { + fn exit_lateral(&mut self) -> Result, AstTransformError> { self.lateral_stack.pop().ok_or_else(|| { AstTransformError::IllegalState("Expected non-empty lateral stack".to_string()) }) @@ -187,7 +188,7 @@ impl<'c> NameResolver<'c> { } #[inline] - fn exit_child_stack(&mut self) -> Result, AstTransformError> { + fn exit_child_stack(&mut self) -> Result, AstTransformError> { self.id_child_stack.pop().ok_or_else(|| { AstTransformError::IllegalState("Expected non-empty child stack".to_string()) }) @@ -212,14 +213,14 @@ impl<'c> NameResolver<'c> { } impl<'ast, 'c> Visitor<'ast> for NameResolver<'c> { - fn enter_ast_node(&mut self, id: ast::NodeId) -> Traverse { + fn enter_ast_node(&mut self, id: NodeId) -> Traverse { self.id_path_to_root.push(id); if let Some(children) = self.id_child_stack.last_mut() { children.push(id); } Traverse::Continue } - fn exit_ast_node(&mut self, id: ast::NodeId) -> Traverse { + fn exit_ast_node(&mut self, id: NodeId) -> Traverse { assert_eq!(self.id_path_to_root.pop(), Some(id)); Traverse::Continue } diff --git a/partiql-ast/Cargo.toml b/partiql-ast/Cargo.toml index 687c7327..0867c82a 100644 --- a/partiql-ast/Cargo.toml +++ b/partiql-ast/Cargo.toml @@ -20,6 +20,7 @@ path = "src/lib.rs" bench = false [dependencies] +partiql-common = { path = "../partiql-common", version = "0.10.*" } indexmap = "2.2" rust_decimal = { version = "1.25.0", default-features = false, features = ["std"] } serde = { version = "1.*", features = ["derive"], optional = true } @@ -33,6 +34,7 @@ serde = [ "rust_decimal/serde-with-str", "rust_decimal/serde", "indexmap/serde", + "partiql-common/serde" ] [dependencies.partiql-ast-macros] diff --git a/partiql-ast/src/ast.rs b/partiql-ast/src/ast.rs index de13bb2d..782ddeb6 100644 --- a/partiql-ast/src/ast.rs +++ b/partiql-ast/src/ast.rs @@ -8,7 +8,6 @@ // As more changes to this AST are expected, unless explicitly advised, using the structures exposed // in this crate directly is not recommended. -use indexmap::IndexMap; use rust_decimal::Decimal as RustDecimal; use std::fmt; @@ -17,12 +16,7 @@ use std::fmt; use serde::{Deserialize, Serialize}; use partiql_ast_macros::Visit; - -pub type AstTypeMap = IndexMap; - -#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub struct NodeId(pub u32); +use partiql_common::node::NodeId; /// Represents an AST node. #[derive(Clone, Debug, Eq, PartialEq)] diff --git a/partiql-ast/src/builder.rs b/partiql-ast/src/builder.rs index 853d1996..b7973204 100644 --- a/partiql-ast/src/builder.rs +++ b/partiql-ast/src/builder.rs @@ -1,53 +1,17 @@ -use crate::ast; -use crate::ast::{AstNode, NodeId}; +use crate::ast::AstNode; +use partiql_common::node::{AutoNodeIdGenerator, NodeIdGenerator, NullIdGenerator}; -/// A provider of 'fresh' [`NodeId`]s. -pub trait IdGenerator { - /// Provides a 'fresh' [`NodeId`]. - fn id(&mut self) -> NodeId; -} - -/// Auto-incrementing [`IdGenerator`] -pub struct AutoNodeIdGenerator { - next_id: ast::NodeId, -} - -impl Default for AutoNodeIdGenerator { - fn default() -> Self { - AutoNodeIdGenerator { next_id: NodeId(1) } - } -} - -impl IdGenerator for AutoNodeIdGenerator { - #[inline] - fn id(&mut self) -> NodeId { - let mut next = NodeId(&self.next_id.0 + 1); - std::mem::swap(&mut self.next_id, &mut next); - next - } -} - -/// A provider of [`NodeId`]s that are always `0`; Useful for testing -#[derive(Default)] -pub struct NullIdGenerator {} - -impl IdGenerator for NullIdGenerator { - fn id(&mut self) -> NodeId { - NodeId(0) - } -} - -/// A Builder for [`AstNode`]s that uses a [`IdGenerator`] to assign [`NodeId`]s -pub struct NodeBuilder { +/// A Builder for [`AstNode`]s that uses a [`NodeIdGenerator`] to assign [`NodeId`]s +pub struct AstNodeBuilder { /// Generator for 'fresh' [`NodeId`]s - pub id_gen: Id, + pub id_gen: IdGen, } -impl NodeBuilder +impl AstNodeBuilder where - Id: IdGenerator, + IdGen: NodeIdGenerator, { - pub fn new(id_gen: Id) -> Self { + pub fn new(id_gen: IdGen) -> Self { Self { id_gen } } @@ -57,17 +21,17 @@ where } } -impl Default for NodeBuilder +impl Default for AstNodeBuilder where - T: IdGenerator + Default, + T: NodeIdGenerator + Default, { fn default() -> Self { Self::new(T::default()) } } -/// A [`NodeBuilder`] whose 'fresh' [`NodeId`]s are Auto-incrementing. -pub type NodeBuilderWithAutoId = NodeBuilder; +/// A [`AstNodeBuilder`] whose 'fresh' [`NodeId`]s are Auto-incrementing. +pub type AstNodeBuilderWithAutoId = AstNodeBuilder; -/// A [`NodeBuilder`] whose 'fresh' [`NodeId`]s are always `0`; Useful for testing -pub type NodeBuilderWithNullId = NodeBuilder; +/// A [`AstNodeBuilder`] whose 'fresh' [`NodeId`]s are always `0`; Useful for testing +pub type AstNodeBuilderWithNullId = AstNodeBuilder; diff --git a/partiql-ast/src/visit.rs b/partiql-ast/src/visit.rs index 19acad7b..cd709596 100644 --- a/partiql-ast/src/visit.rs +++ b/partiql-ast/src/visit.rs @@ -1,5 +1,5 @@ use crate::ast; -use crate::ast::NodeId; +use partiql_common::node::NodeId; /// Indicates if tree traversal of the entire tree should continue or not. #[derive(PartialEq, Debug)] @@ -561,7 +561,8 @@ pub trait Visitor<'ast> { mod tests { use crate::ast; use crate::visit::{Traverse, Visitor}; - use ast::{AstNode, BinOp, BinOpKind, Expr, Lit, NodeId}; + use ast::{AstNode, BinOp, BinOpKind, Expr, Lit}; + use partiql_common::node::NodeId; use std::ops::AddAssign; #[test] diff --git a/partiql-source-map/Cargo.toml b/partiql-common/Cargo.toml similarity index 62% rename from partiql-source-map/Cargo.toml rename to partiql-common/Cargo.toml index 367bc057..203a3b71 100644 --- a/partiql-source-map/Cargo.toml +++ b/partiql-common/Cargo.toml @@ -1,12 +1,12 @@ [package] -name = "partiql-source-map" -description = "PartiQL Source Map" +name = "partiql-common" +description = "PartiQL Core" authors.workspace = true homepage.workspace = true repository.workspace = true license = "Apache-2.0" readme = "../README.md" -keywords = ["sql", "sourcemap", "query", "compilers", "interpreters"] +keywords = ["sql", "ast", "query", "compilers", "interpreters"] categories = ["database", "compilers"] exclude = [ "**/.git/**", @@ -16,18 +16,20 @@ version.workspace = true edition.workspace = true [lib] +path = "src/lib.rs" bench = false [dependencies] -partiql-ast = { path = "../partiql-ast", version = "0.10.*" } - -smallvec = { version = "1.*" } +indexmap = "2.2" +pretty = "0.12" serde = { version = "1.*", features = ["derive"], optional = true } - - -[dev-dependencies] - +smallvec = { version = "1.*" } +thiserror = "1.0" [features] default = [] -serde = ["dep:serde", "smallvec/serde"] +serde = [ + "dep:serde", + "indexmap/serde", + "smallvec/serde" +] diff --git a/partiql-source-map/LICENSE b/partiql-common/LICENSE similarity index 100% rename from partiql-source-map/LICENSE rename to partiql-common/LICENSE diff --git a/partiql-common/src/lib.rs b/partiql-common/src/lib.rs new file mode 100644 index 00000000..3f107b89 --- /dev/null +++ b/partiql-common/src/lib.rs @@ -0,0 +1,4 @@ +#![deny(rust_2018_idioms)] +#![deny(clippy::all)] +pub mod node; +pub mod syntax; diff --git a/partiql-common/src/node.rs b/partiql-common/src/node.rs new file mode 100644 index 00000000..30a81159 --- /dev/null +++ b/partiql-common/src/node.rs @@ -0,0 +1,47 @@ +use indexmap::IndexMap; +use std::hash::Hash; + +#[cfg(feature = "serde")] +use serde::{Deserialize, Serialize}; + +pub type NodeMap = IndexMap; + +#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +pub struct NodeId(pub u32); + +/// Auto-incrementing [`NodeIdGenerator`] +pub struct AutoNodeIdGenerator { + next_id: NodeId, +} + +impl Default for AutoNodeIdGenerator { + fn default() -> Self { + AutoNodeIdGenerator { next_id: NodeId(1) } + } +} + +/// A provider of 'fresh' [`NodeId`]s. +pub trait NodeIdGenerator { + /// Provides a 'fresh' [`NodeId`]. + fn id(&mut self) -> NodeId; +} + +impl NodeIdGenerator for AutoNodeIdGenerator { + #[inline] + fn id(&mut self) -> NodeId { + let mut next = NodeId(&self.next_id.0 + 1); + std::mem::swap(&mut self.next_id, &mut next); + next + } +} + +/// A provider of [`NodeId`]s that are always `0`; Useful for testing +#[derive(Default)] +pub struct NullIdGenerator {} + +impl NodeIdGenerator for NullIdGenerator { + fn id(&mut self) -> NodeId { + NodeId(0) + } +} diff --git a/partiql-source-map/src/line_offset_tracker.rs b/partiql-common/src/syntax/line_offset_tracker.rs similarity index 97% rename from partiql-source-map/src/line_offset_tracker.rs rename to partiql-common/src/syntax/line_offset_tracker.rs index 1595bd4b..5eb94ae1 100644 --- a/partiql-source-map/src/line_offset_tracker.rs +++ b/partiql-common/src/syntax/line_offset_tracker.rs @@ -1,6 +1,6 @@ //! [`LineOffsetTracker`] and related types for mapping locations in source `str`s. -use crate::location::{ByteOffset, BytePosition, LineAndCharPosition, LineOffset}; +use crate::syntax::location::{ByteOffset, BytePosition, LineAndCharPosition, LineOffset}; use smallvec::{smallvec, SmallVec}; use std::ops::Range; @@ -14,8 +14,8 @@ use serde::{Deserialize, Serialize}; /// ## Example /// /// ```rust -/// use partiql_source_map::location::{ByteOffset, LineAndCharPosition}; -/// use partiql_source_map::line_offset_tracker::{LineOffsetError, LineOffsetTracker}; +/// use partiql_common::syntax::location::{ByteOffset, LineAndCharPosition}; +/// use partiql_common::syntax::line_offset_tracker::{LineOffsetError, LineOffsetTracker}; /// /// let source = "12345\n789012345\n789012345\n789012345"; /// let mut tracker = LineOffsetTracker::default(); diff --git a/partiql-source-map/src/location.rs b/partiql-common/src/syntax/location.rs similarity index 97% rename from partiql-source-map/src/location.rs rename to partiql-common/src/syntax/location.rs index c3447eb6..6463ee76 100644 --- a/partiql-source-map/src/location.rs +++ b/partiql-common/src/syntax/location.rs @@ -119,7 +119,7 @@ impl fmt::Display for BytePosition { /// /// ## Example /// ``` -/// # use partiql_source_map::location::LineAndCharPosition; +/// # use partiql_common::syntax::location::LineAndCharPosition; /// assert_eq!("Beginning of &str: LineAndCharPosition { line: LineOffset(0), char: CharOffset(0) }", /// format!("Beginning of &str: {:?}", LineAndCharPosition::new(0, 0))); /// ``` @@ -150,7 +150,7 @@ impl LineAndCharPosition { /// /// ## Example /// ``` -/// # use partiql_source_map::location::LineAndColumn; +/// # use partiql_common::syntax::location::LineAndColumn; /// assert_eq!("Beginning of &str: 1:1",format!("Beginning of &str: {}", LineAndColumn::new(1, 1).unwrap())); /// ``` #[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash)] @@ -253,7 +253,7 @@ pub struct Located { /// ## Example /// /// ```rust -/// # use partiql_source_map::location::{ByteOffset, BytePosition, Located, ToLocated}; +/// # use partiql_common::syntax::location::{ByteOffset, BytePosition, Located, ToLocated}; /// assert_eq!("blah".to_string().to_located(BytePosition::from(5)..BytePosition::from(10)), /// Located{ /// inner: "blah".to_string(), @@ -284,7 +284,7 @@ impl Located { /// ## Example /// /// ```rust - /// # use partiql_source_map::location::{ByteOffset, BytePosition, Located, ToLocated}; + /// # use partiql_common::syntax::location::{ByteOffset, BytePosition, Located, ToLocated}; /// assert_eq!("blah".to_string() /// .to_located(BytePosition::from(5)..BytePosition::from(10)) /// .map_loc(|BytePosition(o)| BytePosition(o+5)), @@ -312,7 +312,7 @@ mod tests { use super::*; use std::num::NonZeroUsize; - use crate::location::{ByteOffset, BytePosition, Located, Location}; + use crate::syntax::location::{ByteOffset, BytePosition, Located, Location}; #[test] fn located() { diff --git a/partiql-common/src/syntax/metadata.rs b/partiql-common/src/syntax/metadata.rs new file mode 100644 index 00000000..a3126e45 --- /dev/null +++ b/partiql-common/src/syntax/metadata.rs @@ -0,0 +1,5 @@ +use crate::node::NodeMap; +use crate::syntax::location::{BytePosition, Location}; + +/// Map of `T` to a [`Location>`] +pub type LocationMap = NodeMap>; diff --git a/partiql-common/src/syntax/mod.rs b/partiql-common/src/syntax/mod.rs new file mode 100644 index 00000000..38b632d0 --- /dev/null +++ b/partiql-common/src/syntax/mod.rs @@ -0,0 +1,3 @@ +pub mod line_offset_tracker; +pub mod location; +pub mod metadata; diff --git a/partiql-logical-planner/Cargo.toml b/partiql-logical-planner/Cargo.toml index 09cb7116..1b9de4c4 100644 --- a/partiql-logical-planner/Cargo.toml +++ b/partiql-logical-planner/Cargo.toml @@ -21,14 +21,15 @@ edition.workspace = true bench = false [dependencies] -partiql-value = { path = "../partiql-value", version = "0.10.*" } -partiql-extension-ion = {path = "../extension/partiql-extension-ion", version = "0.10.*" } -partiql-logical = { path = "../partiql-logical", version = "0.10.*" } partiql-ast = { path = "../partiql-ast", version = "0.10.*" } -partiql-parser = { path = "../partiql-parser", version = "0.10.*" } -partiql-catalog = { path = "../partiql-catalog", version = "0.10.*" } partiql-ast-passes = { path = "../partiql-ast-passes", version = "0.10.*" } +partiql-catalog = { path = "../partiql-catalog", version = "0.10.*" } +partiql-common = { path = "../partiql-common", version = "0.10.*" } +partiql-extension-ion = {path = "../extension/partiql-extension-ion", version = "0.10.*" } +partiql-parser = { path = "../partiql-parser", version = "0.10.*" } +partiql-logical = { path = "../partiql-logical", version = "0.10.*" } partiql-types = { path = "../partiql-types", version = "0.10.*" } +partiql-value = { path = "../partiql-value", version = "0.10.*" } ion-rs = "0.18" ordered-float = "3.*" diff --git a/partiql-logical-planner/src/lower.rs b/partiql-logical-planner/src/lower.rs index 3a7bd23c..adcb5867 100644 --- a/partiql-logical-planner/src/lower.rs +++ b/partiql-logical-planner/src/lower.rs @@ -7,7 +7,7 @@ use partiql_ast::ast::{ Assignment, Bag, BagOpExpr, BagOperator, Between, BinOp, BinOpKind, Call, CallAgg, CallArg, CallArgNamed, CaseSensitivity, CreateIndex, CreateTable, Ddl, DdlOp, Delete, Dml, DmlOp, DropIndex, DropTable, Exclusion, Expr, FromClause, FromLet, FromLetKind, GroupByExpr, GroupKey, - GroupingStrategy, Insert, InsertValue, Item, Join, JoinKind, JoinSpec, Like, List, Lit, NodeId, + GroupingStrategy, Insert, InsertValue, Item, Join, JoinKind, JoinSpec, Like, List, Lit, NullOrderingSpec, OnConflict, OrderByExpr, OrderingSpec, Path, PathStep, ProjectExpr, Projection, ProjectionKind, Query, QuerySet, Remove, SearchedCase, Select, Set, SetQuantifier, Sexp, SimpleCase, SortSpec, Struct, SymbolPrimitive, UniOp, UniOpKind, VarRef, @@ -34,6 +34,7 @@ use partiql_ast_passes::error::{AstTransformError, AstTransformationError}; use partiql_ast_passes::name_resolver::NameRef; use partiql_catalog::Catalog; +use partiql_common::node::NodeId; use partiql_extension_ion::decode::{IonDecoderBuilder, IonDecoderConfig}; use partiql_extension_ion::Encoding; use partiql_logical::AggFunc::{AggAny, AggAvg, AggCount, AggEvery, AggMax, AggMin, AggSum}; @@ -161,7 +162,7 @@ pub struct AstToLogical<'a> { sort_stack: Vec>, aggregate_exprs: Vec, - from_lets: HashSet, + from_lets: HashSet, projection_renames: Vec>>, diff --git a/partiql-parser/Cargo.toml b/partiql-parser/Cargo.toml index 89d059a7..fa666915 100644 --- a/partiql-parser/Cargo.toml +++ b/partiql-parser/Cargo.toml @@ -26,7 +26,7 @@ lalrpop = "0.20" [dependencies] partiql-ast = { path = "../partiql-ast", version = "0.10.*" } -partiql-source-map = { path = "../partiql-source-map", version = "0.10.*" } +partiql-common = { path = "../partiql-common", version = "0.10.*" } thiserror = "1.0" @@ -56,7 +56,7 @@ serde = [ "dep:serde", "rust_decimal/serde-with-str", "partiql-ast/serde", - "partiql-source-map/serde" + "partiql-common/serde" ] [[bench]] diff --git a/partiql-parser/src/error.rs b/partiql-parser/src/error.rs index 72582903..1cd048f2 100644 --- a/partiql-parser/src/error.rs +++ b/partiql-parser/src/error.rs @@ -5,7 +5,7 @@ use std::borrow::Cow; use std::fmt::{Debug, Display}; -use partiql_source_map::location::Located; +use partiql_common::syntax::location::Located; use thiserror::Error; #[cfg(feature = "serde")] @@ -102,7 +102,7 @@ where #[cfg(test)] mod tests { use super::*; - use partiql_source_map::location::{ByteOffset, BytePosition, LineAndColumn, ToLocated}; + use partiql_common::syntax::location::{ByteOffset, BytePosition, LineAndColumn, ToLocated}; use std::num::NonZeroUsize; #[test] diff --git a/partiql-parser/src/lexer.rs b/partiql-parser/src/lexer.rs index 8350d1f1..6e2fbe18 100644 --- a/partiql-parser/src/lexer.rs +++ b/partiql-parser/src/lexer.rs @@ -1,4 +1,4 @@ -use partiql_source_map::location::{ByteOffset, BytePosition, ToLocated}; +use partiql_common::syntax::location::{ByteOffset, BytePosition, ToLocated}; use std::borrow::Cow; use logos::{Logos, Span}; @@ -9,7 +9,7 @@ use std::fmt; use std::fmt::Formatter; use crate::error::{LexError, ParseError}; -use partiql_source_map::line_offset_tracker::LineOffsetTracker; +use partiql_common::syntax::line_offset_tracker::LineOffsetTracker; /// A 3-tuple of (start, `Tok`, end) denoting a token and it start and end offsets. pub type Spanned = (Loc, Tok, Loc); @@ -877,8 +877,8 @@ where #[cfg(test)] mod tests { use super::*; - use partiql_source_map::line_offset_tracker::{LineOffsetError, LineOffsetTracker}; - use partiql_source_map::location::{ + use partiql_common::syntax::line_offset_tracker::{LineOffsetError, LineOffsetTracker}; + use partiql_common::syntax::location::{ CharOffset, LineAndCharPosition, LineAndColumn, LineOffset, Located, Location, }; diff --git a/partiql-parser/src/lib.rs b/partiql-parser/src/lib.rs index 49c8f981..7e9ec266 100644 --- a/partiql-parser/src/lib.rs +++ b/partiql-parser/src/lib.rs @@ -30,9 +30,9 @@ mod token_parser; use parse::{parse_partiql, AstData, ErrorData}; use partiql_ast::ast; -use partiql_source_map::line_offset_tracker::LineOffsetTracker; -use partiql_source_map::location::BytePosition; -use partiql_source_map::metadata::LocationMap; +use partiql_common::syntax::line_offset_tracker::LineOffsetTracker; +use partiql_common::syntax::location::BytePosition; +use partiql_common::syntax::metadata::LocationMap; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; diff --git a/partiql-parser/src/parse/mod.rs b/partiql-parser/src/parse/mod.rs index be9339f2..4ba6a951 100644 --- a/partiql-parser/src/parse/mod.rs +++ b/partiql-parser/src/parse/mod.rs @@ -12,10 +12,10 @@ use crate::parse::parser_state::ParserState; use crate::preprocessor::{PreprocessingPartiqlLexer, BUILT_INS}; use lalrpop_util as lpop; use partiql_ast::ast; -use partiql_ast::builder::IdGenerator; -use partiql_source_map::line_offset_tracker::LineOffsetTracker; -use partiql_source_map::location::{ByteOffset, BytePosition, ToLocated}; -use partiql_source_map::metadata::LocationMap; +use partiql_common::node::NodeIdGenerator; +use partiql_common::syntax::line_offset_tracker::LineOffsetTracker; +use partiql_common::syntax::location::{ByteOffset, BytePosition, ToLocated}; +use partiql_common::syntax::metadata::LocationMap; #[allow(clippy::just_underscores_and_digits)] // LALRPOP generates a lot of names like this #[allow(clippy::all)] @@ -54,7 +54,7 @@ pub(crate) fn parse_partiql(s: &str) -> AstResult<'_> { parse_partiql_with_state(s, ParserState::default()) } -fn parse_partiql_with_state<'input, Id: IdGenerator>( +fn parse_partiql_with_state<'input, Id: NodeIdGenerator>( s: &'input str, mut state: ParserState<'input, Id>, ) -> AstResult<'input> { @@ -537,8 +537,7 @@ mod tests { mod set_ops { use super::*; - - use partiql_ast::builder::NullIdGenerator; + use partiql_common::node::NullIdGenerator; impl<'input> ParserState<'input, NullIdGenerator> { pub(crate) fn new_null_id() -> ParserState<'input, NullIdGenerator> { @@ -761,7 +760,7 @@ mod tests { mod errors { use super::*; use crate::error::{LexError, UnexpectedToken, UnexpectedTokenData}; - use partiql_source_map::location::{Located, Location}; + use partiql_common::syntax::location::{Located, Location}; use std::borrow::Cow; #[test] diff --git a/partiql-parser/src/parse/parse_util.rs b/partiql-parser/src/parse/parse_util.rs index 13261de8..2bb1da0f 100644 --- a/partiql-parser/src/parse/parse_util.rs +++ b/partiql-parser/src/parse/parse_util.rs @@ -2,8 +2,8 @@ use partiql_ast::ast; use crate::parse::parser_state::ParserState; use bitflags::bitflags; -use partiql_ast::builder::IdGenerator; -use partiql_source_map::location::ByteOffset; +use partiql_common::node::NodeIdGenerator; +use partiql_common::syntax::location::ByteOffset; bitflags! { /// Set of AST node attributes to use as synthesized attributes. @@ -110,7 +110,7 @@ pub(crate) fn strip_query_set( hi: ByteOffset, ) -> ast::AstNode where - Id: IdGenerator, + Id: NodeIdGenerator, { if let ast::AstNode { node: ast::QuerySet::Expr(q), diff --git a/partiql-parser/src/parse/parser_state.rs b/partiql-parser/src/parse/parser_state.rs index c6f019ab..7d37111c 100644 --- a/partiql-parser/src/parse/parser_state.rs +++ b/partiql-parser/src/parse/parser_state.rs @@ -7,10 +7,11 @@ use once_cell::sync::Lazy; use regex::Regex; use partiql_ast::ast::{AstNode, SymbolPrimitive}; -use partiql_ast::builder::{AutoNodeIdGenerator, IdGenerator, NodeBuilder}; +use partiql_ast::builder::AstNodeBuilder; +use partiql_common::node::{AutoNodeIdGenerator, NodeIdGenerator}; -use partiql_source_map::location::{ByteOffset, BytePosition, Location}; -use partiql_source_map::metadata::LocationMap; +use partiql_common::syntax::location::{ByteOffset, BytePosition, Location}; +use partiql_common::syntax::metadata::LocationMap; type ParseErrorRecovery<'input> = ErrorRecovery, ParseError<'input, BytePosition>>; @@ -19,9 +20,9 @@ type ParseErrors<'input> = Vec>; const INIT_LOCATIONS: usize = 100; /// State of the parsing during parse. -pub(crate) struct ParserState<'input, Id: IdGenerator> { +pub(crate) struct ParserState<'input, Id: NodeIdGenerator> { /// Generator for 'fresh' [`NodeId`]s - pub node_builder: NodeBuilder, + pub node_builder: AstNodeBuilder, /// Maps AST [`NodeId`]s to the location in the source from which each was derived. pub locations: LocationMap, /// Any errors accumulated during parse. @@ -45,11 +46,11 @@ static KNOWN_AGGREGATE_PATTERN: Lazy = Lazy::new(|| Regex::new(KNOWN_AGGR impl<'input, I> ParserState<'input, I> where - I: IdGenerator, + I: NodeIdGenerator, { pub fn with_id_gen(id_gen: I) -> Self { ParserState { - node_builder: NodeBuilder::new(id_gen), + node_builder: AstNodeBuilder::new(id_gen), locations: LocationMap::with_capacity(INIT_LOCATIONS), errors: ParseErrors::default(), aggregates_pat: &KNOWN_AGGREGATE_PATTERN, @@ -57,7 +58,7 @@ where } } -impl<'input, Id: IdGenerator> ParserState<'input, Id> { +impl<'input, IdGen: NodeIdGenerator> ParserState<'input, IdGen> { /// Create a new [`AstNode`] from the inner data which it is to hold and a source location. pub fn create_node(&mut self, node: T, location: IntoLoc) -> AstNode where diff --git a/partiql-parser/src/parse/partiql.lalrpop b/partiql-parser/src/parse/partiql.lalrpop index 4e28222e..8aa04b52 100644 --- a/partiql-parser/src/parse/partiql.lalrpop +++ b/partiql-parser/src/parse/partiql.lalrpop @@ -7,13 +7,13 @@ use std::str::FromStr; use partiql_ast::ast; -use partiql_source_map::location::{ByteOffset, BytePosition, Location, ToLocated}; +use partiql_common::syntax::location::{ByteOffset, BytePosition, Location, ToLocated}; use crate::parse::parse_util::{strip_expr, strip_query, strip_query_set, CallSite, Attrs, Synth}; use crate::parse::parser_state::ParserState; -use partiql_ast::builder::IdGenerator; +use partiql_common::node::NodeIdGenerator; -grammar<'input, 'state, Id>(input: &'input str, state: &'state mut ParserState<'input, Id>) where Id: IdGenerator; +grammar<'input, 'state, Id>(input: &'input str, state: &'state mut ParserState<'input, Id>) where Id: NodeIdGenerator; pub(crate) TopLevelQuery: ast::AstNode = { diff --git a/partiql-parser/src/preprocessor.rs b/partiql-parser/src/preprocessor.rs index c17e3bc9..d89ea621 100644 --- a/partiql-parser/src/preprocessor.rs +++ b/partiql-parser/src/preprocessor.rs @@ -1,4 +1,4 @@ -use partiql_source_map::location::ByteOffset; +use partiql_common::syntax::location::ByteOffset; use regex::{Regex, RegexSet, RegexSetBuilder}; use std::collections::VecDeque; @@ -11,7 +11,7 @@ use crate::lexer::{InternalLexResult, LexResult, PartiqlLexer, Spanned, Token}; use crate::token_parser::{BufferedToken, TokenParser}; use once_cell::sync::Lazy; -use partiql_source_map::line_offset_tracker::LineOffsetTracker; +use partiql_common::syntax::line_offset_tracker::LineOffsetTracker; pub(crate) static BUILT_INS: Lazy> = Lazy::new(built_ins); @@ -607,7 +607,7 @@ where #[cfg(test)] mod tests { use super::*; - use partiql_source_map::line_offset_tracker::LineOffsetTracker; + use partiql_common::syntax::line_offset_tracker::LineOffsetTracker; use crate::ParseError; diff --git a/partiql-parser/src/token_parser.rs b/partiql-parser/src/token_parser.rs index 33230288..360c58a3 100644 --- a/partiql-parser/src/token_parser.rs +++ b/partiql-parser/src/token_parser.rs @@ -1,6 +1,6 @@ use crate::error::LexError; use crate::lexer::{PartiqlLexer, Spanned, Token}; -use partiql_source_map::location::ByteOffset; +use partiql_common::syntax::location::ByteOffset; use std::collections::VecDeque; /// A [`Token`] and its associated `&str` slice; buffered from the lexer for parsing/matching. diff --git a/partiql-source-map/src/lib.rs b/partiql-source-map/src/lib.rs deleted file mode 100644 index 5fc7ee4b..00000000 --- a/partiql-source-map/src/lib.rs +++ /dev/null @@ -1,8 +0,0 @@ -#![deny(rust_2018_idioms)] -#![deny(clippy::all)] - -//! Source offset & position types and mapping-related helpers. - -pub mod line_offset_tracker; -pub mod location; -pub mod metadata; diff --git a/partiql-source-map/src/metadata.rs b/partiql-source-map/src/metadata.rs deleted file mode 100644 index 51177662..00000000 --- a/partiql-source-map/src/metadata.rs +++ /dev/null @@ -1,5 +0,0 @@ -use crate::location::{BytePosition, Location}; -use partiql_ast::ast::AstTypeMap; - -/// Map of `T` to a [`Location>`] -pub type LocationMap = AstTypeMap>;