Generate the MJCF grammar table and enforce its constraints.

The hand-written MJCF[] table in xml_native_reader.cc is replaced by
mjcf_table.inc, emitted from mjcf.schema by generate_mjcf_table.py and
checked for freshness by doc_test. nMJCF is now self-sizing. The two
tables are identical as trees of (tag, cardinality, attribute-set);
within-row attribute order changes where the schema factors shared
groups and projects default-context rows, and top-level rows follow
the schema's dependency order -- neither affects validation, which is
set-based, nor XMLschema.rst, whose generator orders sections itself
(regenerated here, reading the .inc instead of the reader source).

The schema's constraint declarations become enforcement: the emitter
writes a companion MJCF_constraints[] array (row-indexed into MJCF[]),
and mjXSchema::Check evaluates each element's constraints after its
attribute check, with uniform messages derived from the declaration:
"at most one of 'fovy', 'sensorsize' can be specified", "attributes
'reftype', 'refname' must be specified together", and so on.
Multi-attribute bundles render as ('site1', 'site2').

Fifteen hand-written co-occurrence checks across fourteen elements are
deleted -- connect/weld semantics mixing and completeness, the actuator
transmission mutex, camera fovy/sensorsize, light directional/type,
inertial fullinertia-versus-orientation, rangefinder and the distance
family, contact's matching criteria, user-sensor pairing, the frame
family's reftype/refname, size memory exclusivities, mesh builtin
exclusions, and attach body/frame (newly declared). Tests assert the
uniform messages.

Two findings along the way: sensorsize-requires-resolution is a
value-level compiler rule (positive resolution), not a presence rule --
a presence constraint would be wrong and is not declared; and Size()'s
nstack/njmax range checks tested the spec value before assignment, so
they never validated the parsed value -- now they do.

Verified by compiling all 81 models in the model/ corpus.

PiperOrigin-RevId: 958064622
Change-Id: I802cf5c0aee08a62926e36a281320ff9e34c0668
This commit is contained in:
Yuval Tassa
2026-08-02 16:29:32 -07:00
committed by Copybara-Service
parent 3f8db4c17a
commit 790f8fac30
12 changed files with 1600 additions and 1261 deletions
+16 -8
View File
@@ -2024,7 +2024,7 @@ TEST_F(XMLReaderTest, CameraInvalidFovyAndSensorsize) {
std::array<char, 1024> error;
MjModelPtr m = LoadModelFromString(xml, error.data(), error.size());
EXPECT_THAT(m.get(), IsNull());
EXPECT_THAT(error.data(), HasSubstr("either 'fovy' or 'sensorsize'"));
EXPECT_THAT(error.data(), HasSubstr("at most one of 'fovy', 'sensorsize'"));
EXPECT_THAT(error.data(), HasSubstr("line 6"));
}
@@ -2081,8 +2081,8 @@ TEST_F(XMLReaderTest, InvalidInertialOrientation) {
ASSERT_THAT(model.get(), IsNull());
EXPECT_THAT(
error.data(),
HasSubstr(
"fullinertia and inertial orientation cannot both be specified"));
HasSubstr("at most one of 'fullinertia', 'quat', 'axisangle', "
"'xyaxes', 'zaxis', 'euler' can be specified"));
}
TEST_F(XMLReaderTest, ReadShellParameter) {
@@ -2137,7 +2137,7 @@ TEST_F(XMLReaderTest, BuiltinAndFile) {
MjModelPtr model = LoadModelFromString(xml, error.data(), error.size());
ASSERT_THAT(model.get(), IsNull());
EXPECT_THAT(error.data(),
HasSubstr("builtin mesh cannot be used with user vertex data"));
HasSubstr("at most one of 'builtin', 'vertex' can be specified"));
}
TEST_F(XMLReaderTest, MakePlateNoParameters) {
@@ -2422,7 +2422,9 @@ TEST_F(RelativeFrameSensorParsingTest, RefNameButNoType) {
)";
std::array<char, 1024> error;
LoadModelFromString(xml, error.data(), error.size());
EXPECT_THAT(error.data(), HasSubstr("but reftype is missing"));
EXPECT_THAT(
error.data(),
HasSubstr("attributes 'reftype', 'refname' must be specified together"));
EXPECT_THAT(error.data(), HasSubstr("line 8"));
}
@@ -2440,7 +2442,9 @@ TEST_F(RelativeFrameSensorParsingTest, RefTypeButNoName) {
)";
std::array<char, 1024> error;
LoadModelFromString(xml, error.data(), error.size());
EXPECT_THAT(error.data(), HasSubstr("attribute missing: 'refname'"));
EXPECT_THAT(
error.data(),
HasSubstr("attributes 'reftype', 'refname' must be specified together"));
EXPECT_THAT(error.data(), HasSubstr("line 8"));
}
@@ -3563,7 +3567,9 @@ TEST_F(SensorParseTest, UserObjTypeNoName) {
std::array<char, 1024> error;
MjModelPtr model = LoadModelFromString(xml, error.data(), error.size());
ASSERT_THAT(model.get(), IsNull());
EXPECT_THAT(error.data(), HasSubstr("objtype 'site' given but"));
EXPECT_THAT(
error.data(),
HasSubstr("attributes 'objtype', 'objname' must be specified together"));
EXPECT_THAT(error.data(), HasSubstr("line 4"));
}
@@ -3578,7 +3584,9 @@ TEST_F(SensorParseTest, UserObjNameNoType) {
std::array<char, 1024> error;
MjModelPtr model = LoadModelFromString(xml, error.data(), error.size());
ASSERT_THAT(model.get(), IsNull());
EXPECT_THAT(error.data(), HasSubstr("objname 'kevin' given but"));
EXPECT_THAT(
error.data(),
HasSubstr("attributes 'objtype', 'objname' must be specified together"));
EXPECT_THAT(error.data(), HasSubstr("line 4"));
}