Skip to content

Commit

Permalink
fix formatting, regenerate definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
ChromeGG committed Mar 14, 2024
1 parent 8612fa4 commit cb247d7
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 33 deletions.
3 changes: 2 additions & 1 deletion src/cst-definitions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,9 @@ export interface ICstNodeVisitor<IN, OUT> extends ICstVisitor<IN, OUT> {
multiplication(children: MultiplicationCstChildren, param?: IN): OUT;
unaryExpression(children: UnaryExpressionCstChildren, param?: IN): OUT;
parenthesisExpression(children: ParenthesisExpressionCstChildren, param?: IN): OUT;
mapKeyValues(children: MapKeyValuesCstChildren, param?: IN): OUT;
listExpression(children: ListExpressionCstChildren, param?: IN): OUT;
mapExpression(children: MapExpressionCstChildren, param?: IN): OUT;
mapKeyValues(children: MapKeyValuesCstChildren, param?: IN): OUT;
macrosExpression(children: MacrosExpressionCstChildren, param?: IN): OUT;
identifierExpression(children: IdentifierExpressionCstChildren, param?: IN): OUT;
identifierDotExpression(children: IdentifierDotExpressionCstChildren, param?: IN): OUT;
Expand Down
5 changes: 2 additions & 3 deletions src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,10 @@ const comparisonEqualForMap = (left: object, right: object): boolean => {
}

const comparisonInOperation = (left: unknown, right: unknown) => {
if(isArray(right)) {
if (isArray(right)) {
return right.includes(left)
}
if(isMap(right)) {
if (isMap(right)) {
return Object.keys(right).includes(left as string)
}
throw new CelTypeError(Operations.in, left, right)
Expand Down Expand Up @@ -259,7 +259,6 @@ const comparisonOperation = (
throw new CelTypeError(operation, left, right)
}


export const getResult = (operator: IToken, left: unknown, right: unknown) => {
switch (true) {
case tokenMatcher(operator, Plus):
Expand Down
29 changes: 18 additions & 11 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,12 @@ export class CelParser extends CstParser {
this.MANY2(() => {
this.OR([
{ ALT: () => this.SUBRULE(this.identifierDotExpression) },
{ ALT: () => this.SUBRULE(this.indexExpression, {LABEL: 'identifierIndexExpression'}) },
{
ALT: () =>
this.SUBRULE(this.indexExpression, {
LABEL: 'identifierIndexExpression',
}),
},
])
})
})
Expand Down Expand Up @@ -141,24 +146,26 @@ export class CelParser extends CstParser {
this.MANY(() => {
this.OR([
{ ALT: () => this.SUBRULE(this.identifierDotExpression) },
{ ALT: () => this.SUBRULE(this.indexExpression, {LABEL: 'identifierIndexExpression'}) },
{
ALT: () =>
this.SUBRULE(this.indexExpression, {
LABEL: 'identifierIndexExpression',
}),
},
])
})
})
})

private identifierDotExpression = this.RULE('identifierDotExpression', () => {
this.CONSUME(Dot)
this.CONSUME(Identifier)
})

private indexExpression = this.RULE(
'indexExpression',
() => {
this.CONSUME(OpenBracket)
this.SUBRULE(this.expr)
this.CONSUME(CloseBracket)
}
)
private indexExpression = this.RULE('indexExpression', () => {
this.CONSUME(OpenBracket)
this.SUBRULE(this.expr)
this.CONSUME(CloseBracket)
})

private atomicExpression = this.RULE('atomicExpression', () => {
this.OR([
Expand Down
14 changes: 10 additions & 4 deletions src/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,15 @@ export const OpenBracket = createToken({ name: 'OpenBracket', pattern: /\[/ })

export const CloseBracket = createToken({ name: 'CloseBracket', pattern: /\]/ })

export const OpenCurlyBracket = createToken({ name: 'OpenCurlyBracket', pattern: /{/ })
export const OpenCurlyBracket = createToken({
name: 'OpenCurlyBracket',
pattern: /{/,
})

export const CloseCurlyBracket = createToken({ name: 'CloseCurlyBracket', pattern: /}/ })
export const CloseCurlyBracket = createToken({
name: 'CloseCurlyBracket',
pattern: /}/,
})

export const Dot = createToken({ name: 'Dot', pattern: /\./ })

Expand Down Expand Up @@ -198,13 +204,13 @@ export const Minus = createToken({

export const MacrosIdentifier = createToken({
name: 'MacrosIdentifier',
pattern: Lexer.NA
pattern: Lexer.NA,
})

export const Size = createToken({
name: 'Size',
pattern: /size/,
categories: MacrosIdentifier
categories: MacrosIdentifier,
})

export const Identifier = createToken({
Expand Down
33 changes: 19 additions & 14 deletions src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@ import {
MapExpressionCstChildren,
} from './cst-definitions.js'

import { CelType, getCelType, getPosition, getResult, getUnaryResult } from './helper.js'
import { CelEvaluationError } from './index.js'
import {
CelType,
getCelType,
getPosition,
getResult,
getUnaryResult,
} from './helper.js'
import { CelEvaluationError } from './index.js'

const parserInstance = new CelParser()

Expand All @@ -38,7 +44,7 @@ const size = (arr: unknown) => {
return Object.keys(arr).length
default:
throw new CelEvaluationError(`invalid_argument: ${arr}`)
}
}
}

export class CelVisitor
Expand Down Expand Up @@ -162,14 +168,14 @@ export class CelVisitor
}
}

if(!ctx.Index) {
if (!ctx.Index) {
return result
}

const index = this.visit(ctx.Index)

const indexType = getCelType(index)
if (indexType != CelType.int && indexType != CelType.uint ) {
if (indexType != CelType.int && indexType != CelType.uint) {
throw new CelEvaluationError(`invalid_argument: ${index}`)
}

Expand All @@ -181,7 +187,7 @@ export class CelVisitor
}

mapExpression(ctx: MapExpressionCstChildren) {
const mapExpression: {[key: string]: unknown} = {}
const mapExpression: { [key: string]: unknown } = {}
if (!ctx.keyValues) {
return {}
}
Expand All @@ -200,14 +206,17 @@ export class CelVisitor
mapExpression[key] = value
}

if(!ctx.identifierDotExpression && !ctx.identifierIndexExpression) {
if (!ctx.identifierDotExpression && !ctx.identifierIndexExpression) {
return mapExpression
}

return this.getIndexSection(ctx, mapExpression)
}

private getIndexSection(ctx: MapExpressionCstChildren | IdentifierExpressionCstChildren, mapExpression: unknown) {
private getIndexSection(
ctx: MapExpressionCstChildren | IdentifierExpressionCstChildren,
mapExpression: unknown
) {
const expressions = [
...(ctx.identifierDotExpression || []),
...(ctx.identifierIndexExpression || []),
Expand All @@ -233,7 +242,7 @@ export class CelVisitor
const macrosIdentifier = ctx.MacrosIdentifier[0]
// eslint-disable-next-line sonarjs/no-small-switch
switch (macrosIdentifier.image) {
case 'size':
case 'size': // todo type it
return ctx.arg ? size(this.visit(ctx.arg)) : 0
default:
throw new Error(`Macros ${macrosIdentifier.image} not recognized`)
Expand Down Expand Up @@ -272,7 +281,6 @@ export class CelVisitor

if (ctx.identifierExpression) {
return this.visit(ctx.identifierExpression)
// return this.identifier(ctx)
}

if (ctx.listExpression) {
Expand All @@ -294,7 +302,6 @@ export class CelVisitor
const data = this.context
const result = this.getIdentifier(data, ctx.Identifier[0].image)


if (!ctx.identifierDotExpression && !ctx.identifierIndexExpression) {
return result
}
Expand All @@ -310,9 +317,7 @@ export class CelVisitor
return this.getIdentifier(param, identifierName)
}

indexExpression(
ctx: IndexExpressionCstChildren
): unknown {
indexExpression(ctx: IndexExpressionCstChildren): unknown {
return this.visit(ctx.expr)
}

Expand Down

0 comments on commit cb247d7

Please sign in to comment.