diff --git a/.changeset/green-pets-invent.md b/.changeset/green-pets-invent.md new file mode 100644 index 0000000000..b413ad351a --- /dev/null +++ b/.changeset/green-pets-invent.md @@ -0,0 +1,5 @@ +--- +"@nomicfoundation/slang": minor +--- + +Remove Language#scan API; use the parser API instead diff --git a/crates/codegen/parser/runtime/src/kinds.rs b/crates/codegen/parser/runtime/src/kinds.rs index dbef6a16ac..70dd5f4597 100644 --- a/crates/codegen/parser/runtime/src/kinds.rs +++ b/crates/codegen/parser/runtime/src/kinds.rs @@ -16,7 +16,7 @@ use napi_derive::napi; #[cfg_attr(not(feature = "slang_napi_interfaces"), derive(Clone, Copy))] pub enum TokenKind { SKIPPED, - XXX, + // Expanded by the template engine } #[derive( @@ -35,7 +35,7 @@ pub enum TokenKind { pub enum RuleKind { LeadingTrivia, TrailingTrivia, - XXX, + // Expanded by the template engine } impl RuleKind { @@ -71,18 +71,16 @@ pub enum FieldName { } /// The lexical context of the scanner. -#[derive(strum_macros::FromRepr)] -#[cfg_attr(feature = "slang_napi_interfaces", /* derives `Clone` and `Copy` */ napi(string_enum, namespace = "language"))] -#[cfg_attr(not(feature = "slang_napi_interfaces"), derive(Clone, Copy))] -pub enum LexicalContext { - XXX, +#[derive(strum_macros::FromRepr, Clone, Copy)] +pub(crate) enum LexicalContext { + // Expanded by the template engine } /// Marker trait for type-level [`LexicalContext`] variants. -pub trait IsLexicalContext { +pub(crate) trait IsLexicalContext { /// Returns a run-time [`LexicalContext`] value. fn value() -> LexicalContext; } #[allow(non_snake_case)] -pub mod LexicalContextType {} +pub(crate) mod LexicalContextType {} diff --git a/crates/codegen/parser/runtime/src/lexer.rs b/crates/codegen/parser/runtime/src/lexer.rs index e0951a97e2..fdda0371e9 100644 --- a/crates/codegen/parser/runtime/src/lexer.rs +++ b/crates/codegen/parser/runtime/src/lexer.rs @@ -2,7 +2,7 @@ use crate::cst::{self, NamedNode}; use crate::kinds::{IsLexicalContext, TokenKind}; use crate::support::{ParserContext, ParserResult}; -pub trait Lexer { +pub(crate) trait Lexer { // Generated by the templating engine #[doc(hidden)] fn next_token( diff --git a/crates/codegen/parser/runtime/src/support/mod.rs b/crates/codegen/parser/runtime/src/support/mod.rs index 3590cc222e..00528d0ca1 100644 --- a/crates/codegen/parser/runtime/src/support/mod.rs +++ b/crates/codegen/parser/runtime/src/support/mod.rs @@ -18,7 +18,8 @@ pub use optional_helper::OptionalHelper; pub use parser_function::ParserFunction; pub use parser_result::ParserResult; pub use precedence_helper::PrecedenceHelper; -pub use recovery::RecoverFromNoMatch; +#[allow(unused_imports)] // Used when copied to shipped crate +pub(crate) use recovery::RecoverFromNoMatch; pub use repetition_helper::{OneOrMoreHelper, ZeroOrMoreHelper}; pub use separated_helper::SeparatedHelper; pub use sequence_helper::SequenceHelper; diff --git a/crates/codegen/parser/runtime/src/support/recovery.rs b/crates/codegen/parser/runtime/src/support/recovery.rs index 081dfb9e45..f878372569 100644 --- a/crates/codegen/parser/runtime/src/support/recovery.rs +++ b/crates/codegen/parser/runtime/src/support/recovery.rs @@ -9,7 +9,7 @@ use crate::text_index::{TextRange, TextRangeExtensions as _}; /// An explicit parameter for the [`ParserResult::recover_until_with_nested_delims`] method. #[derive(Clone, Copy)] -pub enum RecoverFromNoMatch { +pub(crate) enum RecoverFromNoMatch { Yes, No, } @@ -41,7 +41,7 @@ impl ParserResult { /// Respects nested delimiters, i.e. the `expected` token is only accepted if it's not nested inside. /// Does not consume the `expected` token. #[must_use] - pub fn recover_until_with_nested_delims( + pub(crate) fn recover_until_with_nested_delims( self, input: &mut ParserContext<'_>, lexer: &L, @@ -117,7 +117,7 @@ impl ParserResult { /// Does not consume the `expected` token. /// /// Returns the found token and the range of skipped tokens on success. -pub fn skip_until_with_nested_delims( +pub(crate) fn skip_until_with_nested_delims( input: &mut ParserContext<'_>, lexer: &L, until: TokenKind, diff --git a/crates/codegen/parser/runtime/src/support/separated_helper.rs b/crates/codegen/parser/runtime/src/support/separated_helper.rs index d077b887a5..c79b5cc796 100644 --- a/crates/codegen/parser/runtime/src/support/separated_helper.rs +++ b/crates/codegen/parser/runtime/src/support/separated_helper.rs @@ -10,7 +10,7 @@ use crate::text_index::TextRangeExtensions; pub struct SeparatedHelper; impl SeparatedHelper { - pub fn run( + pub(crate) fn run( input: &mut ParserContext<'_>, lexer: &L, body_parser: impl Fn(&mut ParserContext<'_>) -> ParserResult, diff --git a/crates/codegen/parser/runtime/src/templates/kinds.rs.jinja2 b/crates/codegen/parser/runtime/src/templates/kinds.rs.jinja2 index 58f1e61401..6659de214d 100644 --- a/crates/codegen/parser/runtime/src/templates/kinds.rs.jinja2 +++ b/crates/codegen/parser/runtime/src/templates/kinds.rs.jinja2 @@ -83,24 +83,22 @@ pub enum TokenKind { {%- endfor -%} } -#[derive(strum_macros::FromRepr)] /// The lexical context of the scanner. -#[cfg_attr(feature = "slang_napi_interfaces", /* derives `Clone` and `Copy` */ napi(string_enum, namespace = "language"))] -#[cfg_attr(not(feature = "slang_napi_interfaces"), derive(Clone, Copy))] -pub enum LexicalContext { +#[derive(strum_macros::FromRepr, Clone, Copy)] +pub(crate) enum LexicalContext { {%- for context in code.scanner_contexts %} {{ context.name }}, {%- endfor %} } /// Marker trait for type-level [`LexicalContext`] variants. -pub trait IsLexicalContext { +pub(crate) trait IsLexicalContext { /// Returns a run-time [`LexicalContext`] value. fn value() -> LexicalContext; } #[allow(non_snake_case)] -pub mod LexicalContextType { +pub(crate) mod LexicalContextType { use super::{IsLexicalContext, LexicalContext}; {%- for context in code.scanner_contexts %} diff --git a/crates/codegen/parser/runtime/src/templates/language.rs.jinja2 b/crates/codegen/parser/runtime/src/templates/language.rs.jinja2 index c10a7d8dda..3a39c6234e 100644 --- a/crates/codegen/parser/runtime/src/templates/language.rs.jinja2 +++ b/crates/codegen/parser/runtime/src/templates/language.rs.jinja2 @@ -12,8 +12,9 @@ use semver::Version; use napi_derive::napi; use crate::cst; -pub use crate::kinds::LexicalContext; -use crate::kinds::{FieldName, IsLexicalContext, LexicalContextType, RuleKind, TokenKind}; +use crate::kinds::{ + FieldName, IsLexicalContext, LexicalContext, LexicalContextType, RuleKind, TokenKind, +}; use crate::lexer::Lexer; #[cfg(feature = "slang_napi_interfaces")] use crate::napi::napi_parse_output::ParseOutput as NAPIParseOutput; @@ -91,16 +92,6 @@ impl Language { fn {{ function.0 | snake_case }}(&self, input: &mut ParserContext<'_>) -> bool { {{ function.1 }} } {% endfor %} - pub fn scan(&self, lexical_context: LexicalContext, input: &str) -> Option { - let mut input = ParserContext::new(input); - match lexical_context { - {%- for lexical_context in code.scanner_contexts -%} - LexicalContext::{{ lexical_context.name }} => - Lexer::next_token::(self, &mut input), - {%- endfor -%} - } - } - pub fn parse(&self, kind: RuleKind, input: &str) -> ParseOutput { match kind { {%- for function in code.parser_functions -%} @@ -214,11 +205,6 @@ impl Language { return Self::SUPPORTED_VERSIONS.iter().map(|v| v.to_string()).collect(); } - #[napi(js_name = "scan", ts_return_type = "kinds.TokenKind | null", catch_unwind)] - pub fn scan_napi(&self, lexical_context: LexicalContext, input: String) -> Option { - self.scan(lexical_context, input.as_str()) - } - #[napi(js_name = "parse", ts_return_type = "parse_output.ParseOutput", catch_unwind)] pub fn parse_napi( &self, diff --git a/crates/solidity/outputs/cargo/crate/src/generated/kinds.rs b/crates/solidity/outputs/cargo/crate/src/generated/kinds.rs index 2177ff5b8a..2674946a58 100644 --- a/crates/solidity/outputs/cargo/crate/src/generated/kinds.rs +++ b/crates/solidity/outputs/cargo/crate/src/generated/kinds.rs @@ -690,24 +690,22 @@ pub enum TokenKind { YulYearsKeyword, } -#[derive(strum_macros::FromRepr)] /// The lexical context of the scanner. -#[cfg_attr(feature = "slang_napi_interfaces", /* derives `Clone` and `Copy` */ napi(string_enum, namespace = "language"))] -#[cfg_attr(not(feature = "slang_napi_interfaces"), derive(Clone, Copy))] -pub enum LexicalContext { +#[derive(strum_macros::FromRepr, Clone, Copy)] +pub(crate) enum LexicalContext { Default, Pragma, Yul, } /// Marker trait for type-level [`LexicalContext`] variants. -pub trait IsLexicalContext { +pub(crate) trait IsLexicalContext { /// Returns a run-time [`LexicalContext`] value. fn value() -> LexicalContext; } #[allow(non_snake_case)] -pub mod LexicalContextType { +pub(crate) mod LexicalContextType { use super::{IsLexicalContext, LexicalContext}; pub struct Default {} impl IsLexicalContext for Default { diff --git a/crates/solidity/outputs/cargo/crate/src/generated/language.rs b/crates/solidity/outputs/cargo/crate/src/generated/language.rs index 21493a8b41..27de6b3b64 100644 --- a/crates/solidity/outputs/cargo/crate/src/generated/language.rs +++ b/crates/solidity/outputs/cargo/crate/src/generated/language.rs @@ -14,8 +14,9 @@ use napi_derive::napi; use semver::Version; use crate::cst; -pub use crate::kinds::LexicalContext; -use crate::kinds::{FieldName, IsLexicalContext, LexicalContextType, RuleKind, TokenKind}; +use crate::kinds::{ + FieldName, IsLexicalContext, LexicalContext, LexicalContextType, RuleKind, TokenKind, +}; use crate::lexer::Lexer; #[cfg(feature = "slang_napi_interfaces")] use crate::napi::napi_parse_output::ParseOutput as NAPIParseOutput; @@ -8196,19 +8197,6 @@ impl Language { } } - pub fn scan(&self, lexical_context: LexicalContext, input: &str) -> Option { - let mut input = ParserContext::new(input); - match lexical_context { - LexicalContext::Default => { - Lexer::next_token::(self, &mut input) - } - LexicalContext::Pragma => { - Lexer::next_token::(self, &mut input) - } - LexicalContext::Yul => Lexer::next_token::(self, &mut input), - } - } - pub fn parse(&self, kind: RuleKind, input: &str) -> ParseOutput { match kind { RuleKind::ABICoderPragma => Self::abi_coder_pragma.parse(self, input), @@ -10738,15 +10726,6 @@ impl Language { .collect(); } - #[napi( - js_name = "scan", - ts_return_type = "kinds.TokenKind | null", - catch_unwind - )] - pub fn scan_napi(&self, lexical_context: LexicalContext, input: String) -> Option { - self.scan(lexical_context, input.as_str()) - } - #[napi( js_name = "parse", ts_return_type = "parse_output.ParseOutput", diff --git a/crates/solidity/outputs/cargo/crate/src/generated/lexer.rs b/crates/solidity/outputs/cargo/crate/src/generated/lexer.rs index 9d9915b3d2..24d119f45c 100644 --- a/crates/solidity/outputs/cargo/crate/src/generated/lexer.rs +++ b/crates/solidity/outputs/cargo/crate/src/generated/lexer.rs @@ -4,7 +4,7 @@ use crate::cst::{self, NamedNode}; use crate::kinds::{IsLexicalContext, TokenKind}; use crate::support::{ParserContext, ParserResult}; -pub trait Lexer { +pub(crate) trait Lexer { // Generated by the templating engine #[doc(hidden)] fn next_token( diff --git a/crates/solidity/outputs/cargo/crate/src/generated/support/mod.rs b/crates/solidity/outputs/cargo/crate/src/generated/support/mod.rs index 119a5b9e19..3d59e51501 100644 --- a/crates/solidity/outputs/cargo/crate/src/generated/support/mod.rs +++ b/crates/solidity/outputs/cargo/crate/src/generated/support/mod.rs @@ -20,7 +20,8 @@ pub use optional_helper::OptionalHelper; pub use parser_function::ParserFunction; pub use parser_result::ParserResult; pub use precedence_helper::PrecedenceHelper; -pub use recovery::RecoverFromNoMatch; +#[allow(unused_imports)] // Used when copied to shipped crate +pub(crate) use recovery::RecoverFromNoMatch; pub use repetition_helper::{OneOrMoreHelper, ZeroOrMoreHelper}; pub use separated_helper::SeparatedHelper; pub use sequence_helper::SequenceHelper; diff --git a/crates/solidity/outputs/cargo/crate/src/generated/support/recovery.rs b/crates/solidity/outputs/cargo/crate/src/generated/support/recovery.rs index cd38e1cf32..d1f92a193a 100644 --- a/crates/solidity/outputs/cargo/crate/src/generated/support/recovery.rs +++ b/crates/solidity/outputs/cargo/crate/src/generated/support/recovery.rs @@ -11,7 +11,7 @@ use crate::text_index::{TextRange, TextRangeExtensions as _}; /// An explicit parameter for the [`ParserResult::recover_until_with_nested_delims`] method. #[derive(Clone, Copy)] -pub enum RecoverFromNoMatch { +pub(crate) enum RecoverFromNoMatch { Yes, No, } @@ -43,7 +43,7 @@ impl ParserResult { /// Respects nested delimiters, i.e. the `expected` token is only accepted if it's not nested inside. /// Does not consume the `expected` token. #[must_use] - pub fn recover_until_with_nested_delims( + pub(crate) fn recover_until_with_nested_delims( self, input: &mut ParserContext<'_>, lexer: &L, @@ -119,7 +119,7 @@ impl ParserResult { /// Does not consume the `expected` token. /// /// Returns the found token and the range of skipped tokens on success. -pub fn skip_until_with_nested_delims( +pub(crate) fn skip_until_with_nested_delims( input: &mut ParserContext<'_>, lexer: &L, until: TokenKind, diff --git a/crates/solidity/outputs/cargo/crate/src/generated/support/separated_helper.rs b/crates/solidity/outputs/cargo/crate/src/generated/support/separated_helper.rs index 408776abc1..b5c07d9c52 100644 --- a/crates/solidity/outputs/cargo/crate/src/generated/support/separated_helper.rs +++ b/crates/solidity/outputs/cargo/crate/src/generated/support/separated_helper.rs @@ -12,7 +12,7 @@ use crate::text_index::TextRangeExtensions; pub struct SeparatedHelper; impl SeparatedHelper { - pub fn run( + pub(crate) fn run( input: &mut ParserContext<'_>, lexer: &L, body_parser: impl Fn(&mut ParserContext<'_>) -> ParserResult, diff --git a/crates/solidity/outputs/cargo/tests/src/cst_output/generated/HexNumberExpression.rs b/crates/solidity/outputs/cargo/tests/src/cst_output/generated/HexNumberExpression.rs index 915fb6c344..0d0dac41fc 100644 --- a/crates/solidity/outputs/cargo/tests/src/cst_output/generated/HexNumberExpression.rs +++ b/crates/solidity/outputs/cargo/tests/src/cst_output/generated/HexNumberExpression.rs @@ -9,6 +9,11 @@ fn hex_consecutive_underscores() -> Result<()> { run("HexNumberExpression", "hex_consecutive_underscores") } +#[test] +fn hex_invalid_alpha_digit() -> Result<()> { + run("HexNumberExpression", "hex_invalid_alpha_digit") +} + #[test] fn hex_leading_underscore() -> Result<()> { run("HexNumberExpression", "hex_leading_underscore") diff --git a/crates/solidity/outputs/cargo/tests/src/cst_output/generated/LeadingTrivia.rs b/crates/solidity/outputs/cargo/tests/src/cst_output/generated/LeadingTrivia.rs new file mode 100644 index 0000000000..ce77affa38 --- /dev/null +++ b/crates/solidity/outputs/cargo/tests/src/cst_output/generated/LeadingTrivia.rs @@ -0,0 +1,30 @@ +// This file is generated automatically by infrastructure scripts. Please don't edit by hand. + +use anyhow::Result; + +use crate::cst_output::runner::run; + +#[test] +fn multi_line() -> Result<()> { + run("LeadingTrivia", "multi_line") +} + +#[test] +fn multi_line_trailing_double_star() -> Result<()> { + run("LeadingTrivia", "multi_line_trailing_double_star") +} + +#[test] +fn new_line() -> Result<()> { + run("LeadingTrivia", "new_line") +} + +#[test] +fn single_line_comment() -> Result<()> { + run("LeadingTrivia", "single_line_comment") +} + +#[test] +fn whitespace() -> Result<()> { + run("LeadingTrivia", "whitespace") +} diff --git a/crates/solidity/outputs/cargo/tests/src/cst_output/generated/mod.rs b/crates/solidity/outputs/cargo/tests/src/cst_output/generated/mod.rs index bcfd0280bf..4b2c056109 100644 --- a/crates/solidity/outputs/cargo/tests/src/cst_output/generated/mod.rs +++ b/crates/solidity/outputs/cargo/tests/src/cst_output/generated/mod.rs @@ -46,6 +46,8 @@ mod ImportDirective; #[allow(non_snake_case)] mod InterfaceDefinition; #[allow(non_snake_case)] +mod LeadingTrivia; +#[allow(non_snake_case)] mod MappingType; #[allow(non_snake_case)] mod NewExpression; diff --git a/crates/solidity/outputs/cargo/tests/src/cst_output/runner.rs b/crates/solidity/outputs/cargo/tests/src/cst_output/runner.rs index a1c4ad384d..d9e1217aec 100644 --- a/crates/solidity/outputs/cargo/tests/src/cst_output/runner.rs +++ b/crates/solidity/outputs/cargo/tests/src/cst_output/runner.rs @@ -6,7 +6,7 @@ use infra_utils::codegen::Codegen; use infra_utils::paths::PathExtensions; use slang_solidity::kinds::RuleKind; use slang_solidity::language::Language; -use solidity_testing_utils::cst_snapshots::CstSnapshots; +use solidity_testing_utils::cst_snapshots::{CstSnapshots, WithWhitespace}; use strum_macros::Display; use crate::cst_output::generated::VERSION_BREAKS; @@ -34,10 +34,10 @@ pub fn run(parser_name: &str, test_name: &str) -> Result<()> { let mut last_output = None; for version in VERSION_BREAKS { - let rule_kind = RuleKind::from_str(parser_name) + let tested_rule_kind = RuleKind::from_str(parser_name) .unwrap_or_else(|_| panic!("No such parser: {parser_name}")); - let output = Language::new(version.clone())?.parse(rule_kind, &source); + let output = Language::new(version.clone())?.parse(tested_rule_kind, &source); let output = match last_output { // Skip this version if it produces the same output. @@ -60,7 +60,13 @@ pub fn run(parser_name: &str, test_name: &str) -> Result<()> { TestStatus::Failure }; - let snapshot = CstSnapshots::render(&source, &errors, cursor)?; + let render_whitespace = if tested_rule_kind.is_trivia() { + WithWhitespace::Yes + } else { + WithWhitespace::No + }; + + let snapshot = CstSnapshots::render(&source, &errors, cursor, render_whitespace)?; let snapshot_path = test_dir .join("generated") diff --git a/crates/solidity/outputs/cargo/tests/src/lib.rs b/crates/solidity/outputs/cargo/tests/src/lib.rs index 7b04712139..806be22678 100644 --- a/crates/solidity/outputs/cargo/tests/src/lib.rs +++ b/crates/solidity/outputs/cargo/tests/src/lib.rs @@ -3,5 +3,4 @@ mod cst_output; mod doc_examples; mod errors; -mod scanner; mod versions; diff --git a/crates/solidity/outputs/cargo/tests/src/scanner/mod.rs b/crates/solidity/outputs/cargo/tests/src/scanner/mod.rs deleted file mode 100644 index 6793286473..0000000000 --- a/crates/solidity/outputs/cargo/tests/src/scanner/mod.rs +++ /dev/null @@ -1,31 +0,0 @@ -use semver::Version; -use slang_solidity::kinds::TokenKind::*; -use slang_solidity::language::{Language, LexicalContext}; - -#[test] -fn test_next_token() { - let language = Language::new(Version::new(0, 8, 0)).unwrap(); - - for (s, k) in &[ - ("contract", ContractKeyword), - (" ", Whitespace), - ("X", Identifier), - ("{", OpenBrace), - ("}", CloseBrace), - ("+=", PlusEqual), - ("1", DecimalLiteral), - ("\n", EndOfLine), - ("unicode'abc'", UnicodeStringLiteral), - ("hex'abcd'", HexStringLiteral), - ("'abc'ZZ", AsciiStringLiteral), // with an identifier afterwards - ("unicode'abc'ZZ", UnicodeStringLiteral), // with an identifier afterwards - ("hex'abcd'ZZz", HexStringLiteral), // with an identifier afterwards - ("// single line\n", SingleLineComment), - ("/* multi-line\n comment */ blah", MultilineComment), - ("/* multi-line comment **/ blah", MultilineComment), - ("0ZZ", SKIPPED), - ("0xabZZ", SKIPPED), - ] { - assert_eq!(language.scan(LexicalContext::Default, s), Some(*k)); - } -} diff --git a/crates/solidity/outputs/npm/crate/src/generated/kinds.rs b/crates/solidity/outputs/npm/crate/src/generated/kinds.rs index 2177ff5b8a..2674946a58 100644 --- a/crates/solidity/outputs/npm/crate/src/generated/kinds.rs +++ b/crates/solidity/outputs/npm/crate/src/generated/kinds.rs @@ -690,24 +690,22 @@ pub enum TokenKind { YulYearsKeyword, } -#[derive(strum_macros::FromRepr)] /// The lexical context of the scanner. -#[cfg_attr(feature = "slang_napi_interfaces", /* derives `Clone` and `Copy` */ napi(string_enum, namespace = "language"))] -#[cfg_attr(not(feature = "slang_napi_interfaces"), derive(Clone, Copy))] -pub enum LexicalContext { +#[derive(strum_macros::FromRepr, Clone, Copy)] +pub(crate) enum LexicalContext { Default, Pragma, Yul, } /// Marker trait for type-level [`LexicalContext`] variants. -pub trait IsLexicalContext { +pub(crate) trait IsLexicalContext { /// Returns a run-time [`LexicalContext`] value. fn value() -> LexicalContext; } #[allow(non_snake_case)] -pub mod LexicalContextType { +pub(crate) mod LexicalContextType { use super::{IsLexicalContext, LexicalContext}; pub struct Default {} impl IsLexicalContext for Default { diff --git a/crates/solidity/outputs/npm/crate/src/generated/language.rs b/crates/solidity/outputs/npm/crate/src/generated/language.rs index 21493a8b41..27de6b3b64 100644 --- a/crates/solidity/outputs/npm/crate/src/generated/language.rs +++ b/crates/solidity/outputs/npm/crate/src/generated/language.rs @@ -14,8 +14,9 @@ use napi_derive::napi; use semver::Version; use crate::cst; -pub use crate::kinds::LexicalContext; -use crate::kinds::{FieldName, IsLexicalContext, LexicalContextType, RuleKind, TokenKind}; +use crate::kinds::{ + FieldName, IsLexicalContext, LexicalContext, LexicalContextType, RuleKind, TokenKind, +}; use crate::lexer::Lexer; #[cfg(feature = "slang_napi_interfaces")] use crate::napi::napi_parse_output::ParseOutput as NAPIParseOutput; @@ -8196,19 +8197,6 @@ impl Language { } } - pub fn scan(&self, lexical_context: LexicalContext, input: &str) -> Option { - let mut input = ParserContext::new(input); - match lexical_context { - LexicalContext::Default => { - Lexer::next_token::(self, &mut input) - } - LexicalContext::Pragma => { - Lexer::next_token::(self, &mut input) - } - LexicalContext::Yul => Lexer::next_token::(self, &mut input), - } - } - pub fn parse(&self, kind: RuleKind, input: &str) -> ParseOutput { match kind { RuleKind::ABICoderPragma => Self::abi_coder_pragma.parse(self, input), @@ -10738,15 +10726,6 @@ impl Language { .collect(); } - #[napi( - js_name = "scan", - ts_return_type = "kinds.TokenKind | null", - catch_unwind - )] - pub fn scan_napi(&self, lexical_context: LexicalContext, input: String) -> Option { - self.scan(lexical_context, input.as_str()) - } - #[napi( js_name = "parse", ts_return_type = "parse_output.ParseOutput", diff --git a/crates/solidity/outputs/npm/crate/src/generated/lexer.rs b/crates/solidity/outputs/npm/crate/src/generated/lexer.rs index 9d9915b3d2..24d119f45c 100644 --- a/crates/solidity/outputs/npm/crate/src/generated/lexer.rs +++ b/crates/solidity/outputs/npm/crate/src/generated/lexer.rs @@ -4,7 +4,7 @@ use crate::cst::{self, NamedNode}; use crate::kinds::{IsLexicalContext, TokenKind}; use crate::support::{ParserContext, ParserResult}; -pub trait Lexer { +pub(crate) trait Lexer { // Generated by the templating engine #[doc(hidden)] fn next_token( diff --git a/crates/solidity/outputs/npm/crate/src/generated/support/mod.rs b/crates/solidity/outputs/npm/crate/src/generated/support/mod.rs index 119a5b9e19..3d59e51501 100644 --- a/crates/solidity/outputs/npm/crate/src/generated/support/mod.rs +++ b/crates/solidity/outputs/npm/crate/src/generated/support/mod.rs @@ -20,7 +20,8 @@ pub use optional_helper::OptionalHelper; pub use parser_function::ParserFunction; pub use parser_result::ParserResult; pub use precedence_helper::PrecedenceHelper; -pub use recovery::RecoverFromNoMatch; +#[allow(unused_imports)] // Used when copied to shipped crate +pub(crate) use recovery::RecoverFromNoMatch; pub use repetition_helper::{OneOrMoreHelper, ZeroOrMoreHelper}; pub use separated_helper::SeparatedHelper; pub use sequence_helper::SequenceHelper; diff --git a/crates/solidity/outputs/npm/crate/src/generated/support/recovery.rs b/crates/solidity/outputs/npm/crate/src/generated/support/recovery.rs index cd38e1cf32..d1f92a193a 100644 --- a/crates/solidity/outputs/npm/crate/src/generated/support/recovery.rs +++ b/crates/solidity/outputs/npm/crate/src/generated/support/recovery.rs @@ -11,7 +11,7 @@ use crate::text_index::{TextRange, TextRangeExtensions as _}; /// An explicit parameter for the [`ParserResult::recover_until_with_nested_delims`] method. #[derive(Clone, Copy)] -pub enum RecoverFromNoMatch { +pub(crate) enum RecoverFromNoMatch { Yes, No, } @@ -43,7 +43,7 @@ impl ParserResult { /// Respects nested delimiters, i.e. the `expected` token is only accepted if it's not nested inside. /// Does not consume the `expected` token. #[must_use] - pub fn recover_until_with_nested_delims( + pub(crate) fn recover_until_with_nested_delims( self, input: &mut ParserContext<'_>, lexer: &L, @@ -119,7 +119,7 @@ impl ParserResult { /// Does not consume the `expected` token. /// /// Returns the found token and the range of skipped tokens on success. -pub fn skip_until_with_nested_delims( +pub(crate) fn skip_until_with_nested_delims( input: &mut ParserContext<'_>, lexer: &L, until: TokenKind, diff --git a/crates/solidity/outputs/npm/crate/src/generated/support/separated_helper.rs b/crates/solidity/outputs/npm/crate/src/generated/support/separated_helper.rs index 408776abc1..b5c07d9c52 100644 --- a/crates/solidity/outputs/npm/crate/src/generated/support/separated_helper.rs +++ b/crates/solidity/outputs/npm/crate/src/generated/support/separated_helper.rs @@ -12,7 +12,7 @@ use crate::text_index::TextRangeExtensions; pub struct SeparatedHelper; impl SeparatedHelper { - pub fn run( + pub(crate) fn run( input: &mut ParserContext<'_>, lexer: &L, body_parser: impl Fn(&mut ParserContext<'_>) -> ParserResult, diff --git a/crates/solidity/outputs/npm/package/src/generated/index.d.ts b/crates/solidity/outputs/npm/package/src/generated/index.d.ts index c125c8348b..1a1b4cfa58 100644 --- a/crates/solidity/outputs/npm/package/src/generated/index.d.ts +++ b/crates/solidity/outputs/npm/package/src/generated/index.d.ts @@ -641,17 +641,10 @@ export namespace kinds { } } export namespace language { - /** The lexical context of the scanner. */ - export enum LexicalContext { - Default = "Default", - Pragma = "Pragma", - Yul = "Yul", - } export class Language { constructor(version: string); get version(): string; static supportedVersions(): Array; - scan(lexicalContext: LexicalContext, input: string): kinds.TokenKind | null; parse(kind: kinds.RuleKind, input: string): parse_output.ParseOutput; } } diff --git a/crates/solidity/outputs/npm/package/src/language/index.ts b/crates/solidity/outputs/npm/package/src/language/index.ts index 94d942764e..19a20f8fa9 100644 --- a/crates/solidity/outputs/npm/package/src/language/index.ts +++ b/crates/solidity/outputs/npm/package/src/language/index.ts @@ -2,6 +2,3 @@ import * as generated from "../generated"; export const Language = generated.language.Language; export type Language = generated.language.Language; - -export const LexicalContext = generated.language.LexicalContext; -export type LexicalContext = generated.language.LexicalContext; diff --git a/crates/solidity/testing/snapshots/cst_output/HexNumberExpression/hex_invalid_alpha_digit/generated/0.4.11-failure.yml b/crates/solidity/testing/snapshots/cst_output/HexNumberExpression/hex_invalid_alpha_digit/generated/0.4.11-failure.yml new file mode 100644 index 0000000000..4fd1c3f039 --- /dev/null +++ b/crates/solidity/testing/snapshots/cst_output/HexNumberExpression/hex_invalid_alpha_digit/generated/0.4.11-failure.yml @@ -0,0 +1,17 @@ +# This file is generated automatically by infrastructure scripts. Please don't edit by hand. + +Source: > + 1 │ 0xabzz │ 0..6 + +Errors: # 1 total + - > + Error: Expected HexLiteral. + ╭─[crates/solidity/testing/snapshots/cst_output/HexNumberExpression/hex_invalid_alpha_digit/input.sol:1:1] + │ + 1 │ 0xabzz + │ ───┬─── + │ ╰───── Error occurred here. + ───╯ + +Tree: + - SKIPPED: "0xabzz\n" # 0..7 diff --git a/crates/solidity/testing/snapshots/cst_output/HexNumberExpression/hex_invalid_alpha_digit/input.sol b/crates/solidity/testing/snapshots/cst_output/HexNumberExpression/hex_invalid_alpha_digit/input.sol new file mode 100644 index 0000000000..b491a7c890 --- /dev/null +++ b/crates/solidity/testing/snapshots/cst_output/HexNumberExpression/hex_invalid_alpha_digit/input.sol @@ -0,0 +1 @@ +0xabzz diff --git a/crates/solidity/testing/snapshots/cst_output/LeadingTrivia/multi_line/generated/0.4.11-success.yml b/crates/solidity/testing/snapshots/cst_output/LeadingTrivia/multi_line/generated/0.4.11-success.yml new file mode 100644 index 0000000000..ba1e2a4067 --- /dev/null +++ b/crates/solidity/testing/snapshots/cst_output/LeadingTrivia/multi_line/generated/0.4.11-success.yml @@ -0,0 +1,12 @@ +# This file is generated automatically by infrastructure scripts. Please don't edit by hand. + +Source: > + 1 │ /* multi-line │ 0..13 + 2 │ comment */ │ 14..27 + +Errors: [] + +Tree: + - LeadingTrivia: # 0..28 "/* multi-line\n comment */\n" + - MultilineComment: "/* multi-line\n comment */" # 0..27 + - EndOfLine: "\n" # 27..28 diff --git a/crates/solidity/testing/snapshots/cst_output/LeadingTrivia/multi_line/input.sol b/crates/solidity/testing/snapshots/cst_output/LeadingTrivia/multi_line/input.sol new file mode 100644 index 0000000000..444d412a12 --- /dev/null +++ b/crates/solidity/testing/snapshots/cst_output/LeadingTrivia/multi_line/input.sol @@ -0,0 +1,2 @@ +/* multi-line + comment */ diff --git a/crates/solidity/testing/snapshots/cst_output/LeadingTrivia/multi_line_trailing_double_star/generated/0.4.11-success.yml b/crates/solidity/testing/snapshots/cst_output/LeadingTrivia/multi_line_trailing_double_star/generated/0.4.11-success.yml new file mode 100644 index 0000000000..418a0a2305 --- /dev/null +++ b/crates/solidity/testing/snapshots/cst_output/LeadingTrivia/multi_line_trailing_double_star/generated/0.4.11-success.yml @@ -0,0 +1,11 @@ +# This file is generated automatically by infrastructure scripts. Please don't edit by hand. + +Source: > + 1 │ /* multi-line comment **/ │ 0..25 + +Errors: [] + +Tree: + - LeadingTrivia: # 0..26 "/* multi-line comment **/\n" + - MultilineComment: "/* multi-line comment **/" # 0..25 + - EndOfLine: "\n" # 25..26 diff --git a/crates/solidity/testing/snapshots/cst_output/LeadingTrivia/multi_line_trailing_double_star/input.sol b/crates/solidity/testing/snapshots/cst_output/LeadingTrivia/multi_line_trailing_double_star/input.sol new file mode 100644 index 0000000000..17d69705d8 --- /dev/null +++ b/crates/solidity/testing/snapshots/cst_output/LeadingTrivia/multi_line_trailing_double_star/input.sol @@ -0,0 +1 @@ +/* multi-line comment **/ diff --git a/crates/solidity/testing/snapshots/cst_output/LeadingTrivia/new_line/generated/0.4.11-success.yml b/crates/solidity/testing/snapshots/cst_output/LeadingTrivia/new_line/generated/0.4.11-success.yml new file mode 100644 index 0000000000..85f37f89ed --- /dev/null +++ b/crates/solidity/testing/snapshots/cst_output/LeadingTrivia/new_line/generated/0.4.11-success.yml @@ -0,0 +1,10 @@ +# This file is generated automatically by infrastructure scripts. Please don't edit by hand. + +Source: > + 1 │ │ 0..0 + +Errors: [] + +Tree: + - LeadingTrivia: # 0..1 "\n" + - EndOfLine: "\n" # 0..1 diff --git a/crates/solidity/testing/snapshots/cst_output/LeadingTrivia/new_line/input.sol b/crates/solidity/testing/snapshots/cst_output/LeadingTrivia/new_line/input.sol new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/crates/solidity/testing/snapshots/cst_output/LeadingTrivia/new_line/input.sol @@ -0,0 +1 @@ + diff --git a/crates/solidity/testing/snapshots/cst_output/LeadingTrivia/single_line_comment/generated/0.4.11-success.yml b/crates/solidity/testing/snapshots/cst_output/LeadingTrivia/single_line_comment/generated/0.4.11-success.yml new file mode 100644 index 0000000000..1e401ddb1c --- /dev/null +++ b/crates/solidity/testing/snapshots/cst_output/LeadingTrivia/single_line_comment/generated/0.4.11-success.yml @@ -0,0 +1,11 @@ +# This file is generated automatically by infrastructure scripts. Please don't edit by hand. + +Source: > + 1 │ // single line │ 0..14 + +Errors: [] + +Tree: + - LeadingTrivia: # 0..15 "// single line\n" + - SingleLineComment: "// single line" # 0..14 + - EndOfLine: "\n" # 14..15 diff --git a/crates/solidity/testing/snapshots/cst_output/LeadingTrivia/single_line_comment/input.sol b/crates/solidity/testing/snapshots/cst_output/LeadingTrivia/single_line_comment/input.sol new file mode 100644 index 0000000000..51741752db --- /dev/null +++ b/crates/solidity/testing/snapshots/cst_output/LeadingTrivia/single_line_comment/input.sol @@ -0,0 +1 @@ +// single line diff --git a/crates/solidity/testing/snapshots/cst_output/LeadingTrivia/whitespace/generated/0.4.11-success.yml b/crates/solidity/testing/snapshots/cst_output/LeadingTrivia/whitespace/generated/0.4.11-success.yml new file mode 100644 index 0000000000..17087802e9 --- /dev/null +++ b/crates/solidity/testing/snapshots/cst_output/LeadingTrivia/whitespace/generated/0.4.11-success.yml @@ -0,0 +1,10 @@ +# This file is generated automatically by infrastructure scripts. Please don't edit by hand. + +Source: > + 1 │ │ 0..3 + +Errors: [] + +Tree: + - LeadingTrivia: # 0..3 " " + - Whitespace: " " # 0..3 diff --git a/crates/solidity/testing/snapshots/cst_output/LeadingTrivia/whitespace/input.sol b/crates/solidity/testing/snapshots/cst_output/LeadingTrivia/whitespace/input.sol new file mode 100644 index 0000000000..01ad2f9a66 --- /dev/null +++ b/crates/solidity/testing/snapshots/cst_output/LeadingTrivia/whitespace/input.sol @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/crates/solidity/testing/utils/src/cst_snapshots/mod.rs b/crates/solidity/testing/utils/src/cst_snapshots/mod.rs index 4c22b4569b..0553e352ad 100644 --- a/crates/solidity/testing/utils/src/cst_snapshots/mod.rs +++ b/crates/solidity/testing/utils/src/cst_snapshots/mod.rs @@ -9,10 +9,22 @@ use slang_solidity::text_index::{TextRange, TextRangeExtensions}; use crate::node_extensions::NodeExtensions; +/// Whether to include the whitespace in the snapshot test CST output. +#[derive(Clone, Copy)] +pub enum WithWhitespace { + Yes, + No, +} + pub struct CstSnapshots; impl CstSnapshots { - pub fn render(source: &str, errors: &Vec, cursor: CursorWithNames) -> Result { + pub fn render( + source: &str, + errors: &Vec, + cursor: CursorWithNames, + with_whitespace: WithWhitespace, + ) -> Result { let mut w = String::new(); write_source(&mut w, source)?; @@ -21,7 +33,7 @@ impl CstSnapshots { write_errors(&mut w, errors)?; writeln!(&mut w)?; - write_tree(&mut w, cursor, source)?; + write_tree(&mut w, cursor, source, with_whitespace)?; Ok(w) } @@ -85,7 +97,14 @@ fn write_errors(w: &mut W, errors: &Vec) -> Result<()> { Ok(()) } -fn write_tree(w: &mut W, mut cursor: CursorWithNames, source: &str) -> Result<()> { +fn write_tree( + w: &mut W, + mut cursor: CursorWithNames, + source: &str, + with_whitespace: WithWhitespace, +) -> Result<()> { + let with_whitespace = matches!(with_whitespace, WithWhitespace::Yes); + write!(w, "Tree:")?; writeln!(w)?; @@ -98,6 +117,7 @@ fn write_tree(w: &mut W, mut cursor: CursorWithNames, source: &str) -> name: _, node: cst::Node::Rule(rule), }) if rule.is_trivia() + && !with_whitespace && rule.children.iter().all(|named| { named .node @@ -110,7 +130,7 @@ fn write_tree(w: &mut W, mut cursor: CursorWithNames, source: &str) -> Some(cst::NamedNode { name: _, node: cst::Node::Token(token), - }) if is_whitespace(token.kind) => continue, + }) if !with_whitespace && is_whitespace(token.kind) => continue, next => break next.map(|item| (item, depth, range)), } });