Skip to content

Commit

Permalink
Merge pull request #12 from getlang-dev/subqueries
Browse files Browse the repository at this point in the history
rename function expressions to subqueries
  • Loading branch information
mattfysh committed Aug 14, 2024
2 parents 48fb559 + 5a73edf commit 36fd979
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 26 deletions.
6 changes: 6 additions & 0 deletions .changeset/slimy-wombats-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@getlang/parser": minor
"@getlang/get": patch
---

rename function expressions to subqueries
5 changes: 5 additions & 0 deletions .github/workflows/pull-request.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
on: pull_request

jobs:
checks:
uses: ./.github/workflows/checks.yaml
4 changes: 2 additions & 2 deletions packages/get/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,13 +254,13 @@ export async function execute(
},
},

FunctionExpr: {
SubqueryExpr: {
async enter(node, visit) {
return ctx(node, visit, async () => {
const ex = await executeBody(visit, node.body, scope.context)
invariant(
ex,
new QuerySyntaxError('Function missing extract statement'),
new QuerySyntaxError('Subquery missing extract statement'),
)
return ex
})
Expand Down
14 changes: 7 additions & 7 deletions packages/parser/ast/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export enum NodeKind {
IdentifierExpr = 'IdentifierExpr',
SelectorExpr = 'SelectorExpr',
ModifierExpr = 'ModifierExpr',
FunctionExpr = 'FunctionExpr',
SubqueryExpr = 'SubqueryExpr',
ModuleCallExpr = 'ModuleCallExpr',
ObjectLiteralExpr = 'ObjectLiteralExpr',
SliceExpr = 'SliceExpr',
Expand Down Expand Up @@ -114,8 +114,8 @@ type ModifierExpr = {
typeInfo: TypeInfo
}

type FunctionExpr = {
kind: NodeKind.FunctionExpr
type SubqueryExpr = {
kind: NodeKind.SubqueryExpr
body: Stmt[]
context?: Expr
typeInfo: TypeInfo
Expand Down Expand Up @@ -158,7 +158,7 @@ export type Expr =
| IdentifierExpr
| SelectorExpr
| ModifierExpr
| FunctionExpr
| SubqueryExpr
| ModuleCallExpr
| ObjectLiteralExpr
| SliceExpr
Expand Down Expand Up @@ -229,8 +229,8 @@ const requestExpr = (
typeInfo: { type: Type.Value },
})

const functionExpr = (body: Stmt[], context?: Expr): FunctionExpr => ({
kind: NodeKind.FunctionExpr,
const subqueryExpr = (body: Stmt[], context?: Expr): SubqueryExpr => ({
kind: NodeKind.SubqueryExpr,
body,
typeInfo: { type: Type.Value },
context,
Expand Down Expand Up @@ -323,6 +323,6 @@ export const t = {
modifierExpr,
sliceExpr,
objectLiteralExpr,
functionExpr,
subqueryExpr,
moduleCallExpr,
}
8 changes: 5 additions & 3 deletions packages/parser/ast/print.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,16 @@ const printVisitor: InterpretVisitor<Doc> = {
return node.context ? [node.context, indent([line, '-> ', slice])] : slice
},

FunctionExpr(node) {
const fn = [
SubqueryExpr(node) {
const subquery = [
'(',
indent(node.body.flatMap(x => [hardline, x])),
hardline,
')',
]
return node.context ? [node.context, indent([line, '-> ', fn])] : fn
return node.context
? [node.context, indent([line, '-> ', subquery])]
: subquery
},
}

Expand Down
2 changes: 1 addition & 1 deletion packages/parser/ast/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class RootScope<T> {

set extracted(data: T) {
if (this.scope.extracted !== undefined) {
console.warn('Functions must contain a single extract statement')
console.warn('Subqueries must contain a single extract statement')
} else {
this.scope.extracted = data
}
Expand Down
4 changes: 2 additions & 2 deletions packages/parser/desugar/inference/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ export function inferBase(): TransformVisitor {
return { ...node, body: insertUrls(node.body, urls) }
},

FunctionExpr: {
SubqueryExpr: {
enter(node, visit) {
const xnode = trace.FunctionExpr.enter(node, visit)
const xnode = trace.SubqueryExpr.enter(node, visit)
return { ...xnode, body: insertUrls(xnode.body, urls) }
},
},
Expand Down
4 changes: 2 additions & 2 deletions packages/parser/desugar/inference/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ export function inferContext(): TransformVisitor {
return { ...node, body: insertParsers(node.body, parsers) }
},

FunctionExpr: {
SubqueryExpr: {
enter(node, visit) {
const xnode = trace.FunctionExpr.enter(node, visit)
const xnode = trace.SubqueryExpr.enter(node, visit)
return { ...xnode, body: insertParsers(xnode.body, parsers) }
},
},
Expand Down
4 changes: 2 additions & 2 deletions packages/parser/desugar/inference/typeinfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,9 @@ export function inferTypeInfo(): TransformVisitor {
},
},

FunctionExpr: {
SubqueryExpr: {
enter(node, visit) {
const xnode = trace.FunctionExpr.enter(node, itemVisit(node, visit))
const xnode = trace.SubqueryExpr.enter(node, itemVisit(node, visit))
const typeInfo = xnode.body.find(
stmt => stmt.kind === NodeKind.ExtractStmt,
)?.value.typeInfo ?? { type: Type.Never }
Expand Down
2 changes: 1 addition & 1 deletion packages/parser/desugar/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function traceVisitor(scope: RootScope<Expr>) {
},

// contextual expressions
FunctionExpr: {
SubqueryExpr: {
enter(node, visit) {
return ctx(node, visit, node => {
scope.push()
Expand Down
6 changes: 3 additions & 3 deletions packages/parser/grammar/getlang.ne
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ drill -> (%drill_arrow _):? expression {% p.drillContext %}

### EXPR
expression -> (template | slice | modifier) {% p.idd %}
expression -> (object | function | module_call) {% p.idd %}
expression -> (object | subquery | module_call) {% p.idd %}
expression -> id_expr {% p.identifier %}


### FUNCTIONS
function -> "(" _ statements _ ")" {% p.fn %}
### SUBQUERIES
subquery -> "(" _ statements _ ")" {% p.subquery %}
module_call -> id_expr "(" object:? ")" {% p.moduleCall %}


Expand Down
2 changes: 1 addition & 1 deletion packages/parser/grammar/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export const assignment: PP = ([, , name, optional, , , , expr]) =>

export const extract: PP = ([, , exports]) => t.extractStmt(exports)

export const fn: PP = ([, , stmts]) => t.functionExpr(stmts)
export const subquery: PP = ([, , stmts]) => t.subqueryExpr(stmts)

export const moduleCall: PP = ([name, , optInputs]) =>
t.moduleCallExpr(name, optInputs?.[0])
Expand Down
4 changes: 2 additions & 2 deletions test/modules.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,15 @@ describe('modules', () => {
expect(result).toEqual({ test: true })
})

test('func scope with context', async () => {
test('subquery scope with context', async () => {
const result = await execute(`
set x = \`{ test: true }\`
extract $x -> ( extract $ )
`)
expect(result).toEqual({ test: true })
})

test('func scope with closures', async () => {
test('subquery scope with closures', async () => {
const result = await execute(`
set x = \`{ test: true }\`
extract (
Expand Down

0 comments on commit 36fd979

Please sign in to comment.