invalid variable names
authorBen Pfaff <blp@cs.stanford.edu>
Tue, 10 Jun 2025 16:21:00 +0000 (09:21 -0700)
committerBen Pfaff <blp@cs.stanford.edu>
Tue, 10 Jun 2025 16:21:00 +0000 (09:21 -0700)
rust/pspp/src/sys/cooked.rs
rust/pspp/src/sys/test.rs
rust/pspp/src/sys/testdata/invalid_variable_name.expected [new file with mode: 0644]
rust/pspp/src/sys/testdata/invalid_variable_name.sack [new file with mode: 0644]

index cb9de8fde2e936eec7542e7614c263b812c7635a..0978487ae9ab53e7433d6a2b9d25244819a77578 100644 (file)
@@ -547,7 +547,9 @@ pub fn decode(
         .filter(|(_index, record)| record.width != RawWidth::Continuation)
     {
         let name = trim_end_spaces(input.name.to_string());
-        let name = match Identifier::from_encoding(name, encoding) {
+        let name = match Identifier::from_encoding(name, encoding)
+            .and_then(Identifier::must_be_ordinary)
+        {
             Ok(name) => {
                 if !dictionary.variables.contains(&name.0) {
                     name
index 3abb9cdc1287b52dc47c928a223352094bee0004..03cb14eda7dd97e0be665504dfd5fd5894a15fbf 100644 (file)
@@ -137,6 +137,11 @@ fn wrong_variable_positions() {
     test_sysfile("wrong_variable_positions");
 }
 
+#[test]
+fn invalid_variable_name() {
+    test_sysfile("invalid_variable_name");
+}
+
 fn test_sysfile(name: &str) {
     let input_filename = Path::new(env!("CARGO_MANIFEST_DIR"))
         .join("src/sys/testdata")
diff --git a/rust/pspp/src/sys/testdata/invalid_variable_name.expected b/rust/pspp/src/sys/testdata/invalid_variable_name.expected
new file mode 100644 (file)
index 0000000..49475a8
--- /dev/null
@@ -0,0 +1,29 @@
+"$UM1" may not be used as an identifier because it begins with disallowed character "$".  Renaming variable to VAR001.
+
+"#UM1" may not be used as an identifier because it begins with disallowed character "#".  Renaming variable to VAR002.
+
+"!UM1" may not be used as an identifier because it begins with disallowed character "!".  Renaming variable to VAR003.
+
+"GE" may not be used as an identifier because it is a reserved word.  Renaming variable to VAR004.
+
+╭──────────────────────┬────────────────────────╮
+│       Created        │    01-JAN-2011 20:53:52│
+├──────────────────────┼────────────────────────┤
+│Writer Product        │PSPP synthetic test file│
+├──────────────────────┼────────────────────────┤
+│       Compression    │SAV                     │
+│       Number of Cases│Unknown                 │
+╰──────────────────────┴────────────────────────╯
+
+╭─────────┬─╮
+│Variables│4│
+╰─────────┴─╯
+
+╭──────┬────────┬─────┬─────────────────┬─────┬─────┬─────────┬────────────┬────────────┬──────────────╮
+│      │Position│Label│Measurement Level│ Role│Width│Alignment│Print Format│Write Format│Missing Values│
+├──────┼────────┼─────┼─────────────────┼─────┼─────┼─────────┼────────────┼────────────┼──────────────┤
+│var001│       1│     │                 │Input│    8│Right    │F8.0        │F8.0        │              │
+│var002│       2│     │                 │Input│    8│Right    │F8.0        │F8.0        │              │
+│var003│       3│     │                 │Input│    8│Right    │F8.0        │F8.0        │              │
+│var004│       4│     │                 │Input│    8│Right    │F8.0        │F8.0        │              │
+╰──────┴────────┴─────┴─────────────────┴─────┴─────┴─────────┴────────────┴────────────┴──────────────╯
diff --git a/rust/pspp/src/sys/testdata/invalid_variable_name.sack b/rust/pspp/src/sys/testdata/invalid_variable_name.sack
new file mode 100644 (file)
index 0000000..d87cf14
--- /dev/null
@@ -0,0 +1,15 @@
+# File header.
+"$FL2"; s60 "$(#) SPSS DATA FILE PSPP synthetic test file";
+2; 4; 1; 0; -1; 100.0; "01 Jan 11"; "20:53:52"; s64 ""; i8 0 *3;
+
+# Numeric variable.
+2; 0; 0; 0; 0x050800 *2; s8 >>"$UM1"<<;
+2; 0; 0; 0; 0x050800 *2; s8 >>"#UM1"<<;
+2; 0; 0; 0; 0x050800 *2; s8 >>"!UM1"<<;
+2; 0; 0; 0; 0x050800 *2; s8 >>"GE"<<;
+
+# Character encoding record.
+7; 20; 1; 12; "windows-1252";
+
+# End of dictionary.
+999; 0;