1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
/*
This file is part of Donet.
Copyright © 2024 Max Rodriguez
Donet is free software; you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License,
as published by the Free Software Foundation, either version 3
of the License, or (at your option) any later version.
Donet is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public
License along with Donet. If not, see <https://www.gnu.org/licenses/>.
*/
//! Enum and Struct definitions that are used to build the DC File [`AST`].
//! Used by [`crate::parser::parser`].
//!
//! [`AST`]: https://en.wikipedia.org/wiki/Abstract_syntax_tree
use super::lexer::{DCToken, Span};
use crate::dctype::DCTypeEnum;
/// Paired with the `type_declarations` production in the Context Free Grammar.
#[derive(Debug, Clone)]
pub struct Root {
pub type_declarations: Vec<TypeDeclaration>,
}
/// Paired with the `type_decl` production in the Context Free Grammar.
#[derive(Debug, Clone)]
pub enum TypeDeclaration {
// A single Python-style DC Import line can translate to
// multiple [`PythonImport`] structures per symbol imported.
PythonImport(PythonImport),
KeywordType(KeywordDefinition),
StructType(Struct),
DClassType(DClass),
TypedefType(TypeDefinition),
// Returned by productions that parsed certain grammar that may be
// deprecated but ignored for compatibility & should not be added
// to the output DC file element structure.
Ignore,
}
/// Paired with the `python_style_import` production in the Context Free Grammar.
#[derive(Debug, Clone)]
pub struct PythonImport {
pub span: Span,
pub module: SymbolWithViews,
pub class: SymbolWithViews,
}
/// Paired with the `py_module` and `dclass_import`
/// productions in the Context Free Grammar.
#[derive(Debug, Clone)]
pub struct SymbolWithViews {
pub span: Span,
pub symbol: String,
pub symbol_views: Vec<ViewSuffix>,
}
/// Paired with the `view_suffixes` production in the Context Free Grammar.
pub type ViewSuffixes = Vec<ViewSuffix>;
/// Paired with the `view_suffix` production in the Context Free Grammar.
#[derive(Debug, Clone)]
pub struct ViewSuffix {
pub span: Span,
pub view: String, // 'AI', 'OV', 'UD', etc.
}
/// Paired with the `type_definition` production in the Context Free Grammar.
#[derive(Debug, Clone)]
pub struct TypeDefinition {
pub span: Span,
/// Used if deprecated type aliases are found, such as `typedef uint8 bool;`
pub deprecated: bool,
pub data_type: NonMethodDataType,
pub array_range: Option<ArrayRange>,
pub alias_identifier: Option<String>,
}
/// Paired with the `keyword_type` production in the Context Free Grammar.
#[derive(Debug, Clone)]
pub struct KeywordDefinition {
pub span: Span,
pub identifier: String,
pub historical: bool,
}
/// Paired with the `distributed_class_type` production in the Context Free Grammar.
#[derive(Debug, Clone)]
pub struct DClass {
pub span: Span,
pub identifier: String,
pub parents: Vec<String>,
pub fields: ClassFields,
}
/// Paired with the `optional_class_fields` production in the Context Free Grammar.
pub type ClassFields = Vec<AtomicOrMolecular>;
/// Paired with the `class_field` production in the Context Free Grammar.
#[derive(Debug, Clone)]
pub enum AtomicOrMolecular {
Atomic(AtomicField),
Molecular(MolecularField),
}
/// The Atomic Field variant of the [`AtomicOrMolecular`] enum.
#[derive(Debug, Clone)]
pub struct AtomicField {
pub span: Span,
pub identifier: Option<String>,
pub keywords: Vec<String>,
pub parameters: MethodBody,
}
impl AtomicField {
pub fn from_named_field(field: NamedField, kw_list: KeywordList, span: Span) -> Self {
match field {
NamedField::ParameterField(pf) => Self {
span,
identifier: pf.parameter.identifier.clone(),
keywords: kw_list,
parameters: vec![pf.parameter],
},
NamedField::MethodAsField(mf) => Self {
span,
identifier: Some(mf.identifier),
keywords: kw_list,
parameters: mf.parameters,
},
}
}
}
/// Paired with the `molecular_field` production in the Context Free Grammar.
#[derive(Debug, Clone)]
pub struct MolecularField {
pub span: Span,
pub identifier: String,
pub atomic_field_identifiers: Vec<String>,
}
/// Paired with the `parameter_values` production in the Context Free Grammar.
pub type ParameterValues = Vec<TypeValue>;
/// Paired with the `struct_type` production in the Context Free Grammar.
#[derive(Debug, Clone)]
pub struct Struct {
pub span: Span,
pub identifier: String,
pub fields: Vec<StructField>,
}
/// Paired with the `struct_field` production in the Context Free Grammar.
#[derive(Debug, Clone)]
pub enum StructField {
ParameterField(ParameterField),
MethodAsField(MethodAsField),
Switch(Switch),
}
impl From<NamedField> for StructField {
fn from(value: NamedField) -> Self {
match value {
NamedField::ParameterField(pf) => Self::ParameterField(pf),
NamedField::MethodAsField(mf) => Self::MethodAsField(mf),
}
}
}
/// Paired with the `switch_type` production in the Context Free Grammar.
#[derive(Debug, Clone)]
pub struct Switch {
pub span: Span,
pub identifier: Option<String>,
pub key_parameter: ParameterField,
pub cases: Vec<Case>,
}
/// Paired with the `switch_case` production in the Context Free Grammar.
#[derive(Debug, Clone)]
pub struct Case {
pub span: Span,
// `None` condition means this is a default case.
pub condition: Option<TypeValue>,
pub fields: Vec<NamedField>,
pub breaks: bool, // if case ends with a break
}
/// Paired with the `named_field` production in the Context Free Grammar.
#[derive(Debug, Clone)]
pub enum NamedField {
ParameterField(ParameterField),
MethodAsField(MethodAsField),
}
/// Paired with the `method_as_field` production in the Context Free Grammar.
#[derive(Debug, Clone)]
pub struct MethodAsField {
pub span: Span,
pub identifier: String,
pub parameters: MethodBody,
}
/// Paired with the `method_body` production in the Context Free Grammar.
pub type MethodBody = Vec<Parameter>;
/// Paired with the `parameter_field` production in the Context Free Grammar.
#[derive(Debug, Clone)]
pub struct ParameterField {
pub parameter: Parameter,
pub keywords: KeywordList,
}
impl From<Parameter> for ParameterField {
fn from(value: Parameter) -> Self {
Self {
parameter: value,
keywords: vec![],
}
}
}
/// Paired with the `dc_keyword_list` production in the Context Free Grammar.
pub type KeywordList = Vec<String>;
/// Paired with the `parameter` production in the Context Free Grammar.
#[derive(Debug, Clone)]
pub struct Parameter {
pub span: Span,
pub data_type: NonMethodDataType,
pub identifier: Option<String>,
pub default_value: Option<TypeValue>,
}
impl From<NonMethodType> for Parameter {
fn from(value: NonMethodType) -> Self {
Self {
span: value.span,
data_type: value.data_type,
identifier: value.identifier,
default_value: None,
}
}
}
/// Paired with the `nonmethod_type` production in the Context Free Grammar.
#[derive(Debug, Clone)]
pub struct NonMethodType {
pub span: Span,
pub identifier: Option<String>,
pub data_type: NonMethodDataType,
}
#[derive(Debug, Clone)]
pub enum NonMethodDataType {
NumericType(NumericType),
StructType(String),
TypeWithArray(TypeWithArray),
}
/// Paired with the `type_with_array` production in the Context Free Grammar.
#[derive(Debug, Clone)]
pub struct TypeWithArray {
pub span: Span,
pub data_type: ArrayableType,
pub array_ranges: Vec<ArrayRange>,
}
#[derive(Debug, Clone)]
pub enum ArrayableType {
Numeric(NumericType),
Struct(String),
Sized(SizedTypeToken),
}
/// Paired with the `array_expansion` production in the Context Free Grammar.
pub type ArrayExpansion = (TypeValue, u32);
/// Paired with the `type_or_sized_value` production in the Context Free Grammar.
#[derive(Debug, Clone)]
pub enum TypeOrSizedValue {
Type(TypeValue),
Sized(DCToken),
}
/// Paired with the `type_value` production in the Context Free Grammar.
#[derive(Debug, Clone)]
pub enum TypeValue {
I64(i64),
Char(char),
String(String),
ArrayValue(Vec<ArrayExpansion>),
}
/// Paired with the `numeric_type` production in the Context Free Grammar.
#[derive(Debug, Clone)]
pub struct NumericType {
pub span: Span,
pub base_type: DCTypeEnum,
// Transforms
pub cast: Option<DataType>,
pub modulus: Option<f64>,
pub divisor: Option<f64>,
pub range: Option<NumericRange>,
}
impl NumericType {
pub fn from_type(value: DCTypeEnum, span: Span) -> Self {
Self {
span,
base_type: value,
cast: None,
modulus: None,
divisor: None,
range: None,
}
}
pub fn add_modulus(&mut self, value: Number) {
match value {
Number::Decimal(dl) => {
self.modulus = Some(dl as f64);
}
Number::Float(fl) => {
self.modulus = Some(fl);
}
}
}
pub fn add_divisor(&mut self, value: Number) {
match value {
Number::Decimal(dl) => {
self.divisor = Some(dl as f64);
}
Number::Float(fl) => {
self.divisor = Some(fl);
}
}
}
}
/// Paired with the `numeric_range` production in the Context Free Grammar.
pub type NumericRange = std::ops::Range<f64>;
/// Paired with the `array_range` production in the Context Free Grammar.
pub type ArrayRange = std::ops::Range<f64>;
/// Paired with the `sized_type_token` production in the Context Free Grammar.
#[derive(Debug, Clone)]
pub enum SizedTypeToken {
String,
Blob,
Blob32,
Int8Array,
Int16Array,
Int32Array,
UInt8Array,
UInt16Array,
UInt32Array,
UInt32UInt8Array,
}
/// Paired with the `char_or_number` production in the Context Free Grammar.
#[derive(Debug, Clone, Copy)]
pub enum CharOrNumber {
Char(char),
I64(i64),
F64(f64),
}
/// Paired with the 'number' production in the Context Free Grammar.
pub enum Number {
Decimal(i64),
Float(f64),
}
/// Paired with the `char_or_u16` production in the Context Free Grammar.
#[derive(Debug, Clone, Copy)]
pub enum CharOrU16 {
Char(char),
U16(u16),
}
#[derive(Debug, Clone)]
pub struct DataType {
pub span: Span,
pub token: DCToken,
pub dctype: DCTypeEnum,
}
impl DataType {
pub fn from_token(value: DCToken, span: Span) -> Self {
Self {
span,
token: value.clone(),
dctype: match value {
DCToken::Float32T => DCTypeEnum::TFloat32,
DCToken::Float64T => DCTypeEnum::TFloat64,
DCToken::Int8T => DCTypeEnum::TInt8,
DCToken::Int16T => DCTypeEnum::TInt16,
DCToken::Int32T => DCTypeEnum::TInt32,
DCToken::Int64T => DCTypeEnum::TInt64,
DCToken::UInt8T => DCTypeEnum::TUInt8,
DCToken::UInt16T => DCTypeEnum::TUInt16,
DCToken::UInt32T => DCTypeEnum::TUInt32,
DCToken::UInt64T => DCTypeEnum::TUInt64,
DCToken::Int8ArrayT => DCTypeEnum::TArray,
DCToken::Int16ArrayT => DCTypeEnum::TArray,
DCToken::Int32ArrayT => DCTypeEnum::TArray,
DCToken::UInt8ArrayT => DCTypeEnum::TArray,
DCToken::UInt16ArrayT => DCTypeEnum::TArray,
DCToken::UInt32ArrayT => DCTypeEnum::TArray,
DCToken::UInt32UInt8ArrayT => DCTypeEnum::TArray,
_ => panic!("DC token matches no production in CFG."),
},
}
}
}