Skip to content
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

Merged
merged 6 commits into from
Oct 22, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<script src="https://cdn.jsdelivr.net/npm/@azimutt/aml@0.1.7/out/bundle.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@azimutt/aml@0.1.8/out/bundle.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@azimutt/parser-sql@0.1.1/out/bundle.min.js"></script>
<!--<script src="/elm/aml.min.js"></script>-->
<!--<script src="/elm/sql.min.js"></script>-->
Expand Down
2 changes: 1 addition & 1 deletion libs/aml/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@azimutt/aml",
"version": "0.1.7",
"version": "0.1.8",
"description": "Parse and Generate AML: the easiest language to define database schema.",
"keywords": ["DSL", "language", "database", "parser"],
"homepage": "https://azimutt.app/aml",
Expand Down
71 changes: 39 additions & 32 deletions libs/aml/src/amlAst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Comment on lines +13 to +17
Copy link

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 in EmptyStatement for consistency.

All other statement types include & ExtraAst to incorporate additional properties like properties, doc, and comment. Including ExtraAst in EmptyStatement would ensure a consistent structure across all statement types and allow for future extensions.


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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Address the TODO: Add DateAst to AttributeValueAst.

There's a TODO comment indicating that DateAst should be added to the AttributeValueAst union type. If date values are intended to be supported, consider defining DateAst and including it to handle date attributes appropriately.

Would you like assistance in implementing DateAst and integrating it into AttributeValueAst?


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
Copy link

Choose a reason for hiding this comment

The 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 kind: string and token: TokenInfo. To enhance code maintainability and reduce repetition, consider creating a base interface:

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 }

Expand Down
Loading
Loading