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
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
//! <div align="center">
//! <a href="https://rune-rs.github.io/rune/">
//!     <b>Read the Book 📖</b>
//! </a>
//! </div>
//!
//! <br>
//!
//! <div align="center">
//! <a href="https://github.com/rune-rs/rune/actions">
//!     <img alt="Build Status" src="https://github.com/rune-rs/rune/workflows/Build/badge.svg">
//! </a>
//!
//! <a href="https://github.com/rune-rs/rune/actions">
//!     <img alt="Book Status" src="https://github.com/rune-rs/rune/workflows/Book/badge.svg">
//! </a>
//!
//! <a href="https://crates.io/crates/rune">
//!     <img alt="crates.io" src="https://img.shields.io/crates/v/rune.svg">
//! </a>
//!
//! <a href="https://docs.rs/rune">
//!     <img alt="docs.rs" src="https://docs.rs/rune/badge.svg">
//! </a>
//!
//! <a href="https://discord.gg/v5AeNkT">
//!     <img alt="Chat on Discord" src="https://img.shields.io/discord/558644981137670144.svg?logo=discord&style=flat-square">
//! </a>
//! </div>
//!
//! <br>
//!
//! An embeddable dynamic programming language for Rust.
//!
//! ## Contributing
//!
//! If you want to help out, there should be a number of optimization tasks
//! available in [Future Optimizations][future-optimizations]. Or have a look at
//! [Open Issues].
//!
//! Create an issue about the optimization you want to work on and communicate that
//! you are working on it.
//!
//! <br>
//!
//! ## Highlights of Rune
//!
//! * Clean [Rust integration 💻][support-rust-integration].
//! * Memory safe through [reference counting 📖][support-reference-counted].
//! * [Template strings 📖][support-templates].
//! * [Try operators 📖][support-try].
//! * [Pattern matching 📖][support-patterns].
//! * [Structs and enums 📖][support-structs] with associated data and functions.
//! * Dynamic [vectors 📖][support-dynamic-vectors], [objects 📖][support-anon-objects], and [tuples 📖][support-anon-tuples] with built-in [serde support 💻][support-serde].
//! * First-class [async support 📖][support-async].
//! * [Generators 📖][support-generators].
//! * Dynamic [instance functions 📖][support-instance-functions].
//! * [Stack isolation 📖][support-stack-isolation] between function calls.
//! * Stack-based C FFI, like Lua's (TBD).
//!
//! <br>
//!
//! ## Rune scripts
//!
//! You can run Rune programs with the bundled CLI:
//!
//! ```text
//! cargo run -- scripts/hello_world.rn
//! ```
//!
//! If you want to see detailed diagnostics of your program while it's running,
//! you can use:
//!
//! ```text
//! cargo run -- scripts/hello_world.rn --dump-unit --trace --dump-vm
//! ```
//!
//! See `--help` for more information.
//!
//! ## Running scripts from Rust
//!
//! > You can find more examples [in the `examples` folder].
//!
//! The following is a complete example, including rich diagnostics using
//! [`termcolor`]. It can be made much simpler if this is not needed.
//!
//! [`termcolor`]: https://docs.rs/termcolor
//!
//! ```rust
//! use rune::termcolor::{ColorChoice, StandardStream};
//! use rune::EmitDiagnostics as _;
//! use runestick::{Vm, FromValue as _, Item, Source};
//!
//! use std::error::Error;
//! use std::sync::Arc;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn Error>> {
//!     let source = Source::new(
//!         "script",
//!         r#"
//!         fn calculate(a, b) {
//!             println("Hello World");
//!             a + b
//!         }
//!         "#,
//!     );
//!
//!     let context = Arc::new(rune::default_context()?);
//!     let options = rune::Options::default();
//!     let mut warnings = rune::Warnings::new();
//!
//!     let unit = match rune::load_source(&*context, &options, source, &mut warnings) {
//!         Ok(unit) => unit,
//!         Err(error) => {
//!             let mut writer = StandardStream::stderr(ColorChoice::Always);
//!             error.emit_diagnostics(&mut writer)?;
//!             return Ok(());
//!         }
//!     };
//!
//!     if !warnings.is_empty() {
//!         let mut writer = StandardStream::stderr(ColorChoice::Always);
//!         rune::emit_warning_diagnostics(&mut writer, &warnings, &unit)?;
//!     }
//!
//!     let vm = Vm::new(context.clone(), Arc::new(unit));
//!
//!     let mut execution = vm.call(&["calculate"], (10i64, 20i64))?;
//!     let value = execution.async_complete().await?;
//!
//!     let value = i64::from_value(value)?;
//!
//!     println!("{}", value);
//!     Ok(())
//! }
//! ```
//!
//! [in the `examples` folder]: https://github.com/rune-rs/rune/tree/master/crates/rune-testing/examples
//! [future-optimizations]: https://github.com/rune-rs/rune/blob/master/FUTURE_OPTIMIZATIONS.md
//! [Open Issues]: https://github.com/rune-rs/rune/issues
//! [support-rust-integration]: https://github.com/rune-rs/rune/tree/master/crates/rune-modules
//! [support-reference-counted]: https://rune-rs.github.io/rune/variables.html
//! [support-templates]: https://rune-rs.github.io/rune/template_strings.html
//! [support-try]: https://rune-rs.github.io/rune/try_operator.html
//! [support-patterns]: https://rune-rs.github.io/rune/pattern_matching.html
//! [support-structs]: https://rune-rs.github.io/rune/structs.html
//! [support-async]: https://rune-rs.github.io/rune/async.html
//! [support-generators]: https://rune-rs.github.io/rune/generators.html
//! [support-instance-functions]: https://rune-rs.github.io/rune/instance_functions.html
//! [support-stack-isolation]: https://rune-rs.github.io/rune/call_frames.html
//! [support-dynamic-vectors]: https://rune-rs.github.io/rune/vectors.html
//! [support-anon-objects]: https://rune-rs.github.io/rune/objects.html
//! [support-anon-tuples]: https://rune-rs.github.io/rune/tuples.html
//! [support-serde]: https://github.com/rune-rs/rune/blob/master/crates/rune-modules/src/json.rs

#![deny(missing_docs)]

pub mod ast;
mod compile;
mod compiler;
#[cfg(feature = "diagnostics")]
mod diagnostics;
mod error;
mod index;
mod index_scopes;
mod items;
mod lexer;
mod load;
mod load_error;
mod loops;
mod options;
mod parser;
mod query;
mod scopes;
mod traits;
mod warning;

/// Internal collection re-export.
mod collections {
    pub use hashbrown::{hash_map, HashMap};
    pub use hashbrown::{hash_set, HashSet};
}

pub use crate::error::{CompileError, ParseError};
pub use crate::lexer::Lexer;
pub use crate::load::{load_path, load_source};
pub use crate::load_error::{LoadError, LoadErrorKind};
pub use crate::options::Options;
pub use crate::parser::Parser;
pub use crate::warning::{Warning, WarningKind, Warnings};
pub use compiler::compile;

#[cfg(feature = "diagnostics")]
pub use diagnostics::{emit_warning_diagnostics, termcolor, DiagnosticsError, EmitDiagnostics};

/// Construct a a default context runestick context.
///
/// If built with the `modules` feature, this includes all available native
/// modules.
///
/// See [load_path](crate::load_path) for how to use.
pub fn default_context() -> Result<runestick::Context, runestick::ContextError> {
    #[allow(unused_mut)]
    let mut context = runestick::Context::with_default_modules()?;

    #[cfg(feature = "modules")]
    {
        context.install(&rune_modules::http::module()?)?;
        context.install(&rune_modules::json::module()?)?;
        context.install(&rune_modules::toml::module()?)?;
        context.install(&rune_modules::time::module()?)?;
        context.install(&rune_modules::process::module()?)?;
        context.install(&rune_modules::fs::module()?)?;
        context.install(&rune_modules::signal::module()?)?;
    }

    Ok(context)
}

/// Parse the given input as the given type that implements
/// [Parse][crate::traits::Parse].
pub fn parse_all<T>(source: &str) -> Result<T, ParseError>
where
    T: crate::traits::Parse,
{
    let mut parser = Parser::new(source);
    let ast = parser.parse::<T>()?;

    if let Some(token) = parser.lexer.next()? {
        return Err(ParseError::ExpectedEof {
            actual: token.kind,
            span: token.span,
        });
    }

    Ok(ast)
}