UNPKG

1.2 kBPlain TextView Raw
1import * as frontend from 'llparse-frontend';
2
3import source = frontend.source;
4
5import { Compiler, ICompilerOptions, ICompilerResult } from './compiler';
6
7export { source, ICompilerOptions, ICompilerResult };
8
9// TODO(indutny): API for disabling/short-circuiting spans
10
11/**
12 * LLParse graph builder and compiler.
13 */
14export class LLParse extends source.Builder {
15 /**
16 * The prefix controls the names of methods and state struct in generated
17 * public C headers:
18 *
19 * ```c
20 * // state struct
21 * struct PREFIX_t {
22 * ...
23 * }
24 *
25 * int PREFIX_init(PREFIX_t* state);
26 * int PREFIX_execute(PREFIX_t* state, const char* p, const char* endp);
27 * ```
28 *
29 * @param prefix Prefix to be used when generating public API.
30 */
31 constructor(private readonly prefix: string = 'llparse') {
32 super();
33 }
34
35 /**
36 * Compile LLParse graph to the bitcode and C headers
37 *
38 * @param root Root node of the parse graph (see `.node()`)
39 * @param options Compiler options.
40 */
41 public build(root: source.node.Node, options: ICompilerOptions = {})
42 : ICompilerResult {
43 const c = new Compiler(this.prefix, options);
44
45 return c.compile(root, this.properties);
46 }
47}