Skip to content

Commit

Permalink
chore: Misc parser fixes and work on propogating values
Browse files Browse the repository at this point in the history
  • Loading branch information
maxrdz committed Mar 6, 2024
1 parent 6a36c75 commit ec9b675
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 26 deletions.
9 changes: 9 additions & 0 deletions libdonet/src/dckeyword.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,24 @@ impl DCKeywordInterface for DCKeyword {
}
}

/// Accumulates the properties of this DC element into the file hash.
fn generate_hash(&self, hashgen: &mut DCHashGenerator) {
hashgen.add_string(self.name.clone());
}

#[inline]
fn get_name(&self) -> String {
self.name.clone()
}

#[inline]
fn get_historical_flag(&self) -> HistoricalFlag {
self.historical_flag.clone()
}

/// Sets the historical flag bitmask to the bitwise complement of 0
/// (!0 in Rust, or ~0 in C/C++), as if the keyword were not one
/// of the historically defined keywords.
fn clear_historical_flag(&mut self) {
self.historical_flag = !0;
}
Expand Down Expand Up @@ -119,6 +125,9 @@ impl Default for DCKeywordList {
Self {
keywords: vec![],
kw_name_2_keyword: MultiMap::new(),
// Panda initializes its keyword list class with the flags bitmask
// set to 0 as a regular int (signed). But, it still confuses me why
// since a clear bitmask (no historical kw flags) is the bitwise complement of 0.
flags: 0_i32,
}
}
Expand Down
76 changes: 50 additions & 26 deletions libdonet/src/dcparser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ enum TypeDeclaration {
TypedefType(DCTypeDefinition),
}

/// Paired with the `type_value` production.
enum TypeValue {
I64(i64),
Char(char),
String(String),
ArrayValue(Vec<(TypeValue, u32)>),
}

/// Paired with the `char_or_u16` production.
#[derive(Clone, Copy)]
enum CharOrU16 {
Expand Down Expand Up @@ -302,12 +310,14 @@ parser! {
keyword_type: dckeyword::DCKeyword {
Keyword Identifier(id) => {
use dckeyword::DCKeywordInterface;

// TODO: register keyword identifier in DC file
dckeyword::DCKeyword::new(id, None)
},
Keyword DCKeyword(historic) => {
// This is already a legacy keyword.
use dckeyword::DCKeywordInterface;

dckeyword::DCKeyword::new(historic, None)
}
}
Expand Down Expand Up @@ -393,8 +403,9 @@ parser! {
type_definition: () {
Typedef nonmethod_type_with_name => {},
// This rule handles a specific piece of illegal grammar that is legal in Panda.
// The parser will panic with a useful message describing the issue.
Typedef UInt8T BoolT => panic!("{:?}\n\n\"typedef uint8 bool;\" is deprecated!\n\n\
// The parser will print a useful message to stdout describing the issue,
// and will ignore this grammar and continue without a panic.
Typedef UInt8T BoolT => println!("{:?}\n\n\"typedef uint8 bool;\" is deprecated!\n\n\
Cannot declare type alias for uint8 as 'bool', as it is a reserved identifier \
in the DC language.\nDonet introduces the 'bool' data type, which is an alias \
for uint8 under the hood.\n", span!()),
Expand Down Expand Up @@ -550,22 +561,26 @@ parser! {
sized_type_token[_] OpenParenthesis array_range CloseParenthesis => {},
}

array_expansion: () {
type_value => {},
signed_integer[_] Star unsigned_32_bit_int[_] => {},
DecimalLiteral(_) Star unsigned_32_bit_int[_] => {},
HexLiteral(_) Star unsigned_32_bit_int[_] => {},
StringT Star unsigned_32_bit_int[_] => {},
// e.g. "blob = [0 * 14]"
array_expansion: (TypeValue, u32) {
type_value[tv] => (tv, 1_u32), // factor of 1 by default
signed_integer[i] Star unsigned_32_bit_int[f] => (TypeValue::I64(i), f),
DecimalLiteral(i) Star unsigned_32_bit_int[f] => (TypeValue::I64(i), f),
HexLiteral(hs) Star unsigned_32_bit_int[f] => (TypeValue::String(hs), f),
StringLiteral(s) Star unsigned_32_bit_int[f] => (TypeValue::String(s), f),
}

element_values: () {
array_expansion => {},
element_values Comma array_expansion => {},
element_values: Vec<(TypeValue, u32)> {
array_expansion[ae] => vec![ae],
element_values[mut ev] Comma array_expansion[ae] => {
ev.push(ae);
ev
},
}

array_value: () {
OpenBrackets CloseBrackets => {},
OpenBrackets element_values CloseBrackets => {},
array_value: Vec<(TypeValue, u32)> {
OpenBrackets CloseBrackets => vec![],
OpenBrackets element_values[ev] CloseBrackets => ev,
}

struct_value: () {
Expand All @@ -589,14 +604,14 @@ parser! {
sized_type_token[_] => {},
}

type_value: () {
DecimalLiteral(_) => {},
CharacterLiteral(_) => {},
StringLiteral(_) => {},
HexLiteral(_) => {},
signed_integer[_] => {},
array_value => {},
struct_value => {},
type_value: TypeValue {
DecimalLiteral(i) => TypeValue::I64(i),
CharacterLiteral(c) => TypeValue::Char(c),
StringLiteral(s) => TypeValue::String(s),
HexLiteral(hs) => TypeValue::String(hs),
signed_integer[i] => TypeValue::I64(i),
array_value[av] => TypeValue::ArrayValue(av),
struct_value[_] => todo!(),
}

numeric_type: DCNumericType {
Expand All @@ -607,13 +622,15 @@ parser! {
numeric_with_range[nt] => nt,
}

// TODO!
numeric_with_range: DCNumericType {
numeric_type_token[nt] OpenParenthesis numeric_range[_] CloseParenthesis => nt,
numeric_with_explicit_cast[nt] OpenParenthesis numeric_range[_] CloseParenthesis => nt,
numeric_with_modulus[nt] OpenParenthesis numeric_range[_] CloseParenthesis => nt,
numeric_with_divisor[nt] OpenParenthesis numeric_range[_] CloseParenthesis => nt,
}

// TODO!
numeric_with_divisor: DCNumericType {
numeric_type_token[nt] ForwardSlash number[_] => nt,
numeric_with_explicit_cast[nt] ForwardSlash number[_] => nt,
Expand Down Expand Up @@ -674,17 +691,23 @@ parser! {
// Also because it is 2:27 AM and its giving me a shift-reduce conflict again.
numeric_type_token[mut nt]
OpenParenthesis signed_integer_type[(_, dct)] CloseParenthesis => {
let _ = nt.set_explicit_cast(DCTypeDefinition::new_with_type(dct));
if let Err(msg) = nt.set_explicit_cast(DCTypeDefinition::new_with_type(dct)) {
panic!("{:?}\n{}", span!(), msg);
}
nt
},
numeric_type_token[mut nt]
OpenParenthesis unsigned_integer_type[(_, dct)] CloseParenthesis => {
let _ = nt.set_explicit_cast(DCTypeDefinition::new_with_type(dct));
if let Err(msg) = nt.set_explicit_cast(DCTypeDefinition::new_with_type(dct)) {
panic!("{:?}\n{}", span!(), msg);
}
nt
},
numeric_type_token[mut nt]
OpenParenthesis floating_point_type[(_, dct)] CloseParenthesis => {
let _ = nt.set_explicit_cast(DCTypeDefinition::new_with_type(dct));
if let Err(msg) = nt.set_explicit_cast(DCTypeDefinition::new_with_type(dct)) {
panic!("{:?}\n{}", span!(), msg);
}
nt
},
}
Expand All @@ -704,7 +727,8 @@ parser! {
char_or_number[min] Hyphen char_or_number[max] => {
assert!(
discriminant(&min) == discriminant(&max),
"Cannot define a numeric range with a min and max of different data types!",
"{:?}\nCannot define a numeric range with a min and max of different data types!",
span!()
);

match min {
Expand Down

0 comments on commit ec9b675

Please sign in to comment.