Public
Edited
Mar 31, 2023
1 fork
9 stars
Insert cell
Insert cell
Insert cell
Parser = buildParser(`
@top Program { expression }

expression { Name | Number | BinaryExpression }

BinaryExpression { "(" expression ("+" | "-") expression ")" }

@tokens {
Name { @asciiLetter+ }
Number { @digit+ }
}
`)
Insert cell
Insert cell
Insert cell
Insert cell
tree
Insert cell
Insert cell
render(Parser.parse('(a+1')) // missing closing parentheses
Insert cell
Insert cell
Insert cell
JSONParser = buildParser(`
@top JsonText { value }

value { True | False | Null | Number | String | Object | Array }

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 { $[ \n\r\t] }

"{" "}" "[" "]"
}

@skip { whitespace }
list<item> { item ("," item)* }

@external propSource jsonHighlighting from "./highlight"

@detectDelim
`)
Insert cell
Insert cell
Insert cell
Insert cell
function render(tree) {
let lists = ''
tree.iterate({
enter({type, from, to}) {
lists += `<ul><li>${type.name} (${from}→${to})`
},
leave() {
lists += '</ul>'
}
})
return Object.assign(html`${lists}`, { value: tree });
}
Insert cell
buildParser = (await import("@lezer/generator@1.2.2")).buildParser
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more