Crates.io | webidl |
lib.rs | webidl |
version | 0.9.0 |
source | src |
created_at | 2017-06-21 00:33:46.718107 |
updated_at | 2019-05-08 06:52:16.287731 |
description | A WebIDL parser |
homepage | https://github.com/sgodwincs/webidl-rs |
repository | https://github.com/sgodwincs/webidl-rs |
max_upload_size | |
id | 19875 |
size | 234,535 |
A parser for WebIDL in Rust.
use webidl::*;
let lexer = Lexer::new("/* Example taken from emscripten site */\n\
enum EnumClass_EnumWithinClass {\n\
\"EnumClass::e_val\"\n\
};");
assert_eq!(lexer.collect::<Vec<_>>(),
vec![Ok((41, Token::Enum, 45)),
Ok((46, Token::Identifier("EnumClass_EnumWithinClass".to_string()), 71)),
Ok((72, Token::LeftBrace, 73)),
Ok((74, Token::StringLiteral("EnumClass::e_val".to_string()), 92)),
Ok((93, Token::RightBrace, 94)),
Ok((94, Token::Semicolon, 95))]);
use webidl::*;
use webidl::ast::*;
let result = parse_string("[Attribute] interface Node { };");
assert_eq!(result,
Ok(vec![Definition::Interface(Interface::NonPartial(NonPartialInterface {
extended_attributes: vec![
Box::new(ExtendedAttribute::NoArguments(
Other::Identifier("Attribute".to_string())))],
inherits: None,
members: vec![],
name: "Node".to_string()
}))]));
An example of a visitor implementation can be found here. Below is an example of how it is used:
use webidl::ast::*;
use webidl::visitor::*;
let ast = vec![Definition::Interface(Interface::NonPartial(NonPartialInterface {
extended_attributes: vec![
Box::new(ExtendedAttribute::NoArguments(
Other::Identifier("Attribute".to_string())))],
inherits: None,
members: vec![InterfaceMember::Attribute(Attribute::Regular(RegularAttribute {
extended_attributes: vec![],
inherits: false,
name: "attr".to_string(),
read_only: true,
type_: Box::new(Type {
extended_attributes: vec![],
kind: TypeKind::SignedLong,
nullable: true
})
}))],
name: "Node".to_string()
}))];
let mut visitor = PrettyPrintVisitor::new();
visitor.visit(&ast);
assert_eq!(visitor.get_output(),
"[Attribute]\ninterface Node {\n readonly attribute long? attr;\n};\n\n");
The parser is conformant with regards to the WebIDL grammar except for three points:
A=B
. The specification states that A
and B
must be identifiers, but this parser accepts B
as any token. If you would like for any extended attributes to be parsed (essentially any sequences of tokens), please consider looking at #8 to help resolve the problem with doing so.implements
keyword that is no longer a part of the official specification. This is for backwards compatibility.legacycaller
keyword that is no longer a part of the official specification. This is for backwards compatibility.