use rantlr_core::analyze_and_parse;
#[test]
fn mesh_protocol() {
let g = r#"
grammar MeshProtocol;
token Id = Identifier;
token Num = Number;
token Ws = " " -> skip;
rule packet { header routing match heartbeat | message | gossip trailer }
rule header { "MESH" "version" Num optional { "flags" Num } }
rule routing { "from" actor_ref "to" actor_ref }
rule actor_ref { Id optional { "@" Id } }
rule heartbeat { "heartbeat" "ttl" Num "payload" Num optional { "epoch" Num } }
rule message { "msg" "ttl" Num "payload" Num optional { "corr" Id } }
rule gossip { "gossip" "peers" Num "ttl" Num "payload" Num }
rule trailer { "crc" Num }
"#;
let r = analyze_and_parse(
g,
"MESH version 2 flags 1 from node_a@dc1 to node_b@dc2 heartbeat ttl 30 payload 128 epoch 9 crc 9001",
)
.unwrap();
assert_eq!(r.tree.kind, "packet");
}
#[test]
fn promql_clone() {
let g = r#"
grammar PromQL;
token Id = Identifier;
token Num = Number;
token Str = QuotedString;
token Ws = " " -> skip;
rule query { expr }
rule expr { match aggregate | call | selector | ("(" expr ")") }
rule aggregate { agg_op optional { "by" "(" label_list ")" } "(" expr ")" }
rule agg_op { match "sum" | "avg" | "min" | "max" | "count" }
rule call { Id "(" expr optional { "," expr } ")" }
rule selector { Id optional { label_matchers } optional { range } }
rule label_matchers { "{" optional { label_matcher repeat { "," label_matcher } } "}" }
rule label_matcher { Id "=" Str }
rule label_list { Id repeat { "," Id } }
rule range { "[" Num Id "]" }
"#;
let r = analyze_and_parse(
g,
r#"sum by (job) (rate(http_requests_total{job="api"}[5m]))"#,
)
.unwrap();
assert_eq!(r.tree.kind, "query");
}
#[test]
fn graphql_sql() {
let g = r#"
grammar GraphQLSql;
token Id = Identifier;
token Num = Number;
token Str = QuotedString;
token Ws = " " -> skip;
rule document { definition repeat { definition } }
rule definition { match operation | frag_def }
rule operation { "query" optional { Id } selection_set }
rule frag_def { "fragment" Id "on" Id selection_set }
rule selection_set { "{" selection repeat { selection } "}" }
rule selection { match field | fragment_spread }
rule field { Id optional { arguments } optional { directive } optional { selection_set } }
rule fragment_spread { "..." Id optional { directive } }
rule arguments { "(" argument repeat { "," argument } ")" }
rule argument { Id ":" value }
rule directive { "@" Id optional { arguments } }
rule value { match Num | Str | Id | ("true") | ("false") }
"#;
let r = analyze_and_parse(
g,
"query GetUser { user(id: 1) { id name ...UserFields @include(if: true) } } fragment UserFields on User { email }",
)
.unwrap();
assert_eq!(r.tree.kind, "document");
}
#[test]
fn data_pipeline() {
let g = r#"
grammar DataPipeline;
token Id = Identifier;
token Num = Number;
token Str = QuotedString;
token Ws = " " -> skip;
rule pipeline { "pipeline" Id "{" step repeat { step } "}" }
rule step { match extract_step | clean_step | transform_step | train_step | load_step }
rule extract_step { "extract" Id Str }
rule clean_step { "clean" "dropna" "subset" Str }
rule transform_step { "transform" "scale" "cols" Str repeat { Str } }
rule train_step { "train" "model" Id "epochs" Num "lr" Num }
rule load_step { "load" "table" Str }
"#;
let r = analyze_and_parse(
g,
r#"pipeline churn { extract csv "s3://raw/events.csv" clean dropna subset "user_id" transform scale cols "amount" "score" train model xgboost epochs 20 lr 0.01 load table "warehouse.churn_features" }"#,
)
.unwrap();
assert_eq!(r.tree.kind, "pipeline");
}
#[test]
fn bytecode_meta() {
let g = r#"
grammar BytecodeMeta;
token Id = Identifier;
token Num = Number;
token Str = QuotedString;
token Ws = " " -> skip;
rule class_file { "class" dotted_name optional { extends_clause } optional { implements_clause } "{" repeat { member } "}" }
rule dotted_name { Id repeat { "." Id } }
rule extends_clause { "extends" dotted_name }
rule implements_clause { "implements" dotted_name repeat { "," dotted_name } }
rule member { match annotation | field_decl | method_decl }
rule annotation { "annotation" Id optional { "value" Str } }
rule field_decl { "field" Id "type" Id }
rule method_decl { "method" Id "returns" Id optional { annotation } }
"#;
let r = analyze_and_parse(
g,
r#"class com.example.User extends Object implements Serializable { annotation Deprecated value "since=21" field id type int method hashCode returns int annotation Override }"#,
)
.unwrap();
assert_eq!(r.tree.kind, "class_file");
}