Skip to content

Commit a5e93ba

Browse files
committed
feat: Support consuming ; at the end of select or set statement
1 parent 01bf1f5 commit a5e93ba

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

crates/gitql-parser/src/parser.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ use gitql_ast::statement::*;
2121
use gitql_ast::types::DataType;
2222
use gitql_ast::types::TABLES_FIELDS_TYPES;
2323

24-
pub fn parse_gql(tokens: Vec<Token>, env: &mut Enviroment) -> Result<Query, GQLError> {
24+
pub fn parse_gql(mut tokens: Vec<Token>, env: &mut Enviroment) -> Result<Query, GQLError> {
25+
consume_optional_semicolon_if_exists(&mut tokens);
26+
2527
let mut position = 0;
2628
let first_token = &tokens[position];
2729
match &first_token.kind {
@@ -1866,6 +1868,19 @@ fn un_expected_expression_error(tokens: &Vec<Token>, position: &usize) -> GQLErr
18661868
}
18671869
}
18681870

1871+
/// Remove last token if it semicolon, because it's optional
1872+
fn consume_optional_semicolon_if_exists(tokens: &mut Vec<Token>) {
1873+
if tokens.is_empty() {
1874+
return;
1875+
}
1876+
1877+
if let Some(last_token) = tokens.last() {
1878+
if last_token.kind == TokenKind::Semicolon {
1879+
tokens.remove(tokens.len() - 1);
1880+
}
1881+
}
1882+
}
1883+
18691884
#[allow(clippy::borrowed_box)]
18701885
fn get_expression_name(expression: &Box<dyn Expression>) -> Result<String, ()> {
18711886
if let Some(symbol) = expression.as_any().downcast_ref::<SymbolExpression>() {

crates/gitql-parser/src/tokenizer.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ pub enum TokenKind {
6767

6868
Comma,
6969
Dot,
70+
Semicolon,
7071

7172
Ascending,
7273
Descending,
@@ -537,6 +538,24 @@ pub fn tokenize(script: String) -> Result<Vec<Token>, GQLError> {
537538
continue;
538539
}
539540

541+
// Semicolon
542+
if char == ';' {
543+
let location = Location {
544+
start: column_start,
545+
end: position,
546+
};
547+
548+
let token = Token {
549+
location,
550+
kind: TokenKind::Semicolon,
551+
literal: ";".to_owned(),
552+
};
553+
554+
tokens.push(token);
555+
position += 1;
556+
continue;
557+
}
558+
540559
// Characters to ignoring
541560
if char == ' ' || char == '\n' || char == '\t' {
542561
position += 1;

0 commit comments

Comments
 (0)