Skip to content

Instantly share code, notes, and snippets.

@erezsh
Last active July 23, 2021 09:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erezsh/ae3130b5f077f704a3983872b2f16e57 to your computer and use it in GitHub Desktop.
Save erezsh/ae3130b5f077f704a3983872b2f16e57 to your computer and use it in GitHub Desktop.
let tree = parser.parse('{"c": ["a", "b",2]}')
// Object-based transformer
let OT = lark.Transformer.fromObj({
array: (args) => args,
pair: (args) => args,
number: ([x]) => parseInt(x.value),
string: ([x]) => x.value.slice(1, -1),
object: (entries) => Object.fromEntries(entries)
})
// Class-based transformer
class CT extends lark.Transformer {
array(args) {
return args
}
pair(args) {
return args
}
string([x]) {
return x.value.slice(1, -1)
}
number([x]) {
return parseInt(x.value)
}
object(entries) {
return Object.fromEntries(entries)
}
}
let json1 = OT.transform(tree)
let json2 = new CT().transform(tree)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment