Skip to content

Instantly share code, notes, and snippets.

@andy0130tw
Created September 10, 2024 19:10
Show Gist options
  • Save andy0130tw/c1f4e986165ed039d2478fdf7bb2d061 to your computer and use it in GitHub Desktop.
Save andy0130tw/c1f4e986165ed039d2478fdf7bb2d061 to your computer and use it in GitHub Desktop.
Lezer grammar definition for JSON diff syntax used by jd (https://github.com/josephburnett/jd). Built on top of JSON's grammar.
@top JSONDiff { section* }
value { True | False | Null | Number | String | Object | Array }
section {
Header
(Deletion+ Insertion* | Insertion+)
}
Header { "@" "[" list<path> "]" "\n" }
path { EmptyObject | PropertyName | Number }
EmptyObject { "{" "}" }
Insertion { "+" value "\n" }
Deletion { "-" value "\n" }
String { string }
Object { "{" list<Property>? "}" }
Array { "[" list<value>? "]" }
Property { PropertyName ":" value }
PropertyName { string }
@tokens {
True { "true" }
False { "false" }
Null { "null" }
Number { '-'? int frac? exp? }
int { '0' | $[1-9] @digit* }
frac { '.' @digit+ }
exp { $[eE] $[+\-]? @digit+ }
string { '"' char* '"' }
char { $[\u{20}\u{21}\u{23}-\u{5b}\u{5d}-\u{10ffff}] | "\\" esc }
esc { $["\\\/bfnrt] | "u" hex hex hex hex }
hex { $[0-9a-fA-F] }
whitespace { $[ \r\t] }
"{" "}" "[" "]" "@" "+" "-"
}
@skip { whitespace }
list<item> { item ("," item)* }
@external propSource jsonHighlighting from "./highlight"
@detectDelim
@andy0130tw
Copy link
Author

demo

demo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment