ExprAsKeyword attempts to interpret the given expression as a static keyword, returning the keyword string if possible, and the empty string if not. A static keyword, for the sake of this function, is a single identifier. For example, the following attribute has an expression that would produce the
(expr Expression)
| 109 | // a fixed set of keywords is used in a structural way in a special attribute |
| 110 | // to affect the further processing of a block. |
| 111 | func ExprAsKeyword(expr Expression) string { |
| 112 | type asTraversal interface { |
| 113 | AsTraversal() Traversal |
| 114 | } |
| 115 | |
| 116 | physExpr := UnwrapExpressionUntil(expr, func(expr Expression) bool { |
| 117 | _, supported := expr.(asTraversal) |
| 118 | return supported |
| 119 | }) |
| 120 | |
| 121 | if asT, supported := physExpr.(asTraversal); supported { |
| 122 | if traversal := asT.AsTraversal(); len(traversal) == 1 { |
| 123 | return traversal.RootName() |
| 124 | } |
| 125 | } |
| 126 | return "" |
| 127 | } |