Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge language, parse_error, parse_output namespaces into the parser namespace #1116

Merged
merged 1 commit into from
Oct 1, 2024
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
5 changes: 5 additions & 0 deletions .changeset/many-forks-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nomicfoundation/slang": minor
---

merge `language`, `parse_error`, `parse_output` namespaces into the `parser` namespace.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use clap::Parser;
use semver::Version;

use crate::diagnostic;
use crate::language::Language;
use crate::parser::Language;

#[derive(Parser, Debug)]
pub struct ParseCommand {
Expand Down
6 changes: 0 additions & 6 deletions crates/codegen/runtime/cargo/src/runtime/language/mod.rs

This file was deleted.

4 changes: 1 addition & 3 deletions crates/codegen/runtime/cargo/src/runtime/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
pub mod cst;
pub mod diagnostic;
pub mod language;
pub mod parse_error;
pub mod parse_output;
pub mod parser;

#[cfg(feature = "__experimental_bindings_api")]
pub mod bindings;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
pub mod cst;
pub mod diagnostic;
pub mod parse_error;
pub mod parse_output;
pub mod parser;

#[path = "generated/ast_selectors.rs"]
pub mod ast_selectors;

type RustCursor = crate::cst::Cursor;
type RustEdge = crate::cst::Edge;
type RustNode = crate::cst::Node;
type RustParseError = crate::parse_error::ParseError;
type RustParseOutput = crate::parse_output::ParseOutput;
type RustParseError = crate::parser::ParseError;
type RustParseOutput = crate::parser::ParseOutput;
type RustQuery = crate::cst::Query;
type RustQueryMatch = crate::cst::QueryMatch;
type RustQueryMatchIterator = crate::cst::QueryMatchIterator;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod parse_error;
mod parse_output;

pub use parse_error::ParseError;
pub use parse_output::ParseOutput;
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use napi_derive::napi;
use crate::expose_diagnostic_trait_interface;
use crate::napi_interface::RustParseError;

#[napi(namespace = "parse_error")]
#[napi(namespace = "parser")]
#[derive(PartialEq, Clone)]
pub struct ParseError(RustParseError);

Expand All @@ -16,4 +16,4 @@ impl From<RustParseError> for ParseError {
}
}

expose_diagnostic_trait_interface!("parse_error", ParseError);
expose_diagnostic_trait_interface!("parser", ParseError);
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use napi::Either;
use napi_derive::napi;

use crate::napi_interface::cst::{Cursor, NAPINodeExtensions, NonterminalNode, TerminalNode};
use crate::napi_interface::{parse_error, RustParseOutput};
use crate::napi_interface::parser::ParseError;
use crate::napi_interface::RustParseOutput;

#[napi(namespace = "parse_output")]
#[napi(namespace = "parser")]
pub struct ParseOutput(RustParseOutput);

impl From<RustParseOutput> for ParseOutput {
Expand All @@ -13,15 +14,15 @@ impl From<RustParseOutput> for ParseOutput {
}
}

#[napi(namespace = "parse_output")]
#[napi(namespace = "parser")]
impl ParseOutput {
#[napi(ts_return_type = "cst.Node", catch_unwind)]
pub fn tree(&self) -> Either<NonterminalNode, TerminalNode> {
self.0.tree().into_js_either_node()
}

#[napi(ts_return_type = "Array<parse_error.ParseError>", catch_unwind)]
pub fn errors(&self) -> Vec<parse_error::ParseError> {
#[napi(ts_return_type = "Array<parser.ParseError>", catch_unwind)]
pub fn errors(&self) -> Vec<ParseError> {
self.0.errors().iter().map(|x| x.clone().into()).collect()
}

Expand Down

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,22 +16,22 @@ use crate::cst;
use crate::cst::{
EdgeLabel, IsLexicalContext, LexicalContext, LexicalContextType, NonterminalKind, TerminalKind,
};
use crate::language::lexer::{KeywordScan, Lexer, ScannedTerminal};
use crate::language::parser_support::{
use crate::parser::lexer::{KeywordScan, Lexer, ScannedTerminal};
use crate::parser::parser_support::{
ChoiceHelper, OneOrMoreHelper, OptionalHelper, ParserContext, ParserFunction, ParserResult,
PrecedenceHelper, SeparatedHelper, SequenceHelper, TerminalAcceptanceThreshold,
ZeroOrMoreHelper,
};
use crate::language::scanner_macros::{
use crate::parser::scanner_macros::{
scan_char_range, scan_chars, scan_choice, scan_keyword_choice, scan_none_of,
scan_not_followed_by, scan_one_or_more, scan_optional, scan_sequence, scan_zero_or_more,
};
#[cfg(feature = "__private_napi_interfaces")]
use crate::napi_interface::parse_output::ParseOutput as NAPIParseOutput;
use crate::parse_output::ParseOutput;
use crate::napi_interface::parser::ParseOutput as NAPIParseOutput;
use crate::parser::ParseOutput;

#[derive(Debug)]
#[cfg_attr(feature = "__private_napi_interfaces", napi(namespace = "language"))]
#[cfg_attr(feature = "__private_napi_interfaces", napi(namespace = "parser"))]
pub struct Language {
{%- if not rendering_in_stubs -%}
{%- for version in model.breaking_language_versions -%}
Expand All @@ -46,7 +46,7 @@ pub struct Language {
}

#[derive(thiserror::Error, Debug)]
pub enum Error {
pub enum LanguageInitializationError {
#[error("Unsupported language version '{0}'.")]
UnsupportedLanguageVersion(Version),

Expand All @@ -56,8 +56,8 @@ pub enum Error {
}

#[cfg(feature = "__private_napi_interfaces")]
impl From<Error> for napi::Error {
fn from(value: Error) -> Self {
impl From<LanguageInitializationError> for napi::Error {
fn from(value: LanguageInitializationError) -> Self {
napi::Error::from_reason(value.to_string())
}
}
Expand All @@ -73,7 +73,7 @@ impl Language {

pub const ROOT_KIND: NonterminalKind = NonterminalKind::{{ model.kinds.root_kind }};

pub fn new(version: Version) -> std::result::Result<Self, Error> {
pub fn new(version: Version) -> std::result::Result<Self, LanguageInitializationError> {
if Self::SUPPORTED_VERSIONS.binary_search(&version).is_ok() {
Ok(Self {
{%- if not rendering_in_stubs -%}
Expand All @@ -85,7 +85,7 @@ impl Language {
version,
})
} else {
Err(Error::UnsupportedLanguageVersion(version))
Err(LanguageInitializationError::UnsupportedLanguageVersion(version))
}
}

Expand Down Expand Up @@ -282,12 +282,12 @@ impl Lexer for Language {
#[cfg(feature = "__private_napi_interfaces")]
// NAPI-exposed functions have to accept owned values.
#[allow(clippy::needless_pass_by_value)]
#[napi(namespace = "language")]
#[napi(namespace = "parser")]
impl Language {

#[napi(constructor, catch_unwind)]
pub fn new_napi(version: String) -> std::result::Result<Self, napi::Error> {
let version = Version::parse(&version).map_err(|_| Error::InvalidSemanticVersion(version))?;
let version = Version::parse(&version).map_err(|_| LanguageInitializationError::InvalidSemanticVersion(version))?;
Self::new(version).map_err(|e| e.into())
}

Expand All @@ -306,7 +306,7 @@ impl Language {
Self::ROOT_KIND
}

#[napi(js_name = "parse", ts_return_type = "parse_output.ParseOutput", catch_unwind)]
#[napi(js_name = "parse", ts_return_type = "parser.ParseOutput", catch_unwind)]
pub fn parse_napi(
&self,
#[napi(ts_arg_type = "cst.NonterminalKind")] kind: NonterminalKind,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::cst::{Edge, IsLexicalContext, Node, TerminalKind};
use crate::language::parser_support::{ParserContext, ParserResult};
use crate::parser::parser_support::{ParserContext, ParserResult};

/// Whether a keyword has been scanned and if so, whether it is reserved (unusable as an identifier)
/// or not.
Expand Down
11 changes: 11 additions & 0 deletions crates/codegen/runtime/cargo/src/runtime/parser/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#[path = "generated/language.rs"]
mod language;
mod lexer;
mod parse_error;
mod parse_output;
mod parser_support;
mod scanner_macros;

pub use language::{Language, LanguageInitializationError};
pub use parse_error::ParseError;
pub use parse_output::ParseOutput;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::cst::{Cursor, Node, TextIndex};
use crate::parse_error::ParseError;
use crate::parser::ParseError;

#[derive(Debug, PartialEq)]
pub struct ParseOutput {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use std::mem;
use std::ops::ControlFlow;

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::parser::parser_support::context::{Marker, ParserContext};
use crate::parser::parser_support::ParserResult;
use crate::parser::ParseError;

/// 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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::mem;
use std::ops::Range;

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

#[derive(Debug)]
pub struct ParserContext<'s> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::language::parser_support::parser_result::ParserResult;
use crate::parser::parser_support::parser_result::ParserResult;

pub struct OptionalHelper;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use std::rc::Rc;

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::{
use crate::parser::lexer::Lexer;
use crate::parser::parser_support::context::ParserContext;
use crate::parser::parser_support::parser_result::{
IncompleteMatch, Match, ParserResult, SkippedUntil,
};
use crate::parse_error::ParseError;
use crate::parse_output::ParseOutput;
use crate::parser::{ParseError, ParseOutput};

pub trait ParserFunction<L>
where
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::cst::{Edge, EdgeLabel, Node, NonterminalKind};
use crate::language::parser_support::parser_result::PrattElement::{
use crate::parser::parser_support::parser_result::PrattElement::{
self, Binary, Expression, Postfix, Prefix,
};
use crate::language::parser_support::parser_result::{ParserResult, PrattOperatorMatch};
use crate::parser::parser_support::parser_result::{ParserResult, PrattOperatorMatch};

pub struct PrecedenceHelper;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::cst::{Edge, IsLexicalContext, TerminalKind, TextRange, TextRangeExtensions};
use crate::language::lexer::{Lexer, ScannedTerminal};
use crate::language::parser_support::context::ParserContext;
use crate::language::parser_support::parser_result::SkippedUntil;
use crate::language::parser_support::ParserResult;
use crate::parse_error::ParseError;
use crate::parser::lexer::{Lexer, ScannedTerminal};
use crate::parser::parser_support::context::ParserContext;
use crate::parser::parser_support::parser_result::SkippedUntil;
use crate::parser::parser_support::ParserResult;
use crate::parser::ParseError;

/// How many terminals have to be matched to trigger the error recovery.
/// For ambiguous syntaxes this needs to be set to at least N, where N
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::language::parser_support::context::ParserContext;
use crate::language::parser_support::parser_result::{
use crate::parser::parser_support::context::ParserContext;
use crate::parser::parser_support::parser_result::{
IncompleteMatch, NoMatch, ParserResult, PrattElement,
};

Expand Down
Loading