Skip to main content

tackler_core/
parser.rs

1/*
2 * Tackler-NG 2022-2025
3 * SPDX-License-Identifier: Apache-2.0
4 */
5pub use crate::parser::pricedb_parser::{pricedb_from_file, pricedb_from_str};
6pub use crate::parser::tackler_txns::git_to_txns;
7pub use crate::parser::tackler_txns::paths_to_txns;
8pub use crate::parser::tackler_txns::string_to_txns;
9use winnow::error::{ErrMode, FromExternalError};
10
11mod error;
12mod pricedb_parser;
13mod tackler_parser;
14mod tackler_txns;
15
16use crate::kernel::settings::Settings;
17use crate::parser::error::TacklerTxnError;
18use winnow::Stateful;
19
20pub(crate) mod parts;
21
22pub(crate) type Stream<'is> = Stateful<&'is str, &'is mut Settings>;
23
24pub(crate) fn make_semantic_error<
25    'is,
26    E: winnow::error::FromExternalError<Stream<'is>, TacklerTxnError>,
27>(
28    is: &mut Stream<'is>,
29    msg: &str,
30) -> ErrMode<E> {
31    ErrMode::from_external_error(is, TacklerTxnError::semantic_error(msg)).cut()
32}
33
34pub(crate) fn from_error<
35    'is,
36    E: winnow::error::FromExternalError<Stream<'is>, TacklerTxnError>,
37    SE: std::error::Error + ?Sized,
38>(
39    is: &mut Stream<'is>,
40    err: &SE,
41) -> ErrMode<E> {
42    ErrMode::from_external_error(
43        is,
44        TacklerTxnError::semantic_error(err.to_string().as_str()),
45    )
46    .cut()
47}
48
49/*
50 * TODO: This logic should be 1:1 with TxnLexer.g4
51 *       (ID, SUBID and NameChar + NameStartChar)
52 * Real account names are coming through parser+lexer,
53 * So these are validating Chart-of-Xyz config/settings
54 * entries (accounts, tags, commodities).
55 * E.g. checking these is a nicety for user
56 * (warn about invalid Chart-Of-Xyz).
57 */
58#[inline]
59fn illegal_characters(c: char) -> bool {
60    c == ':' || c.is_whitespace()
61}
62
63// todo: this is too relaxed
64fn is_valid_id_start_char(c: char) -> bool {
65    !(c.is_ascii_digit() || c == ':' || c == '-' || c == '_' || c == 'ยท' || c.is_whitespace())
66}
67
68// this is fine, once is_valid_id_start_char is fixed
69fn is_valid_sub_id_start_char(c: char) -> bool {
70    c.is_numeric() || is_valid_id_start_char(c)
71}
72
73// todo: this is too relaxed
74pub fn is_valid_id(token: &str) -> bool {
75    !token.is_empty()
76        && token.starts_with(is_valid_id_start_char)
77        && !token.contains(illegal_characters)
78}
79
80// todo: this is too relaxed
81pub fn is_valid_sub_id(token: &str) -> bool {
82    !token.is_empty()
83        && token.starts_with(is_valid_sub_id_start_char)
84        && !token.contains(illegal_characters)
85}
86
87#[cfg(test)]
88mod tests;