Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:Tablam/TablaM into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
mamcx committed Aug 30, 2020
2 parents 10355d0 + 8449349 commit 32f1354
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 7 deletions.
30 changes: 28 additions & 2 deletions lang/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ impl Expression {
}
}

pub fn as_bool_condition_qry(operator: Token, left: Self, right: Self) -> Self {
pub fn create_bool_condition_qry(operator: Token, left: Self, right: Self) -> Self {
let lhs: Comparable = match left {
Expression::Column(col) => match col {
Column::Pos(position) => position.into(),
Expand Down Expand Up @@ -187,6 +187,11 @@ impl QueryOperation {
self
}

pub fn deselect(mut self, columns: Vec<Column>) -> Self {
self.query = self.query.deselect(&columns);
self
}

pub fn filter(mut self, operator: &Token, left: Comparable, right: Comparable) -> Self {
self.query = match operator {
Token::Equal => self.query.eq(left, right),
Expand All @@ -195,9 +200,30 @@ impl QueryOperation {
Token::GreaterEqual => self.query.greater_eq(left, right),
Token::Less => self.query.less(left, right),
Token::LessEqual => self.query.less_eq(left, right),
_ => unreachable!("Ilegal operator in filter condition."),
_ => unreachable!("Illegal operator in filter condition."),
};

self
}

pub fn skip(mut self, token: &Token) -> Self {
self.query = match token {
Token::Integer(offset) => self.query.skip(*offset as usize),
_ => unreachable!("Illegal operator in filter condition."),
};
self
}

pub fn limit(mut self, token: &Token) -> Self {
self.query = match token {
Token::Integer(offset) => self.query.limit(*offset as usize),
_ => unreachable!("Illegal operator in filter condition."),
};
self
}

pub fn distinct(mut self) -> Self {
self.query = self.query.distinct();
self
}
}
Expand Down
10 changes: 10 additions & 0 deletions lang/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,16 @@ pub enum Token {
#[token("return")]
Return,

#[regex(r"---(?s)[.[^-]]*---\n*", |lex| {
increase_current_line(lex);
logos::Skip
})]
#[regex(r"--.*\n", |lex| {
increase_current_line(lex);
logos::Skip
})]
Commentary,

#[token("\n", increase_current_line)]
#[regex(r"[ \t\f]+", logos::skip)]
#[error]
Expand Down
62 changes: 60 additions & 2 deletions lang/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ impl<'source> Parser<'source> {
| Some(Token::Where)
| Some(Token::Limit)
| Some(Token::Skip)
| Some(Token::Distinct) => self.parse_query(name.into())?,
| Some(Token::Distinct)
| Some(Token::Deselect) => self.parse_query(name.into())?,
_ => Expression::Variable(name.into()),
}
}
Expand Down Expand Up @@ -224,7 +225,7 @@ impl<'source> Parser<'source> {
}

if lhs.is_indexed_column() || rhs.is_indexed_column() {
lhs = Expression::as_bool_condition_qry(token, lhs, rhs);
lhs = Expression::create_bool_condition_qry(token, lhs, rhs);
continue;
}

Expand Down Expand Up @@ -370,17 +371,49 @@ impl<'source> Parser<'source> {
self.accept();
self.parse_select_qry(operations)?
}
Some(Token::Deselect) => {
self.accept();
self.parse_deselect_qry(operations)?
}
Some(Token::Where) => {
self.accept();
self.parse_where_qry(operations)?
}
Some(Token::Limit) => {
self.accept();
self.parse_limit_qry(operations)?
}
Some(Token::Skip) => {
self.accept();
self.parse_skip_qry(operations)?
}
Some(Token::Distinct) => {
self.accept();
self.parse_distinct_qry(operations)?
}
_ => break,
}
}

Ok(Expression::QueryOperation(operations))
}

fn parse_distinct_qry(&mut self, operations: QueryOperation) -> ReturnT<QueryOperation> {
Ok(operations.distinct())
}

fn parse_skip_qry(&mut self, operations: QueryOperation) -> ReturnT<QueryOperation> {
let offset = self.check_next_token(Token::Integer(164))?;

Ok(operations.skip(&offset))
}

fn parse_limit_qry(&mut self, operations: QueryOperation) -> ReturnT<QueryOperation> {
let offset = self.check_next_token(Token::Integer(164))?;

Ok(operations.limit(&offset))
}

fn parse_where_qry(&mut self, operations: QueryOperation) -> ReturnT<QueryOperation> {
let expression = self.parse_ast(0)?;
let (operator, left, right) = match expression {
Expand All @@ -395,6 +428,31 @@ impl<'source> Parser<'source> {
Ok(operations.filter(&operator, left, right))
}

fn parse_deselect_qry(&mut self, operations: QueryOperation) -> ReturnT<QueryOperation> {
let mut columns = Vec::<Column>::new();
loop {
let column = self.parse_ast(0)?;

match column {
Expression::Column(column) => columns.push(column),
_ => return Err(ErrorLang::UnexpectedItem(column)),
};

if self.check_next_token(Token::Separator).is_ok() {
continue;
}
break;
}

if columns.is_empty() {
return Err(ErrorLang::Query(String::from(
"You must indicate at least one column.",
)));
}

Ok(operations.deselect(columns))
}

fn parse_select_qry(&mut self, operations: QueryOperation) -> ReturnT<QueryOperation> {
let mut columns = Vec::<Column>::new();
loop {
Expand Down
50 changes: 47 additions & 3 deletions lang/tests/src/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,41 @@ fn test_syntax_v0() {
(Token::Integer(1i64), "1", 11..12),
],
);

assert_lex(
"let sum := 1 + 1 -- sum = 2
-- test one line
var c := 1 + 2
--- header ---
collection ?where #name > 1
--- multiline
commentaries
example
---",
&[
(Token::Let, "let", 0..3),
(Token::Variable(String::from("sum")), "sum", 4..7),
(Token::Assignment, ":=", 8..10),
(Token::Integer(1i64), "1", 11..12),
(Token::Plus, "+", 13..14),
(Token::Integer(1i64), "1", 15..16),
(Token::Var, "var", 54..57),
(Token::Variable(String::from("c")), "c", 58..59),
(Token::Assignment, ":=", 60..62),
(Token::Integer(1i64), "1", 63..64),
(Token::Plus, "+", 65..66),
(Token::Integer(2i64), "2", 67..68),
(
Token::Variable(String::from("collection")),
"collection",
92..102,
),
(Token::Where, "?where", 103..109),
(Token::Column(String::from("name")), "#name", 110..115),
(Token::Greater, ">", 116..117),
(Token::Integer(1i64), "1", 118..119),
],
);
}

#[test]
Expand Down Expand Up @@ -197,10 +232,12 @@ fn test_syntax_query() {
],
);

/*let result: Vec<_> = Token::lexer("let numbers := [name:Int; 1; 2; 3; 4]")
/*
let result: Vec<_> = Token::lexer("complex ?deselect #img ?skip 3 ?limit 6 ?distinct")
.spanned()
.collect();
dbg!(result);*/
dbg!(result);
*/
}

#[test]
Expand Down Expand Up @@ -399,12 +436,19 @@ fn test_parser() {
let input = "complex ?select #img, #real as #r ?where #1 > 20";
let mut parser = Parser::new(input);
let result = parser.parse();
println!("{}", input);
assert_eq!(
result.expect("not getting expression").to_string(),
String::from("complex ?select #img, #real as #r ?where #1 > 20")
);

let input = "complex ?deselect #img ?skip 3 ?limit 6 ?distinct";
let mut parser = Parser::new(input);
let result = parser.parse();
assert_eq!(
result.expect("not getting expression").to_string(),
String::from("complex ?deselect #img ?skip 3 ?limit 6 ?distinct")
);

/*let input = "let n := [9; 8; 10]";
let mut parser = Parser::new(input);
let result = parser.parse();
Expand Down

0 comments on commit 32f1354

Please sign in to comment.