Skip to content

Commit 872e387

Browse files
author
Sam Crow
committed
Made string/byte types and saturated bool optional
1 parent a812df3 commit 872e387

File tree

64 files changed

+638
-167
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+638
-167
lines changed

CHANGELOG.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## Unreleased
88

9-
- `canadensis_dsdl_parser`: Breaking change: Removed `saturated bool` type
9+
- `canadensis_dsdl_parser`: Breaking change: Added Config, with options for `byte`, `utf8`, and `saturated bool`
1010
- `canadensis_dsdl_parser`: Added support for `byte` and `utf8` types (this is a breaking change for code that uses
1111
this library, but it does not break compatibility with any existing DSDL file)
12-
- `canadensis_dsdl_frontend`: Added support for `byte` and `utf8` types (this is a breaking change for code that uses
13-
this library, but it does not break compatibility with any existing DSDL file)
14-
- `canadensis_codegen_rust`: Added support for `byte` and `utf8` (code generation is the same as for uint8)
12+
- `canadensis_dsdl_frontend`: Breaking change: Added Config, with options for `byte`, `utf8`, and `saturated bool`
13+
- `canadensis_macro`: Added options for `byte`, `utf8`, and `saturated bool` (not a breaking change)
14+
- `canadensis_codegen_rust`: Added unstable optional support for `byte` and `utf8` (code generation is the same as for uint8)
15+
- `canadensis_codegen_rust`: Added unstable option to forbid `saturated bool`
1516

1617
## [canadensis-v0.3.3](https://github.com/samcrow/canadensis/releases/tag/canadensis-v0.3.3) - 2025-03-23
1718

canadensis_codegen_rust/src/main.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ extern crate canadensis_codegen_rust;
22
extern crate canadensis_dsdl_frontend;
33
extern crate clap;
44

5-
use canadensis_dsdl_frontend::Package;
5+
use canadensis_dsdl_frontend::{Config, Package};
66
use clap::{value_parser, Arg, Command};
77
use std::collections::BTreeMap;
88
use std::fs::File;
@@ -28,12 +28,13 @@ fn run() -> Result<(), Box<dyn std::error::Error>> {
2828
output_file: output_path,
2929
external_packages,
3030
rustfmt,
31+
config,
3132
} => {
3233
let mut package = Package::new();
3334
for path in input_folders {
3435
package.add_files(path)?;
3536
}
36-
let package = match package.compile_with_warnings() {
37+
let package = match package.compile_with_warnings(&config) {
3738
Ok(package) => package,
3839
Err((e, warnings)) => {
3940
for warning in warnings {
@@ -103,6 +104,8 @@ enum Args {
103104
external_packages: BTreeMap<Vec<String>, Vec<String>>,
104105
/// Run rustfmt on the generated code
105106
rustfmt: bool,
107+
/// Parser configuration
108+
config: Config,
106109
},
107110
PrintDependencies,
108111
}
@@ -142,6 +145,15 @@ fn get_args() -> Args {
142145
.long("rustfmt")
143146
.num_args(0)
144147
.help("Run rustfmt to format the generated code")
148+
)
149+
.arg(Arg::new("allow_utf8_and_byte")
150+
.long("unstable-allow-utf8-and-byte")
151+
.num_args(0)
152+
.help("Allow utf8 and byte DSDL types (this option is unstable)")
153+
).arg(Arg::new("forbid_saturated_bool")
154+
.long("unstable-forbid-saturated-bool")
155+
.num_args(0)
156+
.help("Forbid the saturated bool DSDL type (this option is unstable and may become the default)")
145157
))
146158
.subcommand(Command::new("print-dependencies")
147159
.about("Prints the packages that the generated code depends on (for use in Cargo.toml)"));
@@ -165,6 +177,10 @@ fn get_args() -> Args {
165177
})
166178
.unwrap_or_else(BTreeMap::new),
167179
rustfmt: matches.contains_id("rustfmt"),
180+
config: Config {
181+
allow_utf8_and_byte: matches.contains_id("allow_utf8_and_byte"),
182+
allow_saturated_bool: !matches.contains_id("forbid_saturated_bool"),
183+
},
168184
},
169185
Some(("print-dependencies", _)) => Args::PrintDependencies,
170186
_ => panic!("Unrecognized Subcommand"),

canadensis_codegen_rust/tests/compile_fail.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ extern crate canadensis_codegen_rust;
77
extern crate canadensis_dsdl_frontend;
88

99
use canadensis_dsdl_frontend::compiled::package::CompiledPackage;
10-
use canadensis_dsdl_frontend::Package;
10+
use canadensis_dsdl_frontend::{Config, Package};
1111
use std::ffi::OsString;
1212
use std::fs;
1313
use std::io;
@@ -49,7 +49,8 @@ fn compile_fail() -> io::Result<()> {
4949
fn try_compile_package(path: &Path) -> Result<CompiledPackage, canadensis_dsdl_frontend::Error> {
5050
let mut package = Package::new();
5151
package.add_files(path)?;
52-
package.compile()
52+
let config = Config::default();
53+
package.compile(&config)
5354
}
5455

5556
fn try_compile_and_generate_code(path: &Path) -> Result<(), Box<dyn std::error::Error>> {

canadensis_codegen_rust/tests/compile_pass.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ extern crate canadensis_dsdl_frontend;
44
use std::path::PathBuf;
55

66
use canadensis_dsdl_frontend::compiled::package::CompiledPackage;
7-
use canadensis_dsdl_frontend::Package;
7+
use canadensis_dsdl_frontend::{Config, Package};
88

99
/// Checks that this library can compile the Cyphal public regulated data types, Nunavut test
1010
/// types, and a few additional Canadensis test types
@@ -43,7 +43,11 @@ fn try_compile_package(
4343
for path in paths {
4444
package.add_files(path)?;
4545
}
46-
package.compile()
46+
let config = Config {
47+
allow_utf8_and_byte: true,
48+
allow_saturated_bool: false,
49+
};
50+
package.compile(&config)
4751
}
4852

4953
fn try_compile_and_generate_code(paths: &[PathBuf]) -> Result<(), Box<dyn std::error::Error>> {

canadensis_dsdl_frontend/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,8 @@ path = "../canadensis_dsdl_parser"
2626
[dependencies.canadensis_bit_length_set]
2727
version = "0.3.0"
2828
path = "../canadensis_bit_length_set"
29+
30+
[dev-dependencies]
31+
serde = { version = "1.0", features = ["derive"] }
32+
serde_json = "1.0"
33+
regex = "1.10.5"

canadensis_dsdl_frontend/examples/load_package.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
extern crate canadensis_dsdl_frontend;
22
use canadensis_dsdl_frontend::compiled::DsdlKind;
3-
use canadensis_dsdl_frontend::{Error, Package};
3+
use canadensis_dsdl_frontend::{Config, Error, Package};
44
use std::env;
55
use std::process;
66

@@ -29,7 +29,7 @@ fn run() -> Result<(), Error> {
2929
package.add_files(path)?;
3030
}
3131

32-
let compiled = package.compile()?;
32+
let compiled = package.compile(&Config::default())?;
3333

3434
for (key, compiled_dsdl) in compiled {
3535
println!("{}:", key);

canadensis_dsdl_frontend/src/compile.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::types::expression::convert_type;
1111
use crate::types::{array_length_bits, PrimitiveType, ResolvedScalarType, ResolvedType};
1212
use crate::warning::Warnings;
1313
use canadensis_bit_length_set::BitLengthSet;
14-
use canadensis_dsdl_parser::{Identifier, Span, Statement};
14+
use canadensis_dsdl_parser::{Config, Identifier, Span, Statement};
1515
use once_cell::sync::Lazy;
1616
use std::collections::btree_map::Entry;
1717
use std::collections::BTreeMap;
@@ -32,8 +32,9 @@ static BIT_LENGTH_ZERO: Lazy<BitLengthSet> = Lazy::new(|| BitLengthSet::single(0
3232
///
3333
/// This function returns the compiled DSDL or an error. In either case, it also returns
3434
/// a set of warnings.
35-
pub(crate) fn compile(files: BTreeMap<TypeKey, DsdlFile>) -> CompileOutput {
35+
pub(crate) fn compile(files: BTreeMap<TypeKey, DsdlFile>, config: &Config) -> CompileOutput {
3636
let context = PersistentContext {
37+
config,
3738
pending: files,
3839
done: BTreeMap::new(),
3940
warnings: Warnings::new(),
@@ -53,14 +54,14 @@ pub(crate) struct CompileOutput {
5354
///
5455
/// They use its functions to collect information about the type currently being compiled, and
5556
/// about other types.
56-
pub(crate) struct CompileContext<'p> {
57+
pub(crate) struct CompileContext<'p, 'c: 'p> {
5758
/// Persistent context and information about other types
58-
persistent: &'p mut PersistentContext,
59+
persistent: &'p mut PersistentContext<'c>,
5960
/// Information about the type currently being compiled
6061
current_file: &'p mut FileState,
6162
}
6263

63-
impl<'p> CompileContext<'p> {
64+
impl<'p, 'c: 'p> CompileContext<'p, 'c> {
6465
/// Returns the constants that have been declared in the current file
6566
///
6667
/// If the current file defines a service type, constants declared in the request section
@@ -220,15 +221,19 @@ impl<'p> CompileContext<'p> {
220221
}
221222

222223
/// A convenience function to make a `CompileContext`
223-
fn ctx<'p>(p: &'p mut PersistentContext, c: &'p mut FileState) -> CompileContext<'p> {
224+
fn ctx<'p, 'c: 'p>(
225+
p: &'p mut PersistentContext<'c>,
226+
c: &'p mut FileState,
227+
) -> CompileContext<'p, 'c> {
224228
CompileContext {
225229
persistent: p,
226230
current_file: c,
227231
}
228232
}
229233

230234
/// A context used during the compilation process
231-
struct PersistentContext {
235+
struct PersistentContext<'c> {
236+
config: &'c Config,
232237
/// Files that have not been compiled
233238
pending: BTreeMap<TypeKey, DsdlFile>,
234239
/// Files that have been compiled
@@ -237,7 +242,7 @@ struct PersistentContext {
237242
warnings: Warnings,
238243
}
239244

240-
impl PersistentContext {
245+
impl<'c> PersistentContext<'c> {
241246
fn compile(mut self) -> CompileOutput {
242247
while let Some(key) = self.pending.keys().next().cloned() {
243248
let input = self.pending.remove(&key).unwrap();
@@ -277,7 +282,7 @@ impl PersistentContext {
277282
let mut state = FileState::new(key.name().path());
278283

279284
let text = input.read()?;
280-
let ast = canadensis_dsdl_parser::parse(&text)?;
285+
let ast = canadensis_dsdl_parser::parse(&text, &self.config)?;
281286

282287
for statement in ast.statements {
283288
match statement {

canadensis_dsdl_frontend/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ mod type_key;
2929
pub mod types;
3030
pub mod warning;
3131

32+
pub use canadensis_dsdl_parser::Config;
33+
3234
pub use crate::error::Error;
3335
pub use crate::package::Package;
3436
pub use crate::type_key::{TypeFullName, TypeKey};

canadensis_dsdl_frontend/src/operators/attribute.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use canadensis_dsdl_parser::Span;
88

99
/// Evaluates the attribute operator `expr.attribute`
1010
pub(crate) fn evaluate(
11-
cx: &mut CompileContext<'_>,
11+
cx: &mut CompileContext<'_, '_>,
1212
lhs: Value,
1313
rhs: &str,
1414
span: Span<'_>,
@@ -87,7 +87,7 @@ fn make_set_min_max_gt_undefined_error(
8787
}
8888

8989
fn evaluate_type_attr(
90-
cx: &mut CompileContext<'_>,
90+
cx: &mut CompileContext<'_, '_>,
9191
ty: Type,
9292
rhs: &str,
9393
span: Span<'_>,

canadensis_dsdl_frontend/src/package.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::error::Error;
44
use crate::type_key::{TypeFullName, TypeKey};
55
use crate::types::keywords::{is_reserved_keyword, is_valid_identifier};
66
use crate::warning::Warnings;
7-
use canadensis_dsdl_parser::TypeVersion;
7+
use canadensis_dsdl_parser::{Config, TypeVersion};
88
use once_cell::sync::Lazy;
99
use regex::Regex;
1010
use std::collections::btree_map::Entry;
@@ -162,8 +162,9 @@ impl Package {
162162
/// invalid content, or an `@assert` directive fails.
163163
///
164164
/// If this function returns an error, it cannot return any warnings.
165-
pub fn compile(self) -> Result<CompiledPackage, Error> {
166-
self.compile_with_warnings().map_err(|(e, _warnings)| e)
165+
pub fn compile(self, config: &Config) -> Result<CompiledPackage, Error> {
166+
self.compile_with_warnings(config)
167+
.map_err(|(e, _warnings)| e)
167168
}
168169
/// Compiles all input files that were previously added
169170
///
@@ -174,8 +175,11 @@ impl Package {
174175
///
175176
/// If an error occurs, this function returns the error and any warnings reported before
176177
/// encountering the error.
177-
pub fn compile_with_warnings(self) -> Result<CompiledPackage, (Error, Warnings)> {
178-
match crate::compile::compile(self.files) {
178+
pub fn compile_with_warnings(
179+
self,
180+
config: &Config,
181+
) -> Result<CompiledPackage, (Error, Warnings)> {
182+
match crate::compile::compile(self.files, config) {
179183
CompileOutput {
180184
dsdl: Ok(types),
181185
warnings,

0 commit comments

Comments
 (0)