Skip to content

Commit

Permalink
feat: PromQL Rust Lexer (#12)
Browse files Browse the repository at this point in the history
* feat(lex): skeleton

* refactor(lexer): use enum instead of trait object

* lex: keyword_or_identifier

* fix(ctx): pos, start, peek logic

* refactor: fn -> Fn

* refactor: move shift into Lexer

* lexer: accept_string

* lexer: duration, lable_selector

* example: make example work with lexer

* test: common test done

* test(lex): more test cases

* test(lex): unbalanced parentheses

* test(lexer): subquery

* test(lexer): more cases

* test: token and production fn

* misc(lex): modification based on review comments

- don't expose some public methods
- refactor test cases

Reviewer: @evenyag
Refs: #14 #15

* misc(lex): revision based on review comment

- func in token mod, directly use match arms
- use char default method, instead of a hashmap
- use for loop to asset_eq!(left, right) instead of assert!(all)

reviewer: @evenyag

* misc(lex): use const instead of lazy_static

* perf(token): use hashmap instead of match on &str

* fix(lex): potential panic try to dec depth when depth is less than 1

* misc(lex): use usize instead of u8 for paren_depth

* feat(lex): to check if backup is at the beginning of input

* comment(lex): typo
  • Loading branch information
yuanbohan authored Dec 21, 2022
1 parent 8f721cd commit a5f2822
Show file tree
Hide file tree
Showing 8 changed files with 1,535 additions and 82 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by Cargo
# will have compiled files and executables
/target/
.vscode

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Expand Down
9 changes: 5 additions & 4 deletions examples/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
use promql_parser::parser;

fn main() {
let promql = "node_cpu_seconds_total{cpu=0,mode=idle}";
let promql = r#"prometheus_http_requests_total{code="200", job="prometheus"}"#;

let ast = parser::parse(promql).unwrap();

println!("AST: {:?}", ast);
match parser::parse(promql) {
Ok(ast) => println!("AST: {:?}", ast),
Err(info) => println!("Err: {:?}", info),
}
}
3 changes: 1 addition & 2 deletions src/label/matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::parser::Token;
use crate::parser::{T_EQL, T_EQL_REGEX, T_NEQ, T_NEQ_REGEX};
use crate::parser::token::{Token, T_EQL, T_EQL_REGEX, T_NEQ, T_NEQ_REGEX};
use regex::Regex;

#[derive(Debug)]
Expand Down
Loading

0 comments on commit a5f2822

Please sign in to comment.