pub struct SpannedExpr<'src> {
pub origin: Origin<'src>,
pub inner: Expr<'src>,
}
Expand description
An expression along with its source origin (span and unparsed form).
Important: Because of how our parser works internally, an expression’s
span is its rule’s span, which can be larger than the expression itself.
For example, foo || bar || baz
is covered by a single rule, so each
decomposed Expr::BinOp
within it will have the same span despite
logically having different sub-spans of the parent rule’s span.
Fields§
§origin: Origin<'src>
The expression’s source origin.
inner: Expr<'src>
The expression itself.
Implementations§
Source§impl<'a> SpannedExpr<'a>
impl<'a> SpannedExpr<'a>
Sourcepub fn dataflow_contexts(&self) -> Vec<(&Context<'a>, &Origin<'a>)>
pub fn dataflow_contexts(&self) -> Vec<(&Context<'a>, &Origin<'a>)>
Returns the contexts in this expression that directly flow into the expression’s evaluation.
For example ${{ foo.bar }}
returns foo.bar
since the value
of foo.bar
flows into the evaluation. On the other hand,
${{ foo.bar == 'abc' }}
returns no expanded contexts,
since the value of foo.bar
flows into a boolean evaluation
that gets expanded.
Sourcepub fn computed_indices(&self) -> Vec<&SpannedExpr<'a>>
pub fn computed_indices(&self) -> Vec<&SpannedExpr<'a>>
Returns any computed indices in this expression.
A computed index is any index operation with a non-literal
evaluation, e.g. foo[a.b.c]
.
Sourcepub fn constant_reducible_subexprs(&self) -> Vec<&SpannedExpr<'a>>
pub fn constant_reducible_subexprs(&self) -> Vec<&SpannedExpr<'a>>
Like Expr::constant_reducible
, but for all subexpressions
rather than the top-level expression.
This has slightly different semantics than constant_reducible
:
it doesn’t include “trivially” reducible expressions like literals,
since flagging these as reducible within a larger expression
would be misleading.
Methods from Deref<Target = Expr<'a>>§
Sourcepub fn is_literal(&self) -> bool
pub fn is_literal(&self) -> bool
Returns whether the expression is a literal.
Sourcepub fn constant_reducible(&self) -> bool
pub fn constant_reducible(&self) -> bool
Returns whether the expression is constant reducible.
“Constant reducible” is similar to “constant foldable” but with
meta-evaluation semantics: the expression 5
would not be
constant foldable in a normal program (because it’s already
an atom), but is “constant reducible” in a GitHub Actions expression
because an expression containing it (e.g. ${{ 5 }}
) can be elided
entirely and replaced with 5
.
There are three kinds of reducible expressions:
- Literals, which reduce to their literal value;
- Binops/unops with reducible subexpressions, which reduce to their evaluation;
- Select function calls where the semantics of the function mean that reducible arguments make the call itself reducible.
NOTE: This implementation is sound but not complete.
Sourcepub fn consteval(&self) -> Option<Evaluation>
pub fn consteval(&self) -> Option<Evaluation>
Evaluates a constant-reducible expression to its literal value.
Returns Some(Evaluation)
if the expression can be constant-evaluated,
or None
if the expression contains non-constant elements (like contexts or
non-reducible function calls).
This implementation follows GitHub Actions’ evaluation semantics as documented at: https://docs.github.com/en/actions/reference/workflows-and-actions/expressions
§Examples
use github_actions_expressions::{Expr, Evaluation};
let expr = Expr::parse("'hello'").unwrap();
let result = expr.consteval().unwrap();
assert_eq!(result.sema().to_string(), "hello");
let expr = Expr::parse("true && false").unwrap();
let result = expr.consteval().unwrap();
assert_eq!(result, Evaluation::Boolean(false));
Trait Implementations§
Source§impl<'src> Debug for SpannedExpr<'src>
impl<'src> Debug for SpannedExpr<'src>
Source§impl<'a> Deref for SpannedExpr<'a>
impl<'a> Deref for SpannedExpr<'a>
Source§impl<'doc> From<&SpannedExpr<'doc>> for Fragment<'doc>
impl<'doc> From<&SpannedExpr<'doc>> for Fragment<'doc>
Source§fn from(expr: &SpannedExpr<'doc>) -> Self
fn from(expr: &SpannedExpr<'doc>) -> Self
Source§impl<'src> PartialEq for SpannedExpr<'src>
impl<'src> PartialEq for SpannedExpr<'src>
impl<'src> StructuralPartialEq for SpannedExpr<'src>
Auto Trait Implementations§
impl<'src> Freeze for SpannedExpr<'src>
impl<'src> RefUnwindSafe for SpannedExpr<'src>
impl<'src> Send for SpannedExpr<'src>
impl<'src> Sync for SpannedExpr<'src>
impl<'src> Unpin for SpannedExpr<'src>
impl<'src> UnwindSafe for SpannedExpr<'src>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more