Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions src/AST.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { JSONValue } from './JSON.type';

export interface FieldNode {
readonly type: 'Field';
readonly name: string;
}

export interface LiteralNode {
readonly type: 'Literal';
readonly value: JSONValue;
}

export interface IndexNode {
readonly type: 'Index';
readonly value: number;
}

export interface FilterProjectionNode {
readonly type: 'FilterProjection';
readonly left: ExpressionNode;
readonly right: ExpressionNode;
readonly condition: ExpressionNode;
}

export interface SliceNode {
readonly type: 'Slice';
readonly start: number | null;
readonly stop: number | null;
readonly step: number | null;
}

export type ComparatorType = 'GT' | 'LT' | 'GTE' | 'LTE' | 'NE' | 'EQ';

export interface ComparatorNode {
readonly type: 'Comparator';
readonly name: ComparatorType;
readonly left: ExpressionNode;
readonly right: ExpressionNode;
}

export interface KeyValuePairNode {
readonly type: 'KeyValuePair';
readonly name: string;
readonly value: ExpressionNode;
}

export interface MultiSelectHashNode {
type: 'MultiSelectHash';
children: KeyValuePairNode[];
}

export interface MultiSelectListNode {
type: 'MultiSelectList';
children: ExpressionNode[];
}

export interface FunctionNode {
readonly type: 'Function';
readonly name: string;
readonly children: ExpressionNode[];
}

type BinaryExpressionType =
| 'Subexpression'
| 'Pipe'
| 'ValueProjection'
| 'IndexExpression'
| 'Projection'
| 'OrExpression'
| 'AndExpression';

type UnaryExpressionType = 'NotExpression' | 'Flatten' | 'ExpressionReference';
type SimpleExpressionType = 'Identity' | 'Current' | 'Root';

export interface SimpleExpressionNode<T extends SimpleExpressionType = SimpleExpressionType> {
readonly type: T;
}
export interface UnaryExpressionNode<T extends UnaryExpressionType = UnaryExpressionType> {
readonly type: T;
readonly child: ExpressionNode;
}
export interface BinaryExpressionNode<T extends BinaryExpressionType = BinaryExpressionType> {
readonly type: T;
readonly left: ExpressionNode;
readonly right: ExpressionNode;
}

export type ExpressionNode =
| SimpleExpressionNode
| UnaryExpressionNode
| BinaryExpressionNode
| ComparatorNode
| SliceNode
| FilterProjectionNode
| IndexNode
| LiteralNode
| FieldNode
| MultiSelectHashNode
| MultiSelectListNode
| FunctionNode;

export type ExpressionReference = { expref: true } & ExpressionNode;
6 changes: 6 additions & 0 deletions src/JSON.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export type ObjectDict<T = unknown> = Record<string, T | undefined>;

export type JSONPrimitive = string | number | boolean | null;
export type JSONValue = JSONPrimitive | JSONObject | JSONArray;
export type JSONObject = { [member: string]: JSONValue };
export type JSONArray = JSONValue[];
71 changes: 3 additions & 68 deletions src/Lexer.ts
Original file line number Diff line number Diff line change
@@ -1,71 +1,6 @@
import { JSONValue } from './JSON.type';
import { LexerToken, Token } from './Lexer.type';
import { isAlpha, isNum, isAlphaNum } from './utils/index';
import type { JSONValue } from './index';

export enum Token {
TOK_EOF = 'EOF',
TOK_UNQUOTEDIDENTIFIER = 'UnquotedIdentifier',
TOK_QUOTEDIDENTIFIER = 'QuotedIdentifier',
TOK_RBRACKET = 'Rbracket',
TOK_RPAREN = 'Rparen',
TOK_COMMA = 'Comma',
TOK_COLON = 'Colon',
TOK_RBRACE = 'Rbrace',
TOK_NUMBER = 'Number',
TOK_CURRENT = 'Current',
TOK_ROOT = 'Root',
TOK_EXPREF = 'Expref',
TOK_PIPE = 'Pipe',
TOK_OR = 'Or',
TOK_AND = 'And',
TOK_EQ = 'EQ',
TOK_GT = 'GT',
TOK_LT = 'LT',
TOK_GTE = 'GTE',
TOK_LTE = 'LTE',
TOK_NE = 'NE',
TOK_FLATTEN = 'Flatten',
TOK_STAR = 'Star',
TOK_FILTER = 'Filter',
TOK_DOT = 'Dot',
TOK_NOT = 'Not',
TOK_LBRACE = 'Lbrace',
TOK_LBRACKET = 'Lbracket',
TOK_LPAREN = 'Lparen',
TOK_LITERAL = 'Literal',
}

export type LexerTokenValue = JSONValue;

export interface LexerToken {
type: Token;
value: LexerTokenValue;
start: number;
}

export interface ASTNode {
type: string;
}

export interface ValueNode<T = LexerTokenValue> extends ASTNode {
value: T;
}

export interface FieldNode extends ASTNode {
name: LexerTokenValue;
}

export type KeyValuePairNode = FieldNode & ValueNode<ExpressionNodeTree>;

export interface ExpressionNode<T = ExpressionNodeTree> extends ASTNode {
children: T[];
jmespathType?: Token;
}

export interface ComparitorNode extends ExpressionNode {
name: Token;
}

export type ExpressionNodeTree = ASTNode | ExpressionNode | FieldNode | ValueNode;

export const basicTokens: Record<string, Token> = {
'(': Token.TOK_LPAREN,
Expand Down Expand Up @@ -323,7 +258,7 @@ class StreamLexer {
try {
JSON.parse(literalString);
return true;
} catch (ex) {
} catch {
return false;
}
}
Expand Down
42 changes: 42 additions & 0 deletions src/Lexer.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { JSONValue } from './JSON.type';

export enum Token {
TOK_EOF = 'EOF',
TOK_UNQUOTEDIDENTIFIER = 'UnquotedIdentifier',
TOK_QUOTEDIDENTIFIER = 'QuotedIdentifier',
TOK_RBRACKET = 'Rbracket',
TOK_RPAREN = 'Rparen',
TOK_COMMA = 'Comma',
TOK_COLON = 'Colon',
TOK_RBRACE = 'Rbrace',
TOK_NUMBER = 'Number',
TOK_CURRENT = 'Current',
TOK_ROOT = 'Root',
TOK_EXPREF = 'Expref',
TOK_PIPE = 'Pipe',
TOK_OR = 'Or',
TOK_AND = 'And',
TOK_EQ = 'EQ',
TOK_GT = 'GT',
TOK_LT = 'LT',
TOK_GTE = 'GTE',
TOK_LTE = 'LTE',
TOK_NE = 'NE',
TOK_FLATTEN = 'Flatten',
TOK_STAR = 'Star',
TOK_FILTER = 'Filter',
TOK_DOT = 'Dot',
TOK_NOT = 'Not',
TOK_LBRACE = 'Lbrace',
TOK_LBRACKET = 'Lbracket',
TOK_LPAREN = 'Lparen',
TOK_LITERAL = 'Literal',
}

export type LexerTokenValue = JSONValue;

export interface LexerToken {
type: Token;
value: LexerTokenValue;
start: number;
}
Loading