@posthog/hogql-parser 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +95 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,5 +1,98 @@
|
|
|
1
1
|
# HogQL Parser
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
ANTLR4-based parser for [HogQL](https://posthog.com/docs/hogql) and [Hog](https://posthog.com/docs/hog),
|
|
4
|
+
available as both a Python C++ extension and a WebAssembly module for JavaScript/TypeScript.
|
|
4
5
|
|
|
5
|
-
|
|
6
|
+
## Packages
|
|
7
|
+
|
|
8
|
+
| Package | Runtime | Registry |
|
|
9
|
+
| ----------------------- | -------------------------------- | ---------------------------------------------------------- |
|
|
10
|
+
| `hogql_parser` | CPython (native C++ extension) | [PyPI](https://pypi.org/project/hogql-parser/) |
|
|
11
|
+
| `@posthog/hogql-parser` | Any JS environment (WebAssembly) | [npm](https://www.npmjs.com/package/@posthog/hogql-parser) |
|
|
12
|
+
|
|
13
|
+
Both packages share the same C++ parser core and ANTLR4 grammar,
|
|
14
|
+
so they produce identical ASTs.
|
|
15
|
+
|
|
16
|
+
## npm package (`@posthog/hogql-parser`)
|
|
17
|
+
|
|
18
|
+
### Installation
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm install @posthog/hogql-parser
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
### Usage
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import createHogQLParser from '@posthog/hogql-parser'
|
|
28
|
+
|
|
29
|
+
const parser = await createHogQLParser()
|
|
30
|
+
|
|
31
|
+
// Parse a HogQL expression
|
|
32
|
+
const exprAST = JSON.parse(parser.parseExpr('1 + 2'))
|
|
33
|
+
|
|
34
|
+
// Parse a SELECT statement
|
|
35
|
+
const selectAST = JSON.parse(parser.parseSelect('SELECT event FROM events WHERE timestamp > now()'))
|
|
36
|
+
|
|
37
|
+
// Parse a Hog program
|
|
38
|
+
const programAST = JSON.parse(parser.parseProgram('let x := 42; return x;'))
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
All parse functions return JSON strings.
|
|
42
|
+
On failure they return a JSON error object instead of throwing:
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
const result = JSON.parse(parser.parseExpr('!!!'))
|
|
46
|
+
if ('error' in result) {
|
|
47
|
+
console.error(result.type, result.message) // e.g. "SyntaxError ..."
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### API
|
|
52
|
+
|
|
53
|
+
| Method | Description |
|
|
54
|
+
| --------------------------------------------- | ---------------------------------- |
|
|
55
|
+
| `parseExpr(input, isInternal?)` | Parse a HogQL expression |
|
|
56
|
+
| `parseSelect(input, isInternal?)` | Parse a SELECT statement |
|
|
57
|
+
| `parseOrderExpr(input, isInternal?)` | Parse an ORDER BY expression |
|
|
58
|
+
| `parseProgram(input, isInternal?)` | Parse a Hog program |
|
|
59
|
+
| `parseFullTemplateString(input, isInternal?)` | Parse a template string (`f'...'`) |
|
|
60
|
+
| `parseStringLiteralText(input)` | Unquote a string literal |
|
|
61
|
+
|
|
62
|
+
Setting `isInternal` to `true` omits position information from the AST.
|
|
63
|
+
|
|
64
|
+
## Python package (`hogql_parser`)
|
|
65
|
+
|
|
66
|
+
### Installation
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
pip install hogql_parser
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
The Python package is a native C++ extension and requires a platform with prebuilt wheels
|
|
73
|
+
(macOS and Linux, x86_64 and arm64).
|
|
74
|
+
|
|
75
|
+
### Local development
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
pip install ./common/hogql_parser
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## Building from source
|
|
82
|
+
|
|
83
|
+
### Python
|
|
84
|
+
|
|
85
|
+
```bash
|
|
86
|
+
pip install ./common/hogql_parser
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### WebAssembly
|
|
90
|
+
|
|
91
|
+
Requires the [Emscripten](https://emscripten.org/) toolchain and [Ninja](https://ninja-build.org/).
|
|
92
|
+
|
|
93
|
+
```bash
|
|
94
|
+
cd common/hogql_parser
|
|
95
|
+
npm run build
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
This compiles the parser to WASM and places the output in `dist/`.
|