|
| 1 | +import { Token } from "./tokenizer"; |
| 2 | +import { Signature } from './signatures'; |
| 3 | + |
| 4 | +// Potential AST changes |
| 5 | +// |
| 6 | +// - Make predicate and group into AST nodes instead of optional fields on every node. |
| 7 | +// - Change unary operator "[" to a different type...? |
| 8 | +// - Get errors? off of BaseNode |
| 9 | +// - Rationalize unary nodes |
| 10 | + |
| 11 | +export interface BaseNode { |
| 12 | + type: string; |
| 13 | + value: any; |
| 14 | + position: number; |
| 15 | + // This gets added to nodes to indicate how a value (assuming it is an object) |
| 16 | + // should be grouped. |
| 17 | + // TODO: Rename lhs...? |
| 18 | + group?: { lhs: ASTNode[][], position: number }; |
| 19 | + // This gets added to nodes to specify a list of predicates to filter on. |
| 20 | + predicate?: ASTNode[]; |
| 21 | +} |
| 22 | + |
| 23 | +export interface WildcardNode extends BaseNode { |
| 24 | + type: "wildcard"; |
| 25 | +} |
| 26 | + |
| 27 | +export interface DescendantNode extends BaseNode { |
| 28 | + type: "descendant"; |
| 29 | +} |
| 30 | + |
| 31 | +export interface ErrorFields { |
| 32 | + code: string; |
| 33 | + position?: number; |
| 34 | + token?: string; |
| 35 | + stack?: string; |
| 36 | +} |
| 37 | + |
| 38 | +export interface ErrorNode extends BaseNode { |
| 39 | + type: "error"; |
| 40 | + error: ErrorFields; |
| 41 | + lhs: ASTNode; |
| 42 | + remaining: Token[]; |
| 43 | +} |
| 44 | + |
| 45 | +export interface VariableNode extends BaseNode { |
| 46 | + type: "variable"; |
| 47 | +} |
| 48 | + |
| 49 | +export interface NameNode extends BaseNode { |
| 50 | + type: "name"; |
| 51 | +} |
| 52 | +export interface LiteralNode extends BaseNode { |
| 53 | + type: "literal"; |
| 54 | +} |
| 55 | + |
| 56 | +export interface RegexNode extends BaseNode { |
| 57 | + type: "regex"; |
| 58 | +} |
| 59 | + |
| 60 | +export interface OperatorNode extends BaseNode { |
| 61 | + type: "operator"; |
| 62 | +} |
| 63 | + |
| 64 | +export interface EndNode extends BaseNode { |
| 65 | + type: "end"; |
| 66 | + value: string; |
| 67 | +} |
| 68 | + |
| 69 | +export type TerminalNode = VariableNode | NameNode | LiteralNode | RegexNode | OperatorNode | EndNode; |
| 70 | + |
| 71 | +export interface UnaryMinusNode extends BaseNode { |
| 72 | + type: "unary"; |
| 73 | + value: "-"; |
| 74 | + expression: ASTNode; |
| 75 | +} |
| 76 | + |
| 77 | +export interface ArrayConstructorNode extends BaseNode { |
| 78 | + type: "unary"; |
| 79 | + value: "["; |
| 80 | + expressions: ASTNode[]; |
| 81 | + consarray: boolean; |
| 82 | +} |
| 83 | + |
| 84 | +export interface UnaryObjectNode extends BaseNode { |
| 85 | + type: "unary"; |
| 86 | + value: "{"; |
| 87 | + lhs: ASTNode[][]; |
| 88 | +} |
| 89 | + |
| 90 | +export type UnaryNode = UnaryMinusNode | ArrayConstructorNode | UnaryObjectNode; |
| 91 | + |
| 92 | +export interface BinaryOperationNode extends BaseNode { |
| 93 | + type: "binary"; |
| 94 | + value: "+" | "-" | "*" | "/" | "[" | ".." | "." | "[" | ":=" | "~>"; // TODO: There must be more?!? (e.g., comparisons) |
| 95 | + lhs: ASTNode; |
| 96 | + rhs: ASTNode; // ASTNode if operator is "." | "[" | ":=" | "~>" |
| 97 | +} |
| 98 | + |
| 99 | +export interface BinaryObjectNode extends BaseNode { |
| 100 | + type: "binary"; |
| 101 | + value: "{"; |
| 102 | + lhs: ASTNode; |
| 103 | + rhs: ASTNode[]; // ASTNode[] if operator is "{" |
| 104 | +} |
| 105 | + |
| 106 | +export type BinaryNode = BinaryOperationNode | BinaryObjectNode; |
| 107 | + |
| 108 | +export interface SortTerm { |
| 109 | + descending: boolean; |
| 110 | + expression: ASTNode; |
| 111 | +} |
| 112 | + |
| 113 | +export interface SortNode extends BaseNode { |
| 114 | + type: "sort"; |
| 115 | + lhs: ASTNode; |
| 116 | + rhs: SortTerm[]; |
| 117 | +} |
| 118 | + |
| 119 | +export interface TernaryNode extends BaseNode { |
| 120 | + type: "condition"; |
| 121 | + condition: ASTNode; |
| 122 | + then: ASTNode; |
| 123 | + else: ASTNode; |
| 124 | + position: number; |
| 125 | +} |
| 126 | + |
| 127 | +export interface BlockNode extends BaseNode { |
| 128 | + type: "block"; |
| 129 | + expressions: ASTNode[]; |
| 130 | +} |
| 131 | + |
| 132 | +export interface TransformNode extends BaseNode { |
| 133 | + type: "transform"; |
| 134 | + pattern: ASTNode; |
| 135 | + update: ASTNode; |
| 136 | + delete?: ASTNode; |
| 137 | +} |
| 138 | + |
| 139 | +export interface FunctionInvocationNode extends BaseNode { |
| 140 | + type: "function" | "partial"; |
| 141 | + procedure: ASTNode; |
| 142 | + arguments: ASTNode[]; |
| 143 | + // This is added when creating PathNodes. |
| 144 | + nextFunction?: Function; |
| 145 | +} |
| 146 | + |
| 147 | +export interface LambdaDefinitionNode extends BaseNode { |
| 148 | + type: "lambda"; |
| 149 | + body: ASTNode; |
| 150 | + signature: Signature; |
| 151 | + arguments: ASTNode[]; |
| 152 | + thunk: boolean; |
| 153 | +} |
| 154 | + |
| 155 | +export interface SingletonArrayDecorator extends BaseNode { |
| 156 | + type: "singleton"; |
| 157 | + next: ASTNode; |
| 158 | +} |
| 159 | + |
| 160 | +// This type of node only appears after the AST is optimized |
| 161 | +export interface PathNode extends BaseNode { |
| 162 | + type: "path"; |
| 163 | + steps: ASTNode[]; |
| 164 | + keepSingletonArray: boolean, |
| 165 | +} |
| 166 | + |
| 167 | +export interface BindNode extends BaseNode { |
| 168 | + type: "bind"; |
| 169 | + lhs: ASTNode; |
| 170 | + rhs: ASTNode; |
| 171 | +} |
| 172 | + |
| 173 | +export interface ApplyNode extends BaseNode { |
| 174 | + type: "apply"; |
| 175 | + lhs: ASTNode; |
| 176 | + rhs: ASTNode; |
| 177 | +} |
| 178 | + |
| 179 | +/** |
| 180 | + * These are the AST nodes that come directly out of the parser before |
| 181 | + * ast_optimize is called. |
| 182 | + */ |
| 183 | +export type ASTNode = |
| 184 | + | WildcardNode |
| 185 | + | DescendantNode |
| 186 | + | ErrorNode |
| 187 | + | LiteralNode |
| 188 | + | NameNode |
| 189 | + | VariableNode |
| 190 | + | RegexNode |
| 191 | + | OperatorNode |
| 192 | + | UnaryNode |
| 193 | + | BinaryNode |
| 194 | + | BinaryObjectNode |
| 195 | + | SortNode |
| 196 | + | TernaryNode |
| 197 | + | BlockNode |
| 198 | + | TransformNode |
| 199 | + | FunctionInvocationNode |
| 200 | + | LambdaDefinitionNode |
| 201 | + | PathNode |
| 202 | + | BindNode |
| 203 | + | ApplyNode |
| 204 | + | EndNode |
| 205 | + | SingletonArrayDecorator; |
| 206 | + |
0 commit comments