|  | 
| 1 | 1 | use can_dbc_pest::{Pair, Pairs, Rule}; | 
| 2 | 2 | 
 | 
| 3 | 3 | use crate::ast::MessageId; | 
| 4 |  | -use crate::parser::{inner_str, next, next_rule, validated_inner, DbcError}; | 
|  | 4 | +use crate::parser::{ | 
|  | 5 | +    inner_str, next, next_optional_rule, next_rule, next_string, single_inner, validated_inner, | 
|  | 6 | +    DbcError, | 
|  | 7 | +}; | 
| 5 | 8 | 
 | 
| 6 | 9 | /// Object comments | 
| 7 | 10 | #[derive(Clone, Debug, PartialEq)] | 
| @@ -38,94 +41,59 @@ impl TryFrom<Pair<'_, Rule>> for Comment { | 
| 38 | 41 | 
 | 
| 39 | 42 |         let pair = next(&mut inner_pairs)?; | 
| 40 | 43 | 
 | 
| 41 |  | -        match pair.as_rule() { | 
| 42 |  | -            Rule::comment_signal => parse_signal_comment(pair), | 
| 43 |  | -            Rule::comment_message | Rule::comment_message_implicit => parse_message_comment(pair), | 
| 44 |  | -            Rule::comment_node => parse_node_comment(pair), | 
| 45 |  | -            Rule::comment_env_var => parse_env_var_comment(pair), | 
| 46 |  | -            Rule::comment_plain => parse_plain_comment(pair), | 
| 47 |  | -            rule => Err(DbcError::UnknownRule(rule)), | 
|  | 44 | +        if pair.as_rule() == Rule::comment_plain { | 
|  | 45 | +            // Parse plain comment: `"comment"` | 
|  | 46 | +            let comment = inner_str(single_inner(pair, Rule::quoted_str)?); | 
|  | 47 | +            Ok(Comment::Plain { comment }) | 
|  | 48 | +        } else { | 
|  | 49 | +            let rule = pair.as_rule(); | 
|  | 50 | +            let mut inner = pair.into_inner(); | 
|  | 51 | +            match rule { | 
|  | 52 | +                Rule::comment_signal => parse_signal_comment(inner), | 
|  | 53 | +                Rule::comment_message | Rule::comment_message_implicit => { | 
|  | 54 | +                    // Parse message comment: `BO_ <message_id> "comment"` | 
|  | 55 | +                    //      implicit comment: `<message_id> "comment"` | 
|  | 56 | +                    Ok(Comment::Message { | 
|  | 57 | +                        id: next_rule(&mut inner, Rule::message_id)?.try_into()?, | 
|  | 58 | +                        comment: inner_str(next_rule(&mut inner, Rule::quoted_str)?), | 
|  | 59 | +                    }) | 
|  | 60 | +                } | 
|  | 61 | +                Rule::comment_node => { | 
|  | 62 | +                    // Parse node comment: `BU_ <node_name> "comment"` | 
|  | 63 | +                    Ok(Comment::Node { | 
|  | 64 | +                        name: next_string(&mut inner, Rule::node_name)?, | 
|  | 65 | +                        comment: inner_str(next_rule(&mut inner, Rule::quoted_str)?), | 
|  | 66 | +                    }) | 
|  | 67 | +                } | 
|  | 68 | +                Rule::comment_env_var => { | 
|  | 69 | +                    // Parse environment variable comment: `EV_ <env_var_name> "comment"` | 
|  | 70 | +                    Ok(Comment::EnvVar { | 
|  | 71 | +                        name: next_string(&mut inner, Rule::env_var_name)?, | 
|  | 72 | +                        comment: inner_str(next_rule(&mut inner, Rule::quoted_str)?), | 
|  | 73 | +                    }) | 
|  | 74 | +                } | 
|  | 75 | +                rule => Err(DbcError::UnknownRule(rule)), | 
|  | 76 | +            } | 
| 48 | 77 |         } | 
| 49 | 78 |     } | 
| 50 | 79 | } | 
| 51 | 80 | 
 | 
| 52 | 81 | /// Parse signal comment: `SG_ <message_id> [<signal_name>] "comment"` | 
| 53 | 82 | /// If signal_name is omitted, this is treated as a message comment. | 
| 54 |  | -fn parse_signal_comment(pair: Pair<'_, Rule>) -> Result<Comment, DbcError> { | 
| 55 |  | -    let mut pairs = pair.into_inner(); | 
|  | 83 | +fn parse_signal_comment(mut pairs: Pairs<Rule>) -> Result<Comment, DbcError> { | 
| 56 | 84 |     let message_id = next_rule(&mut pairs, Rule::message_id)?.try_into()?; | 
| 57 |  | -    let next_pair = next(&mut pairs)?; | 
| 58 |  | -    match next_pair.as_rule() { | 
| 59 |  | -        Rule::signal_name => { | 
| 60 |  | -            // This is a proper signal comment with signal name | 
| 61 |  | -            Ok(Comment::Signal { | 
| 62 |  | -                message_id, | 
| 63 |  | -                name: next_pair.as_str().to_string(), | 
| 64 |  | -                comment: inner_str(next_rule(&mut pairs, Rule::quoted_str)?), | 
| 65 |  | -            }) | 
| 66 |  | -        } | 
| 67 |  | -        Rule::quoted_str => { | 
| 68 |  | -            // No signal name - treat as message comment | 
| 69 |  | -            Ok(Comment::Message { | 
| 70 |  | -                id: message_id, | 
| 71 |  | -                comment: inner_str(next_rule(&mut pairs, Rule::quoted_str)?), | 
| 72 |  | -            }) | 
| 73 |  | -        } | 
| 74 |  | -        rule => Err(DbcError::UnknownRule(rule)), | 
| 75 |  | -    } | 
| 76 |  | -} | 
| 77 |  | - | 
| 78 |  | -/// Parse message comment: `BO_ <message_id> "comment"` | 
| 79 |  | -fn parse_message_comment(pair: Pair<'_, Rule>) -> Result<Comment, DbcError> { | 
| 80 |  | -    let mut inner = pair.into_inner(); | 
| 81 |  | -    let message_id = next_rule(&mut inner, Rule::message_id)?.try_into()?; | 
| 82 |  | -    let comment = inner_str(next_rule(&mut inner, Rule::quoted_str)?); | 
| 83 |  | - | 
| 84 |  | -    Ok(Comment::Message { | 
| 85 |  | -        id: message_id, | 
| 86 |  | -        comment, | 
| 87 |  | -    }) | 
| 88 |  | -} | 
| 89 |  | - | 
| 90 |  | -/// Parse node comment: `BU_ <node_name> "comment"` | 
| 91 |  | -fn parse_node_comment(pair: Pair<'_, Rule>) -> Result<Comment, DbcError> { | 
| 92 |  | -    let mut inner = pair.into_inner(); | 
| 93 |  | -    let node_name = next_ident(&mut inner)?; | 
| 94 |  | -    let comment = inner_str(next_rule(&mut inner, Rule::quoted_str)?); | 
| 95 |  | - | 
| 96 |  | -    Ok(Comment::Node { | 
| 97 |  | -        name: node_name, | 
| 98 |  | -        comment, | 
| 99 |  | -    }) | 
| 100 |  | -} | 
| 101 |  | - | 
| 102 |  | -/// Parse environment variable comment: `EV_ <env_var_name> "comment"` | 
| 103 |  | -fn parse_env_var_comment(pair: Pair<'_, Rule>) -> Result<Comment, DbcError> { | 
| 104 |  | -    let mut inner = pair.into_inner(); | 
| 105 |  | -    let env_var_name = next_ident(&mut inner)?; | 
| 106 |  | -    let comment = inner_str(next_rule(&mut inner, Rule::quoted_str)?); | 
| 107 |  | - | 
| 108 |  | -    Ok(Comment::EnvVar { | 
| 109 |  | -        name: env_var_name, | 
| 110 |  | -        comment, | 
| 111 |  | -    }) | 
| 112 |  | -} | 
| 113 |  | - | 
| 114 |  | -/// Parse plain comment: `"comment"` | 
| 115 |  | -fn parse_plain_comment(pair: Pair<'_, Rule>) -> Result<Comment, DbcError> { | 
| 116 |  | -    // comment_plain contains a quoted_str, so we need to get the inner pair | 
| 117 |  | -    let mut inner = pair.into_inner(); | 
| 118 |  | -    let comment = inner_str(next_rule(&mut inner, Rule::quoted_str)?); | 
| 119 |  | -    Ok(Comment::Plain { comment }) | 
| 120 |  | -} | 
| 121 |  | - | 
| 122 |  | -/// Helper to get next identifier string from pairs iterator | 
| 123 |  | -fn next_ident<'a>(iter: &'a mut Pairs<Rule>) -> Result<String, DbcError> { | 
| 124 |  | -    let pair = next(iter)?; | 
| 125 |  | -    match pair.as_rule() { | 
| 126 |  | -        Rule::signal_name | Rule::node_name | Rule::env_var_name | Rule::ident => { | 
| 127 |  | -            Ok(pair.as_str().to_string()) | 
| 128 |  | -        } | 
| 129 |  | -        rule => Err(DbcError::UnknownRule(rule)), | 
|  | 85 | +    if let Some(name) = next_optional_rule(&mut pairs, Rule::signal_name)? { | 
|  | 86 | +        // This is a proper signal comment with signal name | 
|  | 87 | +        Ok(Comment::Signal { | 
|  | 88 | +            message_id, | 
|  | 89 | +            name: name.as_str().to_string(), | 
|  | 90 | +            comment: inner_str(next_rule(&mut pairs, Rule::quoted_str)?), | 
|  | 91 | +        }) | 
|  | 92 | +    } else { | 
|  | 93 | +        // No signal name - treat as message comment | 
|  | 94 | +        Ok(Comment::Message { | 
|  | 95 | +            id: message_id, | 
|  | 96 | +            comment: inner_str(next_rule(&mut pairs, Rule::quoted_str)?), | 
|  | 97 | +        }) | 
| 130 | 98 |     } | 
| 131 | 99 | } | 
0 commit comments