| 1 | //! Accessors for `vpath` directives. |
| 2 | |
| 3 | use crate::lossless::Vpath; |
| 4 | use crate::SyntaxKind::*; |
| 5 | use rowan::ast::AstNode; |
| 6 | |
| 7 | impl Vpath { |
| 8 | /// Returns the pattern argument of the `vpath` directive, if any. |
| 9 | /// |
| 10 | /// `vpath` (no args) returns `None`. |
| 11 | /// `vpath PATTERN` returns `Some("PATTERN")`. |
| 12 | /// `vpath PATTERN DIRS` returns `Some("PATTERN")`. |
| 13 | pub fn pattern(&self) -> Option<String> { |
| 14 | // Walk tokens: skip the leading `vpath` keyword and whitespace, |
| 15 | // then collect tokens up to the next whitespace or to the EXPR |
| 16 | // (directories) node. |
| 17 | let mut after_keyword = false; |
| 18 | let mut out = String::new(); |
| 19 | for child in self.syntax().children_with_tokens() { |
| 20 | match child { |
| 21 | rowan::NodeOrToken::Token(t) => { |
| 22 | if !after_keyword { |
| 23 | if t.kind() == IDENTIFIER && t.text() == "vpath" { |
| 24 | after_keyword = true; |
| 25 | } |
| 26 | continue; |
| 27 | } |
| 28 | if t.kind() == WHITESPACE { |
| 29 | if out.is_empty() { |
| 30 | continue; |
| 31 | } else { |
| 32 | break; |
| 33 | } |
| 34 | } |
| 35 | if t.kind() == NEWLINE { |
| 36 | break; |
| 37 | } |
| 38 | out.push_str(t.text()); |
| 39 | } |
| 40 | rowan::NodeOrToken::Node(_) => { |
| 41 | // The EXPR (directories) node marks the end of the |
| 42 | // pattern. |
| 43 | break; |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | if out.is_empty() { |
| 48 | None |
| 49 | } else { |
| 50 | Some(out) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /// Returns the raw directory-list text (everything after the pattern), |
| 55 | /// or `None` if the directive has no directories. |
| 56 | pub fn directories_text(&self) -> Option<String> { |
| 57 | self.syntax() |
| 58 | .children() |
| 59 | .find(|c| c.kind() == EXPR) |
| 60 | .map(|n| n.text().to_string()) |
| 61 | } |
| 62 | } |