-
Notifications
You must be signed in to change notification settings - Fork 102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve AML #328
Improve AML #328
Changes from all commits
5177e0e
b2f78ca
2a6342b
78a58c3
25db729
4c4406b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,52 +7,59 @@ import { | |
TokenPosition | ||
} from "@azimutt/models"; | ||
|
||
// statements | ||
export type AmlAst = StatementAst[] | ||
export type StatementAst = NamespaceStatement | EntityStatement | RelationStatement | TypeStatement | EmptyStatement | ||
export type NamespaceStatement = { statement: 'Namespace', line: number, schema?: IdentifierToken, catalog?: IdentifierToken, database?: IdentifierToken } & ExtraAst | ||
export type EntityStatement = { statement: 'Entity', name: IdentifierToken, view?: TokenInfo, alias?: IdentifierToken, attrs?: AttributeAstNested[] } & NamespaceRefAst & ExtraAst | ||
export type RelationStatement = { statement: 'Relation', src: AttributeRefCompositeAst, ref: AttributeRefCompositeAst, srcCardinality: RelationCardinality, refCardinality: RelationCardinality, polymorphic?: RelationPolymorphicAst } & ExtraAst & { warning?: TokenInfo } | ||
export type TypeStatement = { statement: 'Type', name: IdentifierToken, content?: TypeContentAst } & NamespaceRefAst & ExtraAst | ||
export type EmptyStatement = { statement: 'Empty', comment?: CommentToken } | ||
export type NamespaceStatement = { kind: 'Namespace', line: number, schema?: IdentifierAst, catalog?: IdentifierAst, database?: IdentifierAst } & ExtraAst | ||
export type EntityStatement = { kind: 'Entity', name: IdentifierAst, view?: TokenInfo, alias?: IdentifierAst, attrs?: AttributeAstNested[] } & NamespaceRefAst & ExtraAst | ||
export type RelationStatement = { kind: 'Relation', src: AttributeRefCompositeAst, srcCardinality: RelationCardinalityAst, polymorphic?: RelationPolymorphicAst, refCardinality: RelationCardinalityAst, ref: AttributeRefCompositeAst } & ExtraAst & { warning?: TokenInfo } | ||
export type TypeStatement = { kind: 'Type', name: IdentifierAst, content?: TypeContentAst } & NamespaceRefAst & ExtraAst | ||
export type EmptyStatement = { kind: 'Empty', comment?: CommentAst } | ||
|
||
export type AttributeAstFlat = { nesting: TokenInfo & {depth: number}, name: IdentifierToken, nullable?: TokenInfo } & AttributeTypeAst & AttributeConstraintsAst & { relation?: AttributeRelationAst } & ExtraAst | ||
export type AttributeAstNested = { path: IdentifierToken[], nullable?: TokenInfo } & AttributeTypeAst & AttributeConstraintsAst & { relation?: AttributeRelationAst } & ExtraAst & { attrs?: AttributeAstNested[], warning?: TokenInfo } | ||
export type AttributeTypeAst = { type?: IdentifierToken, enumValues?: AttributeValueAst[], defaultValue?: AttributeValueAst } | ||
export type AttributeConstraintsAst = { primaryKey?: AttributeConstraintAst, index?: AttributeConstraintAst, unique?: AttributeConstraintAst, check?: AttributeCheckAst } | ||
export type AttributeConstraintAst = { keyword: TokenInfo, name?: IdentifierToken } | ||
export type AttributeCheckAst = AttributeConstraintAst & { predicate?: ExpressionToken } | ||
export type AttributeRelationAst = { ref: AttributeRefCompositeAst, srcCardinality: RelationCardinality, refCardinality: RelationCardinality, polymorphic?: RelationPolymorphicAst, warning?: TokenInfo } | ||
// clauses | ||
export type AttributeAstFlat = { nesting: {token: TokenInfo, depth: number}, name: IdentifierAst, nullable?: TokenInfo } & AttributeTypeAst & { constraints?: AttributeConstraintAst[] } & ExtraAst | ||
export type AttributeAstNested = { path: IdentifierAst[], nullable?: TokenInfo } & AttributeTypeAst & { constraints?: AttributeConstraintAst[] } & ExtraAst & { attrs?: AttributeAstNested[], warning?: TokenInfo } | ||
export type AttributeTypeAst = { type?: IdentifierAst, enumValues?: AttributeValueAst[], defaultValue?: AttributeValueAst } | ||
export type AttributeConstraintAst = AttributePkAst | AttributeUniqueAst | AttributeIndexAst | AttributeCheckAst | AttributeRelationAst | ||
export type AttributePkAst = { kind: 'PrimaryKey', token: TokenInfo, name?: IdentifierAst } | ||
export type AttributeUniqueAst = { kind: 'Unique', token: TokenInfo, name?: IdentifierAst } | ||
export type AttributeIndexAst = { kind: 'Index', token: TokenInfo, name?: IdentifierAst } | ||
export type AttributeCheckAst = { kind: 'Check', token: TokenInfo, name?: IdentifierAst, predicate?: ExpressionAst } | ||
export type AttributeRelationAst = { kind: 'Relation', token: TokenInfo, srcCardinality: RelationCardinalityAst, polymorphic?: RelationPolymorphicAst, refCardinality: RelationCardinalityAst, ref: AttributeRefCompositeAst, warning?: TokenInfo } | ||
|
||
export type RelationCardinalityAst = { kind: RelationCardinality, token: TokenInfo } | ||
export type RelationPolymorphicAst = { attr: AttributePathAst, value: AttributeValueAst } | ||
|
||
export type TypeContentAst = TypeAliasAst | TypeEnumAst | TypeStructAst | TypeCustomAst | ||
export type TypeAliasAst = { kind: 'alias', name: IdentifierToken } | ||
export type TypeEnumAst = { kind: 'enum', values: AttributeValueAst[] } | ||
export type TypeStructAst = { kind: 'struct', attrs: AttributeAstNested[] } | ||
export type TypeCustomAst = { kind: 'custom', definition: ExpressionToken } | ||
export type TypeAliasAst = { kind: 'Alias', name: IdentifierAst } | ||
export type TypeEnumAst = { kind: 'Enum', values: AttributeValueAst[] } | ||
export type TypeStructAst = { kind: 'Struct', attrs: AttributeAstNested[] } | ||
export type TypeCustomAst = { kind: 'Custom', definition: ExpressionAst } | ||
|
||
export type NamespaceRefAst = { database?: IdentifierToken, catalog?: IdentifierToken, schema?: IdentifierToken } | ||
export type EntityRefAst = { entity: IdentifierToken } & NamespaceRefAst | ||
export type AttributePathAst = IdentifierToken & { path?: IdentifierToken[] } | ||
// basic parts | ||
export type NamespaceRefAst = { database?: IdentifierAst, catalog?: IdentifierAst, schema?: IdentifierAst } | ||
export type EntityRefAst = { entity: IdentifierAst } & NamespaceRefAst | ||
export type AttributePathAst = IdentifierAst & { path?: IdentifierAst[] } | ||
export type AttributeRefAst = EntityRefAst & { attr: AttributePathAst, warning?: TokenInfo } | ||
export type AttributeRefCompositeAst = EntityRefAst & { attrs: AttributePathAst[], warning?: TokenInfo } | ||
export type AttributeValueAst = NullToken | DecimalToken | IntegerToken | BooleanToken | ExpressionToken | IdentifierToken // TODO: add date | ||
export type AttributeValueAst = NullAst | DecimalAst | IntegerAst | BooleanAst | ExpressionAst | IdentifierAst // TODO: add date | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Address the TODO: Add There's a TODO comment indicating that Would you like assistance in implementing |
||
|
||
export type ExtraAst = { properties?: PropertiesAst, doc?: DocToken, comment?: CommentToken } | ||
export type ExtraAst = { properties?: PropertiesAst, doc?: DocAst, comment?: CommentAst } | ||
export type PropertiesAst = PropertyAst[] | ||
export type PropertyAst = { key: IdentifierToken, sep?: TokenInfo, value?: PropertyValueAst } | ||
export type PropertyValueAst = NullToken | DecimalToken | IntegerToken | BooleanToken | ExpressionToken | IdentifierToken | PropertyValueAst[] | ||
export type PropertyAst = { key: IdentifierAst, sep?: TokenInfo, value?: PropertyValueAst } | ||
export type PropertyValueAst = NullAst | DecimalAst | IntegerAst | BooleanAst | ExpressionAst | IdentifierAst | PropertyValueAst[] | ||
export type DocAst = { kind: 'Doc', token: TokenInfo, value: string, multiLine?: boolean } | ||
|
||
// basic tokens | ||
export type NullToken = { token: 'Null' } & TokenInfo | ||
export type DecimalToken = { token: 'Decimal', value: number } & TokenInfo | ||
export type IntegerToken = { token: 'Integer', value: number } & TokenInfo | ||
export type BooleanToken = { token: 'Boolean', value: boolean } & TokenInfo | ||
export type ExpressionToken = { token: 'Expression', value: string } & TokenInfo | ||
export type IdentifierToken = { token: 'Identifier', value: string } & TokenInfo | ||
export type DocToken = { token: 'Doc', value: string } & TokenPosition | ||
export type CommentToken = { token: 'Comment', value: string } & TokenPosition | ||
// elements | ||
export type ExpressionAst = { kind: 'Expression', token: TokenInfo, value: string } | ||
export type IdentifierAst = { kind: 'Identifier', token: TokenInfo, value: string, quoted?: boolean } | ||
export type IntegerAst = { kind: 'Integer', token: TokenInfo, value: number } | ||
export type DecimalAst = { kind: 'Decimal', token: TokenInfo, value: number } | ||
export type BooleanAst = { kind: 'Boolean', token: TokenInfo, value: boolean } | ||
export type NullAst = { kind: 'Null', token: TokenInfo } | ||
export type CommentAst = { kind: 'Comment', token: TokenInfo, value: string } | ||
Comment on lines
+54
to
+60
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Consider introducing a base interface for AST nodes to reduce duplication. Currently, each AST node type includes interface BaseAst {
kind: string;
token: TokenInfo;
} Then, extend this interface in your AST node types: export type ExpressionAst = BaseAst & { value: string };
export type IdentifierAst = BaseAst & { value: string; quoted?: boolean };
export type IntegerAst = BaseAst & { value: number };
// ...and so on for other AST node types. This approach ensures consistency across your AST nodes and facilitates future extensions. |
||
|
||
// helpers | ||
export type TokenInfo = TokenPosition & { issues?: TokenIssue[] } | ||
export type TokenIssue = { message: string, kind: string, level: ParserErrorLevel } | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Consider including
ExtraAst
inEmptyStatement
for consistency.All other statement types include
& ExtraAst
to incorporate additional properties likeproperties
,doc
, andcomment
. IncludingExtraAst
inEmptyStatement
would ensure a consistent structure across all statement types and allow for future extensions.