Skip to content

Commit

Permalink
custom visitor
Browse files Browse the repository at this point in the history
  • Loading branch information
stargazing-dino committed May 18, 2024
1 parent 3c4d42a commit ba357c8
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 11 deletions.
2 changes: 1 addition & 1 deletion crates/compiler/src/file_parse_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DialogueContextAll<'input>>,
Expand Down
3 changes: 2 additions & 1 deletion crates/compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ 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;
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.
Expand Down
6 changes: 3 additions & 3 deletions crates/compiler/src/output/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
2 changes: 1 addition & 1 deletion crates/compiler/src/parser.rs
Original file line number Diff line number Diff line change
@@ -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::*;
Expand Down
8 changes: 4 additions & 4 deletions crates/compiler/src/parser/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
46 changes: 45 additions & 1 deletion crates/yarnspinner/tests/language_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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(),
Expand All @@ -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
}

0 comments on commit ba357c8

Please sign in to comment.