mirror of
https://codeberg.org/icewind/logsmash.git
synced 2026-06-03 10:04:12 +02:00
fmt extractor
This commit is contained in:
parent
095c957d0d
commit
9140759082
2 changed files with 26 additions and 7 deletions
|
|
@ -213,7 +213,11 @@ impl LogExtractor {
|
|||
.unwrap_or("")
|
||||
}
|
||||
|
||||
fn get_context_assignments<'a>(&'a self, code: &'a str, mut node: Node<'a>) -> HashMap<&'a str, Node<'a>> {
|
||||
fn get_context_assignments<'a>(
|
||||
&'a self,
|
||||
code: &'a str,
|
||||
mut node: Node<'a>,
|
||||
) -> HashMap<&'a str, Node<'a>> {
|
||||
let mut assignments = HashMap::new();
|
||||
let mut cursor = node.walk();
|
||||
while let Some(parent) = node.parent() {
|
||||
|
|
@ -221,10 +225,19 @@ impl LogExtractor {
|
|||
if ["method_declaration", "function_definition"].contains(&parent.grammar_name()) {
|
||||
break;
|
||||
}
|
||||
let child_assignments = node.children(&mut cursor)
|
||||
.filter_map(|child| (child.grammar_name() == "expression_statement").then(|| child.child(0).unwrap()))
|
||||
let child_assignments = node
|
||||
.children(&mut cursor)
|
||||
.filter_map(|child| {
|
||||
(child.grammar_name() == "expression_statement")
|
||||
.then(|| child.child(0).unwrap())
|
||||
})
|
||||
.filter(|child| child.grammar_name() == "assignment_expression")
|
||||
.filter_map(|child| Some((child.child_by_field_name("left")?.child(1).unwrap(), child.child_by_field_name("right")?)))
|
||||
.filter_map(|child| {
|
||||
Some((
|
||||
child.child_by_field_name("left")?.child(1).unwrap(),
|
||||
child.child_by_field_name("right")?,
|
||||
))
|
||||
})
|
||||
.map(|(left, right)| (left.utf8_text(code.as_bytes()).unwrap(), right));
|
||||
assignments.extend(child_assignments);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
use std::collections::HashMap;
|
||||
use crate::string::{unescape, DoubleQuoteString, SingleQuoteString};
|
||||
use crate::MessagePart;
|
||||
use regex::Regex;
|
||||
use sprintf::parser::{parse_format_string, FormatElement};
|
||||
use std::collections::HashMap;
|
||||
use tree_sitter::{Node, TreeCursor};
|
||||
|
||||
pub struct MessageBuilder {
|
||||
|
|
@ -99,7 +99,10 @@ impl MessageBuilder {
|
|||
}
|
||||
}
|
||||
"variable_name" => {
|
||||
let name = node.child(1).map(|c| c.utf8_text(code.as_bytes()).unwrap()).unwrap_or_default();
|
||||
let name = node
|
||||
.child(1)
|
||||
.map(|c| c.utf8_text(code.as_bytes()).unwrap())
|
||||
.unwrap_or_default();
|
||||
if let Some(replacement) = context.remove(name) {
|
||||
if has_literal(replacement, code, context) {
|
||||
self.push_node(replacement, code, context);
|
||||
|
|
@ -159,7 +162,10 @@ impl MessageBuilder {
|
|||
fn has_literal(node: Node, code: &str, context: &HashMap<&str, Node>) -> bool {
|
||||
let mut replacement_builder = MessageBuilder::with_capacity(4);
|
||||
replacement_builder.push_node(node.clone(), code, &mut context.clone());
|
||||
replacement_builder.parts.iter().any(|part| matches!(part, MessagePart::Literal(_)))
|
||||
replacement_builder
|
||||
.parts
|
||||
.iter()
|
||||
.any(|part| matches!(part, MessagePart::Literal(_)))
|
||||
}
|
||||
|
||||
impl From<MessageBuilder> for Vec<MessagePart> {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue