Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions crates/typst-cli/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use typst::diag::{bail, HintedStrResult, StrResult, Warned};
use typst::engine::Sink;
use typst::foundations::{Content, IntoValue, LocatableSelector, Scope};
use typst::layout::PagedDocument;
use typst::syntax::Span;
use typst::syntax::{Span, SyntaxMode};
use typst::World;
use typst_eval::{eval_string, EvalMode};
use typst_eval::eval_string;

use crate::args::{QueryCommand, SerializationFormat};
use crate::compile::print_diagnostics;
Expand Down Expand Up @@ -63,7 +63,7 @@ fn retrieve(
Sink::new().track_mut(),
&command.selector,
Span::detached(),
EvalMode::Code,
SyntaxMode::Code,
Scope::default(),
)
.map_err(|errors| {
Expand Down
17 changes: 8 additions & 9 deletions crates/typst-eval/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ pub use self::call::{eval_closure, CapturesVisitor};
pub use self::flow::FlowEvent;
pub use self::import::import;
pub use self::vm::Vm;
pub use typst_library::routines::EvalMode;

use self::access::*;
use self::binding::*;
Expand All @@ -32,7 +31,7 @@ use typst_library::introspection::Introspector;
use typst_library::math::EquationElem;
use typst_library::routines::Routines;
use typst_library::World;
use typst_syntax::{ast, parse, parse_code, parse_math, Source, Span};
use typst_syntax::{ast, parse, parse_code, parse_math, Source, Span, SyntaxMode};

/// Evaluate a source file and return the resulting module.
#[comemo::memoize]
Expand Down Expand Up @@ -104,13 +103,13 @@ pub fn eval_string(
sink: TrackedMut<Sink>,
string: &str,
span: Span,
mode: EvalMode,
mode: SyntaxMode,
scope: Scope,
) -> SourceResult<Value> {
let mut root = match mode {
EvalMode::Code => parse_code(string),
EvalMode::Markup => parse(string),
EvalMode::Math => parse_math(string),
SyntaxMode::Code => parse_code(string),
SyntaxMode::Markup => parse(string),
SyntaxMode::Math => parse_math(string),
};

root.synthesize(span);
Expand Down Expand Up @@ -141,11 +140,11 @@ pub fn eval_string(

// Evaluate the code.
let output = match mode {
EvalMode::Code => root.cast::<ast::Code>().unwrap().eval(&mut vm)?,
EvalMode::Markup => {
SyntaxMode::Code => root.cast::<ast::Code>().unwrap().eval(&mut vm)?,
SyntaxMode::Markup => {
Value::Content(root.cast::<ast::Markup>().unwrap().eval(&mut vm)?)
}
EvalMode::Math => Value::Content(
SyntaxMode::Math => Value::Content(
EquationElem::new(root.cast::<ast::Math>().unwrap().eval(&mut vm)?)
.with_block(false)
.pack()
Expand Down
17 changes: 16 additions & 1 deletion crates/typst-library/src/foundations/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::ops::Add;

use ecow::eco_format;
use smallvec::SmallVec;
use typst_syntax::{Span, Spanned};
use typst_syntax::{Span, Spanned, SyntaxMode};
use unicode_math_class::MathClass;

use crate::diag::{At, HintedStrResult, HintedString, SourceResult, StrResult};
Expand Down Expand Up @@ -459,6 +459,21 @@ impl FromValue for Never {
}
}

cast! {
SyntaxMode,
self => IntoValue::into_value(match self {
SyntaxMode::Markup => "markup",
SyntaxMode::Math => "math",
SyntaxMode::Code => "code",
}),
/// Evaluate as markup, as in a Typst file.
"markup" => SyntaxMode::Markup,
/// Evaluate as math, as in an equation.
"math" => SyntaxMode::Math,
/// Evaluate as code, as after a hash.
"code" => SyntaxMode::Code,
}

cast! {
MathClass,
self => IntoValue::into_value(match self {
Expand Down
6 changes: 3 additions & 3 deletions crates/typst-library/src/foundations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ pub use self::ty::*;
pub use self::value::*;
pub use self::version::*;
pub use typst_macros::{scope, ty};
use typst_syntax::SyntaxMode;

#[rustfmt::skip]
#[doc(hidden)]
Expand All @@ -83,7 +84,6 @@ use typst_syntax::Spanned;

use crate::diag::{bail, SourceResult, StrResult};
use crate::engine::Engine;
use crate::routines::EvalMode;
use crate::{Feature, Features};

/// Hook up all `foundations` definitions.
Expand Down Expand Up @@ -273,8 +273,8 @@ pub fn eval(
/// #eval("1_2^3", mode: "math")
/// ```
#[named]
#[default(EvalMode::Code)]
mode: EvalMode,
#[default(SyntaxMode::Code)]
mode: SyntaxMode,
/// A scope of definitions that are made available.
///
/// ```example
Expand Down
6 changes: 3 additions & 3 deletions crates/typst-library/src/model/bibliography.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use hayagriva::{
};
use indexmap::IndexMap;
use smallvec::{smallvec, SmallVec};
use typst_syntax::{Span, Spanned};
use typst_syntax::{Span, Spanned, SyntaxMode};
use typst_utils::{Get, ManuallyHash, NonZeroExt, PicoStr};

use crate::diag::{
Expand All @@ -39,7 +39,7 @@ use crate::model::{
CitationForm, CiteGroup, Destination, FootnoteElem, HeadingElem, LinkElem, ParElem,
Url,
};
use crate::routines::{EvalMode, Routines};
use crate::routines::Routines;
use crate::text::{
FontStyle, Lang, LocalName, Region, Smallcaps, SubElem, SuperElem, TextElem,
WeightDelta,
Expand Down Expand Up @@ -1024,7 +1024,7 @@ impl ElemRenderer<'_> {
Sink::new().track_mut(),
math,
self.span,
EvalMode::Math,
SyntaxMode::Math,
Scope::new(),
)
.map(Value::display)
Expand Down
15 changes: 2 additions & 13 deletions crates/typst-library/src/routines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::hash::{Hash, Hasher};
use std::num::NonZeroUsize;

use comemo::{Tracked, TrackedMut};
use typst_syntax::Span;
use typst_syntax::{Span, SyntaxMode};
use typst_utils::LazyHash;

use crate::diag::SourceResult;
Expand Down Expand Up @@ -58,7 +58,7 @@ routines! {
sink: TrackedMut<Sink>,
string: &str,
span: Span,
mode: EvalMode,
mode: SyntaxMode,
scope: Scope,
) -> SourceResult<Value>

Expand Down Expand Up @@ -312,17 +312,6 @@ routines! {
) -> SourceResult<Fragment>
}

/// In which mode to evaluate a string.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Cast)]
pub enum EvalMode {
/// Evaluate as code, as after a hash.
Code,
/// Evaluate as markup, like in a Typst file.
Markup,
/// Evaluate as math, as in an equation.
Math,
}

/// Defines what kind of realization we are performing.
pub enum RealizationKind<'a> {
/// This the root realization for layout. Requires a mutable reference
Expand Down
37 changes: 13 additions & 24 deletions crates/typst-syntax/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use unicode_script::{Script, UnicodeScript};
use unicode_segmentation::UnicodeSegmentation;
use unscanny::Scanner;

use crate::{SyntaxError, SyntaxKind, SyntaxNode};
use crate::{SyntaxError, SyntaxKind, SyntaxMode, SyntaxNode};

/// An iterator over a source code string which returns tokens.
#[derive(Clone)]
Expand All @@ -13,28 +13,17 @@ pub(super) struct Lexer<'s> {
s: Scanner<'s>,
/// The mode the lexer is in. This determines which kinds of tokens it
/// produces.
mode: LexMode,
mode: SyntaxMode,
/// Whether the last token contained a newline.
newline: bool,
/// An error for the last token.
error: Option<SyntaxError>,
}

/// What kind of tokens to emit.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub(super) enum LexMode {
/// Text and markup.
Markup,
/// Math atoms, operators, etc.
Math,
/// Keywords, literals and operators.
Code,
}

impl<'s> Lexer<'s> {
/// Create a new lexer with the given mode and a prefix to offset column
/// calculations.
pub fn new(text: &'s str, mode: LexMode) -> Self {
pub fn new(text: &'s str, mode: SyntaxMode) -> Self {
Self {
s: Scanner::new(text),
mode,
Expand All @@ -44,12 +33,12 @@ impl<'s> Lexer<'s> {
}

/// Get the current lexing mode.
pub fn mode(&self) -> LexMode {
pub fn mode(&self) -> SyntaxMode {
self.mode
}

/// Change the lexing mode.
pub fn set_mode(&mut self, mode: LexMode) {
pub fn set_mode(&mut self, mode: SyntaxMode) {
self.mode = mode;
}

Expand Down Expand Up @@ -92,7 +81,7 @@ impl Lexer<'_> {
}
}

/// Shared methods with all [`LexMode`].
/// Shared methods with all [`SyntaxMode`].
impl Lexer<'_> {
/// Return the next token in our text. Returns both the [`SyntaxNode`]
/// and the raw [`SyntaxKind`] to make it more ergonomic to check the kind
Expand All @@ -114,14 +103,14 @@ impl Lexer<'_> {
);
kind
}
Some('`') if self.mode != LexMode::Math => return self.raw(),
Some('`') if self.mode != SyntaxMode::Math => return self.raw(),
Some(c) => match self.mode {
LexMode::Markup => self.markup(start, c),
LexMode::Math => match self.math(start, c) {
SyntaxMode::Markup => self.markup(start, c),
SyntaxMode::Math => match self.math(start, c) {
(kind, None) => kind,
(kind, Some(node)) => return (kind, node),
},
LexMode::Code => self.code(start, c),
SyntaxMode::Code => self.code(start, c),
},

None => SyntaxKind::End,
Expand All @@ -145,7 +134,7 @@ impl Lexer<'_> {
};

self.newline = newlines > 0;
if self.mode == LexMode::Markup && newlines >= 2 {
if self.mode == SyntaxMode::Markup && newlines >= 2 {
SyntaxKind::Parbreak
} else {
SyntaxKind::Space
Expand Down Expand Up @@ -965,9 +954,9 @@ impl ScannerExt for Scanner<'_> {

/// Whether a character will become a [`SyntaxKind::Space`] token.
#[inline]
fn is_space(character: char, mode: LexMode) -> bool {
fn is_space(character: char, mode: SyntaxMode) -> bool {
match mode {
LexMode::Markup => matches!(character, ' ' | '\t') || is_newline(character),
SyntaxMode::Markup => matches!(character, ' ' | '\t') || is_newline(character),
_ => character.is_whitespace(),
}
}
Expand Down
13 changes: 12 additions & 1 deletion crates/typst-syntax/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,16 @@ pub use self::path::VirtualPath;
pub use self::source::Source;
pub use self::span::{Span, Spanned};

use self::lexer::{LexMode, Lexer};
use self::lexer::Lexer;
use self::parser::{reparse_block, reparse_markup};

/// The syntax mode of a portion of Typst code.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum SyntaxMode {
/// Text and markup, as in the top level.
Markup,
/// Math atoms, operators, etc., as in equations.
Math,
/// Keywords, literals and operators, as after hashes.
Code,
}
Loading