Skip to content

Commit 9b96870

Browse files
committed
Renamed NumberValue in EncodedNumberValue and fixed requested changes
1 parent 4dd546d commit 9b96870

File tree

6 files changed

+39
-22
lines changed

6 files changed

+39
-22
lines changed

nemo-physical/src/function/definitions/casting.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,18 @@ impl UnaryFunction for CastingIntoInteger64 {
3636
let lex_val = parameter.lexical_value();
3737

3838
// Handle decimal, binary (0b), octal (0o) and hexadecimal (0x) encoded strings
39-
let result = match &lex_val[0..2] {
40-
"0b" => <i64>::from_str_radix(&lex_val[2..], 2).ok()?,
41-
"0o" => <i64>::from_str_radix(&lex_val[2..], 8).ok()?,
42-
"0x" => <i64>::from_str_radix(&lex_val[2..], 16).ok()?,
43-
_ => lex_val.parse::<i64>().ok()?,
39+
// Expect 2 chars for encoding prefix and at least one char as suffix
40+
let result = {
41+
if lex_val.chars().count() >= 3 {
42+
match &lex_val[0..2] {
43+
"0b" => <i64>::from_str_radix(&lex_val[2..], 2).ok()?,
44+
"0o" => <i64>::from_str_radix(&lex_val[2..], 8).ok()?,
45+
"0x" => <i64>::from_str_radix(&lex_val[2..], 16).ok()?,
46+
_ => lex_val.parse::<i64>().ok()?,
47+
}
48+
} else {
49+
lex_val.parse::<i64>().ok()?
50+
}
4451
};
4552

4653
Some(AnyDataValue::new_integer_from_i64(result))

nemo/src/parser/ast/expression/basic/enc_number.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::parser::{
1313
span::Span,
1414
ParserInput, ParserResult,
1515
};
16-
use num::{BigInt, Num};
16+
use num::{BigInt,Num};
1717

1818
/// Define a different type for each prefix token
1919
#[derive(Assoc, Debug, Clone, Copy, PartialEq, Eq)]
@@ -32,7 +32,6 @@ enum Encoding {
3232
}
3333

3434
impl Encoding {
35-
3635
/// Returns the base of each encoding type
3736
pub fn radix(&self) -> u32 {
3837
match self {
@@ -48,6 +47,7 @@ impl Encoding {
4847
pub struct EncodedNumber<'a> {
4948
/// [Span] associated with this node
5049
span: Span<'a>,
50+
5151
/// The prefix of the encoded number
5252
prefix: Encoding,
5353
/// The suffix of the encoded number
@@ -64,7 +64,6 @@ pub enum EncodedNumberValue {
6464
}
6565

6666
impl<'a> EncodedNumber<'a> {
67-
6867
/// Removes the binary prefix (0b) and returns the binary suffix
6968
fn parse_binary(input: ParserInput<'a>) -> ParserResult<'a, (Encoding, Token<'a>)> {
7069
preceded(Token::binary_prefix, Token::bin_number)(input)
@@ -96,7 +95,7 @@ impl<'a> EncodedNumber<'a> {
9695
// Otherwise, return string representation of the decoded number
9796
if let Ok(integer) = <i64>::from_str_radix(suffix, nr_encoding) {
9897
return EncodedNumberValue::Integer(integer);
99-
} else if let Ok(bigint) = <BigInt as Num>::from_str_radix(suffix, nr_encoding) {
98+
} else if let Ok(bigint) = BigInt::from_str_radix(suffix, nr_encoding) {
10099
return EncodedNumberValue::Large(bigint.to_string());
101100
}
102101
EncodedNumberValue::Large(string)

nemo/src/parser/ast/expression/basic/number.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ impl<'a> Number<'a> {
136136
if let Ok(float) = str::parse::<f32>(&string) {
137137
return NumberValue::Float(float);
138138
}
139-
139+
140140
NumberValue::Large(string)
141141
}
142142

nemo/src/parser/ast/token.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ use crate::{
2828
syntax::{
2929
self, comment,
3030
datavalues::{self, boolean, iri, map, string, tuple, RDF_DATATYPE_INDICATOR},
31-
directive,
31+
directive, encoding_prefixes,
3232
expression::{aggregate, atom, format_string, operation, variable},
33-
operator, rule,
33+
operator, rule,
3434
},
3535
};
3636

@@ -185,21 +185,21 @@ pub enum TokenKind {
185185
/// Digits
186186
#[assoc(name = "digits")]
187187
Digits,
188-
/// Binary prefix
189-
#[assoc(name = "0b")]
188+
/// Token preceding a binary encoded number as defined in [BIN](encoding_prefixes::BIN)
189+
#[assoc(name = encoding_prefixes::BIN)]
190190
BinaryPrefix,
191+
/// Token preceding an octal encoded number as defined in [OCT](encoding_prefixes::OCT)
192+
#[assoc(name = encoding_prefixes::OCT)]
193+
OctalPrefix,
194+
/// Token preceding a hexadecimal encoded number as defined in [HEX](encoding_prefixes::HEX)
195+
#[assoc(name = encoding_prefixes::HEX)]
196+
HexPrefix,
191197
/// Binary suffix
192198
#[assoc(name = "bin_suffix")]
193199
BinarySuffix,
194-
/// Octal prefix
195-
#[assoc(name = "0o")]
196-
OctalPrefix,
197200
/// Octal suffix
198201
#[assoc(name = "oct_suffix")]
199202
OctalSuffix,
200-
/// Hexadecimal prefix
201-
#[assoc(name = "0x")]
202-
HexPrefix,
203203
/// Hexadecimal suffix
204204
#[assoc(name = "hex_suffix")]
205205
HexSuffix,

nemo/src/rule_model/translation/basic/enc_number.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ impl TranslationComponent for EncodedNumberLiteral {
2020
enc_number: &'b Self::Ast<'a>,
2121
) -> Result<Self, TranslationError> {
2222
Ok(EncodedNumberLiteral(match enc_number.value() {
23-
ast::expression::basic::enc_number::NumberValue::Integer(integer) => {
23+
ast::expression::basic::enc_number::EncodedNumberValue::Integer(integer) => {
2424
AnyDataValue::new_integer_from_i64(integer)
2525
}
26-
ast::expression::basic::enc_number::NumberValue::Large(large) => {
26+
ast::expression::basic::enc_number::EncodedNumberValue::Large(large) => {
2727
AnyDataValue::new_other(large, String::from("xsd:integer"))
2828
}
2929
}))

nemo/src/syntax.rs

+11
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,17 @@ pub mod datavalues {
366366
pub const DOT: &str = ".";
367367
}
368368

369+
pub mod encoding_prefixes {
370+
//! This module defines the prefixes for encoded numbers
371+
372+
/// Reprsents the prefix for binary-encoded unsigned integers
373+
pub const BIN: &str = "0b";
374+
/// Reprsents the prefix for octal-encoded unsigned integers
375+
pub const OCT: &str = "0o";
376+
/// Reprsents the prefix for hex-encoded unsigned integers
377+
pub const HEX: &str = "0x";
378+
}
379+
369380
pub mod import_export {
370381
//! This module defines the import/export configuration options.
371382

0 commit comments

Comments
 (0)