diff options
| author | Laurenz <laurmaedje@gmail.com> | 2022-12-02 15:47:25 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2022-12-02 15:47:25 +0100 |
| commit | 56923ee472f1eaa67d3543e19372823139205885 (patch) | |
| tree | accd9e05fb5875457967c5b456626767ff3e9c9e /src | |
| parent | 9bc90c371fb41a2d6dc08eb4673e5be15f829514 (diff) | |
Multi-part numbering patterns
Diffstat (limited to 'src')
| -rw-r--r-- | src/model/library.rs | 2 | ||||
| -rw-r--r-- | src/syntax/ast.rs | 2 | ||||
| -rw-r--r-- | src/syntax/kind.rs | 3 | ||||
| -rw-r--r-- | src/syntax/tokens.rs | 12 | ||||
| -rw-r--r-- | src/util/eco.rs | 8 |
5 files changed, 20 insertions, 7 deletions
diff --git a/src/model/library.rs b/src/model/library.rs index eee69675..c890fef1 100644 --- a/src/model/library.rs +++ b/src/model/library.rs @@ -60,7 +60,7 @@ pub struct LangItems { /// An item in an unordered list: `- ...`. pub list_item: fn(body: Content) -> Content, /// An item in an enumeration (ordered list): `+ ...` or `1. ...`. - pub enum_item: fn(number: Option<usize>, body: Content) -> Content, + pub enum_item: fn(number: Option<NonZeroUsize>, body: Content) -> Content, /// An item in a description list: `/ Term: Details`. pub desc_item: fn(term: Content, body: Content) -> Content, /// A mathematical formula: `$x$`, `$ x^2 $`. diff --git a/src/syntax/ast.rs b/src/syntax/ast.rs index 81ddd596..3c60acbb 100644 --- a/src/syntax/ast.rs +++ b/src/syntax/ast.rs @@ -365,7 +365,7 @@ node! { impl EnumItem { /// The explicit numbering, if any: `23.`. - pub fn number(&self) -> Option<usize> { + pub fn number(&self) -> Option<NonZeroUsize> { self.0.children().find_map(|node| match node.kind() { SyntaxKind::EnumNumbering(num) => Some(*num), _ => None, diff --git a/src/syntax/kind.rs b/src/syntax/kind.rs index b7ee6a79..a7425d70 100644 --- a/src/syntax/kind.rs +++ b/src/syntax/kind.rs @@ -1,4 +1,5 @@ use std::hash::{Hash, Hasher}; +use std::num::NonZeroUsize; use std::sync::Arc; use crate::geom::{AbsUnit, AngleUnit}; @@ -164,7 +165,7 @@ pub enum SyntaxKind { /// An item in an enumeration (ordered list): `+ ...` or `1. ...`. EnumItem, /// An explicit enumeration numbering: `23.`. - EnumNumbering(usize), + EnumNumbering(NonZeroUsize), /// An item in a description list: `/ Term: Details`. DescItem, /// A mathematical formula: `$x$`, `$ x^2 $`. diff --git a/src/syntax/tokens.rs b/src/syntax/tokens.rs index e0ef2fa1..e7015bb2 100644 --- a/src/syntax/tokens.rs +++ b/src/syntax/tokens.rs @@ -1,3 +1,4 @@ +use std::num::NonZeroUsize; use std::sync::Arc; use unicode_xid::UnicodeXID; @@ -395,8 +396,11 @@ impl<'s> Tokens<'s> { self.s.eat_while(char::is_ascii_digit); let read = self.s.from(start); if self.s.eat_if('.') { - if let Ok(number) = read.parse() { - return SyntaxKind::EnumNumbering(number); + if let Ok(number) = read.parse::<usize>() { + return match NonZeroUsize::new(number) { + Some(number) => SyntaxKind::EnumNumbering(number), + None => SyntaxKind::Error(ErrorPos::Full, "must be positive".into()), + }; } } @@ -933,8 +937,8 @@ mod tests { t!(Markup["a "]: r"a--" => Text("a"), Shorthand('\u{2013}')); t!(Markup["a1/"]: "- " => Minus, Space(0)); t!(Markup[" "]: "+" => Plus); - t!(Markup[" "]: "1." => EnumNumbering(1)); - t!(Markup[" "]: "1.a" => EnumNumbering(1), Text("a")); + t!(Markup[" "]: "1." => EnumNumbering(NonZeroUsize::new(1).unwrap())); + t!(Markup[" "]: "1.a" => EnumNumbering(NonZeroUsize::new(1).unwrap()), Text("a")); t!(Markup[" /"]: "a1." => Text("a1.")); } diff --git a/src/util/eco.rs b/src/util/eco.rs index 5a4d7629..8f519504 100644 --- a/src/util/eco.rs +++ b/src/util/eco.rs @@ -368,6 +368,14 @@ impl FromIterator<Self> for EcoString { } } +impl Extend<char> for EcoString { + fn extend<T: IntoIterator<Item = char>>(&mut self, iter: T) { + for c in iter { + self.push(c); + } + } +} + impl From<EcoString> for String { fn from(s: EcoString) -> Self { match s.0 { |
