|
| 1 | +package ast |
| 2 | + |
| 3 | +// Stmt is an interface for all statement types |
| 4 | +type Stmt interface { |
| 5 | + Type() string |
| 6 | +} |
| 7 | + |
| 8 | +// BaseStmt is a struct that all statement types can embed to implement the Stmt interface |
| 9 | +type BaseStmt struct { |
| 10 | + StmtType string `json:"type"` |
| 11 | +} |
| 12 | + |
| 13 | +func (b BaseStmt) Type() string { |
| 14 | + return b.StmtType |
| 15 | +} |
| 16 | + |
| 17 | +// Define all the statement types |
| 18 | +type ( |
| 19 | + // TypeAliasStmt represents a type alias statement, e.g. |
| 20 | + // |
| 21 | + // type StrOrInt = str | int |
| 22 | + TypeAliasStmt struct { |
| 23 | + BaseStmt |
| 24 | + TypeName *Node[Identifier] `json:"type_name"` |
| 25 | + TypeValue *Node[string] `json:"type_value"` |
| 26 | + Ty *Node[Type] `json:"ty"` |
| 27 | + } |
| 28 | + // ExprStmt represents a expression statement, e.g. |
| 29 | + // |
| 30 | + // 1 |
| 31 | + // |
| 32 | + // """A long string""" |
| 33 | + // |
| 34 | + // 'A string' |
| 35 | + ExprStmt struct { |
| 36 | + BaseStmt |
| 37 | + Exprs []*Node[Expr] `json:"exprs"` |
| 38 | + } |
| 39 | + // UnificationStmt represents a declare statement with the union operator, e.g. |
| 40 | + // |
| 41 | + // data: ASchema {} |
| 42 | + UnificationStmt struct { |
| 43 | + BaseStmt |
| 44 | + Target *Node[Identifier] `json:"target"` |
| 45 | + Value *Node[SchemaConfig] `json:"value"` |
| 46 | + } |
| 47 | + // AssignStmt represents an assignment, e.g. |
| 48 | + // |
| 49 | + // a: int = 1 |
| 50 | + // |
| 51 | + // a = 1 |
| 52 | + // |
| 53 | + // a = b = 1 |
| 54 | + AssignStmt struct { |
| 55 | + BaseStmt |
| 56 | + Targets []*Node[Target] `json:"targets"` |
| 57 | + Value *Node[Expr] `json:"value"` |
| 58 | + Ty *Node[Type] `json:"ty"` |
| 59 | + } |
| 60 | + // AugAssignStmt represents an augmented assignment, e.g. |
| 61 | + // |
| 62 | + // a += 1 |
| 63 | + // |
| 64 | + // a -= 1 |
| 65 | + AugAssignStmt struct { |
| 66 | + BaseStmt |
| 67 | + Target *Node[Target] `json:"target"` |
| 68 | + Value *Node[Expr] `json:"value"` |
| 69 | + Op AugOp `json:"op"` |
| 70 | + } |
| 71 | + // AssertStmt represents an assert statement, e.g. |
| 72 | + // |
| 73 | + // assert True if condition, "Assert failed message" |
| 74 | + AssertStmt struct { |
| 75 | + BaseStmt |
| 76 | + Test *Node[Expr] `json:"test"` |
| 77 | + IfCond *Node[Expr] `json:"if_cond,omitempty"` |
| 78 | + Msg *Node[Expr] `json:"msg,omitempty"` |
| 79 | + } |
| 80 | + // IfStmt represents an if statement, e.g. |
| 81 | + // |
| 82 | + // if condition1: |
| 83 | + // |
| 84 | + // if condition2: |
| 85 | + // a = 1 |
| 86 | + // |
| 87 | + // elif condition3: |
| 88 | + // |
| 89 | + // b = 2 |
| 90 | + // |
| 91 | + // else: |
| 92 | + // |
| 93 | + // c = 3 |
| 94 | + IfStmt struct { |
| 95 | + BaseStmt |
| 96 | + Body []*Node[Stmt] `json:"body"` |
| 97 | + Cond *Node[Expr] `json:"cond"` |
| 98 | + Orelse []*Node[Stmt] `json:"orelse,omitempty"` |
| 99 | + } |
| 100 | + // ImportStmt represents an import statement, e.g. |
| 101 | + // |
| 102 | + // import pkg as pkg_alias |
| 103 | + ImportStmt struct { |
| 104 | + BaseStmt |
| 105 | + Path *Node[string] `json:"path"` |
| 106 | + Rawpath string `json:"rawpath"` |
| 107 | + Name string `json:"name"` |
| 108 | + Asname *Node[string] `json:"asname,omitempty"` |
| 109 | + PkgName string `json:"pkg_name"` |
| 110 | + } |
| 111 | + // SchemaAttr represents schema attribute definitions, e.g. |
| 112 | + // |
| 113 | + // schema SchemaAttrExample: |
| 114 | + // |
| 115 | + // x: int |
| 116 | + // y: str |
| 117 | + SchemaAttr struct { |
| 118 | + BaseStmt |
| 119 | + Doc string `json:"doc,omitempty"` |
| 120 | + Name *Node[string] `json:"name"` |
| 121 | + Op AugOp `json:"op,omitempty"` |
| 122 | + Value *Node[Expr] `json:"value,omitempty"` |
| 123 | + IsOptional bool `json:"is_optional"` |
| 124 | + Decorators []*Node[Decorator] `json:"decorators,omitempty"` |
| 125 | + Ty *Node[Type] `json:"ty,omitempty"` |
| 126 | + } |
| 127 | + // SchemaStmt represents a schema statement, e.g. |
| 128 | + // |
| 129 | + // schema BaseSchema: |
| 130 | + // |
| 131 | + // schema SchemaExample(BaseSchema)[arg: str]: |
| 132 | + // |
| 133 | + // """Schema documents""" |
| 134 | + // attr?: str = arg |
| 135 | + // check: |
| 136 | + // len(attr) > 3 if attr, "Check failed message" |
| 137 | + // |
| 138 | + // mixin MixinExample for ProtocolExample: |
| 139 | + // |
| 140 | + // attr: int |
| 141 | + // |
| 142 | + // protocol ProtocolExample: |
| 143 | + // |
| 144 | + // attr: int |
| 145 | + SchemaStmt struct { |
| 146 | + BaseStmt |
| 147 | + Doc *Node[string] `json:"doc,omitempty"` |
| 148 | + Name *Node[string] `json:"name"` |
| 149 | + ParentName *Node[Identifier] `json:"parent_name,omitempty"` |
| 150 | + ForHostName *Node[Identifier] `json:"for_host_name,omitempty"` |
| 151 | + IsMixin bool `json:"is_mixin"` |
| 152 | + IsProtocol bool `json:"is_protocol"` |
| 153 | + Args *Node[Arguments] `json:"args,omitempty"` |
| 154 | + Mixins []*Node[Identifier] `json:"mixins,omitempty"` |
| 155 | + Body []*Node[Stmt] `json:"body,omitempty"` |
| 156 | + Decorators []*Node[Decorator] `json:"decorators,omitempty"` |
| 157 | + Checks []*Node[CheckExpr] `json:"checks,omitempty"` |
| 158 | + IndexSignature *Node[SchemaIndexSignature] `json:"index_signature,omitempty"` |
| 159 | + } |
| 160 | + // RuleStmt represents a rule statement, e.g. |
| 161 | + // |
| 162 | + // rule RuleExample: |
| 163 | + // |
| 164 | + // a > 1 |
| 165 | + // b < 0 |
| 166 | + RuleStmt struct { |
| 167 | + BaseStmt |
| 168 | + Doc *Node[string] `json:"doc,omitempty"` |
| 169 | + Name *Node[string] `json:"name"` |
| 170 | + ParentRules []*Node[Identifier] `json:"parent_rules,omitempty"` |
| 171 | + Decorators []*Node[Decorator] `json:"decorators,omitempty"` |
| 172 | + Checks []*Node[CheckExpr] `json:"checks,omitempty"` |
| 173 | + Args *Node[Arguments] `json:"args,omitempty"` |
| 174 | + ForHostName *Node[Identifier] `json:"for_host_name,omitempty"` |
| 175 | + } |
| 176 | +) |
| 177 | + |
| 178 | +// NewTypeAliasStmt creates a new TypeAliasStmt |
| 179 | +func NewTypeAliasStmt() *TypeAliasStmt { |
| 180 | + return &TypeAliasStmt{ |
| 181 | + BaseStmt: BaseStmt{StmtType: "TypeAlias"}, |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +// NewExprStmt creates a new ExprStmt |
| 186 | +func NewExprStmt() *ExprStmt { |
| 187 | + return &ExprStmt{ |
| 188 | + BaseStmt: BaseStmt{StmtType: "Expr"}, |
| 189 | + Exprs: make([]*Node[Expr], 0), |
| 190 | + } |
| 191 | +} |
| 192 | + |
| 193 | +// NewUnificationStmt creates a new UnificationStmt |
| 194 | +func NewUnificationStmt() *UnificationStmt { |
| 195 | + return &UnificationStmt{ |
| 196 | + BaseStmt: BaseStmt{StmtType: "Unification"}, |
| 197 | + } |
| 198 | +} |
| 199 | + |
| 200 | +// NewAssignStmt creates a new AssignStmt |
| 201 | +func NewAssignStmt() *AssignStmt { |
| 202 | + return &AssignStmt{ |
| 203 | + BaseStmt: BaseStmt{StmtType: "Assign"}, |
| 204 | + } |
| 205 | +} |
| 206 | + |
| 207 | +// NewAugAssignStmt creates a new AugAssignStmt |
| 208 | +func NewAugAssignStmt() *AugAssignStmt { |
| 209 | + return &AugAssignStmt{ |
| 210 | + BaseStmt: BaseStmt{StmtType: "AugAssign"}, |
| 211 | + } |
| 212 | +} |
| 213 | + |
| 214 | +// NewAssertStmt creates a new AssertStmt |
| 215 | +func NewAssertStmt() *AssertStmt { |
| 216 | + return &AssertStmt{ |
| 217 | + BaseStmt: BaseStmt{StmtType: "Assert"}, |
| 218 | + } |
| 219 | +} |
| 220 | + |
| 221 | +// NewIfStmt creates a new IfStmt |
| 222 | +func NewIfStmt() *IfStmt { |
| 223 | + return &IfStmt{ |
| 224 | + BaseStmt: BaseStmt{StmtType: "If"}, |
| 225 | + Body: make([]*Node[Stmt], 0), |
| 226 | + Orelse: make([]*Node[Stmt], 0), |
| 227 | + } |
| 228 | +} |
| 229 | + |
| 230 | +// NewImportStmt creates a new ImportStmt |
| 231 | +func NewImportStmt() *ImportStmt { |
| 232 | + return &ImportStmt{ |
| 233 | + BaseStmt: BaseStmt{StmtType: "Import"}, |
| 234 | + } |
| 235 | +} |
| 236 | + |
| 237 | +// NewSchemaAttr creates a new SchemaAttr |
| 238 | +func NewSchemaAttr() *SchemaAttr { |
| 239 | + return &SchemaAttr{ |
| 240 | + BaseStmt: BaseStmt{StmtType: "SchemaAttr"}, |
| 241 | + Decorators: make([]*Node[Decorator], 0), |
| 242 | + } |
| 243 | +} |
| 244 | + |
| 245 | +// NewSchemaStmt creates a new SchemaStmt |
| 246 | +func NewSchemaStmt() *SchemaStmt { |
| 247 | + return &SchemaStmt{ |
| 248 | + BaseStmt: BaseStmt{StmtType: "Schema"}, |
| 249 | + Mixins: make([]*Node[Identifier], 0), |
| 250 | + Body: make([]*Node[Stmt], 0), |
| 251 | + Decorators: make([]*Node[Decorator], 0), |
| 252 | + Checks: make([]*Node[CheckExpr], 0), |
| 253 | + } |
| 254 | +} |
| 255 | + |
| 256 | +// NewRuleStmt creates a new RuleStmt |
| 257 | +func NewRuleStmt() *RuleStmt { |
| 258 | + return &RuleStmt{ |
| 259 | + BaseStmt: BaseStmt{StmtType: "Rule"}, |
| 260 | + ParentRules: make([]*Node[Identifier], 0), |
| 261 | + Decorators: make([]*Node[Decorator], 0), |
| 262 | + Checks: make([]*Node[CheckExpr], 0), |
| 263 | + } |
| 264 | +} |
| 265 | + |
| 266 | +// SchemaIndexSignature represents a schema index signature, e.g. |
| 267 | +// |
| 268 | +// schema SchemaIndexSignatureExample: |
| 269 | +// |
| 270 | +// [str]: int |
| 271 | +type SchemaIndexSignature struct { |
| 272 | + KeyName *Node[string] `json:"key_name,omitempty"` |
| 273 | + Value *Node[Expr] `json:"value,omitempty"` |
| 274 | + AnyOther bool `json:"any_other"` |
| 275 | + KeyTy *Node[Type] `json:"key_ty,omitempty"` |
| 276 | + ValueTy *Node[Type] `json:"value_ty,omitempty"` |
| 277 | +} |
| 278 | + |
| 279 | +// NewSchemaIndexSignature creates a new SchemaIndexSignature |
| 280 | +func NewSchemaIndexSignature() *SchemaIndexSignature { |
| 281 | + return &SchemaIndexSignature{} |
| 282 | +} |
0 commit comments