DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (cf48e59e88de)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
use spec::{self, Laziness, SpecBuilder, TypeSpec, TypeSum};
use weedle::common::Identifier;
use weedle::types::*;
use weedle::*;

fn nullable<T: std::fmt::Debug>(src: &MayBeNull<T>, dst: TypeSpec) -> spec::Type {
    if src.q_mark.is_some() {
        dst.optional()
            .unwrap_or_else(|| panic!("This type could not be made optional {:?}", src.type_))
    } else {
        dst.required()
    }
}

pub struct Importer {
    builder: SpecBuilder,
    /// The interfaces we have traversed so far.
    path: Vec<String>,
}

impl Importer {
    /// Import a WebIDL spec into a SpecBuilder.
    ///
    /// A WebIDL spec may consist in several files. Files are parsed in the order
    /// of `sources`. An extension file (e.g. `es6-extended.webidl`) MUST appear
    /// after the files it extends.
    ///
    /// ```
    /// extern crate binjs_meta;
    /// use binjs_meta::spec::SpecOptions;
    ///
    /// let mut builder = binjs_meta::import::Importer::import(vec!["
    ///    interface FooContents {
    ///      attribute boolean value;
    ///    };
    ///    interface LazyFoo {
    ///       [Lazy] attribute FooContents contents;
    ///    };
    ///    interface EagerFoo {
    ///       attribute FooContents contents;
    ///    };
    /// "].into_iter()).expect("Could not parse");
    ///
    /// let fake_root = builder.node_name("@@ROOT@@"); // Unused
    /// let null = builder.node_name(""); // Used
    /// let spec = builder.into_spec(SpecOptions {
    ///     root: &fake_root,
    ///     null: &null,
    /// });
    ///
    /// let name_eager = spec.get_node_name("EagerFoo")
    ///     .expect("Missing name EagerFoo");
    /// let name_lazy = spec.get_node_name("LazyFoo")
    ///     .expect("Missing name LazyFoo");
    /// let name_contents = spec.get_field_name("contents")
    ///     .expect("Missing name contents");
    ///
    /// {
    ///     let interface_eager = spec.get_interface_by_name(&name_eager)
    ///         .expect("Missing interface EagerFoo");
    ///     let contents_field =
    ///         interface_eager.get_field_by_name(&name_contents)
    ///         .expect("Missing field contents");
    ///     assert_eq!(contents_field.is_lazy(), false);
    /// }
    ///
    /// {
    ///     let interface_lazy = spec.get_interface_by_name(&name_lazy)
    ///         .expect("Missing interface LazyFoo");
    ///     let contents_field =
    ///         interface_lazy.get_field_by_name(&name_contents)
    ///         .expect("Missing field contents");
    ///     assert_eq!(contents_field.is_lazy(), true);
    /// }
    /// ```
    pub fn import<'a>(
        sources: impl IntoIterator<Item = &'a str>,
    ) -> Result<SpecBuilder, weedle::Err<(&'a str, ErrorKind)>> {
        let mut importer = Importer {
            path: Vec::with_capacity(256),
            builder: SpecBuilder::new(),
        };
        for source in sources {
            let ast = weedle::parse(source)?;
            importer.import_all_definitions(&ast);
        }
        Ok(importer.builder)
    }

    fn import_all_definitions(&mut self, ast: &Definitions) {
        for definition in ast {
            self.import_definition(&definition)
        }
    }

    fn import_definition(&mut self, def: &Definition) {
        match *def {
            Definition::Enum(ref enum_) => self.import_enum(enum_),
            Definition::Typedef(ref typedef) => self.import_typedef(typedef),
            Definition::Interface(ref interface) => self.import_interface(interface),
            _ => panic!("Not implemented: importing {:?}", def),
        }
    }

    fn import_enum(&mut self, enum_: &EnumDefinition) {
        let name = self.builder.node_name(enum_.identifier.0);
        let mut node = self
            .builder
            .add_string_enum(&name)
            .expect("Name already present");
        for variant in &enum_.values.body.list {
            node.with_string(&variant.0);
        }
    }

    fn import_typedef(&mut self, typedef: &TypedefDefinition) {
        let name = self.builder.node_name(typedef.identifier.0);
        // The following are, unfortunately, not true typedefs.
        // Ignore their definition.
        let type_ = match typedef.identifier.0 {
            "Identifier" => TypeSpec::IdentifierName.required(),
            "IdentifierName" => TypeSpec::IdentifierName.required(),
            "PropertyKey" => TypeSpec::PropertyKey.required(),
            _ => self.convert_type(&typedef.type_.type_),
        };
        debug!(target: "meta::import", "Importing typedef {type_:?} {name:?}",
            type_ = type_,
            name = name);
        let mut node = self.builder.add_typedef(&name).unwrap_or_else(|| {
            panic!(
                "Error: Name {} is defined more than once in the spec.",
                name
            )
        });
        assert!(!type_.is_optional());
        node.with_spec(type_.spec);
    }

    fn import_interface(&mut self, interface: &InterfaceDefinition) {
        // Handle special, hardcoded, interfaces.
        match interface.identifier.0 {
            "Node" => {
                // We're not interested in the root interface.
                return;
            }
            "IdentifierName" => unimplemented!(),
            _ => {}
        }
        if let Some(ref parent) = interface.inheritance {
            assert_eq!(parent.identifier.0, "Node");
        }

        self.path.push(interface.identifier.0.to_owned());

        // Now handle regular stuff.
        let mut fields = Vec::new();
        for member in &interface.members.body {
            if let interface::InterfaceMember::Attribute(interface::AttributeInterfaceMember {
                modifier: None,
                attributes,
                identifier,
                type_,
                ..
            }) = member
            {
                let name = self.builder.field_name(identifier.0);
                let type_ = self.convert_type(&type_.type_);

                let is_lazy = attributes
                    .iter()
                    .flat_map(|attribute| &attribute.body.list)
                    .find(|attribute| match attribute {
                        attribute::ExtendedAttribute::NoArgs(
                            attribute::ExtendedAttributeNoArgs(Identifier("Lazy")),
                        ) => true,
                        _ => false,
                    })
                    .is_some();

                fields.push((
                    name,
                    type_,
                    if is_lazy {
                        Laziness::Lazy
                    } else {
                        Laziness::Eager
                    },
                ));
            } else {
                panic!("Expected an attribute, got {:?}", member);
            }
        }
        let name = self.builder.node_name(interface.identifier.0);

        // Set to `Some("Foo")` if this interface has attribute
        // `[ExtendsTypeSum=Foo]`.
        let mut extends_type_sum = None;
        let mut scoped_dictionary = None;
        {
            let mut node = self
                .builder
                .add_interface(&name)
                .expect("Name already present");
            for (field_name, field_type, laziness) in fields.drain(..) {
                node.with_field_laziness(&field_name, field_type, laziness);
            }

            for attribute in interface
                .attributes
                .iter()
                .flat_map(|attribute| &attribute.body.list)
            {
                use weedle::attribute::ExtendedAttribute::*;
                use weedle::attribute::*;
                match *attribute {
                    NoArgs(ExtendedAttributeNoArgs(Identifier("Skippable"))) => {
                        panic!("Encountered deprecated attribute [Skippable]");
                    }
                    NoArgs(ExtendedAttributeNoArgs(Identifier("Scope"))) => {
                        node.with_scope(true);
                    }
                    Ident(ExtendedAttributeIdent {
                        lhs_identifier: Identifier("ExtendsTypeSum"),
                        assign: _,
                        rhs: IdentifierOrString::Identifier(ref rhs),
                    }) => {
                        assert!(extends_type_sum.is_none());
                        extends_type_sum = Some(rhs.0);
                    }
                    Ident(ExtendedAttributeIdent {
                        lhs_identifier: Identifier("ScopedDictionary"),
                        assign: _,
                        rhs: IdentifierOrString::Identifier(ref rhs),
                    }) => {
                        assert!(scoped_dictionary.is_none());
                        scoped_dictionary = Some(rhs.0);
                    }
                    _ => panic!("Unknown attribute {:?}", attribute),
                }
            }

            // If the node contains an attribute `[ScopedDictionary=field]`,
            // mark the node as inserting a scoped dictionary with this field.
            if let Some(ref field_name) = scoped_dictionary {
                node.with_scoped_dictionary_str(field_name);
            }
        }

        // If the node contains an attribute `[ExtendsTypeSum=Foobar]`,
        // extend `typedef (... or ... or ...) Foobar` into
        // `typedef (... or ... or ... or CurrentNode) Foobar`.
        if let Some(ref extended) = extends_type_sum {
            let node_name = self
                .builder
                .get_node_name(extended)
                .unwrap_or_else(|| panic!("Could not find node name {}", extended));
            let mut typedef = self
                .builder
                .get_typedef_mut(&node_name)
                .unwrap_or_else(|| panic!("Could not find typedef {}", extended));
            let typespec = typedef.spec_mut();
            let typesum = if let TypeSpec::TypeSum(ref mut typesum) = *typespec {
                typesum
            } else {
                panic!(
                    "Attempting to extend a node that is not a type sum {}",
                    extended
                );
            };
            typesum.with_type_case(TypeSpec::NamedType(name));
        }

        self.path.pop();
    }

    fn convert_single_type(&mut self, t: &NonAnyType) -> spec::Type {
        match t {
            NonAnyType::Boolean(ref b) => nullable(b, TypeSpec::Boolean),
            NonAnyType::Identifier(ref id) => nullable(id, {
                let name = self.builder.node_name(id.type_.0);
                // Sadly, some identifiers are not truly `typedef`s.
                match name.to_str() {
                    "IdentifierName" if self.is_at_interface("StaticMemberAssignmentTarget") => {
                        TypeSpec::PropertyKey
                    }
                    "IdentifierName" if self.is_at_interface("StaticMemberExpression") => {
                        TypeSpec::PropertyKey
                    }
                    "IdentifierName" if self.is_at_interface("ImportSpecifier") => {
                        TypeSpec::PropertyKey
                    }
                    "IdentifierName" if self.is_at_interface("ExportSpecifier") => {
                        TypeSpec::PropertyKey
                    }
                    "IdentifierName" if self.is_at_interface("ExportLocalSpecifier") => {
                        TypeSpec::PropertyKey
                    }
                    "IdentifierName" => TypeSpec::IdentifierName,
                    "Identifier" => TypeSpec::IdentifierName,
                    _ => TypeSpec::NamedType(name.clone()),
                }
            }),
            NonAnyType::DOMString(ref s) => nullable(
                s,
                if self.is_at_interface("LiteralPropertyName") {
                    TypeSpec::PropertyKey
                } else {
                    TypeSpec::String
                },
            ),
            NonAnyType::FrozenArrayType(ref t) => nullable(
                t,
                TypeSpec::Array {
                    contents: Box::new(self.convert_type(&t.type_.generics.body)),
                    supports_empty: true,
                },
            ),
            NonAnyType::FloatingPoint(ref t) => nullable(t, TypeSpec::Number),
            NonAnyType::Integer(ref t) => nullable(t, TypeSpec::UnsignedLong),
            _ => {
                panic!("I don't know how to import {:?} yet", t);
            }
        }
    }

    fn convert_union_type(&mut self, types: &MayBeNull<UnionType>) -> spec::Type {
        let converted_types: Vec<_> = types
            .type_
            .body
            .list
            .iter()
            .map(|t| match t {
                UnionMemberType::Single(AttributedNonAnyType { type_: t, .. }) => {
                    self.convert_single_type(t)
                }
                UnionMemberType::Union(t) => self.convert_union_type(t),
            })
            .map(|t| t.spec)
            .collect();

        nullable(types, TypeSpec::TypeSum(TypeSum::new(converted_types)))
    }

    fn convert_type(&mut self, t: &Type) -> spec::Type {
        match t {
            Type::Single(SingleType::NonAny(t)) => self.convert_single_type(t),
            Type::Union(types) => self.convert_union_type(types),
            _ => panic!("I don't know how to import {:?} yet", t),
        }
    }

    fn is_at_interface(&self, name: &str) -> bool {
        if self.path.len() == 0 {
            return false;
        }
        self.path[0].as_str() == name
    }
}