From 02dc29d18a7b67edf0eaa5d125be22eec6cfebb7 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Thu, 6 Feb 2020 11:15:48 +0100 Subject: =?UTF-8?q?Highlight=20bold=20/=20italic=20/=20monospace=20?= =?UTF-8?q?=F0=9F=8E=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/syntax/mod.rs | 8 +++++++- src/syntax/parsing.rs | 30 ++++++++++++++++++++++++++++++ src/syntax/tokens.rs | 6 ++---- 3 files changed, 39 insertions(+), 5 deletions(-) (limited to 'src/syntax') diff --git a/src/syntax/mod.rs b/src/syntax/mod.rs index 8596a618..620a929e 100644 --- a/src/syntax/mod.rs +++ b/src/syntax/mod.rs @@ -100,7 +100,6 @@ pub enum Decoration { /// ^^^ /// ``` ValidFuncName, - /// An invalid function name: /// ```typst /// [blabla] @@ -114,6 +113,13 @@ pub enum Decoration { /// ^^^^^ /// ``` ArgumentKey, + + /// Italic. + Italic, + /// Bold. + Bold, + /// Monospace. + Monospace, } impl dyn Model { diff --git a/src/syntax/parsing.rs b/src/syntax/parsing.rs index 1526a5cb..d24985a6 100644 --- a/src/syntax/parsing.rs +++ b/src/syntax/parsing.rs @@ -375,6 +375,7 @@ mod tests { Space as S, Newline as N, ToggleItalic as Italic, ToggleBolder as Bold, ToggleMonospace as Mono, }; + use Decoration::*; pub use Expr::{Number as Num, Bool}; pub fn Id(text: &str) -> Expr { Expr::Ident(Ident(text.to_string())) } @@ -421,9 +422,32 @@ mod tests { }; } + /// Test whether the given string yields the given decorations. + macro_rules! d { + ($s:expr => [$(($sl:tt:$sc:tt, $el:tt:$ec:tt, $d:expr)),* $(,)?]) => { + let ctx = ParseContext { scope: &scope() }; + let decos = parse(Position::ZERO, $s, ctx).feedback.decos; + + let expected = vec![ + $(Spanned { + v: $d, + span: Span { + start: Position { line: $sl, column: $sc }, + end: Position { line: $el, column: $ec }, + }, + }),* + ]; + + if decos != expected { + fail($s, decos, expected); + } + }; + } + fn scope() -> Scope { let mut scope = Scope::new::(); scope.add::("f"); + scope.add::("n"); scope.add::("box"); scope } @@ -552,4 +576,10 @@ mod tests { (0:21, 0:21, "expected closing bracket"), ]); } + + #[test] + fn parse_decos() { + d!("*Technische Universität Berlin* [n]\n [n]" + => [(0:33, 0:34, ValidFuncName), (1:33, 1:34, ValidFuncName)]); + } } diff --git a/src/syntax/tokens.rs b/src/syntax/tokens.rs index 7b52f655..f4ea5daf 100644 --- a/src/syntax/tokens.rs +++ b/src/syntax/tokens.rs @@ -403,15 +403,13 @@ impl<'s> Tokens<'s> { fn eat(&mut self) -> Option { let c = self.iter.next()?; - let len = c.len_utf8(); - - self.index += len; + self.index += c.len_utf8(); if is_newline_char(c) && !(c == '\r' && self.peek() == Some('\n')) { self.position.line += 1; self.position.column = 0; } else { - self.position.column += len; + self.position.column += 1; } Some(c) -- cgit v1.2.3