webidl-rs

LICENSE Build Status Crates.io Version

A parser for WebIDL in Rust.

Documentation

Example

Lexing

```rust use webidl::*;

let lexer = Lexer::new("/* Example taken from emscripten site */\n\ enum EnumClassEnumWithinClass {\n\ \"EnumClass::eval\"\n\ };"); asserteq!(lexer.collect::>(), vec![Ok((41, Token::Enum, 45)), Ok((46, Token::Identifier("EnumClassEnumWithinClass".tostring()), 71)), Ok((72, Token::LeftBrace, 73)), Ok((74, Token::StringLiteral("EnumClass::eval".to_string()), 92)), Ok((93, Token::RightBrace, 94)), Ok((94, Token::Semicolon, 95))]); ```

Parsing

```rust use webidl::; use webidl::ast::;

let result = parse_string("[Attribute] interface Node { };");

asserteq!(result, Ok(vec![Definition::Interface(Interface::NonPartial(NonPartialInterface { extendedattributes: vec![ Box::new(ExtendedAttribute::NoArguments( Other::Identifier("Attribute".tostring())))], inherits: None, members: vec![], name: "Node".tostring() }))])); ```

Pretty printing AST

An example of a visitor implementation can be found here. Below is an example of how it is used:

```rust use webidl::ast::; use webidl::visitor::;

let ast = vec![Definition::Interface(Interface::NonPartial(NonPartialInterface { extendedattributes: vec![ Box::new(ExtendedAttribute::NoArguments( Other::Identifier("Attribute".tostring())))], inherits: None, members: vec![InterfaceMember::Attribute(Attribute::Regular(RegularAttribute { extendedattributes: vec![], inherits: false, name: "attr".tostring(), readonly: true, type: Box::new(Type { extendedattributes: vec![], kind: TypeKind::SignedLong, nullable: true }) }))], name: "Node".tostring() }))]; let mut visitor = PrettyPrintVisitor::new(); visitor.visit(&ast); asserteq!(visitor.getoutput(), "[Attribute]\ninterface Node {\n readonly attribute long? attr;\n};\n\n"); ```

Conformance

The parser is conformant with regards to the WebIDL grammar except for three points: