mirror of
https://github.com/typst/typst
synced 2025-05-14 17:15:28 +08:00
ColorToken serialization + function fix 🚧
This commit is contained in:
parent
70878885f5
commit
277f2d2176
@ -12,6 +12,7 @@ byteorder = "1"
|
|||||||
smallvec = "1"
|
smallvec = "1"
|
||||||
unicode-xid = "0.2"
|
unicode-xid = "0.2"
|
||||||
async-trait = "0.1"
|
async-trait = "0.1"
|
||||||
|
serde = { version = "1", features = ["derive"] }
|
||||||
futures-executor = { version = "0.3", optional = true }
|
futures-executor = { version = "0.3", optional = true }
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
use std::fmt::{self, Display, Formatter};
|
use std::fmt::{self, Display, Formatter};
|
||||||
use unicode_xid::UnicodeXID;
|
use unicode_xid::UnicodeXID;
|
||||||
|
use serde::Serialize;
|
||||||
|
|
||||||
use crate::func::LayoutFunc;
|
use crate::func::LayoutFunc;
|
||||||
use crate::size::{Size, ScaleSize};
|
use crate::size::{Size, ScaleSize};
|
||||||
@ -257,7 +258,7 @@ fn expect<E: ExpressionKind>(opt: ParseResult<Option<E>>) -> ParseResult<E> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
#[derive(Debug, Clone, Eq, PartialEq, Serialize)]
|
||||||
pub struct Colorization {
|
pub struct Colorization {
|
||||||
pub tokens: Vec<Spanned<ColorToken>>,
|
pub tokens: Vec<Spanned<ColorToken>>,
|
||||||
}
|
}
|
||||||
@ -277,7 +278,8 @@ impl Colorization {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Entities which can be colored by syntax highlighting.
|
/// Entities which can be colored by syntax highlighting.
|
||||||
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
#[derive(Debug, Copy, Clone, Eq, PartialEq, Serialize)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
pub enum ColorToken {
|
pub enum ColorToken {
|
||||||
Comment,
|
Comment,
|
||||||
Bracket,
|
Bracket,
|
||||||
@ -299,7 +301,7 @@ pub enum ColorToken {
|
|||||||
Invalid,
|
Invalid,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
#[derive(Debug, Clone, Eq, PartialEq, Serialize)]
|
||||||
pub struct ErrorMap {
|
pub struct ErrorMap {
|
||||||
pub errors: Vec<Spanned<String>>,
|
pub errors: Vec<Spanned<String>>,
|
||||||
}
|
}
|
||||||
|
@ -104,8 +104,11 @@ impl<'s> Parser<'s> {
|
|||||||
let name = self.parse_func_name()?;
|
let name = self.parse_func_name()?;
|
||||||
|
|
||||||
self.skip_whitespace();
|
self.skip_whitespace();
|
||||||
let args = match self.eat() {
|
let args = match self.peek() {
|
||||||
Some(Spanned { v: Colon, .. }) => self.parse_func_args(),
|
Some(Spanned { v: Colon, .. }) => {
|
||||||
|
self.eat();
|
||||||
|
self.parse_func_args()
|
||||||
|
}
|
||||||
Some(Spanned { v: RightBracket, .. }) => FuncArgs::new(),
|
Some(Spanned { v: RightBracket, .. }) => FuncArgs::new(),
|
||||||
other => {
|
other => {
|
||||||
self.expected_at("colon or closing bracket", name.span.end);
|
self.expected_at("colon or closing bracket", name.span.end);
|
||||||
@ -119,8 +122,9 @@ impl<'s> Parser<'s> {
|
|||||||
/// Parses the function name if is the next token. Otherwise, it adds an
|
/// Parses the function name if is the next token. Otherwise, it adds an
|
||||||
/// error and returns `None`.
|
/// error and returns `None`.
|
||||||
fn parse_func_name(&mut self) -> Option<Spanned<Ident>> {
|
fn parse_func_name(&mut self) -> Option<Spanned<Ident>> {
|
||||||
match self.eat() {
|
match self.peek() {
|
||||||
Some(Spanned { v: ExprIdent(ident), span }) => {
|
Some(Spanned { v: ExprIdent(ident), span }) => {
|
||||||
|
self.eat();
|
||||||
self.colorization.replace_last(ColorToken::FuncName);
|
self.colorization.replace_last(ColorToken::FuncName);
|
||||||
return Some(Spanned { v: Ident(ident.to_string()), span });
|
return Some(Spanned { v: Ident(ident.to_string()), span });
|
||||||
}
|
}
|
||||||
@ -280,7 +284,7 @@ impl<'s> Parser<'s> {
|
|||||||
None
|
None
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
Some(FuncCall(parser(header, body, self.ctx).unwrap()))
|
parser(header, body, self.ctx).ok().map(|f| FuncCall(f))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Skip all whitespace/comment tokens.
|
/// Skip all whitespace/comment tokens.
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
//! Spans map elements to the part of source code they originate from.
|
//! Spans map elements to the part of source code they originate from.
|
||||||
|
|
||||||
use std::fmt::{self, Debug, Display, Formatter};
|
use std::fmt::{self, Debug, Display, Formatter};
|
||||||
|
use serde::Serialize;
|
||||||
|
|
||||||
|
|
||||||
/// Annotates a value with the part of the source code it corresponds to.
|
/// Annotates a value with the part of the source code it corresponds to.
|
||||||
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
|
#[derive(Copy, Clone, Eq, PartialEq, Hash, Serialize)]
|
||||||
pub struct Spanned<T> {
|
pub struct Spanned<T> {
|
||||||
pub v: T,
|
pub v: T,
|
||||||
pub span: Span,
|
pub span: Span,
|
||||||
@ -45,7 +46,7 @@ impl<T> Debug for Spanned<T> where T: std::fmt::Debug {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Describes a slice of source code.
|
/// Describes a slice of source code.
|
||||||
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
|
#[derive(Copy, Clone, Eq, PartialEq, Hash, Serialize)]
|
||||||
pub struct Span {
|
pub struct Span {
|
||||||
pub start: Position,
|
pub start: Position,
|
||||||
pub end: Position,
|
pub end: Position,
|
||||||
@ -83,7 +84,7 @@ impl Display for Span {
|
|||||||
debug_display!(Span);
|
debug_display!(Span);
|
||||||
|
|
||||||
/// A line-column position in source code.
|
/// A line-column position in source code.
|
||||||
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
|
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize)]
|
||||||
pub struct Position {
|
pub struct Position {
|
||||||
/// The 0-indexed line (inclusive).
|
/// The 0-indexed line (inclusive).
|
||||||
pub line: usize,
|
pub line: usize,
|
||||||
|
1
tests/layouter/test.typ
Normal file
1
tests/layouter/test.typ
Normal file
@ -0,0 +1 @@
|
|||||||
|
[box][hi]
|
@ -107,7 +107,7 @@ fn test(name: &str, src: &str) -> DynResult<()> {
|
|||||||
|
|
||||||
// Render the layout into a PNG.
|
// Render the layout into a PNG.
|
||||||
Command::new("python")
|
Command::new("python")
|
||||||
.arg("tests/render.py")
|
.arg("tests/src/render.py")
|
||||||
.arg(name)
|
.arg(name)
|
||||||
.spawn()
|
.spawn()
|
||||||
.expect("failed to run python renderer");
|
.expect("failed to run python renderer");
|
||||||
|
@ -38,7 +38,7 @@ class MultiboxRenderer:
|
|||||||
parts = lines[i + 1].split(' ', 2)
|
parts = lines[i + 1].split(' ', 2)
|
||||||
index = int(parts[0]), int(parts[1])
|
index = int(parts[0]), int(parts[1])
|
||||||
path = parts[2]
|
path = parts[2]
|
||||||
self.fonts[index] = os.path.join(BASE, '../../fonts', path)
|
self.fonts[index] = os.path.join(BASE, '../../../fonts', path)
|
||||||
|
|
||||||
self.content = lines[font_count + 1:]
|
self.content = lines[font_count + 1:]
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user