Skip to content

Commit

Permalink
Merge pull request #2118 from undb-io/release/v1.0.0-112
Browse files Browse the repository at this point in the history
Release version v1.0.0-112
  • Loading branch information
nichenqin authored Oct 30, 2024
2 parents 97d49a5 + b9b59dc commit 66f9ccb
Show file tree
Hide file tree
Showing 16 changed files with 832 additions and 918 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Changelog

## v1.0.0-112


### 🩹 Fixes

- Remove antlr4ts ([13eaa3d](https://github.com/undb-io/undb/commit/13eaa3d))

### ❤️ Contributors

- Nichenqin ([@nichenqin](http://github.com/nichenqin))

## v1.0.0-111


Expand Down
33 changes: 16 additions & 17 deletions apps/frontend/src/lib/components/formula/formula-cursor.visitor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
AbstractParseTreeVisitor,
AddSubExprContext,
AndExprContext,
ArgumentListContext,
Expand All @@ -16,7 +15,7 @@ import {
VariableContext,
} from "@undb/formula"

export class FormulaCursorVisitor extends AbstractParseTreeVisitor<void> implements FormulaParserVisitor<void> {
export class FormulaCursorVisitor extends FormulaParserVisitor<void> {
private pathNodes: ParseTree[] = []
private variables: Set<string> = new Set()
public readonly targetPosition: number
Expand Down Expand Up @@ -46,7 +45,7 @@ export class FormulaCursorVisitor extends AbstractParseTreeVisitor<void> impleme

public getFunctionName(): string | undefined {
const functionCall = this.getNearestFunctionNode()
return functionCall?.IDENTIFIER()?.text
return functionCall?.IDENTIFIER().getText()
}

protected defaultResult(): void {
Expand All @@ -60,8 +59,8 @@ export class FormulaCursorVisitor extends AbstractParseTreeVisitor<void> impleme
visitPositionInRange(ctx: ExpressionContext) {
if (!ctx.start || !ctx.stop) return

const start = ctx.start.startIndex
const stop = ctx.stop.stopIndex
const start = ctx.start.tokenIndex
const stop = ctx.stop.tokenIndex
const isPositionWithinRange = start <= this.targetPosition && stop >= this.targetPosition

if (isPositionWithinRange) {
Expand All @@ -70,48 +69,48 @@ export class FormulaCursorVisitor extends AbstractParseTreeVisitor<void> impleme
}
}

visitFormula(ctx: FormulaContext) {
visitFormula = (ctx: FormulaContext) => {
this.visitPositionInRange(ctx)
}

visitComparisonExpr(ctx: ComparisonExprContext) {
visitComparisonExpr = (ctx: ComparisonExprContext) => {
this.visitPositionInRange(ctx)
}

visitAndExpr(ctx: AndExprContext) {
visitAndExpr = (ctx: AndExprContext) => {
this.visitPositionInRange(ctx)
}

visitOrExpr(ctx: OrExprContext) {
visitOrExpr = (ctx: OrExprContext) => {
this.visitPositionInRange(ctx)
}

visitNotExpr(ctx: NotExprContext) {
visitNotExpr = (ctx: NotExprContext) => {
this.visitPositionInRange(ctx)
}

visitMulDivModExpr(ctx: MulDivModExprContext) {
visitMulDivModExpr = (ctx: MulDivModExprContext) => {
this.visitPositionInRange(ctx)
}

visitAddSubExpr(ctx: AddSubExprContext) {
visitAddSubExpr = (ctx: AddSubExprContext) => {
this.visitPositionInRange(ctx)
}

visitFunctionExpr(ctx: FunctionExprContext) {
visitFunctionExpr = (ctx: FunctionExprContext) => {
this.visitPositionInRange(ctx)
}

visitFunctionCall(ctx: FunctionCallContext) {
visitFunctionCall = (ctx: FunctionCallContext) => {
this.visitPositionInRange(ctx)
}

visitArgumentList(ctx: ArgumentListContext) {
visitArgumentList = (ctx: ArgumentListContext) => {
this.visitPositionInRange(ctx)
}

visitVariable(ctx: VariableContext) {
this.variables.add(ctx.IDENTIFIER().text)
visitVariable = (ctx: VariableContext) => {
this.variables.add(ctx.IDENTIFIER().getText())
this.visitPositionInRange(ctx)
}
}
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "undb",
"version": "1.0.0-111",
"version": "1.0.0-112",
"private": true,
"scripts": {
"build": "NODE_ENV=production bun --bun turbo build",
Expand Down
7 changes: 3 additions & 4 deletions packages/formula/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@
"type": "module",
"types": "src/index.d.ts",
"devDependencies": {
"@types/bun": "latest",
"antlr4ts-cli": "^0.5.0-alpha.4"
"@types/bun": "latest"
},
"peerDependencies": {
"typescript": "^5.0.0"
},
"dependencies": {
"@undb/zod": "*",
"antlr4ts": "^0.5.0-alpha.4"
"antlr4": "^4.13.2"
},
"scripts": {
"generate-parser": "bun run scripts/generate-parser.ts",
"generate": "bun generate-parser"
"generate": "bun run generate-parser"
}
}
2 changes: 1 addition & 1 deletion packages/formula/scripts/generate-parser.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { $ } from "bun"

await $`antlr4ts -visitor -no-listener src/grammar/*.g4`
await $`antlr -Dlanguage=TypeScript -visitor -no-listener src/grammar/*.g4`
64 changes: 30 additions & 34 deletions packages/formula/src/formula.visitor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { AbstractParseTreeVisitor } from "antlr4ts/tree/AbstractParseTreeVisitor"
import { FormulaFunction } from "./formula/formula.type"
import { globalFunctionRegistry } from "./formula/registry"
import {
Expand All @@ -18,7 +17,7 @@ import {
VariableContext,
VariableExprContext,
} from "./grammar/FormulaParser"
import type { FormulaParserVisitor } from "./grammar/FormulaParserVisitor"
import FormulaParserVisitor from "./grammar/FormulaParserVisitor"
import {
ArgumentListResult,
ReturnType,
Expand All @@ -28,10 +27,7 @@ import {
type VariableResult,
} from "./types"

export class FormulaVisitor
extends AbstractParseTreeVisitor<ExpressionResult>
implements FormulaParserVisitor<ExpressionResult>
{
export class FormulaVisitor extends FormulaParserVisitor<ExpressionResult> {
private variables: Set<string> = new Set()

private assertType(result: ExpressionResult, types: ReturnType[]): boolean {
Expand All @@ -53,11 +49,11 @@ export class FormulaVisitor
return true
}

visitFormula(ctx: FormulaContext): ExpressionResult {
visitFormula = (ctx: FormulaContext): ExpressionResult => {
return this.visit(ctx.expression())
}

visitMulDivModExpr(ctx: MulDivModExprContext): ExpressionResult {
visitMulDivModExpr = (ctx: MulDivModExprContext): ExpressionResult => {
const left = this.visit(ctx.expression(0)) as NumberResult | VariableResult
const right = this.visit(ctx.expression(1)) as NumberResult | VariableResult

Expand All @@ -70,11 +66,11 @@ export class FormulaVisitor
name: op,
arguments: [left, right],
returnType: "number",
value: ctx.text,
value: ctx.getText(),
}
}

visitAddSubExpr(ctx: AddSubExprContext): ExpressionResult {
visitAddSubExpr = (ctx: AddSubExprContext): ExpressionResult => {
const left = this.visit(ctx.expression(0)) as NumberResult | VariableResult
const right = this.visit(ctx.expression(1)) as NumberResult | VariableResult

Expand All @@ -87,11 +83,11 @@ export class FormulaVisitor
name: op,
arguments: [left, right],
returnType: "number",
value: ctx.text,
value: ctx.getText(),
}
}

visitComparisonExpr(ctx: ComparisonExprContext): ExpressionResult {
visitComparisonExpr = (ctx: ComparisonExprContext): ExpressionResult => {
const left = this.visit(ctx.expression(0))
const right = this.visit(ctx.expression(1))

Expand All @@ -104,11 +100,11 @@ export class FormulaVisitor
name: op,
arguments: [left, right],
returnType: "boolean",
value: ctx.text,
value: ctx.getText(),
}
}

visitAndExpr(ctx: AndExprContext): ExpressionResult {
visitAndExpr = (ctx: AndExprContext): ExpressionResult => {
const left = this.visit(ctx.expression(0))
const right = this.visit(ctx.expression(1))

Expand All @@ -120,11 +116,11 @@ export class FormulaVisitor
name: "AND",
arguments: [left, right],
returnType: "boolean",
value: ctx.text,
value: ctx.getText(),
}
}

visitOrExpr(ctx: OrExprContext): ExpressionResult {
visitOrExpr = (ctx: OrExprContext): ExpressionResult => {
const left = this.visit(ctx.expression(0))
const right = this.visit(ctx.expression(1))

Expand All @@ -136,44 +132,44 @@ export class FormulaVisitor
name: "OR",
arguments: [left, right],
returnType: "boolean",
value: ctx.text,
value: ctx.getText(),
}
}

visitNotExpr(ctx: NotExprContext): ExpressionResult {
visitNotExpr = (ctx: NotExprContext): ExpressionResult => {
const expr = this.visit(ctx.expression())
this.assertType(expr, ["boolean"])
return {
type: "functionCall",
name: "NOT",
arguments: [expr],
returnType: "boolean",
value: ctx.text,
value: ctx.getText(),
}
}

visitFunctionExpr(ctx: FunctionExprContext): ExpressionResult {
visitFunctionExpr = (ctx: FunctionExprContext): ExpressionResult => {
return this.visit(ctx.functionCall())
}

visitVariableExpr(ctx: VariableExprContext): ExpressionResult {
visitVariableExpr = (ctx: VariableExprContext): ExpressionResult => {
return this.visit(ctx.variable())
}

visitNumberExpr(ctx: NumberExprContext): ExpressionResult {
return { type: "number", value: Number(ctx.NUMBER().text) }
visitNumberExpr = (ctx: NumberExprContext): ExpressionResult => {
return { type: "number", value: Number(ctx.NUMBER().getText()) }
}

visitStringExpr(ctx: StringExprContext): ExpressionResult {
return { type: "string", value: ctx.STRING().text.slice(1, -1) }
visitStringExpr = (ctx: StringExprContext): ExpressionResult => {
return { type: "string", value: ctx.STRING().getText().slice(1, -1) }
}

visitParenExpr(ctx: ParenExprContext): ExpressionResult {
visitParenExpr = (ctx: ParenExprContext): ExpressionResult => {
return this.visit(ctx.expression())
}

visitFunctionCall(ctx: FunctionCallContext): ExpressionResult {
const funcName = ctx.IDENTIFIER().text as FormulaFunction
visitFunctionCall = (ctx: FunctionCallContext): ExpressionResult => {
const funcName = ctx.IDENTIFIER().getText() as FormulaFunction
const args = ctx.argumentList() ? (this.visit(ctx.argumentList()!) as FunctionExpressionResult) : undefined

if (!globalFunctionRegistry.isValid(funcName)) {
Expand All @@ -191,20 +187,20 @@ export class FormulaVisitor
name: funcName,
arguments: args?.arguments ?? [],
returnType,
value: ctx.text,
value: ctx.getText(),
}
}

visitArgumentList(ctx: ArgumentListContext): ArgumentListResult {
const args = ctx.expression().map((expr) => this.visit(expr))
visitArgumentList = (ctx: ArgumentListContext): ArgumentListResult => {
const args = ctx.expression_list().map((expr) => this.visit(expr))
return {
type: "argumentList",
arguments: args,
}
}
visitVariable(ctx: VariableContext): VariableResult {
const variableName = ctx.IDENTIFIER().text
const raw = ctx.text
visitVariable = (ctx: VariableContext): VariableResult => {
const variableName = ctx.IDENTIFIER().getText()
const raw = ctx.getText()
this.variables.add(variableName)
return { type: "variable", value: raw, variable: variableName }
}
Expand Down
Loading

0 comments on commit 66f9ccb

Please sign in to comment.