MCPcopy Create free account
hub / github.com/PHPantom-dev/phpantom_lsp / parse_attribute_target_flags

Function parse_attribute_target_flags

src/parser/classes.rs:86–128  ·  view source on GitHub ↗

Parse a target-flag expression from the argument to `#[\Attribute(…)]`. Handles `|`-separated lists of `Attribute::TARGET_*` or bare `TARGET_*` constants, as well as plain integer literals.

(text: &str)

Source from the content-addressed store, hash-verified

84/// Handles `|`-separated lists of `Attribute::TARGET_*` or bare
85/// `TARGET_*` constants, as well as plain integer literals.
86fn parse_attribute_target_flags(text: &str) -> u8 {
87 use crate::types::attribute_target;
88
89 let text = text.trim();
90
91 // Try plain integer literal first.
92 if let Ok(n) = text.parse::<u8>() {
93 return n;
94 }
95
96 let mut flags: u8 = 0;
97 for part in text.split('|') {
98 let part = part.trim();
99 // Strip optional `Attribute::` or `self::` prefix.
100 let constant = part
101 .strip_prefix("Attribute::")
102 .or_else(|| part.strip_prefix("\\Attribute::"))
103 .or_else(|| part.strip_prefix("self::"))
104 .unwrap_or(part);
105
106 flags |= match constant {
107 "TARGET_CLASS" => attribute_target::TARGET_CLASS,
108 "TARGET_FUNCTION" => attribute_target::TARGET_FUNCTION,
109 "TARGET_METHOD" => attribute_target::TARGET_METHOD,
110 "TARGET_PROPERTY" => attribute_target::TARGET_PROPERTY,
111 "TARGET_CLASS_CONSTANT" => attribute_target::TARGET_CLASS_CONSTANT,
112 "TARGET_PARAMETER" => attribute_target::TARGET_PARAMETER,
113 "TARGET_ALL" => attribute_target::TARGET_ALL,
114 _ => {
115 // Unrecognised constant — try parsing as an integer.
116 constant.trim().parse::<u8>().unwrap_or_default()
117 }
118 };
119 }
120
121 // If we matched the `#[Attribute]` name but couldn't parse any
122 // flags, default to TARGET_ALL.
123 if flags == 0 {
124 attribute_target::TARGET_ALL
125 } else {
126 flags
127 }
128}
129
130/// Class, interface, trait, and enum extraction.
131///

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected