urc-converter 0.1.0__tar.gz
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.
- urc_converter-0.1.0/.gitignore +24 -0
- urc_converter-0.1.0/.python-version +1 -0
- urc_converter-0.1.0/LICENSE +21 -0
- urc_converter-0.1.0/PKG-INFO +29 -0
- urc_converter-0.1.0/README.md +18 -0
- urc_converter-0.1.0/pyproject.toml +36 -0
- urc_converter-0.1.0/src/urc_converter/__init__.py +31 -0
- urc_converter-0.1.0/src/urc_converter/error.py +11 -0
- urc_converter-0.1.0/src/urc_converter/model.py +93 -0
- urc_converter-0.1.0/src/urc_converter/parser/__init__.py +1 -0
- urc_converter-0.1.0/src/urc_converter/parser/assembly.py +70 -0
- urc_converter-0.1.0/src/urc_converter/parser/checks.py +72 -0
- urc_converter-0.1.0/src/urc_converter/parser/fields.py +144 -0
- urc_converter-0.1.0/src/urc_converter/parser/lex.py +65 -0
- urc_converter-0.1.0/src/urc_converter/parser/scan.py +78 -0
- urc_converter-0.1.0/src/urc_converter/parser/state.py +29 -0
- urc_converter-0.1.0/src/urc_converter/sources/__init__.py +20 -0
- urc_converter-0.1.0/src/urc_converter/sources/_shared.py +126 -0
- urc_converter-0.1.0/src/urc_converter/sources/bms/__init__.py +11 -0
- urc_converter-0.1.0/src/urc_converter/sources/bms/convert.py +310 -0
- urc_converter-0.1.0/src/urc_converter/sources/bms/model.py +22 -0
- urc_converter-0.1.0/src/urc_converter/sources/bms/parse.py +205 -0
- urc_converter-0.1.0/src/urc_converter/sources/ojn/__init__.py +7 -0
- urc_converter-0.1.0/src/urc_converter/sources/ojn/convert.py +163 -0
- urc_converter-0.1.0/src/urc_converter/sources/ojn/model.py +38 -0
- urc_converter-0.1.0/src/urc_converter/sources/ojn/parse.py +109 -0
- urc_converter-0.1.0/src/urc_converter/sources/osu/__init__.py +13 -0
- urc_converter-0.1.0/src/urc_converter/sources/osu/convert.py +103 -0
- urc_converter-0.1.0/src/urc_converter/sources/osu/model.py +40 -0
- urc_converter-0.1.0/src/urc_converter/sources/osu/parse.py +118 -0
- urc_converter-0.1.0/src/urc_converter/sources/qua/__init__.py +14 -0
- urc_converter-0.1.0/src/urc_converter/sources/qua/convert.py +93 -0
- urc_converter-0.1.0/src/urc_converter/sources/qua/model.py +45 -0
- urc_converter-0.1.0/src/urc_converter/sources/qua/parse.py +101 -0
- urc_converter-0.1.0/src/urc_converter/sources/sm/__init__.py +14 -0
- urc_converter-0.1.0/src/urc_converter/sources/sm/convert.py +203 -0
- urc_converter-0.1.0/src/urc_converter/sources/sm/model.py +52 -0
- urc_converter-0.1.0/src/urc_converter/sources/sm/parse.py +308 -0
- urc_converter-0.1.0/src/urc_converter/sources/sm/preprocess.py +134 -0
- urc_converter-0.1.0/src/urc_converter/strings.py +54 -0
- urc_converter-0.1.0/src/urc_converter/writer.py +77 -0
- urc_converter-0.1.0/uv.lock +73 -0
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# Python
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
.venv/
|
|
5
|
+
.idea
|
|
6
|
+
.ruff_cache
|
|
7
|
+
.mypy_cache
|
|
8
|
+
|
|
9
|
+
# Rust
|
|
10
|
+
target/
|
|
11
|
+
|
|
12
|
+
# Node
|
|
13
|
+
node_modules/
|
|
14
|
+
dist/
|
|
15
|
+
|
|
16
|
+
# .NET
|
|
17
|
+
bin/
|
|
18
|
+
obj/
|
|
19
|
+
.vs/
|
|
20
|
+
|
|
21
|
+
/CLAUDE.md
|
|
22
|
+
/AGENTS.md
|
|
23
|
+
/.claude/
|
|
24
|
+
/scratch/
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
3.14
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 CloudHolic
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: urc-converter
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: URC (Universal Rhythm Chart) parser and converter library
|
|
5
|
+
Author-email: CloudHolic <cloudholic@pm.me>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Requires-Python: >=3.14
|
|
9
|
+
Requires-Dist: pyyaml>=6.0.3
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
|
|
12
|
+
# urc-converter (Python)
|
|
13
|
+
|
|
14
|
+
URC 1.x parser and writer. See the [repo root](../../README.md) for the format
|
|
15
|
+
overview and the other ports (TypeScript, F#, Rust).
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
pip install urc-converter
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
```python
|
|
22
|
+
from urc_converter import UrcError, parse, write
|
|
23
|
+
|
|
24
|
+
try:
|
|
25
|
+
chart = parse(text) # raises UrcError(category, line, message) on failure
|
|
26
|
+
print(write(chart)) # canonical URC text
|
|
27
|
+
except UrcError as error:
|
|
28
|
+
print(error.category, error.line)
|
|
29
|
+
```
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# urc-converter (Python)
|
|
2
|
+
|
|
3
|
+
URC 1.x parser and writer. See the [repo root](../../README.md) for the format
|
|
4
|
+
overview and the other ports (TypeScript, F#, Rust).
|
|
5
|
+
|
|
6
|
+
```sh
|
|
7
|
+
pip install urc-converter
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
```python
|
|
11
|
+
from urc_converter import UrcError, parse, write
|
|
12
|
+
|
|
13
|
+
try:
|
|
14
|
+
chart = parse(text) # raises UrcError(category, line, message) on failure
|
|
15
|
+
print(write(chart)) # canonical URC text
|
|
16
|
+
except UrcError as error:
|
|
17
|
+
print(error.category, error.line)
|
|
18
|
+
```
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "urc-converter"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "URC (Universal Rhythm Chart) parser and converter library"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.14"
|
|
7
|
+
license = "MIT"
|
|
8
|
+
authors = [
|
|
9
|
+
{ name = "CloudHolic", email = "cloudholic@pm.me" }
|
|
10
|
+
]
|
|
11
|
+
dependencies = [
|
|
12
|
+
"pyyaml>=6.0.3",
|
|
13
|
+
]
|
|
14
|
+
|
|
15
|
+
[build-system]
|
|
16
|
+
requires = ["hatchling"]
|
|
17
|
+
build-backend = "hatchling.build"
|
|
18
|
+
|
|
19
|
+
[dependency-groups]
|
|
20
|
+
dev = [
|
|
21
|
+
"ruff"
|
|
22
|
+
]
|
|
23
|
+
|
|
24
|
+
[tool.hatch.build.targets.wheel]
|
|
25
|
+
packages = ["src/urc_converter"]
|
|
26
|
+
|
|
27
|
+
[tool.ruff]
|
|
28
|
+
line-length = 100
|
|
29
|
+
indent-width = 4
|
|
30
|
+
|
|
31
|
+
[tool.ruff.lint]
|
|
32
|
+
select = ["E", "F", "I", "UP", "B", "SIM", "N"]
|
|
33
|
+
|
|
34
|
+
[tool.ruff.format]
|
|
35
|
+
indent-style = "tab"
|
|
36
|
+
quote-style = "double"
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"""URC (Universal Rhythm Chart) parser and converter library."""
|
|
2
|
+
|
|
3
|
+
from .error import UrcError
|
|
4
|
+
from .model import (
|
|
5
|
+
Chart,
|
|
6
|
+
Judgment,
|
|
7
|
+
Layout,
|
|
8
|
+
Metadata,
|
|
9
|
+
Meter,
|
|
10
|
+
Note,
|
|
11
|
+
NoteType,
|
|
12
|
+
TimingPoint,
|
|
13
|
+
Version,
|
|
14
|
+
)
|
|
15
|
+
from .parser.scan import parse
|
|
16
|
+
from .writer import write
|
|
17
|
+
|
|
18
|
+
__all__ = [
|
|
19
|
+
"Chart",
|
|
20
|
+
"Judgment",
|
|
21
|
+
"Layout",
|
|
22
|
+
"Metadata",
|
|
23
|
+
"Meter",
|
|
24
|
+
"Note",
|
|
25
|
+
"NoteType",
|
|
26
|
+
"TimingPoint",
|
|
27
|
+
"UrcError",
|
|
28
|
+
"Version",
|
|
29
|
+
"parse",
|
|
30
|
+
"write",
|
|
31
|
+
]
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"""Structured errors raised while parsing URC documents."""
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class UrcError(Exception):
|
|
5
|
+
"""Parse failure with a machine-readable category and source line."""
|
|
6
|
+
|
|
7
|
+
def __init__(self, category: str, line: int, message: str) -> None:
|
|
8
|
+
super().__init__(f"{category} at line {line}: {message}")
|
|
9
|
+
self.category: str = category
|
|
10
|
+
self.line: int = line
|
|
11
|
+
self.message: str = message
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"""URC data model shared by the parser, writer, and converters."""
|
|
2
|
+
|
|
3
|
+
import enum
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class NoteType(enum.Enum):
|
|
8
|
+
"""Types of a chart object in the @Notes section."""
|
|
9
|
+
|
|
10
|
+
N = "N"
|
|
11
|
+
LS = "LS"
|
|
12
|
+
LE = "LE"
|
|
13
|
+
M = "M"
|
|
14
|
+
F = "F"
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
@dataclass
|
|
18
|
+
class Version:
|
|
19
|
+
"""File format version from the @URC header."""
|
|
20
|
+
|
|
21
|
+
major: int
|
|
22
|
+
minor: int
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@dataclass
|
|
26
|
+
class Metadata:
|
|
27
|
+
"""Song and chart metadata from the @Metadata secion."""
|
|
28
|
+
|
|
29
|
+
original: str
|
|
30
|
+
title: str
|
|
31
|
+
artist: str
|
|
32
|
+
creator: str
|
|
33
|
+
version: str
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@dataclass
|
|
37
|
+
class Judgment:
|
|
38
|
+
"""Timing windows (ms) and scoring rates from the optional @Judgment section."""
|
|
39
|
+
|
|
40
|
+
windows: list[float]
|
|
41
|
+
rates: list[float]
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
@dataclass
|
|
45
|
+
class Layout:
|
|
46
|
+
"""Key layout form the @Layout section."""
|
|
47
|
+
|
|
48
|
+
keys: int
|
|
49
|
+
special_keys: int
|
|
50
|
+
special_lanes: list[int] | None
|
|
51
|
+
|
|
52
|
+
@property
|
|
53
|
+
def total_lanes(self) -> int:
|
|
54
|
+
return self.keys + self.special_keys
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
@dataclass
|
|
58
|
+
class Meter:
|
|
59
|
+
"""Time signature numerator and denominator."""
|
|
60
|
+
|
|
61
|
+
beats: int
|
|
62
|
+
note_value: int
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
@dataclass
|
|
66
|
+
class TimingPoint:
|
|
67
|
+
"""One @Timing entry, multiplier is None when omitted."""
|
|
68
|
+
|
|
69
|
+
timestamp_ms: int
|
|
70
|
+
bpm: float
|
|
71
|
+
meter: Meter
|
|
72
|
+
multiplier: float | None
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
@dataclass
|
|
76
|
+
class Note:
|
|
77
|
+
"""One @Notes entry."""
|
|
78
|
+
|
|
79
|
+
timestamp_ms: int
|
|
80
|
+
lane: int
|
|
81
|
+
type: NoteType
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
@dataclass
|
|
85
|
+
class Chart:
|
|
86
|
+
"""Complete parsed URC document."""
|
|
87
|
+
|
|
88
|
+
format_version: Version
|
|
89
|
+
metadata: Metadata
|
|
90
|
+
judgment: Judgment | None
|
|
91
|
+
layout: Layout
|
|
92
|
+
timing: list[TimingPoint]
|
|
93
|
+
notes: list[Note]
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Parser for URC 1.x documents."""
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"""Chart assembly: note validation (Phase C) and model construction."""
|
|
2
|
+
|
|
3
|
+
from ..error import UrcError
|
|
4
|
+
from ..model import Chart, Judgment, Layout, Metadata, Note, NoteType
|
|
5
|
+
from ..strings import REQUIRED_SECTIONS, rule
|
|
6
|
+
from .state import ParseState
|
|
7
|
+
|
|
8
|
+
_NOTE_TYPES = {member.value: member for member in NoteType}
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def build(state: ParseState, end_line: int) -> Chart:
|
|
12
|
+
for name in REQUIRED_SECTIONS:
|
|
13
|
+
if name not in state.seen:
|
|
14
|
+
raise UrcError(rule(2), end_line, f"missing required section: {name}")
|
|
15
|
+
|
|
16
|
+
assert state.layout_type is not None
|
|
17
|
+
keys, special_keys = state.layout_type
|
|
18
|
+
layout = Layout(keys, special_keys, state.special)
|
|
19
|
+
total = layout.total_lanes
|
|
20
|
+
|
|
21
|
+
notes: list[Note] = []
|
|
22
|
+
open_ls: dict[int, int] = {}
|
|
23
|
+
ordered = sorted(state.notes, key=lambda raw: (raw.timestamp, raw.lane))
|
|
24
|
+
|
|
25
|
+
for raw in ordered:
|
|
26
|
+
if raw.timestamp < 0:
|
|
27
|
+
raise UrcError(rule(22), raw.line, "note timestamps must be non-negative")
|
|
28
|
+
|
|
29
|
+
if raw.lane < 0 or raw.lane >= total:
|
|
30
|
+
raise UrcError(rule(18), raw.line, f"lane out of range: {raw.lane}")
|
|
31
|
+
|
|
32
|
+
note_type = _NOTE_TYPES.get(raw.type_token)
|
|
33
|
+
if note_type is None:
|
|
34
|
+
raise UrcError(rule(19), raw.line, f"unknown note type: {raw.type_token!r}")
|
|
35
|
+
|
|
36
|
+
if note_type is NoteType.LE:
|
|
37
|
+
if raw.lane not in open_ls:
|
|
38
|
+
raise UrcError(rule(20), raw.line, f"LE without an open LS on lane {raw.lane}")
|
|
39
|
+
del open_ls[raw.lane]
|
|
40
|
+
elif note_type is NoteType.LS:
|
|
41
|
+
if raw.lane in open_ls:
|
|
42
|
+
raise UrcError(rule(21), raw.line, f"overlapping long notes on lane {raw.lane}")
|
|
43
|
+
open_ls[raw.lane] = raw.line
|
|
44
|
+
|
|
45
|
+
notes.append(Note(raw.timestamp, raw.lane, note_type))
|
|
46
|
+
|
|
47
|
+
if open_ls:
|
|
48
|
+
lane, line = next(iter(open_ls.items()))
|
|
49
|
+
raise UrcError(rule(20), line, f"unterminated LS on lane {lane}")
|
|
50
|
+
|
|
51
|
+
judgment = None
|
|
52
|
+
if state.windows is not None:
|
|
53
|
+
assert state.rates is not None
|
|
54
|
+
judgment = Judgment(state.windows, state.rates)
|
|
55
|
+
|
|
56
|
+
metadata = state.metadata
|
|
57
|
+
return Chart(
|
|
58
|
+
format_version=state.version,
|
|
59
|
+
metadata=Metadata(
|
|
60
|
+
original=metadata["Original"],
|
|
61
|
+
title=metadata["Title"],
|
|
62
|
+
artist=metadata["Artist"],
|
|
63
|
+
creator=metadata["Creator"],
|
|
64
|
+
version=metadata["Version"],
|
|
65
|
+
),
|
|
66
|
+
judgment=judgment,
|
|
67
|
+
layout=layout,
|
|
68
|
+
timing=state.timing,
|
|
69
|
+
notes=notes,
|
|
70
|
+
)
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"""Finalize-time semantic checks (Phase A completion and Phase B)."""
|
|
2
|
+
|
|
3
|
+
from ..error import UrcError
|
|
4
|
+
from ..strings import (
|
|
5
|
+
METADATA_FIELDS,
|
|
6
|
+
SECTION_JUDGMENT,
|
|
7
|
+
SECTION_LAYOUT,
|
|
8
|
+
SECTION_METADATA,
|
|
9
|
+
SECTION_TIMING,
|
|
10
|
+
rule,
|
|
11
|
+
)
|
|
12
|
+
from .state import ParseState
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def finalize_section(state: ParseState, section: str, line: int) -> None:
|
|
16
|
+
if section == SECTION_METADATA:
|
|
17
|
+
_check_metadata_complete(state, line)
|
|
18
|
+
elif section == SECTION_JUDGMENT:
|
|
19
|
+
_check_judgment(state, line)
|
|
20
|
+
elif section == SECTION_LAYOUT:
|
|
21
|
+
_check_layout(state, line)
|
|
22
|
+
elif section == SECTION_TIMING and not state.timing:
|
|
23
|
+
raise UrcError(rule(14), line, "first timing point must be at timestamp 0")
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _check_metadata_complete(state: ParseState, line: int) -> None:
|
|
27
|
+
for name in METADATA_FIELDS:
|
|
28
|
+
if name not in state.metadata:
|
|
29
|
+
raise UrcError(rule(4), line, f"Metadata is missing field: {name}")
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _check_judgment(state: ParseState, line: int) -> None:
|
|
33
|
+
windows = state.windows
|
|
34
|
+
rates = state.rates
|
|
35
|
+
|
|
36
|
+
if windows is None or rates is None:
|
|
37
|
+
raise UrcError(rule(4), line, "Judgment requires both Window and Rate")
|
|
38
|
+
|
|
39
|
+
if len(windows) != len(rates):
|
|
40
|
+
raise UrcError(rule(7), line, "Window and Rate must have the same count")
|
|
41
|
+
|
|
42
|
+
for earlier, later in zip(windows, windows[1:], strict=False):
|
|
43
|
+
if later < earlier:
|
|
44
|
+
raise UrcError(rule(8), line, "Window values must be ascending")
|
|
45
|
+
|
|
46
|
+
for earlier, later in zip(rates, rates[1:], strict=False):
|
|
47
|
+
if later > earlier:
|
|
48
|
+
raise UrcError(rule(9), line, "Rate values must be descending")
|
|
49
|
+
|
|
50
|
+
for rate in rates:
|
|
51
|
+
if rate < 0 or rate > 100:
|
|
52
|
+
raise UrcError(rule(10), line, "Rate values must be in 0-100")
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def _check_layout(state: ParseState, line: int) -> None:
|
|
56
|
+
layout_type = state.layout_type
|
|
57
|
+
if layout_type is None:
|
|
58
|
+
raise UrcError(rule(4), line, "Layout is missing field: Type")
|
|
59
|
+
if not state.special_seen:
|
|
60
|
+
raise UrcError(rule(4), line, "Layout is missing field: Special")
|
|
61
|
+
|
|
62
|
+
special = state.special
|
|
63
|
+
if special is None:
|
|
64
|
+
return
|
|
65
|
+
|
|
66
|
+
total = layout_type[0] + layout_type[1]
|
|
67
|
+
for lane in special:
|
|
68
|
+
if lane < 0 or lane >= total:
|
|
69
|
+
raise UrcError(rule(12), line, f"special lane out of range: {lane}")
|
|
70
|
+
|
|
71
|
+
if len(set(special)) != len(special):
|
|
72
|
+
raise UrcError(rule(13), line, "duplicate special lanes")
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
"""Per-section field line parsing (Phase A content handlers)."""
|
|
2
|
+
|
|
3
|
+
from collections.abc import Callable
|
|
4
|
+
|
|
5
|
+
from ..error import UrcError
|
|
6
|
+
from ..model import TimingPoint
|
|
7
|
+
from ..strings import (
|
|
8
|
+
JUDGMENT_FIELD_RATE,
|
|
9
|
+
JUDGMENT_FIELD_WINDOW,
|
|
10
|
+
LAYOUT_FIELD_SPECIAL,
|
|
11
|
+
LAYOUT_FIELD_TYPE,
|
|
12
|
+
METADATA_FIELDS,
|
|
13
|
+
SECTION_JUDGMENT,
|
|
14
|
+
SECTION_LAYOUT,
|
|
15
|
+
SECTION_METADATA,
|
|
16
|
+
SECTION_NOTES,
|
|
17
|
+
SECTION_TIMING,
|
|
18
|
+
SPECIAL_NONE,
|
|
19
|
+
SYNTAX,
|
|
20
|
+
rule,
|
|
21
|
+
)
|
|
22
|
+
from . import lex
|
|
23
|
+
from .state import ParseState, RawNote
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def metadata_field(state: ParseState, text: str, line: int) -> None:
|
|
27
|
+
name, separator, value = text.partition(":")
|
|
28
|
+
|
|
29
|
+
if not separator:
|
|
30
|
+
raise UrcError(SYNTAX, line, f"expected 'Field: Value', got: {text!r}")
|
|
31
|
+
|
|
32
|
+
name = name.strip()
|
|
33
|
+
if name not in METADATA_FIELDS:
|
|
34
|
+
raise UrcError(rule(6), line, f"unknown metadata field: {name}")
|
|
35
|
+
if name in state.metadata:
|
|
36
|
+
raise UrcError(SYNTAX, line, f"duplicate metadata field: {name}")
|
|
37
|
+
|
|
38
|
+
value = value.strip()
|
|
39
|
+
if value == "":
|
|
40
|
+
raise UrcError(rule(5), line, f"metadata field has no value: {name}")
|
|
41
|
+
|
|
42
|
+
state.metadata[name] = value
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def judgment_field(state: ParseState, text: str, line: int) -> None:
|
|
46
|
+
name, separator, value = text.partition(":")
|
|
47
|
+
|
|
48
|
+
if not separator:
|
|
49
|
+
raise UrcError(SYNTAX, line, f"expected 'Field: values', got: {text!r}")
|
|
50
|
+
|
|
51
|
+
name = name.strip()
|
|
52
|
+
if name == JUDGMENT_FIELD_WINDOW:
|
|
53
|
+
if state.windows is not None:
|
|
54
|
+
raise UrcError(SYNTAX, line, "duplicate judgment field: Window")
|
|
55
|
+
state.windows = lex.float_list(value, line)
|
|
56
|
+
elif name == JUDGMENT_FIELD_RATE:
|
|
57
|
+
if state.rates is not None:
|
|
58
|
+
raise UrcError(SYNTAX, line, "duplicate judgment field: Rate")
|
|
59
|
+
state.rates = lex.float_list(value, line)
|
|
60
|
+
else:
|
|
61
|
+
raise UrcError(rule(6), line, f"unknown judgment field: {name}")
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def layout_field(state: ParseState, text: str, line: int) -> None:
|
|
65
|
+
name, separator, value = text.partition(":")
|
|
66
|
+
|
|
67
|
+
if not separator:
|
|
68
|
+
raise UrcError(SYNTAX, line, f"expected 'Field: Value', got: {text!r}")
|
|
69
|
+
|
|
70
|
+
name = name.strip()
|
|
71
|
+
if name == LAYOUT_FIELD_TYPE:
|
|
72
|
+
if state.layout_type is not None:
|
|
73
|
+
raise UrcError(SYNTAX, line, "duplicate layout field: Type")
|
|
74
|
+
state.layout_type = lex.layout_type(value.strip(), line)
|
|
75
|
+
elif name == LAYOUT_FIELD_SPECIAL:
|
|
76
|
+
if state.special_seen:
|
|
77
|
+
raise UrcError(SYNTAX, line, "duplicate layout field: Special")
|
|
78
|
+
|
|
79
|
+
if value.strip() == SPECIAL_NONE:
|
|
80
|
+
state.special = None
|
|
81
|
+
else:
|
|
82
|
+
lanes: list[int] = []
|
|
83
|
+
for token in value.split(","):
|
|
84
|
+
token = token.strip()
|
|
85
|
+
if token == "":
|
|
86
|
+
raise UrcError(SYNTAX, line, "empty lane in Special list")
|
|
87
|
+
lanes.append(lex.int_value(token, line))
|
|
88
|
+
|
|
89
|
+
state.special = lanes
|
|
90
|
+
|
|
91
|
+
state.special_seen = True
|
|
92
|
+
else:
|
|
93
|
+
raise UrcError(rule(6), line, f"unknown layout field: {name}")
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def timing_point(state: ParseState, text: str, line: int) -> None:
|
|
97
|
+
fields = [field.strip() for field in text.split(",")]
|
|
98
|
+
|
|
99
|
+
if len(fields) not in (3, 4):
|
|
100
|
+
raise UrcError(SYNTAX, line, f"timing point needs 3 or 4 fields, got {len(fields)}")
|
|
101
|
+
|
|
102
|
+
timestamp = lex.int_value(fields[0], line)
|
|
103
|
+
bpm = lex.float_value(fields[1], line)
|
|
104
|
+
meter = lex.meter(fields[2], line)
|
|
105
|
+
multiplier = None
|
|
106
|
+
|
|
107
|
+
if len(fields) == 4 and fields[3] != "":
|
|
108
|
+
multiplier = lex.float_value(fields[3], line)
|
|
109
|
+
|
|
110
|
+
if not state.timing:
|
|
111
|
+
if timestamp != 0:
|
|
112
|
+
raise UrcError(rule(14), line, "first timing point must be at timestamp 0")
|
|
113
|
+
elif timestamp <= state.timing[-1].timestamp_ms:
|
|
114
|
+
raise UrcError(rule(15), line, "timing timestamps must be strictly ascending")
|
|
115
|
+
|
|
116
|
+
if bpm <= 0:
|
|
117
|
+
raise UrcError(rule(16), line, "bpm must be positive")
|
|
118
|
+
|
|
119
|
+
state.timing.append(TimingPoint(timestamp, bpm, meter, multiplier))
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def note_line(state: ParseState, text: str, line: int) -> None:
|
|
123
|
+
fields = [field.strip() for field in text.split(",")]
|
|
124
|
+
|
|
125
|
+
if len(fields) != 3:
|
|
126
|
+
raise UrcError(SYNTAX, line, f"note needs 3 fields, got {len(fields)}")
|
|
127
|
+
|
|
128
|
+
state.notes.append(
|
|
129
|
+
RawNote(lex.int_value(fields[0], line), lex.int_value(fields[1], line), fields[2], line)
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
_HANDLERS = {
|
|
134
|
+
SECTION_METADATA: metadata_field,
|
|
135
|
+
SECTION_JUDGMENT: judgment_field,
|
|
136
|
+
SECTION_LAYOUT: layout_field,
|
|
137
|
+
SECTION_TIMING: timing_point,
|
|
138
|
+
SECTION_NOTES: note_line,
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def field_handler(section: str) -> Callable[[ParseState, str, int], None] | None:
|
|
143
|
+
"""Content handler for a section, or None outside data sections."""
|
|
144
|
+
return _HANDLERS.get(section)
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"""Scalar and list token parsing for URC field values."""
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
|
|
5
|
+
from ..error import UrcError
|
|
6
|
+
from ..model import Meter
|
|
7
|
+
from ..strings import SYNTAX, rule
|
|
8
|
+
|
|
9
|
+
_INT_RE = re.compile(r"^-?\d+$")
|
|
10
|
+
_FLOAT_RE = re.compile(r"^-?\d+(\.\d+)?$")
|
|
11
|
+
_METER_RE = re.compile(r"^(\d+)/(\d+)$")
|
|
12
|
+
_TYPE_RE = re.compile(r"^(\d+)(?:\+(\d+))?$")
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def int_value(token: str, line: int) -> int:
|
|
16
|
+
if _INT_RE.match(token) is None:
|
|
17
|
+
raise UrcError(SYNTAX, line, f"invalid integer: {token!r}")
|
|
18
|
+
|
|
19
|
+
return int(token)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def float_value(token: str, line: int) -> float:
|
|
23
|
+
if _FLOAT_RE.match(token) is None:
|
|
24
|
+
raise UrcError(SYNTAX, line, f"invalid float: {token!r}")
|
|
25
|
+
|
|
26
|
+
return float(token)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def float_list(value: str, line: int) -> list[float]:
|
|
30
|
+
values: list[float] = []
|
|
31
|
+
|
|
32
|
+
for token in value.split(","):
|
|
33
|
+
token = token.strip()
|
|
34
|
+
if token == "":
|
|
35
|
+
raise UrcError(SYNTAX, line, "empty value in list")
|
|
36
|
+
|
|
37
|
+
values.append(float_value(token, line))
|
|
38
|
+
|
|
39
|
+
return values
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def layout_type(value: str, line: int) -> tuple[int, int]:
|
|
43
|
+
match = _TYPE_RE.match(value)
|
|
44
|
+
if match is None:
|
|
45
|
+
raise UrcError(SYNTAX, line, f"invalid Type value: {value!r}")
|
|
46
|
+
|
|
47
|
+
keys = int(match.group(1))
|
|
48
|
+
special = int(match.group(2) or "0")
|
|
49
|
+
if keys < 1 or (match.group(2) is not None and special < 1):
|
|
50
|
+
raise UrcError(SYNTAX, line, "Type values must be positive")
|
|
51
|
+
|
|
52
|
+
return keys, special
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def meter(token: str, line: int) -> Meter:
|
|
56
|
+
match = _METER_RE.match(token)
|
|
57
|
+
if match is None:
|
|
58
|
+
raise UrcError(rule(17), line, f"invalid meter: {token!r}")
|
|
59
|
+
|
|
60
|
+
beats = int(match.group(1))
|
|
61
|
+
note_value = int(match.group(2))
|
|
62
|
+
if beats < 1 or note_value < 1:
|
|
63
|
+
raise UrcError(rule(17), line, f"invalid meter: {token!r}")
|
|
64
|
+
|
|
65
|
+
return Meter(beats, note_value)
|