forge
Explore
Log in

Don't you dare go Hollow

project_note

Clone
★ 0

0 watching0 forksRust3.6 MiBc49f5e7Tahinli1 month ago2 commits

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