Skip to content

Commit 52f961e

Browse files
committed
Expand macros earlier so they include constants
This also simplifies macro expansion significantly, having it operate in-place.
1 parent ff5d068 commit 52f961e

28 files changed

+622
-683
lines changed

crates/rune-modules/README.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ See each module for documentation:
5454
* [fs]
5555
* [process]
5656
* [signal]
57+
* [rand]
5758

5859
### Features
5960

@@ -62,9 +63,10 @@ See each module for documentation:
6263
* `json` for the [json module][json]
6364
* `toml` for the [toml module][toml]
6465
* `time` for the [time module][time]
65-
* `fs` for the [fs module]][fs]
66-
* `process` for the [process module]][process]
67-
* `signal` for the [process module]][signal]
66+
* `fs` for the [fs module][fs]
67+
* `process` for the [process module][process]
68+
* `signal` for the [signal module][signal]
69+
* `rand` for the [rand module][rand]
6870

6971
[http]: https://docs.rs/rune-modules/0/rune_modules/http/
7072
[json]: https://docs.rs/rune-modules/0/rune_modules/json/
@@ -73,3 +75,4 @@ See each module for documentation:
7375
[fs]: https://docs.rs/rune-modules/0/rune_modules/fs/
7476
[process]: https://docs.rs/rune-modules/0/rune_modules/process/
7577
[signal]: https://docs.rs/rune-modules/0/rune_modules/signal/
78+
[rand]: https://docs.rs/rune-modules/0/rune_modules/rand/

crates/rune/src/ast/block.rs

+2-48
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::ast;
2-
use crate::{OptionSpanned as _, Parse, ParseError, ParseErrorKind, Parser, Spanned, ToTokens};
2+
use crate::{Parse, ParseError, Parser, Spanned, ToTokens};
33

44
/// A block of expressions.
55
#[derive(Debug, Clone, PartialEq, Eq, ToTokens, Spanned)]
@@ -63,55 +63,9 @@ impl Parse for Block {
6363
let mut statements = Vec::new();
6464

6565
let open = parser.parse()?;
66-
let mut must_be_last = None;
6766

6867
while !parser.peek::<ast::CloseBrace>()? {
69-
let attributes = parser.parse()?;
70-
let visibility = parser.parse()?;
71-
72-
if ast::Item::peek_as_stmt(parser)? {
73-
let decl: ast::Item = ast::Item::parse_with_meta(parser, attributes, visibility)?;
74-
75-
if let Some(span) = must_be_last {
76-
return Err(ParseError::new(
77-
span,
78-
ParseErrorKind::ExpectedBlockSemiColon {
79-
followed_span: decl.span(),
80-
},
81-
));
82-
}
83-
84-
statements.push(ast::Stmt::Item(decl));
85-
continue;
86-
}
87-
88-
if let Some(span) = visibility.option_span() {
89-
return Err(ParseError::new(
90-
span,
91-
ParseErrorKind::UnsupportedExprVisibility,
92-
));
93-
}
94-
95-
let expr: ast::Expr = ast::Expr::parse_primary_with_attributes(parser, attributes)?;
96-
97-
if let Some(span) = must_be_last {
98-
return Err(ParseError::new(
99-
span,
100-
ParseErrorKind::ExpectedBlockSemiColon {
101-
followed_span: expr.span(),
102-
},
103-
));
104-
}
105-
106-
if parser.peek::<ast::SemiColon>()? {
107-
statements.push(ast::Stmt::Semi(expr, parser.parse()?));
108-
} else {
109-
if expr.needs_semi() {
110-
must_be_last = Some(expr.span());
111-
}
112-
113-
statements.push(ast::Stmt::Expr(expr));
114-
}
68+
statements.push(parser.parse()?);
11569
}
11670

11771
let close = parser.parse()?;

crates/rune/src/ast/expr.rs

+62-53
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::ast;
2-
use crate::{Parse, ParseError, ParseErrorKind, Parser, Peek, Spanned, ToTokens};
2+
use crate::{
3+
OptionSpanned as _, Parse, ParseError, ParseErrorKind, Parser, Peek, Spanned, ToTokens,
4+
};
35
use std::mem::take;
46
use std::ops;
57

@@ -173,28 +175,27 @@ impl Expr {
173175
/// are arguments to statements immediately followed by blocks. Like `if`,
174176
/// `while`, and `match`.
175177
pub(crate) fn parse_without_eager_brace(parser: &mut Parser<'_>) -> Result<Self, ParseError> {
176-
Self::parse_full(parser, EagerBrace(false), ExprChain(true), vec![])
178+
Self::parse_full(parser, EagerBrace(false), ExprChain(true), &mut vec![])
177179
}
178180

179181
/// Full, configurable parsing of an expression.
180-
pub(crate) fn parse_full(
182+
fn parse_full(
181183
parser: &mut Parser<'_>,
182184
eager_brace: EagerBrace,
183185
expr_chain: ExprChain,
184-
attributes: Vec<ast::Attribute>,
186+
attributes: &mut Vec<ast::Attribute>,
185187
) -> Result<Self, ParseError> {
186188
let lhs = Self::parse_primary(parser, eager_brace, expr_chain, attributes)?;
187189
Ok(Self::parse_expr_binary(parser, lhs, 0, eager_brace)?)
188190
}
189191

190192
/// Parse expressions that start with an identifier.
191-
pub(crate) fn parse_ident_start(
193+
pub(crate) fn parse_with_path(
192194
parser: &mut Parser<'_>,
195+
path: ast::Path,
193196
eager_brace: EagerBrace,
194197
attributes: &mut Vec<ast::Attribute>,
195198
) -> Result<Self, ParseError> {
196-
let path = parser.parse::<ast::Path>()?;
197-
198199
if *eager_brace && parser.peek::<ast::OpenBrace>()? {
199200
let ident = ast::LitObjectIdent::Named(path);
200201

@@ -226,7 +227,7 @@ impl Expr {
226227
}
227228

228229
let open = parser.parse::<ast::OpenParen>()?;
229-
let expr = ast::Expr::parse_full(parser, EagerBrace(true), ExprChain(true), vec![])?;
230+
let expr = ast::Expr::parse_full(parser, EagerBrace(true), ExprChain(true), &mut vec![])?;
230231

231232
if parser.peek::<ast::CloseParen>()? {
232233
return Ok(Expr::ExprGroup(ast::ExprGroup {
@@ -244,19 +245,27 @@ impl Expr {
244245
}))
245246
}
246247

247-
pub(crate) fn parse_primary_with_attributes(
248+
pub(crate) fn parse_with_meta(
248249
parser: &mut Parser<'_>,
249-
attributes: Vec<ast::Attribute>,
250+
attributes: &mut Vec<ast::Attribute>,
251+
path: Option<ast::Path>,
250252
) -> Result<Self, ParseError> {
251-
Self::parse_full(parser, EagerBrace(true), ExprChain(true), attributes)
253+
let lhs = if let Some(path) = path {
254+
let expr = Self::parse_with_path(parser, path, EagerBrace(true), attributes)?;
255+
Self::parse_expr_chain(parser, expr)?
256+
} else {
257+
Self::parse_primary(parser, EagerBrace(true), ExprChain(true), attributes)?
258+
};
259+
260+
Ok(Self::parse_expr_binary(parser, lhs, 0, EagerBrace(true))?)
252261
}
253262

254263
/// Parse a single expression value.
255264
pub(crate) fn parse_primary(
256265
parser: &mut Parser<'_>,
257266
eager_brace: EagerBrace,
258267
expr_chain: ExprChain,
259-
mut attributes: Vec<ast::Attribute>,
268+
attributes: &mut Vec<ast::Attribute>,
260269
) -> Result<Self, ParseError> {
261270
while parser.peek::<ast::Attribute>()? {
262271
attributes.push(parser.parse::<ast::attribute::InnerAttribute>()?.0);
@@ -265,25 +274,26 @@ impl Expr {
265274
let expr = if ast::Lit::peek_in_expr(parser)? {
266275
ast::Expr::ExprLit(ast::ExprLit::parse_with_attributes(
267276
parser,
268-
take(&mut attributes),
277+
take(attributes),
269278
)?)
270279
} else {
271280
let token = parser.token_peek_eof()?;
272281

273282
match token.kind {
274283
ast::Kind::Async => {
275284
let async_: ast::Async = parser.parse()?;
276-
let expr: Self = Self::parse_primary(parser, eager_brace, expr_chain, vec![])?;
285+
let expr: Self =
286+
Self::parse_primary(parser, eager_brace, expr_chain, &mut vec![])?;
277287

278288
match expr {
279289
Self::ExprClosure(expr_closure) => Self::ExprClosure(ast::ExprClosure {
280-
attributes: take(&mut attributes),
290+
attributes: take(attributes),
281291
async_: Some(async_),
282292
args: expr_closure.args,
283293
body: expr_closure.body,
284294
}),
285295
Self::ExprBlock(expr_block) => Self::ExprAsync(ast::ExprAsync {
286-
attributes: take(&mut attributes),
296+
attributes: take(attributes),
287297
async_,
288298
block: expr_block.block,
289299
}),
@@ -296,12 +306,12 @@ impl Expr {
296306
}
297307
}
298308
ast::Kind::PipePipe | ast::Kind::Pipe => Self::ExprClosure(
299-
ast::ExprClosure::parse_with_attributes(parser, take(&mut attributes))?,
309+
ast::ExprClosure::parse_with_attributes(parser, take(attributes))?,
300310
),
301311
ast::Kind::Self_ => Self::Self_(parser.parse()?),
302312
ast::Kind::Select => Self::ExprSelect(ast::ExprSelect::parse_with_attributes(
303313
parser,
304-
take(&mut attributes),
314+
take(attributes),
305315
)?),
306316
ast::Kind::Label(..) => {
307317
let label =
@@ -312,21 +322,21 @@ impl Expr {
312322
ast::Kind::While => {
313323
Self::ExprWhile(ast::ExprWhile::parse_with_attributes_and_label(
314324
parser,
315-
take(&mut attributes),
325+
take(attributes),
316326
label,
317327
)?)
318328
}
319329
ast::Kind::Loop => {
320330
Self::ExprLoop(ast::ExprLoop::parse_with_attributes_and_label(
321331
parser,
322-
take(&mut attributes),
332+
take(attributes),
323333
label,
324334
)?)
325335
}
326336
ast::Kind::For => {
327337
Self::ExprFor(ast::ExprFor::parse_with_attributes_and_label(
328338
parser,
329-
take(&mut attributes),
339+
take(attributes),
330340
label,
331341
)?)
332342
}
@@ -340,70 +350,56 @@ impl Expr {
340350
}
341351
ast::Kind::While => Self::ExprWhile(ast::ExprWhile::parse_with_attributes(
342352
parser,
343-
take(&mut attributes),
353+
take(attributes),
344354
)?),
345355
ast::Kind::Loop => Self::ExprLoop(ast::ExprLoop::parse_with_attributes(
346356
parser,
347-
take(&mut attributes),
357+
take(attributes),
348358
)?),
349359
ast::Kind::For => Self::ExprFor(ast::ExprFor::parse_with_attributes(
350360
parser,
351-
take(&mut attributes),
361+
take(attributes),
352362
)?),
353363
ast::Kind::Let => Self::ExprLet(ast::ExprLet::parse_with_attributes(
354364
parser,
355-
take(&mut attributes),
365+
take(attributes),
356366
)?),
357367
ast::Kind::If => Self::ExprIf(ast::ExprIf::parse_with_attributes(
358368
parser,
359-
take(&mut attributes),
369+
take(attributes),
360370
)?),
361371
ast::Kind::Match => Self::ExprMatch(ast::ExprMatch::parse_with_attributes(
362372
parser,
363-
take(&mut attributes),
373+
take(attributes),
364374
)?),
365375
ast::Kind::Open(ast::Delimiter::Parenthesis) => {
366-
Self::parse_open_paren(parser, &mut attributes)?
376+
Self::parse_open_paren(parser, attributes)?
367377
}
368378
ast::Kind::Open(ast::Delimiter::Brace) => Self::ExprBlock(
369-
ast::ExprBlock::parse_with_attributes(parser, take(&mut attributes))?,
379+
ast::ExprBlock::parse_with_attributes(parser, take(attributes))?,
370380
),
371381
ast::Kind::Ident(..) => {
372-
Self::parse_ident_start(parser, eager_brace, &mut attributes)?
382+
let path = parser.parse()?;
383+
Self::parse_with_path(parser, path, eager_brace, attributes)?
373384
}
374385
ast::Kind::Break => Self::ExprBreak(ast::ExprBreak::parse_with_attributes(
375386
parser,
376-
take(&mut attributes),
387+
take(attributes),
377388
)?),
378389
ast::Kind::Yield => Self::ExprYield(ast::ExprYield::parse_with_attributes(
379390
parser,
380-
take(&mut attributes),
391+
take(attributes),
381392
)?),
382393
ast::Kind::Return => Self::ExprReturn(ast::ExprReturn::parse_with_attributes(
383394
parser,
384-
take(&mut attributes),
395+
take(attributes),
385396
)?),
386397
_ => {
387398
return Err(ParseError::expected(token, "expression"));
388399
}
389400
}
390401
};
391402

392-
let mut it = attributes.into_iter();
393-
394-
if let Some(first) = it.next() {
395-
let span = if let Some(last) = it.next_back() {
396-
first.span().join(last.span())
397-
} else {
398-
first.span()
399-
};
400-
401-
return Err(ParseError::new(
402-
span,
403-
ParseErrorKind::AttributesNotSupported,
404-
));
405-
}
406-
407403
if !*expr_chain {
408404
return Ok(expr);
409405
}
@@ -469,8 +465,12 @@ impl Expr {
469465
}
470466
}
471467

472-
let next =
473-
Expr::parse_primary(parser, EagerBrace(false), ExprChain(false), vec![])?;
468+
let next = Expr::parse_primary(
469+
parser,
470+
EagerBrace(false),
471+
ExprChain(false),
472+
&mut vec![],
473+
)?;
474474

475475
let span = match next {
476476
Expr::Path(path) => {
@@ -536,7 +536,7 @@ impl Expr {
536536
parser.token_next()?;
537537
}
538538

539-
let mut rhs = Self::parse_primary(parser, eager_brace, ExprChain(true), vec![])?;
539+
let mut rhs = Self::parse_primary(parser, eager_brace, ExprChain(true), &mut vec![])?;
540540

541541
lookahead_tok = parser.token_peek_pair()?;
542542

@@ -607,8 +607,17 @@ impl Expr {
607607
/// ```
608608
impl Parse for Expr {
609609
fn parse(parser: &mut Parser<'_>) -> Result<Self, ParseError> {
610-
let attributes = parser.parse()?;
611-
Self::parse_full(parser, EagerBrace(true), ExprChain(true), attributes)
610+
let mut attributes = parser.parse()?;
611+
let expr = Self::parse_full(parser, EagerBrace(true), ExprChain(true), &mut attributes)?;
612+
613+
if let Some(span) = attributes.option_span() {
614+
return Err(ParseError::new(
615+
span,
616+
ParseErrorKind::AttributesNotSupported,
617+
));
618+
}
619+
620+
Ok(expr)
612621
}
613622
}
614623

crates/rune/src/ast/expr_unary.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl Parse for ExprUnary {
3838
parser,
3939
EagerBrace(true),
4040
ExprChain(true),
41-
vec![],
41+
&mut vec![],
4242
)?),
4343
})
4444
}

0 commit comments

Comments
 (0)