scalameta-parsers
v4.12.2
Published
Library to parse Scala programs
Downloads
1,853
Readme
scalameta-parsers
A JS API for the parsers
module of scalameta
.
It exposes two functions:
parseSource(code: string)
: parses the given code as a full-fledged source fileparseStat(code: string)
: parses the given code a statement (useful for parsing single expressions without wrapping them into an object or class)
Example:
const { parseStat, parseSource } = require('scalameta-parsers');
const tree1 = parseStat('val answer = 42');
console.log(tree1);
// Output:
// { type: 'Defn.Val',
// children:
// [ { type: 'Pat.Var.Term', children: [Object], pos: [Object] },
// { type: 'Lit.Int', children: [], pos: [Object], value: 42 } ],
// pos: { start: 0, end: 15 } }
const tree2 = parseSource(`
object Main {
def main(args: Array[String]): Unit =
println("Hello, World!")
}`);
console.log(tree2);
// Output:
// { type: 'Source',
// children: [ { type: 'Defn.Object', children: [Object], pos: [Object] } ],
// pos: { start: 0, end: 85 } }