project_note
Clone
https://git.tahinli.com/Tahinli/project_note.git
0 watching0 forksRust3.6 MiBc49f5e7Tahinli1 month ago2 commits
| 1 | // JSONPath Parser |
| 2 | // This grammar defines the structure for parsing JSONPath expressions. |
| 3 | // It follows a structured approach with grouped rules for clarity and maintainability. |
| 4 | |
| 5 | // Entry Point |
| 6 | // Defines the overall structure of a JSONPath expression, starting with SOI (start of input) and ending with EOI (end of input). |
| 7 | jsonpath = _{ SOI ~ jsonpath_query ~ EOI } |
| 8 | |
| 9 | // Core Query Structure |
| 10 | jsonpath_query = _{ root_identifier ~ segments } |
| 11 | |
| 12 | // Segments |
| 13 | segments = _{ (S ~ segment)* } |
| 14 | |
| 15 | // Whitespace Handling |
| 16 | B = _{ "\x20" | "\x09" | "\x0A" | "\x0D" } // Basic whitespace characters: space, tab, newline, carriage return |
| 17 | S = _{ B* } // Optional whitespace |
| 18 | |
| 19 | // Identifiers |
| 20 | root_identifier = _{ "$" } // Root node identifier |
| 21 | current_node_identifier = _{ "@" } // Current node identifier |
| 22 | |
| 23 | // Segment Types |
| 24 | segment = _{ child_segment | descendant_segment } |
| 25 | child_segment = { bracketed_selection | ("." ~ (wildcard_selector | member_name_shorthand)) } |
| 26 | descendant_segment = { ".." ~ (bracketed_selection | wildcard_selector | member_name_shorthand) } |
| 27 | |
| 28 | // Bracketed Selection |
| 29 | bracketed_selection = { "[" ~ S ~ selector ~ (S ~ "," ~ S ~ selector)* ~ S ~ "]" } |
| 30 | |
| 31 | // Selectors |
| 32 | selector = _{ name_selector | wildcard_selector | slice_selector | index_selector | filter_selector } |
| 33 | wildcard_selector = { "*" } // Matches any element |
| 34 | name_selector = _{ string_literal } // Matches a specific name |
| 35 | index_selector = @{ int } // Matches a specific index |
| 36 | slice_selector = { (start ~ S)? ~ ":" ~ S ~ (stop ~ S)? ~ (":" ~ (S ~ step)?)? } // Array slicing |
| 37 | filter_selector = { "?" ~ S ~ logical_expr } // Filter based on logical expression |
| 38 | |
| 39 | // Integer and Slice Components |
| 40 | int = { "0" | ("-"? ~ ASCII_NONZERO_DIGIT ~ ASCII_DIGIT*) } |
| 41 | start = @{ int } |
| 42 | stop = @{ int } |
| 43 | step = @{ int } |
| 44 | |
| 45 | // String Literals |
| 46 | string_literal = _{ ("\x22" ~ double_quoted ~ "\x22") | ("\x27" ~ single_quoted ~ "\x27") } |
| 47 | double_quoted = { double_quoted_char* } |
| 48 | single_quoted = { single_quoted_char* } |
| 49 | double_quoted_char = _{ unescaped | "\x27" | (ESC ~ "\x22") | (ESC ~ escapable) } |
| 50 | single_quoted_char = _{ unescaped | "\x22" | (ESC ~ "\x27") | (ESC ~ escapable) } |
| 51 | ESC = _{ "\x5C" } // Escape character (backslash) |
| 52 | |
| 53 | // Escapable Characters |
| 54 | escapable = _{ "\x62" | "\x66" | "\x6E" | "\x72" | "\x74" | "/" | "\\" | ("\x75" ~ hexchar) } |
| 55 | hexchar = _{ non_surrogate | (high_surrogate ~ "\\u" ~ low_surrogate) } |
| 56 | non_surrogate = _{ ((ASCII_DIGIT | ^"A" | ^"B" | ^"C" | ^"E" | ^"F") ~ ASCII_HEX_DIGIT{3}) | (^"D" ~ '\u{30}'..'\u{37}' ~ ASCII_HEX_DIGIT{2}) } |
| 57 | high_surrogate = _{ ^"D" ~ ("8" | "9" | ^"A" | ^"B") ~ ASCII_HEX_DIGIT{2} } |
| 58 | low_surrogate = _{ ^"D" ~ (^"C" | ^"D" | ^"E" | ^"F") ~ ASCII_HEX_DIGIT{2} } |
| 59 | |
| 60 | // Unescaped Characters |
| 61 | unescaped = _{ '\u{20}'..'\u{21}' | '\u{23}'..'\u{26}' | '\u{28}'..'\u{5B}' | '\u{5D}'..'\u{D7FF}' | '\u{E000}'..'\u{10FFFF}' } |
| 62 | |
| 63 | // Logical Expressions |
| 64 | logical_expr = _{ logical_or_expr } |
| 65 | logical_or_expr = { logical_and_expr ~ (S ~ "||" ~ S ~ logical_and_expr)* } |
| 66 | logical_and_expr = { basic_expr ~ (S ~ "&&" ~ S ~ basic_expr)* } |
| 67 | basic_expr = _{ paren_expr | comparison_expr | test_expr } |
| 68 | paren_expr = { (logical_not_op ~ S)? ~ "(" ~ S ~ logical_expr ~ S ~ ")" } |
| 69 | logical_not_op = { "!" } |
| 70 | test_expr = { (logical_not_op ~ S)? ~ (filter_query | function_expr) } |
| 71 | |
| 72 | // Queries in Filters |
| 73 | filter_query = _{ rel_query | root_query } |
| 74 | root_query = { jsonpath_query } |
| 75 | rel_query = { current_node_identifier ~ segments } |
| 76 | |
| 77 | // Comparison Expressions |
| 78 | comparison_expr = { comparable ~ S ~ comparison_op ~ S ~ comparable } |
| 79 | comparison_op = { "==" | "!=" | "<=" | ">=" | "<" | ">" | "contains" | "in" } |
| 80 | comparable = _{ literal | singular_query | function_expr } |
| 81 | |
| 82 | // Literals |
| 83 | literal = _{ number | string_literal | array_literal | true_literal | false_literal | null } |
| 84 | true_literal = { "true" } |
| 85 | false_literal = { "false" } |
| 86 | null = { "null" } |
| 87 | number = { (int | "-0") ~ frac? ~ exp? } |
| 88 | frac = { "." ~ ASCII_DIGIT+ } |
| 89 | exp = { ^"e" ~ ("-" | "+")? ~ ASCII_DIGIT+ } |
| 90 | array_literal = { "[" ~ S ~ (literal ~ (S ~ "," ~ S ~ literal)*)? ~ S ~ "]" } |
| 91 | |
| 92 | // Singular Queries |
| 93 | singular_query = _{ rel_singular_query | abs_singular_query } |
| 94 | rel_singular_query = { current_node_identifier ~ singular_query_segments } |
| 95 | abs_singular_query = { root_identifier ~ singular_query_segments } |
| 96 | singular_query_segments = _{ (S ~ (name_segment | index_segment))* } |
| 97 | name_segment = { ("[" ~ name_selector ~ "]") | ("." ~ member_name_shorthand) } |
| 98 | index_segment = { "[" ~ index_selector ~ "]" } |
| 99 | |
| 100 | // Function Expressions |
| 101 | function_expr = { function_name ~ "(" ~ S ~ (function_argument ~ (S ~ "," ~ S ~ function_argument)*)? ~ S ~ ")" } |
| 102 | function_name = { function_name_first ~ function_name_char* } |
| 103 | function_name_first = _{ ASCII_ALPHA_LOWER } |
| 104 | function_name_char = _{ function_name_first | "_" | ASCII_DIGIT } |
| 105 | function_argument = _{ filter_query | logical_expr | function_expr | literal } |
| 106 | |
| 107 | // Member Names |
| 108 | member_name_shorthand = { name_first ~ name_char* } |
| 109 | name_first = _{ ASCII_ALPHA | "_" | '\u{80}'..'\u{D7FF}' | '\u{E000}'..'\u{10FFFF}' } |
| 110 | name_char = _{ name_first | ASCII_DIGIT } |