omml 0.2.11 → 0.2.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitattributes +12 -0
- data/.github/workflows/corpus.yml +106 -0
- data/.github/workflows/portable.yml +47 -0
- data/.github/workflows/release.yml +1 -0
- data/.rubocop.yml +5 -0
- data/README.adoc +27 -0
- data/lib/omml/models/ct_anchor.rb +2 -2
- data/lib/omml/models/ct_on_off.rb +1 -1
- data/lib/omml/models/ct_wordprocessing_on_off.rb +1 -1
- data/lib/omml/models/simple_types/st_hex_color_auto.rb +15 -0
- data/lib/omml/models/simple_types/st_hex_color_rgb.rb +9 -0
- data/lib/omml/models/simple_types/st_lang_code.rb +9 -0
- data/lib/omml/models.rb +3 -0
- data/lib/omml/opal.rb +1 -0
- data/lib/omml/portable.rb +192 -0
- data/lib/omml/version.rb +1 -1
- data/lib/omml.rb +1 -0
- data/model/portable/bindings/coordinate.rs +153 -0
- data/model/portable/bindings/coordinate.ts +74 -0
- data/model/portable/bindings/integer.rs +110 -0
- data/model/portable/bindings/integer.ts +74 -0
- data/model/portable/bindings/token.rs +30 -0
- data/model/portable/bindings/token.ts +16 -0
- data/model/portable/enum-subset-v1.json +1 -0
- data/model/portable/manifest.json +933 -0
- data/model/portable/runtime-graph-v1.json +1 -0
- data/model/portable/scalar-subset-v1.json +1 -0
- data/omml.gemspec +2 -1
- data/scripts/export_portable_models.rb +90 -0
- data/scripts/replay_corpus.rb +458 -0
- metadata +21 -3
- data/CLAUDE.md +0 -74
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
// Canonical strings preserve BigDecimal's fractional .0 and signed zero.
|
|
2
|
+
use regex::Regex;
|
|
3
|
+
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
|
4
|
+
use std::sync::LazyLock;
|
|
5
|
+
|
|
6
|
+
const PORTABLE_DIGITS: usize = 10_000;
|
|
7
|
+
static DECIMAL: LazyLock<Regex> =
|
|
8
|
+
LazyLock::new(|| Regex::new(r"^-?(?:0|[1-9][0-9]*)\.(?:0|[0-9]*[1-9])\z").unwrap());
|
|
9
|
+
static LEXICAL: LazyLock<Regex> = LazyLock::new(|| {
|
|
10
|
+
Regex::new(r"^([+-]?)(?:([0-9](?:_?[0-9])*)(?:\.([0-9](?:_?[0-9])*)?)?|\.([0-9](?:_?[0-9])*))(?:[eEdD]([+-]?[0-9]+))?\z").unwrap()
|
|
11
|
+
});
|
|
12
|
+
static TRAILING_UNDERSCORE: LazyLock<Regex> =
|
|
13
|
+
LazyLock::new(|| Regex::new(r"^([+-]?[0-9](?:_?[0-9])*)_\z").unwrap());
|
|
14
|
+
|
|
15
|
+
fn normalize(raw: &str) -> Result<Option<String>, &'static str> {
|
|
16
|
+
let token = raw.trim_matches([' ', '\t', '\n', '\r', '\x0b', '\x0c']);
|
|
17
|
+
if matches!(token, "NaN" | "Infinity" | "+Infinity" | "-Infinity") {
|
|
18
|
+
return Err("nonfinite OMML coordinate");
|
|
19
|
+
}
|
|
20
|
+
let short = TRAILING_UNDERSCORE.captures(token);
|
|
21
|
+
let token = short
|
|
22
|
+
.as_ref()
|
|
23
|
+
.and_then(|m| m.get(1))
|
|
24
|
+
.map_or(token, |m| m.as_str());
|
|
25
|
+
let Some(parts) = LEXICAL.captures(token) else {
|
|
26
|
+
return Ok(None);
|
|
27
|
+
};
|
|
28
|
+
let whole = parts.get(2).map_or("", |m| m.as_str()).replace('_', "");
|
|
29
|
+
let fraction = parts
|
|
30
|
+
.get(3)
|
|
31
|
+
.or_else(|| parts.get(4))
|
|
32
|
+
.map_or("", |m| m.as_str())
|
|
33
|
+
.replace('_', "");
|
|
34
|
+
let mantissa = format!("{whole}{fraction}");
|
|
35
|
+
let significant = mantissa.trim_start_matches('0');
|
|
36
|
+
let prefix = if &parts[1] == "-" { "-" } else { "" };
|
|
37
|
+
if significant.is_empty() {
|
|
38
|
+
return Ok(Some(format!("{prefix}0.0")));
|
|
39
|
+
}
|
|
40
|
+
let exponent = parts
|
|
41
|
+
.get(5)
|
|
42
|
+
.map_or("0", |m| m.as_str())
|
|
43
|
+
.parse::<i64>()
|
|
44
|
+
.map_err(|_| "coordinate exceeds the portable decimal digit limit")?;
|
|
45
|
+
let leading_zeroes = mantissa.len() - significant.len();
|
|
46
|
+
let digits = significant.trim_end_matches('0');
|
|
47
|
+
let len = digits.len() as i128;
|
|
48
|
+
let position = whole.len() as i128 - leading_zeroes as i128 + exponent as i128;
|
|
49
|
+
let output_digits = if position <= 0 {
|
|
50
|
+
1 - position + len
|
|
51
|
+
} else {
|
|
52
|
+
position.max(len) + 1
|
|
53
|
+
};
|
|
54
|
+
if output_digits > PORTABLE_DIGITS as i128 {
|
|
55
|
+
return Err("coordinate exceeds the portable decimal digit limit");
|
|
56
|
+
}
|
|
57
|
+
if position <= 0 {
|
|
58
|
+
return Ok(Some(format!(
|
|
59
|
+
"{prefix}0.{}{digits}",
|
|
60
|
+
"0".repeat((-position) as usize)
|
|
61
|
+
)));
|
|
62
|
+
}
|
|
63
|
+
let point = position as usize;
|
|
64
|
+
if point >= digits.len() {
|
|
65
|
+
return Ok(Some(format!(
|
|
66
|
+
"{prefix}{digits}{}.0",
|
|
67
|
+
"0".repeat(point - digits.len())
|
|
68
|
+
)));
|
|
69
|
+
}
|
|
70
|
+
let (integer, fraction) = digits.split_at(point);
|
|
71
|
+
Ok(Some(format!("{prefix}{integer}.{fraction}")))
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
fn xml_spelling(value: &str) -> String {
|
|
75
|
+
let negative = value.starts_with('-');
|
|
76
|
+
let (whole, fraction) = value.trim_start_matches('-').split_once('.').unwrap();
|
|
77
|
+
let mantissa = format!("{whole}{fraction}");
|
|
78
|
+
let significant = mantissa.trim_start_matches('0');
|
|
79
|
+
if significant.is_empty() {
|
|
80
|
+
return value.into();
|
|
81
|
+
}
|
|
82
|
+
let digits = significant.trim_end_matches('0');
|
|
83
|
+
let exponent = whole.len() as isize - (mantissa.len() - significant.len()) as isize;
|
|
84
|
+
let prefix = if negative { "-" } else { "" };
|
|
85
|
+
format!("{prefix}0.{digits}e{exponent}")
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
fn bounded(value: &str, min: i64, max: i64) -> bool {
|
|
89
|
+
if !DECIMAL.is_match(value) {
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
let negative = value.starts_with('-');
|
|
93
|
+
let (whole, fraction) = value.trim_start_matches('-').split_once('.').unwrap();
|
|
94
|
+
if whole.len() + fraction.len() > PORTABLE_DIGITS || whole.len() > 14 {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
let magnitude = whole.parse::<i64>().unwrap();
|
|
98
|
+
if magnitude == 0 && fraction == "0" {
|
|
99
|
+
return true;
|
|
100
|
+
}
|
|
101
|
+
let limit = if negative { -min } else { max };
|
|
102
|
+
magnitude < limit || (magnitude == limit && fraction == "0")
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
macro_rules! define_coordinate {
|
|
106
|
+
($module:ident, $name:ident, $min:expr, $max:expr) => {
|
|
107
|
+
pub mod $module {
|
|
108
|
+
use super::*;
|
|
109
|
+
|
|
110
|
+
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
111
|
+
pub struct $name(String);
|
|
112
|
+
|
|
113
|
+
impl $name {
|
|
114
|
+
pub fn new(value: String) -> Result<Self, &'static str> {
|
|
115
|
+
if bounded(&value, $min, $max) {
|
|
116
|
+
Ok(Self(value))
|
|
117
|
+
} else {
|
|
118
|
+
Err("expected a canonical decimal within OMML bounds")
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
impl Serialize for $name {
|
|
123
|
+
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
|
|
124
|
+
s.serialize_str(&self.0)
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
impl<'de> Deserialize<'de> for $name {
|
|
128
|
+
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
|
|
129
|
+
Self::new(String::deserialize(d)?).map_err(serde::de::Error::custom)
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
pub fn parse(raw: &str) -> Result<Option<$name>, &'static str> {
|
|
133
|
+
normalize(raw)?.map($name::new).transpose()
|
|
134
|
+
}
|
|
135
|
+
pub fn format(value: &$name) -> Result<String, std::convert::Infallible> {
|
|
136
|
+
Ok(xml_spelling(&value.0))
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
define_coordinate!(
|
|
143
|
+
st_coordinate,
|
|
144
|
+
STCoordinate,
|
|
145
|
+
-27_273_042_329_600,
|
|
146
|
+
27_273_042_316_900
|
|
147
|
+
);
|
|
148
|
+
define_coordinate!(
|
|
149
|
+
st_positive_coordinate,
|
|
150
|
+
STPositiveCoordinate,
|
|
151
|
+
0,
|
|
152
|
+
27_273_042_316_900
|
|
153
|
+
);
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// Canonical strings preserve BigDecimal's fractional .0 and signed zero.
|
|
2
|
+
const portableDigits = 10_000;
|
|
3
|
+
const decimal = /^(-)?(?:0|[1-9][0-9]*)\.(?:0|[0-9]*[1-9])(?![\s\S])/;
|
|
4
|
+
const lexical = /^([+-]?)(?:(\d(?:_?\d)*)(?:\.(\d(?:_?\d)*)?)?|\.(\d(?:_?\d)*))(?:[eEdD]([+-]?\d+))?(?![\s\S])/;
|
|
5
|
+
const trailingUnderscore = /^([+-]?\d(?:_?\d)*)_(?![\s\S])/;
|
|
6
|
+
|
|
7
|
+
function normalize(raw: string): string | null {
|
|
8
|
+
const token = raw.replace(/^[\t\n\r\f\v ]+|[\t\n\r\f\v ]+$/g, "");
|
|
9
|
+
if (/^(?:NaN|[+-]?Infinity)(?![\s\S])/.test(token)) throw new RangeError("nonfinite OMML coordinate");
|
|
10
|
+
const short = trailingUnderscore.exec(token);
|
|
11
|
+
const parts = lexical.exec(short?.[1] ?? token);
|
|
12
|
+
if (!parts) return null;
|
|
13
|
+
const [, sign, wholeRaw, fractionAfterWhole, fractionOnly, exponent] = parts;
|
|
14
|
+
const whole = (wholeRaw ?? "").replaceAll("_", "");
|
|
15
|
+
const fraction = (fractionAfterWhole ?? fractionOnly ?? "").replaceAll("_", "");
|
|
16
|
+
const mantissa = whole + fraction;
|
|
17
|
+
const significant = mantissa.replace(/^0+/, "");
|
|
18
|
+
const prefix = sign === "-" ? "-" : "";
|
|
19
|
+
if (significant.length === 0) return prefix + "0.0";
|
|
20
|
+
const leadingZeroes = mantissa.length - significant.length;
|
|
21
|
+
const digits = significant.replace(/0+$/, "");
|
|
22
|
+
const position = BigInt(whole.length - leadingZeroes) + BigInt(exponent ?? "0");
|
|
23
|
+
const length = BigInt(digits.length);
|
|
24
|
+
const outputDigits = position <= 0n ? 1n - position + length : (position > length ? position : length) + 1n;
|
|
25
|
+
if (outputDigits > BigInt(portableDigits)) throw new RangeError("coordinate exceeds the portable decimal digit limit");
|
|
26
|
+
const point = Number(position);
|
|
27
|
+
if (point <= 0) return prefix + "0." + "0".repeat(-point) + digits;
|
|
28
|
+
if (point >= digits.length) return prefix + digits + "0".repeat(point - digits.length) + ".0";
|
|
29
|
+
return prefix + digits.slice(0, point) + "." + digits.slice(point);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function xmlSpelling(value: string): string {
|
|
33
|
+
const negative = value.startsWith("-");
|
|
34
|
+
const [whole, fraction] = (negative ? value.slice(1) : value).split(".") as [string, string];
|
|
35
|
+
const mantissa = whole + fraction;
|
|
36
|
+
const significant = mantissa.replace(/^0+/, "");
|
|
37
|
+
if (significant.length === 0) return value;
|
|
38
|
+
const digits = significant.replace(/0+$/, "");
|
|
39
|
+
const exponent = whole.length - (mantissa.length - significant.length);
|
|
40
|
+
return (negative ? "-" : "") + "0." + digits + "e" + exponent;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function coordinate(min: bigint, max: bigint) {
|
|
44
|
+
const is = (value: unknown): value is string => {
|
|
45
|
+
if (typeof value !== "string" || !decimal.test(value)) return false;
|
|
46
|
+
const negative = value.startsWith("-");
|
|
47
|
+
const [whole, fraction] = (negative ? value.slice(1) : value).split(".") as [string, string];
|
|
48
|
+
if (whole.length + fraction.length > portableDigits || whole.length > 14) return false;
|
|
49
|
+
const magnitude = BigInt(whole);
|
|
50
|
+
if (magnitude === 0n && fraction === "0") return true;
|
|
51
|
+
const limit = negative ? -min : max;
|
|
52
|
+
return magnitude < limit || (magnitude === limit && fraction === "0");
|
|
53
|
+
};
|
|
54
|
+
const parse = (value: string): string => {
|
|
55
|
+
if (!is(value)) throw new RangeError("expected a canonical decimal within OMML bounds");
|
|
56
|
+
return value;
|
|
57
|
+
};
|
|
58
|
+
const parseXml = (raw: string): string | null => {
|
|
59
|
+
const value = normalize(raw);
|
|
60
|
+
return value === null ? null : parse(value);
|
|
61
|
+
};
|
|
62
|
+
const formatXml = (value: string): string => xmlSpelling(parse(value));
|
|
63
|
+
return { is, parse, parseXml, formatXml };
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export type STCoordinate = string;
|
|
67
|
+
const signed = coordinate(-27_273_042_329_600n, 27_273_042_316_900n);
|
|
68
|
+
export const { is: isSTCoordinate, parse: parseSTCoordinate, parseXml: parseSTCoordinateXml, formatXml: formatSTCoordinateXml } = signed;
|
|
69
|
+
export const formatSTCoordinate = parseSTCoordinate;
|
|
70
|
+
|
|
71
|
+
export type STPositiveCoordinate = string;
|
|
72
|
+
const positive = coordinate(0n, 27_273_042_316_900n);
|
|
73
|
+
export const { is: isSTPositiveCoordinate, parse: parseSTPositiveCoordinate, parseXml: parseSTPositiveCoordinateXml, formatXml: formatSTPositiveCoordinateXml } = positive;
|
|
74
|
+
export const formatSTPositiveCoordinate = parseSTPositiveCoordinate;
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
// OMML Integer descendants use the Ruby LutaML caster for XML and write
|
|
2
|
+
// canonical integer strings through the portable JSON codec.
|
|
3
|
+
use regex::Regex;
|
|
4
|
+
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
|
5
|
+
use std::sync::LazyLock;
|
|
6
|
+
|
|
7
|
+
static OCTAL_LINE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^0[0-7]+\z").unwrap());
|
|
8
|
+
static FLOAT_LINE: LazyLock<Regex> =
|
|
9
|
+
LazyLock::new(|| Regex::new(r"(?i)^-?[0-9]+(\.[0-9]+)?(e-?[0-9]+)?\z").unwrap());
|
|
10
|
+
static DECIMAL: LazyLock<Regex> =
|
|
11
|
+
LazyLock::new(|| Regex::new(r"^[+-]?(?:0[dD])?[0-9](?:_?[0-9])*\z").unwrap());
|
|
12
|
+
static OCTAL_PREFIX: LazyLock<Regex> =
|
|
13
|
+
LazyLock::new(|| Regex::new(r"^([+-]?)([0-7](?:_?[0-7])*)").unwrap());
|
|
14
|
+
static FLOAT_TOKEN: LazyLock<Regex> =
|
|
15
|
+
LazyLock::new(|| Regex::new(r"(?i)^[+-]?[0-9]+(?:\.[0-9]+)?(?:e[+-]?[0-9]+)?\z").unwrap());
|
|
16
|
+
static CANONICAL: LazyLock<Regex> =
|
|
17
|
+
LazyLock::new(|| Regex::new(r"^(?:0|-?[1-9][0-9]*)\z").unwrap());
|
|
18
|
+
|
|
19
|
+
fn parse_xml_integer(raw: &str) -> Result<i128, &'static str> {
|
|
20
|
+
let token = raw.trim_matches([' ', '\t', '\n', '\r', '\x0b', '\x0c']);
|
|
21
|
+
if raw.split('\n').any(|line| OCTAL_LINE.is_match(line)) {
|
|
22
|
+
let Some(parts) = OCTAL_PREFIX.captures(token) else {
|
|
23
|
+
return Ok(0);
|
|
24
|
+
};
|
|
25
|
+
let digits = parts[2].replace('_', "");
|
|
26
|
+
let value = i128::from_str_radix(&digits, 8).map_err(|_| "integer outside OMML bounds")?;
|
|
27
|
+
return Ok(if &parts[1] == "-" { -value } else { value });
|
|
28
|
+
}
|
|
29
|
+
if raw.split('\n').any(|line| FLOAT_LINE.is_match(line)) {
|
|
30
|
+
if !FLOAT_TOKEN.is_match(token) {
|
|
31
|
+
return Err("invalid numeric XML value");
|
|
32
|
+
}
|
|
33
|
+
let number: f64 = token.parse().map_err(|_| "invalid numeric XML value")?;
|
|
34
|
+
if !number.is_finite() {
|
|
35
|
+
return Err("nonfinite numeric XML value");
|
|
36
|
+
}
|
|
37
|
+
return Ok(number.trunc() as i128);
|
|
38
|
+
}
|
|
39
|
+
if !DECIMAL.is_match(token) {
|
|
40
|
+
return Err("invalid integer XML value");
|
|
41
|
+
}
|
|
42
|
+
let token = token.trim_start_matches(['+', '-']);
|
|
43
|
+
let digits = token
|
|
44
|
+
.strip_prefix("0d")
|
|
45
|
+
.or_else(|| token.strip_prefix("0D"))
|
|
46
|
+
.unwrap_or(token)
|
|
47
|
+
.replace('_', "");
|
|
48
|
+
let value = digits
|
|
49
|
+
.parse::<i128>()
|
|
50
|
+
.map_err(|_| "integer outside OMML bounds")?;
|
|
51
|
+
Ok(
|
|
52
|
+
if raw
|
|
53
|
+
.trim_start_matches([' ', '\t', '\n', '\r', '\x0b', '\x0c'])
|
|
54
|
+
.starts_with('-')
|
|
55
|
+
{
|
|
56
|
+
-value
|
|
57
|
+
} else {
|
|
58
|
+
value
|
|
59
|
+
},
|
|
60
|
+
)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
macro_rules! define_bound {
|
|
64
|
+
($module:ident, $name:ident, $min:expr, $max:expr) => {
|
|
65
|
+
pub mod $module {
|
|
66
|
+
use super::*;
|
|
67
|
+
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
68
|
+
pub struct $name(i128);
|
|
69
|
+
impl $name {
|
|
70
|
+
pub fn new(value: i128) -> Result<Self, &'static str> {
|
|
71
|
+
if ($min..=$max).contains(&value) {
|
|
72
|
+
Ok(Self(value))
|
|
73
|
+
} else {
|
|
74
|
+
Err("integer outside OMML bounds")
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
impl Serialize for $name {
|
|
79
|
+
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
|
|
80
|
+
s.serialize_str(&self.0.to_string())
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
impl<'de> Deserialize<'de> for $name {
|
|
84
|
+
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
|
|
85
|
+
let token = String::deserialize(d)?;
|
|
86
|
+
if !CANONICAL.is_match(&token) {
|
|
87
|
+
return Err(serde::de::Error::custom(
|
|
88
|
+
"expected a canonical integer string",
|
|
89
|
+
));
|
|
90
|
+
}
|
|
91
|
+
let value = token.parse().map_err(serde::de::Error::custom)?;
|
|
92
|
+
Self::new(value).map_err(serde::de::Error::custom)
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
pub fn parse(raw: &str) -> Result<Option<$name>, &'static str> {
|
|
96
|
+
$name::new(parse_xml_integer(raw)?).map(Some)
|
|
97
|
+
}
|
|
98
|
+
pub fn format(value: &$name) -> Result<String, std::convert::Infallible> {
|
|
99
|
+
Ok(value.0.to_string())
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
define_bound!(unsigned_int, UnsignedInt, 0, 4_294_967_295);
|
|
106
|
+
define_bound!(unsigned_long, UnsignedLong, 0, 18_446_744_073_709_551_615);
|
|
107
|
+
define_bound!(st_integer2, STInteger2, -2, 2);
|
|
108
|
+
define_bound!(st_integer255, STInteger255, 1, 255);
|
|
109
|
+
define_bound!(st_spacing_rule, STSpacingRule, 0, 4);
|
|
110
|
+
define_bound!(st_text_scale, STTextScale, 0, 600);
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// OMML Integer descendants call LutaML's Ruby integer caster with fixed bounds.
|
|
2
|
+
// Keep XML coercion distinct from the canonical portable JSON integer strings.
|
|
3
|
+
const octalLine = /^0[0-7]+(?![\s\S])/;
|
|
4
|
+
const floatLine = /^-?\d+(\.\d+)?(e-?\d+)?(?![\s\S])/i;
|
|
5
|
+
const decimal = /^[+-]?(?:0[dD])?\d(?:_?\d)*(?![\s\S])/;
|
|
6
|
+
const octalPrefix = /^([+-]?)([0-7](?:_?[0-7])*)/;
|
|
7
|
+
const floatToken = /^[+-]?\d+(?:\.\d+)?(?:e[+-]?\d+)?(?![\s\S])/i;
|
|
8
|
+
const canonical = /^(?:0|-?[1-9]\d*)(?![\s\S])/;
|
|
9
|
+
const trimRuby = (value: string): string => value.replace(/^[\t\n\r\f\v ]+|[\t\n\r\f\v ]+$/g, "");
|
|
10
|
+
|
|
11
|
+
function fromXml(raw: string): bigint {
|
|
12
|
+
const token = trimRuby(raw);
|
|
13
|
+
if (raw.split("\n").some((line) => octalLine.test(line))) {
|
|
14
|
+
const match = octalPrefix.exec(token);
|
|
15
|
+
if (!match) return 0n;
|
|
16
|
+
const value = BigInt("0o" + match[2]!.replaceAll("_", ""));
|
|
17
|
+
return match[1] === "-" ? -value : value;
|
|
18
|
+
}
|
|
19
|
+
if (raw.split("\n").some((line) => floatLine.test(line))) {
|
|
20
|
+
if (!floatToken.test(token)) throw new TypeError("invalid numeric XML value");
|
|
21
|
+
const value = Number(token);
|
|
22
|
+
if (!Number.isFinite(value)) throw new RangeError("nonfinite numeric XML value");
|
|
23
|
+
return BigInt(Math.trunc(value));
|
|
24
|
+
}
|
|
25
|
+
if (!decimal.test(token)) throw new TypeError("invalid integer XML value");
|
|
26
|
+
return BigInt(token.replace(/^(\+|-)?0[dD]/, "$1").replaceAll("_", ""));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function bound(min: bigint, max: bigint) {
|
|
30
|
+
const is = (value: unknown): value is bigint => typeof value === "bigint" && value >= min && value <= max;
|
|
31
|
+
const requireBound = (value: bigint): bigint => {
|
|
32
|
+
if (!is(value)) throw new RangeError("integer outside OMML bounds");
|
|
33
|
+
return value;
|
|
34
|
+
};
|
|
35
|
+
return {
|
|
36
|
+
is,
|
|
37
|
+
parse: (token: string): bigint => {
|
|
38
|
+
if (!canonical.test(token)) throw new TypeError("expected a canonical integer string");
|
|
39
|
+
return requireBound(BigInt(token));
|
|
40
|
+
},
|
|
41
|
+
parseXml: (token: string): bigint => requireBound(fromXml(token)),
|
|
42
|
+
format: (value: bigint): string => requireBound(value).toString(),
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export type UnsignedInt = bigint;
|
|
47
|
+
const uint = bound(0n, 4_294_967_295n);
|
|
48
|
+
export const {is: isUnsignedInt, parse: parseUnsignedInt, parseXml: parseUnsignedIntXml, format: formatUnsignedInt} = uint;
|
|
49
|
+
export const formatUnsignedIntXml = formatUnsignedInt;
|
|
50
|
+
|
|
51
|
+
export type UnsignedLong = bigint;
|
|
52
|
+
const ulong = bound(0n, 18_446_744_073_709_551_615n);
|
|
53
|
+
export const {is: isUnsignedLong, parse: parseUnsignedLong, parseXml: parseUnsignedLongXml, format: formatUnsignedLong} = ulong;
|
|
54
|
+
export const formatUnsignedLongXml = formatUnsignedLong;
|
|
55
|
+
|
|
56
|
+
export type STInteger2 = bigint;
|
|
57
|
+
const int2 = bound(-2n, 2n);
|
|
58
|
+
export const {is: isSTInteger2, parse: parseSTInteger2, parseXml: parseSTInteger2Xml, format: formatSTInteger2} = int2;
|
|
59
|
+
export const formatSTInteger2Xml = formatSTInteger2;
|
|
60
|
+
|
|
61
|
+
export type STInteger255 = bigint;
|
|
62
|
+
const int255 = bound(1n, 255n);
|
|
63
|
+
export const {is: isSTInteger255, parse: parseSTInteger255, parseXml: parseSTInteger255Xml, format: formatSTInteger255} = int255;
|
|
64
|
+
export const formatSTInteger255Xml = formatSTInteger255;
|
|
65
|
+
|
|
66
|
+
export type STSpacingRule = bigint;
|
|
67
|
+
const spacing = bound(0n, 4n);
|
|
68
|
+
export const {is: isSTSpacingRule, parse: parseSTSpacingRule, parseXml: parseSTSpacingRuleXml, format: formatSTSpacingRule} = spacing;
|
|
69
|
+
export const formatSTSpacingRuleXml = formatSTSpacingRule;
|
|
70
|
+
|
|
71
|
+
export type STTextScale = bigint;
|
|
72
|
+
const scale = bound(0n, 600n);
|
|
73
|
+
export const {is: isSTTextScale, parse: parseSTTextScale, parseXml: parseSTTextScaleXml, format: formatSTTextScale} = scale;
|
|
74
|
+
export const formatSTTextScaleXml = formatSTTextScale;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
|
2
|
+
|
|
3
|
+
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
4
|
+
pub struct Token(String);
|
|
5
|
+
|
|
6
|
+
impl Token {
|
|
7
|
+
pub fn new(value: String) -> Result<Self, &'static str> {
|
|
8
|
+
if value.is_empty()
|
|
9
|
+
|| value
|
|
10
|
+
.split(' ')
|
|
11
|
+
.all(|word| !word.is_empty() && !word.contains(['\t', '\n', '\x0c', '\r']))
|
|
12
|
+
{
|
|
13
|
+
Ok(Self(value))
|
|
14
|
+
} else {
|
|
15
|
+
Err("invalid OMML token")
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
impl Serialize for Token {
|
|
21
|
+
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
|
22
|
+
serializer.serialize_str(&self.0)
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
impl<'de> Deserialize<'de> for Token {
|
|
27
|
+
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
|
|
28
|
+
Self::new(String::deserialize(deserializer)?).map_err(serde::de::Error::custom)
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export type Token = string;
|
|
2
|
+
|
|
3
|
+
export function isToken(value: unknown): value is Token {
|
|
4
|
+
if (typeof value !== "string") return false;
|
|
5
|
+
if (value === "") return true;
|
|
6
|
+
return value.split(" ").every((word) => word !== "" && !/[\t\n\f\r]/.test(word));
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function parseToken(value: string): Token {
|
|
10
|
+
if (!isToken(value)) throw new TypeError("invalid OMML token");
|
|
11
|
+
return value;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export const formatToken = parseToken;
|
|
15
|
+
export const parseTokenXml = parseToken;
|
|
16
|
+
export const formatTokenXml = formatToken;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"lml_bundle":1,"roots":["Omml::Models::CTBr@omml"],"models":{"Omml::Models::CTBr@omml":{"lml":2,"model":"Omml::Models::CTBr","register":"omml","attributes":[{"name":"type","type":{"kind":"value","id":"Omml::Models::STBrType@omml"},"required":false,"collection":false,"default":{"present":false},"initialize_empty":false},{"name":"clear","type":{"kind":"value","id":"Omml::Models::STBrClear@omml"},"required":false,"collection":false,"default":{"present":false},"initialize_empty":false}],"mappings":{"xml":{"root":{"element":"CT_Br"},"rules":[{"kind":"attribute","name":["type"],"to":"type","render":{"nil":"omit","empty":"omit","default":false},"namespace":{"mode":"uri","uri":"http://schemas.openxmlformats.org/wordprocessingml/2006/main","prefix":"w"}},{"kind":"attribute","name":["clear"],"to":"clear","render":{"nil":"omit","empty":"omit","default":false},"namespace":{"mode":"uri","uri":"http://schemas.openxmlformats.org/wordprocessingml/2006/main","prefix":"w"}}],"namespace":{"mode":"uri","uri":"http://schemas.openxmlformats.org/wordprocessingml/2006/main","prefix":"w"}}}}},"value_types":{"Omml::Models::STBrClear@omml":{"base":"string","facets":{"enumeration":["none","left","right","all",""]}},"Omml::Models::STBrType@omml":{"base":"string","facets":{"enumeration":["page","column","textWrapping",""]}}},"capabilities":["xml"]}
|