Skip to content

Commit 6f93c6e

Browse files
committed
Simplify parsing primary int, float, inf
1 parent deff071 commit 6f93c6e

File tree

2 files changed

+23
-55
lines changed

2 files changed

+23
-55
lines changed

crates/gitql-ast/src/expression.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,20 @@ pub struct NumberExpr {
191191
pub value: Number,
192192
}
193193

194+
impl NumberExpr {
195+
pub fn int(int_value: i64) -> Self {
196+
NumberExpr {
197+
value: Number::Int(int_value),
198+
}
199+
}
200+
201+
pub fn float(int_value: f64) -> Self {
202+
NumberExpr {
203+
value: Number::Float(int_value),
204+
}
205+
}
206+
}
207+
194208
impl Expr for NumberExpr {
195209
fn kind(&self) -> ExprKind {
196210
ExprKind::Number

crates/gitql-parser/src/parser.rs

Lines changed: 9 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use core::f64;
12
use std::collections::HashMap;
23
use std::vec;
34

@@ -3260,8 +3261,8 @@ fn parse_primary_expression(
32603261
}
32613262

32623263
match &tokens[*position].kind {
3263-
TokenKind::Integer(_) => parse_const_integer_expression(tokens, position),
3264-
TokenKind::Float(_) => parse_const_float_expression(tokens, position),
3264+
TokenKind::Integer(value) => Ok(Box::new(NumberExpr::int(*value))),
3265+
TokenKind::Float(value) => Ok(Box::new(NumberExpr::float(*value))),
32653266
TokenKind::Infinity => parse_float_infinity_or_nan_expression(tokens, position),
32663267
TokenKind::NaN => parse_float_infinity_or_nan_expression(tokens, position),
32673268
TokenKind::Symbol(_) => parse_symbol_expression(context, env, tokens, position),
@@ -3297,64 +3298,17 @@ fn parse_primary_expression(
32973298
}
32983299
}
32993300

3300-
fn parse_const_integer_expression(
3301-
tokens: &[Token],
3302-
position: &mut usize,
3303-
) -> Result<Box<dyn Expr>, Box<Diagnostic>> {
3304-
match tokens[*position].kind {
3305-
TokenKind::Integer(integer) => {
3306-
*position += 1;
3307-
let value = Number::Int(integer);
3308-
Ok(Box::new(NumberExpr { value }))
3309-
}
3310-
_ => Err(Diagnostic::error("Too big Integer value")
3311-
.add_help("Try to use smaller value")
3312-
.add_note(&format!(
3313-
"Integer value must be between {} and {}",
3314-
i64::MIN,
3315-
i64::MAX
3316-
))
3317-
.with_location(tokens[*position].location)
3318-
.as_boxed()),
3319-
}
3320-
}
3321-
3322-
fn parse_const_float_expression(
3323-
tokens: &[Token],
3324-
position: &mut usize,
3325-
) -> Result<Box<dyn Expr>, Box<Diagnostic>> {
3326-
match tokens[*position].kind {
3327-
TokenKind::Float(float) => {
3328-
*position += 1;
3329-
let value = Number::Float(float);
3330-
Ok(Box::new(NumberExpr { value }))
3331-
}
3332-
_ => Err(Diagnostic::error("Too big Float value")
3333-
.add_help("Try to use smaller value")
3334-
.add_note(&format!(
3335-
"Float value must be between {} and {}",
3336-
f64::MIN,
3337-
f64::MAX
3338-
))
3339-
.with_location(tokens[*position].location)
3340-
.as_boxed()),
3341-
}
3342-
}
3343-
33443301
fn parse_float_infinity_or_nan_expression(
33453302
tokens: &[Token],
33463303
position: &mut usize,
33473304
) -> Result<Box<dyn Expr>, Box<Diagnostic>> {
3348-
if tokens[*position].kind == TokenKind::Infinity {
3349-
*position += 1;
3350-
let value = Number::Float(f64::INFINITY);
3351-
return Ok(Box::new(NumberExpr { value }));
3352-
}
3353-
33543305
*position += 1;
3355-
3356-
let value = Number::Float(f64::NAN);
3357-
Ok(Box::new(NumberExpr { value }))
3306+
let value = if tokens[*position].kind == TokenKind::Infinity {
3307+
f64::INFINITY
3308+
} else {
3309+
f64::NAN
3310+
};
3311+
Ok(Box::new(NumberExpr::float(value)))
33583312
}
33593313

33603314
fn parse_symbol_expression(

0 commit comments

Comments
 (0)