Skip to content

Commit

Permalink
Merge branch 'main' into sql-parser
Browse files Browse the repository at this point in the history
  • Loading branch information
loicknuchel committed Oct 23, 2024
2 parents 43faa10 + b4706a6 commit fc47152
Show file tree
Hide file tree
Showing 7 changed files with 1,121 additions and 1,140 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<script src="https://cdn.jsdelivr.net/npm/@azimutt/[email protected].7/out/bundle.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@azimutt/[email protected].8/out/bundle.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@azimutt/[email protected]/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 }

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

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 }

// helpers
export type TokenInfo = TokenPosition & { issues?: TokenIssue[] }
export type TokenIssue = { message: string, kind: string, level: ParserErrorLevel }

Expand Down
Loading

0 comments on commit fc47152

Please sign in to comment.