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
#[cfg_attr(rustfmt, rustfmt_skip)] #[allow(clippy)] #[allow(unknown_lints)] mod grammar; /// Contains all structures related to the AST for the WebIDL grammar. pub mod ast; /// Contains the visitor trait needed to traverse the AST and helper walk functions. pub mod visitor; use lalrpop_util::ParseError; use lexer::{LexicalError, Token}; /// The result that is returned when an input string is parsed. If the parse succeeds, the `Ok` /// result will be a vector of definitions representing the AST. If the parse fails, the `Err` will /// be either an error from the lexer or the parser. pub type ParseResult = Result<Vec<ast::Definition>, ParseError<usize, Token, LexicalError>>; /// The parser that is used to parse WebIDL. It really serves as a wrapper around the parse /// function exposed by lalrpop. #[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)] pub struct Parser; impl Parser { /// This exists because the parser may be extended later to support merging ASTs to perform /// semantic analysis or create symbol tables. pub fn new() -> Self { Parser } /// Parses a given input string and returns an AST. /// /// # Example /// /// ``` /// use webidl::*; /// use webidl::ast::*; /// /// let parser = Parser::new(); /// let result = parser.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() /// }))])); /// ``` pub fn parse_string(&self, input: &str) -> ParseResult { grammar::parse_Definitions(::Lexer::new(input)) } }