summaryrefslogtreecommitdiff
path: root/src/syntax
diff options
context:
space:
mode:
Diffstat (limited to 'src/syntax')
-rw-r--r--src/syntax/mod.rs21
1 files changed, 20 insertions, 1 deletions
diff --git a/src/syntax/mod.rs b/src/syntax/mod.rs
index 57488121..bf395c2b 100644
--- a/src/syntax/mod.rs
+++ b/src/syntax/mod.rs
@@ -235,13 +235,17 @@ debug_display!(Expression);
pub struct Ident(pub String);
impl Ident {
- fn new(string: String) -> ParseResult<Ident> {
+ pub fn new(string: String) -> ParseResult<Ident> {
if is_identifier(&string) {
Ok(Ident(string))
} else {
error!("invalid identifier: `{}`", string);
}
}
+
+ pub fn as_str(&self) -> &str {
+ self.0.as_str()
+ }
}
impl Display for Ident {
@@ -317,3 +321,18 @@ impl<T> ExpressionKind for Spanned<T> where T: ExpressionKind {
.map(|v| Spanned::new(v, span))
}
}
+
+impl<T> ExpressionKind for Option<T> where T: ExpressionKind {
+ const NAME: &'static str = T::NAME;
+
+ fn from_expr(expr: Spanned<Expression>) -> ParseResult<Option<T>> {
+ if let Expression::Ident(ident) = &expr.v {
+ match ident.as_str() {
+ "default" | "none" => return Ok(None),
+ _ => {},
+ }
+ }
+
+ T::from_expr(expr).map(|v| Some(v))
+ }
+}