Skip to content

Commit 9be8c26

Browse files
committed
formated files and resolved token links
1 parent e67c424 commit 9be8c26

File tree

8 files changed

+77
-89
lines changed

8 files changed

+77
-89
lines changed

nemo-language-server/src/language_server/lsp_component.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,10 @@ where
6666
{
6767
fn identifier(&self) -> Option<LSPIdentifier> {
6868
let scope = match self.context() {
69-
ParserContext::Number | ParserContext::EncodedNumber | ParserContext::Variable | ParserContext::RdfLiteral => {
70-
Some(ParserContext::Rule)
71-
}
69+
ParserContext::Number
70+
| ParserContext::EncodedNumber
71+
| ParserContext::Variable
72+
| ParserContext::RdfLiteral => Some(ParserContext::Rule),
7273
ParserContext::Iri
7374
| ParserContext::Constant
7475
| ParserContext::String

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

+4-5
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,14 @@ impl UnaryFunction for CastingIntoInteger64 {
3333
| crate::datavalues::ValueDomain::Iri
3434
| crate::datavalues::ValueDomain::LanguageTaggedString => None,
3535
crate::datavalues::ValueDomain::PlainString | crate::datavalues::ValueDomain::Other => {
36-
3736
let lex_val = parameter.lexical_value();
3837

3938
// Handle decimal, binary (0b), octal (0o) and hexadecimal (0x) encoded strings
4039
let result = match &lex_val[0..2] {
41-
"0b" => {<i64>::from_str_radix(&lex_val[2..],2).ok()?},
42-
"0o" => {<i64>::from_str_radix(&lex_val[2..],8).ok()?},
43-
"0x" => {<i64>::from_str_radix(&lex_val[2..],16).ok()?},
44-
_ => {lex_val.parse::<i64>().ok()?},
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()?,
4544
};
4645

4746
Some(AnyDataValue::new_integer_from_i64(result))

nemo/src/parser/ast/expression.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ pub mod basic;
44
pub mod complex;
55

66
use basic::{
7-
blank::Blank, boolean::Boolean, constant::Constant, number::Number, enc_number::EncodedNumber,rdf_literal::RdfLiteral, string::StringLiteral, variable::Variable
7+
blank::Blank, boolean::Boolean, constant::Constant, enc_number::EncodedNumber, number::Number,
8+
rdf_literal::RdfLiteral, string::StringLiteral, variable::Variable,
89
};
910
use complex::{
1011
aggregation::Aggregation, arithmetic::Arithmetic, atom::Atom, fstring::FormatString, map::Map,

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
pub mod blank;
44
pub mod boolean;
55
pub mod constant;
6+
pub mod enc_number;
67
pub mod iri;
78
pub mod number;
89
pub mod rdf_literal;
910
pub mod string;
1011
pub mod variable;
11-
pub mod enc_number;

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

+40-57
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22
#![allow(missing_docs)]
33

44
use enum_assoc::Assoc;
5-
use nom::{
6-
branch::alt,
7-
sequence::preceded,
8-
};
5+
use nom::{branch::alt, sequence::preceded};
96

107
use crate::parser::{
118
ast::{
@@ -16,7 +13,7 @@ use crate::parser::{
1613
span::Span,
1714
ParserInput, ParserResult,
1815
};
19-
use num::{BigInt,Num};
16+
use num::{BigInt, Num};
2017

2118
#[derive(Assoc, Debug, Clone, Copy, PartialEq, Eq)]
2219
#[func(pub fn token(token: &TokenKind) -> Option<Self>)]
@@ -44,7 +41,7 @@ impl Encoding {
4441
}
4542

4643
#[derive(Debug)]
47-
pub struct EncodedNumber<'a>{
44+
pub struct EncodedNumber<'a> {
4845
/// [Span] associated with this node
4946
span: Span<'a>,
5047
/// The prefix of the encoded number
@@ -53,7 +50,6 @@ pub struct EncodedNumber<'a>{
5350
suffix: Token<'a>,
5451
}
5552

56-
5753
/// Value of [EncodedNumber]
5854
#[derive(Debug)]
5955
pub enum NumberValue {
@@ -64,52 +60,42 @@ pub enum NumberValue {
6460
}
6561

6662
impl<'a> EncodedNumber<'a> {
67-
6863
/// Removes the binary prefix (0b) and returns the binary suffix
6964
fn parse_binary(input: ParserInput<'a>) -> ParserResult<'a, (Encoding, Token<'a>)> {
70-
preceded(Token::binary_prefix, Token::bin_number)(input).map(
71-
|(remaining, bin_digits)| {
72-
(remaining,(Encoding::Binary,bin_digits))
73-
})
65+
preceded(Token::binary_prefix, Token::bin_number)(input)
66+
.map(|(remaining, bin_digits)| (remaining, (Encoding::Binary, bin_digits)))
7467
}
7568

76-
7769
/// Removes the octal prefix (0o) and returns the octal suffix
7870
fn parse_octal(input: ParserInput<'a>) -> ParserResult<'a, (Encoding, Token<'a>)> {
79-
preceded(Token::octal_prefix, Token::oct_number)(input).map(
80-
|(remaining, oct_digits)| {
81-
(remaining,(Encoding::Octal,oct_digits))
82-
})
71+
preceded(Token::octal_prefix, Token::oct_number)(input)
72+
.map(|(remaining, oct_digits)| (remaining, (Encoding::Octal, oct_digits)))
8373
}
8474

8575
/// Removes the hex prefix (0x) and returns the hex suffix
8676
fn parse_hex(input: ParserInput<'a>) -> ParserResult<'a, (Encoding, Token<'a>)> {
87-
preceded(Token::hex_prefix, Token::hex_number)(input).map(
88-
|(remaining, hex_chars)| {
89-
(remaining,(Encoding::Hexadecimal,hex_chars))
90-
})
77+
preceded(Token::hex_prefix, Token::hex_number)(input)
78+
.map(|(remaining, hex_chars)| (remaining, (Encoding::Hexadecimal, hex_chars)))
9179
}
9280

93-
9481
/// Return the value of this number, represented as a [NumberValue].
9582
pub fn value(&self) -> NumberValue {
96-
let string = format!("{}{}",self.prefix.print(),self.suffix);
83+
let string = format!("{}{}", self.prefix.print(), self.suffix);
9784

9885
// Retrieves the base of encoded number based on [Encoding]
9986
let nr_encoding = self.prefix.radix();
10087
let span = &self.suffix.span();
10188
let suffix = span.fragment();
102-
89+
10390
// Returns decoded number as <i64> if it is not too big
104-
// Otherwise, return string representation of the decoded number
91+
// Otherwise, return string representation of the decoded number
10592
if let Ok(integer) = <i64>::from_str_radix(suffix, nr_encoding) {
10693
return NumberValue::Integer(integer);
107-
}else if let Ok(bigint) = <BigInt as Num>::from_str_radix(suffix, nr_encoding) {
94+
} else if let Ok(bigint) = <BigInt as Num>::from_str_radix(suffix, nr_encoding) {
10895
return NumberValue::Large(bigint.to_string());
10996
}
11097
NumberValue::Large(string)
11198
}
112-
11399
}
114100

115101
const CONTEXT: ParserContext = ParserContext::EncodedNumber;
@@ -131,26 +117,20 @@ impl<'a> ProgramAST<'a> for EncodedNumber<'a> {
131117

132118
context(
133119
CONTEXT,
134-
alt((
135-
Self::parse_binary,
136-
Self::parse_octal,
137-
Self::parse_hex,
138-
)),
120+
alt((Self::parse_binary, Self::parse_octal, Self::parse_hex)),
139121
)(input)
140-
.map(
141-
|(rest, (prefix,suffix))| {
142-
let rest_span = rest.span;
143-
144-
(
145-
rest,
146-
EncodedNumber {
147-
span: input_span.until_rest(&rest_span),
148-
prefix,
149-
suffix,
150-
},
151-
)
152-
},
153-
)
122+
.map(|(rest, (prefix, suffix))| {
123+
let rest_span = rest.span;
124+
125+
(
126+
rest,
127+
EncodedNumber {
128+
span: input_span.until_rest(&rest_span),
129+
prefix,
130+
suffix,
131+
},
132+
)
133+
})
154134
}
155135

156136
fn context(&self) -> ParserContext {
@@ -163,32 +143,35 @@ mod test {
163143
use nom::combinator::all_consuming;
164144

165145
use crate::parser::{
166-
ast::{expression::basic::enc_number::{EncodedNumber,NumberValue}, ProgramAST},
146+
ast::{
147+
expression::basic::enc_number::{EncodedNumber, NumberValue},
148+
ProgramAST,
149+
},
167150
ParserInput, ParserState,
168151
};
169152

170153
#[test]
171154
fn parse_numbers() {
172155
let valid_numbers = vec![
173-
("0x11",17),
174-
("0o11",9),
175-
("0b11",3),
176-
("0xAB2CE",701134),
177-
("0xab2ce",701134),
178-
("0o777",511),
179-
("0b01010101",85),
156+
("0x11", 17),
157+
("0o11", 9),
158+
("0b11", 3),
159+
("0xAB2CE", 701134),
160+
("0xab2ce", 701134),
161+
("0o777", 511),
162+
("0b01010101", 85),
180163
];
181164

182-
let invalid_numbers = vec!["0xG", "0o8", "0b2", "0x","0b", "0o"];
165+
let invalid_numbers = vec!["0xG", "0o8", "0b2", "0x", "0b", "0o"];
183166

184167
for (valid, exp_value) in valid_numbers {
185168
let input = ParserInput::new(valid, ParserState::default());
186169
let result = all_consuming(EncodedNumber::parse)(input);
187170
assert!(result.is_ok());
188171

189172
if let Ok((_, ast_node)) = result {
190-
if let NumberValue::Integer(integer) = ast_node.value(){
191-
assert_eq!(integer,exp_value);
173+
if let NumberValue::Integer(integer) = ast_node.value() {
174+
assert_eq!(integer, exp_value);
192175
}
193176
};
194177
}

nemo/src/parser/ast/token.rs

+20-17
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ use enum_assoc::Assoc;
77

88
use nom::{
99
branch::alt,
10-
bytes::complete::{is_a, is_not, tag, take_until,take_while1},
11-
character::complete::{alpha1, alphanumeric1, digit1, multispace1, space0, space1, oct_digit1, hex_digit1},
10+
bytes::complete::{is_a, is_not, tag, take_until, take_while1},
11+
character::complete::{
12+
alpha1, alphanumeric1, digit1, hex_digit1, multispace1, oct_digit1, space0, space1,
13+
},
1214
combinator::{map, opt, recognize, verify},
1315
multi::many0,
1416
sequence::pair,
@@ -331,7 +333,6 @@ macro_rules! string_token {
331333
};
332334
}
333335

334-
335336
impl<'a> Token<'a> {
336337
/// Return the [Span] of this token.
337338
pub fn span(&self) -> Span<'a> {
@@ -500,22 +501,24 @@ impl<'a> Token<'a> {
500501
}
501502

502503
// The built-in bin_digit1 can be used, once nom is updated to version 8.0.0-alpha2
503-
/// Parse [TokenKind:BinaryPrefix]
504+
/// Parse binary number consisting of [TokenKind::BinaryPrefix] and [TokenKind::BinarySuffix]
504505
pub fn bin_number(input: ParserInput<'a>) -> ParserResult<'a, Token<'a>> {
505-
context(ParserContext::token(TokenKind::BinarySuffix), take_while1(|c| c == '0' || c == '1'))(input).map(
506-
|(rest_input, result)| {
507-
(
508-
rest_input,
509-
Token {
510-
span: result.span,
511-
kind: TokenKind::BinarySuffix,
512-
},
513-
)
514-
},
515-
)
506+
context(
507+
ParserContext::token(TokenKind::BinarySuffix),
508+
take_while1(|c| c == '0' || c == '1'),
509+
)(input)
510+
.map(|(rest_input, result)| {
511+
(
512+
rest_input,
513+
Token {
514+
span: result.span,
515+
kind: TokenKind::BinarySuffix,
516+
},
517+
)
518+
})
516519
}
517520

518-
/// Parse [TokenKind:OctalPrefix].
521+
/// Parse binary number consisting of [TokenKind::OctalPrefix] and [TokenKind::OctalSuffix]
519522
pub fn oct_number(input: ParserInput<'a>) -> ParserResult<'a, Token<'a>> {
520523
context(ParserContext::token(TokenKind::OctalSuffix), oct_digit1)(input).map(
521524
|(rest_input, result)| {
@@ -530,7 +533,7 @@ impl<'a> Token<'a> {
530533
)
531534
}
532535

533-
/// Parse [TokenKindHexNumber].
536+
/// Parse binary number consisting of [TokenKind::HexPrefix] and [TokenKind::HexSuffix]
534537
pub fn hex_number(input: ParserInput<'a>) -> ParserResult<'a, Token<'a>> {
535538
context(ParserContext::token(TokenKind::HexSuffix), hex_digit1)(input).map(
536539
|(rest_input, result)| {

nemo/src/rule_model/translation/basic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
pub(super) mod blank;
44
pub(super) mod boolean;
55
pub(super) mod constant;
6-
pub(super) mod number;
76
pub(super) mod enc_number;
7+
pub(super) mod number;
88
pub(super) mod rdf;
99
pub(super) mod string;
1010
pub(super) mod variable;

nemo/src/rule_model/translation/term.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ use crate::{
1515
use super::{
1616
basic::{
1717
blank::BlankLiteral, boolean::BooleanLiteral, constant::ConstantLiteral,
18-
number::NumberLiteral,enc_number::EncodedNumberLiteral, rdf::RdfLiteral, string::StringLiteral,
18+
enc_number::EncodedNumberLiteral, number::NumberLiteral, rdf::RdfLiteral,
19+
string::StringLiteral,
1920
},
2021
complex::{
2122
arithmetic::ArithmeticOperation, fstring::FormatStringLiteral,
@@ -50,9 +51,9 @@ impl TranslationComponent for Term {
5051
ast::expression::Expression::Number(number) => {
5152
Term::from(NumberLiteral::build_component(translation, number)?.into_inner())
5253
}
53-
ast::expression::Expression::EncodedNumber(enc_number) => {
54-
Term::from(EncodedNumberLiteral::build_component(translation, enc_number)?.into_inner())
55-
}
54+
ast::expression::Expression::EncodedNumber(enc_number) => Term::from(
55+
EncodedNumberLiteral::build_component(translation, enc_number)?.into_inner(),
56+
),
5657
ast::expression::Expression::RdfLiteral(rdf_literal) => {
5758
Term::from(RdfLiteral::build_component(translation, rdf_literal)?.into_inner())
5859
}

0 commit comments

Comments
 (0)