tagua_parser/
lib.rs

1#![crate_type = "lib"]
2
3// Tagua VM
4//
5//
6// New BSD License
7//
8// Copyright © 2016-2016, Ivan Enderlin.
9// All rights reserved.
10//
11// Redistribution and use in source and binary forms, with or without
12// modification, are permitted provided that the following conditions are met:
13//     * Redistributions of source code must retain the above copyright
14//       notice, this list of conditions and the following disclaimer.
15//     * Redistributions in binary form must reproduce the above copyright
16//       notice, this list of conditions and the following disclaimer in the
17//       documentation and/or other materials provided with the distribution.
18//     * Neither the name of the Hoa nor the names of its contributors may be
19//       used to endorse or promote products derived from this software without
20//       specific prior written permission.
21//
22// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
26// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32// POSSIBILITY OF SUCH DAMAGE.
33
34//! Tagua VM
35//!
36//! Tagua VM is an experimental [PHP](http://php.net/) Virtual Machine written
37//! with [the Rust language](https://www.rust-lang.org/) and [the LLVM Compiler
38//! Infrastructure](http://llvm.org/).
39//!
40//! This library contains lexical and syntactic analysers, aka the parser,
41//! for the PHP language.
42//!
43//! This is a parser combinator. The immediate consequence is that the lexical
44//! and syntax analyzers form a monolithic algorithm. The organization is the
45//! following:
46//!
47//!   * The `tokens` module declares all the lexemes,
48//!   * The `rules` module declares the grammar as a set of rules,
49//!   * The `ast` module contains the structure that will constitute the AST.
50//!
51//! The parser is based on [nom](https://github.com/Geal/nom). nom is a parser
52//! combinator library with a focus on safe parsing, streaming patterns, and as
53//! much as possible zero copy. We try to enforce the zero copy property to
54//! hold.
55
56// Increase the macro recursion limit.
57#![recursion_limit="128"]
58
59#[macro_use]
60extern crate lazy_static;
61#[macro_use]
62extern crate nom;
63extern crate regex;
64#[cfg(test)]
65#[macro_use]
66extern crate quickcheck;
67
68pub mod internal;
69#[macro_use]
70pub mod macros;
71pub mod ast;
72pub mod rules;
73pub mod tokens;
74
75pub use self::internal::*;
76
77/// Complete parsing of a datum starting by the sentence symbol of the grammar.
78///
79/// The grammar is a set of rules. By definition, it has a sentence symbol,
80/// also called the root rule. The `parse` function will lex, parse and produce
81/// the associated AST of the `input` datum.
82///
83/// # Examples
84///
85/// ```
86/// use tagua_parser as parser;
87///
88/// let expression = b"1+2";
89/// parser::parse(&expression[..]);
90/// ```
91pub fn parse(input: &[u8]) -> ast::Addition {
92    rules::root(input)
93}
94
95
96#[cfg(test)]
97mod tests {
98    use super::parse;
99    use super::ast;
100
101    #[test]
102    fn case_expr() {
103        assert_eq!(
104            parse(b"1+2"),
105            ast::Addition {
106                a: ast::Term {
107                    t: ast::Literal::Integer(1i64)
108                },
109                b: ast::Term {
110                    t: ast::Literal::Integer(2i64)
111                }
112            }
113        );
114    }
115}