Skip to content

Commit 662a672

Browse files
authored
Hide Lexer-related facilities from the public API (#728)
Ticks a box in #640
1 parent 72b4947 commit 662a672

41 files changed

Lines changed: 203 additions & 160 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.changeset/green-pets-invent.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@nomicfoundation/slang": minor
3+
---
4+
5+
Remove Language#scan API; use the parser API instead

crates/codegen/parser/runtime/src/kinds.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use napi_derive::napi;
1616
#[cfg_attr(not(feature = "slang_napi_interfaces"), derive(Clone, Copy))]
1717
pub enum TokenKind {
1818
SKIPPED,
19-
XXX,
19+
// Expanded by the template engine
2020
}
2121

2222
#[derive(
@@ -35,7 +35,7 @@ pub enum TokenKind {
3535
pub enum RuleKind {
3636
LeadingTrivia,
3737
TrailingTrivia,
38-
XXX,
38+
// Expanded by the template engine
3939
}
4040

4141
impl RuleKind {
@@ -71,18 +71,16 @@ pub enum FieldName {
7171
}
7272

7373
/// The lexical context of the scanner.
74-
#[derive(strum_macros::FromRepr)]
75-
#[cfg_attr(feature = "slang_napi_interfaces", /* derives `Clone` and `Copy` */ napi(string_enum, namespace = "language"))]
76-
#[cfg_attr(not(feature = "slang_napi_interfaces"), derive(Clone, Copy))]
77-
pub enum LexicalContext {
78-
XXX,
74+
#[derive(strum_macros::FromRepr, Clone, Copy)]
75+
pub(crate) enum LexicalContext {
76+
// Expanded by the template engine
7977
}
8078

8179
/// Marker trait for type-level [`LexicalContext`] variants.
82-
pub trait IsLexicalContext {
80+
pub(crate) trait IsLexicalContext {
8381
/// Returns a run-time [`LexicalContext`] value.
8482
fn value() -> LexicalContext;
8583
}
8684

8785
#[allow(non_snake_case)]
88-
pub mod LexicalContextType {}
86+
pub(crate) mod LexicalContextType {}

crates/codegen/parser/runtime/src/lexer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::cst::{self, NamedNode};
22
use crate::kinds::{IsLexicalContext, TokenKind};
33
use crate::support::{ParserContext, ParserResult};
44

5-
pub trait Lexer {
5+
pub(crate) trait Lexer {
66
// Generated by the templating engine
77
#[doc(hidden)]
88
fn next_token<LexCtx: IsLexicalContext>(

crates/codegen/parser/runtime/src/support/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ pub use optional_helper::OptionalHelper;
1818
pub use parser_function::ParserFunction;
1919
pub use parser_result::ParserResult;
2020
pub use precedence_helper::PrecedenceHelper;
21-
pub use recovery::RecoverFromNoMatch;
21+
#[allow(unused_imports)] // Used when copied to shipped crate
22+
pub(crate) use recovery::RecoverFromNoMatch;
2223
pub use repetition_helper::{OneOrMoreHelper, ZeroOrMoreHelper};
2324
pub use separated_helper::SeparatedHelper;
2425
pub use sequence_helper::SequenceHelper;

crates/codegen/parser/runtime/src/support/recovery.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::text_index::{TextRange, TextRangeExtensions as _};
99

1010
/// An explicit parameter for the [`ParserResult::recover_until_with_nested_delims`] method.
1111
#[derive(Clone, Copy)]
12-
pub enum RecoverFromNoMatch {
12+
pub(crate) enum RecoverFromNoMatch {
1313
Yes,
1414
No,
1515
}
@@ -41,7 +41,7 @@ impl ParserResult {
4141
/// Respects nested delimiters, i.e. the `expected` token is only accepted if it's not nested inside.
4242
/// Does not consume the `expected` token.
4343
#[must_use]
44-
pub fn recover_until_with_nested_delims<L: Lexer, LexCtx: IsLexicalContext>(
44+
pub(crate) fn recover_until_with_nested_delims<L: Lexer, LexCtx: IsLexicalContext>(
4545
self,
4646
input: &mut ParserContext<'_>,
4747
lexer: &L,
@@ -117,7 +117,7 @@ impl ParserResult {
117117
/// Does not consume the `expected` token.
118118
///
119119
/// Returns the found token and the range of skipped tokens on success.
120-
pub fn skip_until_with_nested_delims<L: Lexer, LexCtx: IsLexicalContext>(
120+
pub(crate) fn skip_until_with_nested_delims<L: Lexer, LexCtx: IsLexicalContext>(
121121
input: &mut ParserContext<'_>,
122122
lexer: &L,
123123
until: TokenKind,

crates/codegen/parser/runtime/src/support/separated_helper.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::text_index::TextRangeExtensions;
1010
pub struct SeparatedHelper;
1111

1212
impl SeparatedHelper {
13-
pub fn run<L: Lexer, LexCtx: IsLexicalContext>(
13+
pub(crate) fn run<L: Lexer, LexCtx: IsLexicalContext>(
1414
input: &mut ParserContext<'_>,
1515
lexer: &L,
1616
body_parser: impl Fn(&mut ParserContext<'_>) -> ParserResult,

crates/codegen/parser/runtime/src/templates/kinds.rs.jinja2

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,24 +83,22 @@ pub enum TokenKind {
8383
{%- endfor -%}
8484
}
8585

86-
#[derive(strum_macros::FromRepr)]
8786
/// The lexical context of the scanner.
88-
#[cfg_attr(feature = "slang_napi_interfaces", /* derives `Clone` and `Copy` */ napi(string_enum, namespace = "language"))]
89-
#[cfg_attr(not(feature = "slang_napi_interfaces"), derive(Clone, Copy))]
90-
pub enum LexicalContext {
87+
#[derive(strum_macros::FromRepr, Clone, Copy)]
88+
pub(crate) enum LexicalContext {
9189
{%- for context in code.scanner_contexts %}
9290
{{ context.name }},
9391
{%- endfor %}
9492
}
9593

9694
/// Marker trait for type-level [`LexicalContext`] variants.
97-
pub trait IsLexicalContext {
95+
pub(crate) trait IsLexicalContext {
9896
/// Returns a run-time [`LexicalContext`] value.
9997
fn value() -> LexicalContext;
10098
}
10199

102100
#[allow(non_snake_case)]
103-
pub mod LexicalContextType {
101+
pub(crate) mod LexicalContextType {
104102
use super::{IsLexicalContext, LexicalContext};
105103

106104
{%- for context in code.scanner_contexts %}

crates/codegen/parser/runtime/src/templates/language.rs.jinja2

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ use semver::Version;
1212
use napi_derive::napi;
1313

1414
use crate::cst;
15-
pub use crate::kinds::LexicalContext;
16-
use crate::kinds::{FieldName, IsLexicalContext, LexicalContextType, RuleKind, TokenKind};
15+
use crate::kinds::{
16+
FieldName, IsLexicalContext, LexicalContext, LexicalContextType, RuleKind, TokenKind,
17+
};
1718
use crate::lexer::Lexer;
1819
#[cfg(feature = "slang_napi_interfaces")]
1920
use crate::napi::napi_parse_output::ParseOutput as NAPIParseOutput;
@@ -91,16 +92,6 @@ impl Language {
9192
fn {{ function.0 | snake_case }}(&self, input: &mut ParserContext<'_>) -> bool { {{ function.1 }} }
9293
{% endfor %}
9394

94-
pub fn scan(&self, lexical_context: LexicalContext, input: &str) -> Option<TokenKind> {
95-
let mut input = ParserContext::new(input);
96-
match lexical_context {
97-
{%- for lexical_context in code.scanner_contexts -%}
98-
LexicalContext::{{ lexical_context.name }} =>
99-
Lexer::next_token::<LexicalContextType::{{ lexical_context.name }}>(self, &mut input),
100-
{%- endfor -%}
101-
}
102-
}
103-
10495
pub fn parse(&self, kind: RuleKind, input: &str) -> ParseOutput {
10596
match kind {
10697
{%- for function in code.parser_functions -%}
@@ -214,11 +205,6 @@ impl Language {
214205
return Self::SUPPORTED_VERSIONS.iter().map(|v| v.to_string()).collect();
215206
}
216207

217-
#[napi(js_name = "scan", ts_return_type = "kinds.TokenKind | null", catch_unwind)]
218-
pub fn scan_napi(&self, lexical_context: LexicalContext, input: String) -> Option<TokenKind> {
219-
self.scan(lexical_context, input.as_str())
220-
}
221-
222208
#[napi(js_name = "parse", ts_return_type = "parse_output.ParseOutput", catch_unwind)]
223209
pub fn parse_napi(
224210
&self,

crates/solidity/outputs/cargo/crate/src/generated/kinds.rs

Lines changed: 4 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/solidity/outputs/cargo/crate/src/generated/language.rs

Lines changed: 3 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)