Skip to content

Commit

Permalink
merge cursor, kinds, query, and text_index namespaces into th…
Browse files Browse the repository at this point in the history
…e `cst` namespace (NomicFoundation#1115)
  • Loading branch information
OmarTawfik authored Oct 1, 2024
1 parent 7989648 commit 96df645
Show file tree
Hide file tree
Showing 230 changed files with 4,849 additions and 5,415 deletions.
5 changes: 5 additions & 0 deletions .changeset/silver-cherries-punch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nomicfoundation/slang": minor
---

merge `cursor`, `kinds`, `query`, and `text_index` namespaces into the `cst` namespace.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use napi_derive::napi;
strum_macros::EnumString,
strum_macros::IntoStaticStr,
)]
#[cfg_attr(feature = "__private_napi_interfaces", /* derives `Clone` and `Copy` */ napi(string_enum, namespace = "kinds"))]
#[cfg_attr(feature = "__private_napi_interfaces", /* derives `Clone` and `Copy` */ napi(string_enum, namespace = "cst"))]
#[cfg_attr(not(feature = "__private_napi_interfaces"), derive(Clone, Copy))]
pub enum NonterminalKind {
{%- if rendering_in_stubs -%}
Expand All @@ -31,7 +31,7 @@ pub enum NonterminalKind {
{%- endif -%}
}

impl metaslang_cst::NonterminalKind for NonterminalKind {}
impl crate::cst::NonterminalKindExtensions for NonterminalKind {}

// This needs to stay in sync with the wit-bindgen output
{% if model.kinds.labels|length <= 256 %} #[repr(u8)] {% else %} #[repr(u16)] {% endif %}
Expand All @@ -49,7 +49,7 @@ impl metaslang_cst::NonterminalKind for NonterminalKind {}
strum_macros::IntoStaticStr,
)]
#[strum(serialize_all = "snake_case")]
#[cfg_attr(feature = "__private_napi_interfaces", /* derives `Clone` and `Copy` */ napi(string_enum, namespace = "kinds"))]
#[cfg_attr(feature = "__private_napi_interfaces", /* derives `Clone` and `Copy` */ napi(string_enum, namespace = "cst"))]
#[cfg_attr(not(feature = "__private_napi_interfaces"), derive(Clone, Copy))]
pub enum EdgeLabel {
// Built-in:
Expand All @@ -69,7 +69,7 @@ pub enum EdgeLabel {
{%- endif -%}
}

impl metaslang_cst::EdgeLabel for EdgeLabel {}
impl crate::cst::EdgeLabelExtensions for EdgeLabel {}

// This needs to stay in sync with the wit-bindgen output
{% if model.kinds.terminal_kinds|length <= 256 %} #[repr(u8)] {% else %} #[repr(u16)] {% endif %}
Expand All @@ -86,8 +86,9 @@ impl metaslang_cst::EdgeLabel for EdgeLabel {}
strum_macros::EnumString,
strum_macros::IntoStaticStr,
)]
#[cfg_attr(feature = "__private_napi_interfaces", /* derives `Clone` and `Copy` */ napi(string_enum, namespace = "kinds"))]
#[cfg_attr(feature = "__private_napi_interfaces", /* derives `Clone` and `Copy` */ napi(string_enum, namespace = "cst"))]
#[cfg_attr(not(feature = "__private_napi_interfaces"), derive(Clone, Copy))]
#[allow(clippy::upper_case_acronyms)]
pub enum TerminalKind {
// Built-in:
UNRECOGNIZED,
Expand All @@ -106,7 +107,7 @@ pub enum TerminalKind {
{%- endif -%}
}

impl metaslang_cst::TerminalKind for TerminalKind {
impl crate::cst::TerminalKindExtensions for TerminalKind {
fn is_trivia(&self) -> bool {
{%- if rendering_in_stubs -%}
false
Expand Down
34 changes: 25 additions & 9 deletions crates/codegen/runtime/cargo/src/runtime/cst/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
use metaslang_cst::cst;
#[path = "generated/kinds.rs"]
mod kinds;

pub use kinds::{EdgeLabel, NonterminalKind, TerminalKind};
pub(crate) use kinds::{IsLexicalContext, LexicalContext, LexicalContextType};
pub use metaslang_cst::kinds::{
EdgeLabelExtensions, NonterminalKindExtensions, TerminalKindExtensions,
};

// These derives are because default #[derive(...)] on a generic type implements only the trait
// with default bounds also implied for the generic types as well, i.e.
Expand All @@ -14,13 +21,22 @@ use metaslang_cst::cst;
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize)]
pub enum KindTypes {}

impl metaslang_cst::KindTypes for KindTypes {
type NonterminalKind = crate::kinds::NonterminalKind;
type TerminalKind = crate::kinds::TerminalKind;
type EdgeLabel = crate::kinds::EdgeLabel;
impl metaslang_cst::kinds::KindTypes for KindTypes {
type NonterminalKind = NonterminalKind;
type TerminalKind = TerminalKind;
type EdgeLabel = EdgeLabel;
}

pub type Node = cst::Node<KindTypes>;
pub type NonterminalNode = cst::NonterminalNode<KindTypes>;
pub type TerminalNode = cst::TerminalNode<KindTypes>;
pub type Edge = cst::Edge<KindTypes>;
pub type Node = metaslang_cst::nodes::Node<KindTypes>;
pub type NonterminalNode = metaslang_cst::nodes::NonterminalNode<KindTypes>;
pub type TerminalNode = metaslang_cst::nodes::TerminalNode<KindTypes>;
pub type Edge = metaslang_cst::nodes::Edge<KindTypes>;

pub type Cursor = metaslang_cst::cursor::Cursor<KindTypes>;
pub type CursorWithEdges = metaslang_cst::cursor::CursorWithEdges<KindTypes>;

pub type Query = metaslang_cst::query::Query<KindTypes>;
pub type QueryMatch = metaslang_cst::query::QueryMatch<KindTypes>;
pub type QueryMatchIterator = metaslang_cst::query::QueryMatchIterator<KindTypes>;
pub use metaslang_cst::query::QueryError;
pub use metaslang_cst::text_index::{TextIndex, TextRange, TextRangeExtensions};
6 changes: 0 additions & 6 deletions crates/codegen/runtime/cargo/src/runtime/cursor/mod.rs

This file was deleted.

2 changes: 1 addition & 1 deletion crates/codegen/runtime/cargo/src/runtime/diagnostic/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::text_index::TextRange;
use crate::cst::TextRange;

/// The severity of a diagnostic.
///
Expand Down
3 changes: 0 additions & 3 deletions crates/codegen/runtime/cargo/src/runtime/kinds/mod.rs

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::cst::{self, Edge};
use crate::kinds::{IsLexicalContext, TerminalKind};
use crate::cst::{Edge, IsLexicalContext, Node, TerminalKind};
use crate::language::parser_support::{ParserContext, ParserResult};

/// Whether a keyword has been scanned and if so, whether it is reserved (unusable as an identifier)
Expand Down Expand Up @@ -100,7 +99,7 @@ pub(crate) trait Lexer {
let end = input.position();

ParserResult::r#match(
vec![Edge::anonymous(cst::Node::terminal(
vec![Edge::anonymous(Node::terminal(
kind,
input.content(start.utf8..end.utf8),
))],
Expand Down Expand Up @@ -133,7 +132,7 @@ pub(crate) trait Lexer {
return ParserResult::no_match(vec![kind]);
}
let end = input.position();
children.push(Edge::anonymous(cst::Node::terminal(
children.push(Edge::anonymous(Node::terminal(
kind,
input.content(start.utf8..end.utf8),
)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use semver::Version;
use napi_derive::napi;

use crate::cst;
use crate::kinds::{
use crate::cst::{
EdgeLabel, IsLexicalContext, LexicalContext, LexicalContextType, NonterminalKind, TerminalKind,
};
use crate::language::lexer::{KeywordScan, Lexer, ScannedTerminal};
Expand Down Expand Up @@ -301,15 +301,15 @@ impl Language {
return Self::SUPPORTED_VERSIONS.iter().map(|v| v.to_string()).collect();
}

#[napi(js_name = "rootKind", ts_return_type = "kinds.NonterminalKind", catch_unwind)]
#[napi(js_name = "rootKind", ts_return_type = "cst.NonterminalKind", catch_unwind)]
pub fn root_kind_napi() -> NonterminalKind {
Self::ROOT_KIND
}

#[napi(js_name = "parse", ts_return_type = "parse_output.ParseOutput", catch_unwind)]
pub fn parse_napi(
&self,
#[napi(ts_arg_type = "kinds.NonterminalKind")] kind: NonterminalKind,
#[napi(ts_arg_type = "cst.NonterminalKind")] kind: NonterminalKind,
input: String
) -> NAPIParseOutput {
self.parse(kind, input.as_str()).into()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
use std::mem;
use std::ops::ControlFlow;

use metaslang_cst::TerminalKind as _;

use crate::cst;
use crate::cst::{Node, TerminalKindExtensions, TextIndex};
use crate::language::parser_support::context::{Marker, ParserContext};
use crate::language::parser_support::ParserResult;
use crate::parse_error::ParseError;
use crate::text_index::TextIndex;

/// Starting from a given position in the input, this helper will try to pick (and remember) a best match. Settles on
/// a first full match if possible, otherwise on the best incomplete match.
Expand Down Expand Up @@ -140,7 +137,7 @@ pub fn total_not_skipped_span(result: &ParserResult) -> usize {
.iter()
.flat_map(|child| child.cursor_with_offset(TextIndex::ZERO))
.filter_map(|node| match node {
cst::Node::Terminal(terminal) if terminal.kind.is_valid() => Some(terminal.text.len()),
Node::Terminal(terminal) if terminal.kind.is_valid() => Some(terminal.text.len()),
_ => None,
})
.sum()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::mem;
use std::ops::Range;

use crate::kinds::TerminalKind;
use crate::cst::{TerminalKind, TextIndex};
use crate::parse_error::ParseError;
use crate::text_index::TextIndex;

#[derive(Debug)]
pub struct ParserContext<'s> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
use std::rc::Rc;

use metaslang_cst::TerminalKind as _;

use crate::cst::{self, Edge};
use crate::kinds::TerminalKind;
use crate::cst::{Edge, Node, TerminalKind, TerminalKindExtensions, TextIndex};
use crate::language::lexer::Lexer;
use crate::language::parser_support::context::ParserContext;
use crate::language::parser_support::parser_result::{
IncompleteMatch, Match, ParserResult, SkippedUntil,
};
use crate::parse_error::ParseError;
use crate::parse_output::ParseOutput;
use crate::text_index::TextIndex;

pub trait ParserFunction<L>
where
Expand Down Expand Up @@ -45,13 +41,13 @@ where
_ => None,
};

if let (cst::Node::Nonterminal(nonterminal), Some(eof_trivia)) =
if let (Node::Nonterminal(nonterminal), Some(eof_trivia)) =
(&mut topmost.node, eof_trivia)
{
let mut new_children = nonterminal.children.clone();
new_children.extend(eof_trivia);

topmost.node = cst::Node::nonterminal(nonterminal.kind, new_children);
topmost.node = Node::nonterminal(nonterminal.kind, new_children);
}
}

Expand All @@ -69,7 +65,7 @@ where
};

ParseOutput {
parse_tree: cst::Node::terminal(kind, input.to_string()),
parse_tree: Node::terminal(kind, input.to_string()),
errors: vec![ParseError::new(
TextIndex::ZERO..input.into(),
no_match.expected_terminals,
Expand All @@ -96,7 +92,7 @@ where
};

let topmost_node = match &nodes[..] {
[Edge { node: cst::Node::Nonterminal(nonterminal), ..} ] => Rc::clone(nonterminal),
[Edge { node: Node::Nonterminal(nonterminal), ..} ] => Rc::clone(nonterminal),
[_] => unreachable!(
"(Incomplete)Match at the top level of a parser is not a Nonterminal node"
),
Expand All @@ -121,19 +117,19 @@ where
} else {
TerminalKind::UNRECOGNIZED
};
let skipped_node = cst::Node::terminal(kind, input[start.utf8..].to_string());
let skipped_node = Node::terminal(kind, input[start.utf8..].to_string());
let mut new_children = topmost_node.children.clone();
new_children.push(Edge::anonymous(skipped_node));

let mut errors = stream.into_errors();
errors.push(ParseError::new(start..input.into(), expected_terminals));

ParseOutput {
parse_tree: cst::Node::nonterminal(topmost_node.kind, new_children),
parse_tree: Node::nonterminal(topmost_node.kind, new_children),
errors,
}
} else {
let parse_tree = cst::Node::Nonterminal(topmost_node);
let parse_tree = Node::Nonterminal(topmost_node);
let errors = stream.into_errors();

// Sanity check: Make sure that succesful parse is equivalent to not having any invalid nodes
Expand Down
Loading

0 comments on commit 96df645

Please sign in to comment.