12 releases (7 breaking)

Uses old Rust 2015

0.9.0 May 8, 2019
0.8.0 Oct 10, 2018
0.7.0 Jul 14, 2018
0.5.0 Mar 9, 2018
0.3.2 Jul 3, 2017

#1839 in Parser implementations

Download history 169/week @ 2024-07-21 260/week @ 2024-07-28 171/week @ 2024-08-04 219/week @ 2024-08-11 173/week @ 2024-08-18 238/week @ 2024-08-25 233/week @ 2024-09-01 186/week @ 2024-09-08 174/week @ 2024-09-15 226/week @ 2024-09-22 205/week @ 2024-09-29 33/week @ 2024-10-06 139/week @ 2024-10-13 142/week @ 2024-10-20 183/week @ 2024-10-27 154/week @ 2024-11-03

618 downloads per month
Used in 11 crates (3 directly)

MIT license

225KB
5K SLoC

Rust 3.5K SLoC // 0.0% comments LALRPOP 1.5K SLoC // 0.0% comments

webidl-rs

LICENSE Build Status Crates.io Version

A parser for WebIDL in Rust.

Documentation

Example

Lexing

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))]);

Parsing

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()
           }))]));

Pretty printing AST

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");

Conformance

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

  • Extended attributes, as described by the grammar, are not supported due to their lack of semantic meaning when parsed. Instead, limited forms are supported (as shown in the table). This parser allows a bit more flexibility when parsing extended attributes of the form 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.
  • This parser supports the old implements keyword that is no longer a part of the official specification. This is for backwards compatibility.
  • This parser supports the old legacycaller keyword that is no longer a part of the official specification. This is for backwards compatibility.

Dependencies

~0–1.1MB
~22K SLoC