diff --git a/crates/compiler/src/file_parse_result.rs b/crates/compiler/src/file_parse_result.rs index 9fb4cb4d..d22db4a9 100644 --- a/crates/compiler/src/file_parse_result.rs +++ b/crates/compiler/src/file_parse_result.rs @@ -9,7 +9,7 @@ use std::{fmt::Formatter, rc::Rc}; /// it provides access to the parse tree, and the stream of tokens used to /// produce that parse tree. #[derive(Clone)] -pub(crate) struct FileParseResult<'input> { +pub struct FileParseResult<'input> { pub name: String, pub tree: Rc>, diff --git a/crates/compiler/src/lib.rs b/crates/compiler/src/lib.rs index d7aed7f1..ccb25ab5 100644 --- a/crates/compiler/src/lib.rs +++ b/crates/compiler/src/lib.rs @@ -15,7 +15,7 @@ pub(crate) mod error_strategy; mod file_parse_result; pub(crate) mod listeners; mod output; -mod parser; +pub mod parser; pub(crate) mod parser_rule_context_ext; mod string_table_manager; pub(crate) mod token_ext; @@ -23,6 +23,7 @@ pub(crate) mod visitors; pub use crate::compiler::run_compilation; pub use crate::compiler::Result; +pub use antlr_rust::tree::ParseTreeVisitorCompat; pub mod prelude { //! Everything you need to get started with the Yarn Spinner compiler. diff --git a/crates/compiler/src/output/declaration.rs b/crates/compiler/src/output/declaration.rs index c305c38d..580e3235 100644 --- a/crates/compiler/src/output/declaration.rs +++ b/crates/compiler/src/output/declaration.rs @@ -239,7 +239,7 @@ impl Display for DeclarationSource { } #[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub(crate) struct DeferredTypeDiagnostic { - pub(crate) name: String, - pub(crate) diagnostic: Diagnostic, +pub struct DeferredTypeDiagnostic { + pub name: String, + pub diagnostic: Diagnostic, } diff --git a/crates/compiler/src/parser.rs b/crates/compiler/src/parser.rs index e2d68058..16d64452 100644 --- a/crates/compiler/src/parser.rs +++ b/crates/compiler/src/parser.rs @@ -1,7 +1,7 @@ //! The parser for the compiler. mod actual_types; -pub(crate) mod generated; +pub mod generated; mod indent_aware_lexer; pub(crate) use actual_types::*; diff --git a/crates/compiler/src/parser/generated.rs b/crates/compiler/src/parser/generated.rs index f37520a2..484adf19 100644 --- a/crates/compiler/src/parser/generated.rs +++ b/crates/compiler/src/parser/generated.rs @@ -3,22 +3,22 @@ #[cfg_attr(rustfmt, rustfmt_skip)] #[allow(warnings)] #[allow(clippy)] -pub(crate) mod yarnspinnerlexer; +pub mod yarnspinnerlexer; #[cfg_attr(rustfmt, rustfmt_skip)] #[allow(warnings)] #[allow(clippy)] -pub(crate) mod yarnspinnerparser; +pub mod yarnspinnerparser; #[cfg_attr(rustfmt, rustfmt_skip)] #[allow(warnings)] #[allow(clippy)] -pub(crate) mod yarnspinnerparserlistener; +pub mod yarnspinnerparserlistener; #[cfg_attr(rustfmt, rustfmt_skip)] #[allow(warnings)] #[allow(clippy)] -pub(crate) mod yarnspinnerparservisitor; +pub mod yarnspinnerparservisitor; #[cfg(test)] mod tests { diff --git a/crates/yarnspinner/tests/language_tests.rs b/crates/yarnspinner/tests/language_tests.rs index 112fb3b9..7d3dc903 100644 --- a/crates/yarnspinner/tests/language_tests.rs +++ b/crates/yarnspinner/tests/language_tests.rs @@ -11,7 +11,11 @@ use yarnspinner::compiler::*; use yarnspinner::core::*; use yarnspinner::runtime::*; use yarnspinner_compiler::compilation_steps::*; +use yarnspinner_compiler::parser::generated::yarnspinnerparser::ValueStringContext; +use yarnspinner_compiler::parser::generated::yarnspinnerparser::YarnSpinnerParserContextType; +use yarnspinner_compiler::parser::generated::yarnspinnerparservisitor::YarnSpinnerParserVisitorCompat; use yarnspinner_compiler::run_compilation::CompilationIntermediate; +use yarnspinner_compiler::ParseTreeVisitorCompat; mod test_base; @@ -271,7 +275,7 @@ fn test_compile_with_custom_steps() { .compile_with_custom_steps(custom_steps) .unwrap(); - // Check that the custom line is present in the string table + // Check if our line is present in the string table assert!(result.string_table.contains_key(&"custom_line_id".into())); assert_eq!( result.string_table.get(&"custom_line_id".into()).unwrap(), @@ -285,3 +289,43 @@ fn test_compile_with_custom_steps() { } ); } + +struct StringLiteralCounter { + pub count: usize, + _dummy: (), +} + +impl StringLiteralCounter { + pub fn new() -> Self { + Self { + count: 0, + _dummy: (), + } + } +} + +impl<'input> ParseTreeVisitorCompat<'input> for StringLiteralCounter { + type Node = YarnSpinnerParserContextType; + type Return = (); + + fn temp_result(&mut self) -> &mut Self::Return { + &mut self._dummy + } +} + +impl<'input> YarnSpinnerParserVisitorCompat<'input> for StringLiteralCounter { + fn visit_valueString(&mut self, _ctx: &ValueStringContext<'input>) -> Self::Return { + self.count += 1; + } +} + +pub fn count_string_literals(mut state: CompilationIntermediate) -> CompilationIntermediate { + let mut string_literal_count = 0; + for file in &state.parsed_files { + let mut visitor = StringLiteralCounter::new(); + visitor.visit(file.tree.as_ref()); + string_literal_count += visitor.count; + } + // state.count = string_literal_count; // Won't work until we traitify CompilationIntermediate + state +}