Skip to content

Commit 914fb46

Browse files
authored
feat: support '_' in ident (#30)
1 parent 1d4b630 commit 914fb46

4 files changed

Lines changed: 49 additions & 28 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22

33
name = "eventql-parser"
4-
version = "0.1.13"
4+
version = "0.1.14"
55
authors = ["Yorick Laupa <yo.eight@gmail.com>"]
66
description = "EventQL Lexer and Parser"
77
homepage = "https://github.com/YoEight/eventql-parser"

src/lexer.rs

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ use crate::error::LexerError;
1111
use crate::token::{Operator, Sym, Symbol, Text, Token};
1212
use nom::branch::alt;
1313
use nom::bytes::complete::{tag, take_while};
14-
use nom::character::complete::{alpha1, alphanumeric0, char, multispace1};
14+
use nom::character::complete::{alpha1, char, multispace1, satisfy};
1515
use nom::character::one_of;
1616
use nom::combinator::{eof, opt, recognize};
1717
use nom::error::{Error, context};
1818
use nom::multi::many0;
1919
use nom::number::complete::double;
2020
use nom::sequence::{delimited, pair};
21-
use nom::{IResult, Parser};
21+
use nom::{AsChar, IResult, Parser};
2222

2323
/// Tokenize an EventQL query string.
2424
///
@@ -165,31 +165,34 @@ fn operator_2(input: Text) -> IResult<Text, Token> {
165165
}
166166

167167
fn ident(input: Text) -> IResult<Text, Token> {
168-
recognize(pair(alpha1, alphanumeric0))
169-
.map(|value: Text| {
170-
let sym = if value.fragment().eq_ignore_ascii_case("and") {
171-
Sym::Operator(Operator::And)
172-
} else if value.fragment().eq_ignore_ascii_case("or") {
173-
Sym::Operator(Operator::Or)
174-
} else if value.fragment().eq_ignore_ascii_case("xor") {
175-
Sym::Operator(Operator::Xor)
176-
} else if value.fragment().eq_ignore_ascii_case("not") {
177-
Sym::Operator(Operator::Not)
178-
} else if value.fragment().eq_ignore_ascii_case("contains") {
179-
Sym::Operator(Operator::Contains)
180-
} else if value.fragment().eq_ignore_ascii_case("as") {
181-
Sym::Operator(Operator::As)
182-
} else {
183-
Sym::Id(value.fragment())
184-
};
185-
186-
Token {
187-
sym,
188-
line: value.location_line(),
189-
col: value.get_column() as u32,
190-
}
191-
})
192-
.parse(input)
168+
recognize(pair(
169+
alpha1,
170+
many0(satisfy(|c| AsChar::is_alphanum(c) || c == '_')),
171+
))
172+
.map(|value: Text| {
173+
let sym = if value.fragment().eq_ignore_ascii_case("and") {
174+
Sym::Operator(Operator::And)
175+
} else if value.fragment().eq_ignore_ascii_case("or") {
176+
Sym::Operator(Operator::Or)
177+
} else if value.fragment().eq_ignore_ascii_case("xor") {
178+
Sym::Operator(Operator::Xor)
179+
} else if value.fragment().eq_ignore_ascii_case("not") {
180+
Sym::Operator(Operator::Not)
181+
} else if value.fragment().eq_ignore_ascii_case("contains") {
182+
Sym::Operator(Operator::Contains)
183+
} else if value.fragment().eq_ignore_ascii_case("as") {
184+
Sym::Operator(Operator::As)
185+
} else {
186+
Sym::Id(value.fragment())
187+
};
188+
189+
Token {
190+
sym,
191+
line: value.location_line(),
192+
col: value.get_column() as u32,
193+
}
194+
})
195+
.parse(input)
193196
}
194197

195198
fn number(input: Text) -> IResult<Text, Token> {

src/tests/lexer.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,9 @@ fn test_lexer_comment() {
1717
let session = Session::builder().build();
1818
insta::assert_yaml_snapshot!(session.tokenize("// useless comment\n "));
1919
}
20+
21+
#[test]
22+
fn test_lexer_id_with_underscore() {
23+
let session = Session::builder().build();
24+
insta::assert_yaml_snapshot!(session.tokenize("event_type"));
25+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
source: src/tests/lexer.rs
3+
expression: "session.tokenize(\"event_type\")"
4+
---
5+
Ok:
6+
- sym:
7+
Id: event_type
8+
line: 1
9+
col: 1
10+
- sym: Eof
11+
line: 1
12+
col: 11

0 commit comments

Comments
 (0)