def _LookupValue(module, parent_kind, implied_kind, ast_leaf_node): """Resolves a leaf node in the form ('IDENTIFIER', 'x') to a constant value identified by 'x' in some mojom definition. parent_kind is used as context when resolving the identifier. If the given leaf node is not an IDENTIFIER (e.g. already a constant value), it is returned as-is. If implied_kind is provided, the parsed identifier may also be resolved within its scope as fallback. This can be useful for more concise value references when assigning enum-typed constants or field values.""" if not isinstance(ast_leaf_node, tuple) or ast_leaf_node[0] != 'IDENTIFIER': return ast_leaf_node # First look for a known user-defined identifier to resolve this within the # enclosing scope. identifier = ast_leaf_node[1] value = _LookupValueInScope(module, parent_kind, identifier) if value: return value # Next look in the scope of implied_kind, if provided. value = (implied_kind and implied_kind.module and _LookupValueInScope( implied_kind.module, implied_kind, identifier)) if value: return value # Fall back on defined builtin symbols if _IsBuiltinValue(identifier): return mojom.BuiltinValue(identifier) raise ValueError('Unknown identifier %s' % identifier)
def _FixupExpression(module, value, scope, kind): """Translates an IDENTIFIER into a built-in value or structured NamedValue object.""" if isinstance(value, tuple) and value[0] == 'IDENTIFIER': # Allow user defined values to shadow builtins. result = _LookupValue(module.values, value[1], scope, kind) if result: if isinstance(result, tuple): raise Exception('Unable to resolve expression: %r' % value[1]) return result if _IsBuiltinValue(value[1]): return mojom.BuiltinValue(value[1]) return value