331 lines
11 KiB
Go
331 lines
11 KiB
Go
package lang
|
|
|
|
import (
|
|
"github.com/alecthomas/participle/v2"
|
|
)
|
|
|
|
// Root AST node containing all definitions
|
|
type AST struct {
|
|
Definitions []Definition `parser:"@@*"`
|
|
}
|
|
|
|
// Union type for top-level definitions
|
|
type Definition struct {
|
|
Server *Server `parser:"@@"`
|
|
Entity *Entity `parser:"| @@"`
|
|
Endpoint *Endpoint `parser:"| @@"`
|
|
Page *Page `parser:"| @@"`
|
|
}
|
|
|
|
// Clean server syntax
|
|
type Server struct {
|
|
Name string `parser:"'server' @Ident"`
|
|
Settings []ServerSetting `parser:"@@*"`
|
|
}
|
|
|
|
type ServerSetting struct {
|
|
Host *string `parser:"('host' @String)"`
|
|
Port *int `parser:"| ('port' @Int)"`
|
|
}
|
|
|
|
// Clean entity syntax with better readability
|
|
type Entity struct {
|
|
Name string `parser:"'entity' @Ident"`
|
|
Description *string `parser:"('desc' @String)?"`
|
|
Fields []Field `parser:"@@*"`
|
|
}
|
|
|
|
// Much cleaner field syntax
|
|
type Field struct {
|
|
Name string `parser:"@Ident ':'"`
|
|
Type string `parser:"@Ident"`
|
|
Required bool `parser:"@'required'?"`
|
|
Unique bool `parser:"@'unique'?"`
|
|
Index bool `parser:"@'indexed'?"`
|
|
Default *string `parser:"('default' @String)?"`
|
|
Validations []Validation `parser:"@@*"`
|
|
Relationship *Relationship `parser:"@@?"`
|
|
}
|
|
|
|
// Simple validation syntax
|
|
type Validation struct {
|
|
Type string `parser:"'validate' @Ident"`
|
|
Value *string `parser:"@String?"`
|
|
}
|
|
|
|
// Clear relationship syntax
|
|
type Relationship struct {
|
|
Type string `parser:"'relates' 'to' @Ident"`
|
|
Cardinality string `parser:"'as' @('one' | 'many')"`
|
|
ForeignKey *string `parser:"('via' @String)?"`
|
|
Through *string `parser:"('through' @String)?"`
|
|
}
|
|
|
|
// Endpoint definitions with clean, readable syntax
|
|
type Endpoint struct {
|
|
Method string `parser:"'endpoint' @('GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH')"`
|
|
Path string `parser:"@String"`
|
|
Entity *string `parser:"('for' @Ident)?"`
|
|
Description *string `parser:"('desc' @String)?"`
|
|
Auth bool `parser:"@'auth'?"`
|
|
Params []EndpointParam `parser:"@@*"`
|
|
Response *ResponseSpec `parser:"@@?"`
|
|
CustomLogic *string `parser:"('custom' @String)?"`
|
|
}
|
|
|
|
// Clean parameter syntax
|
|
type EndpointParam struct {
|
|
Name string `parser:"'param' @Ident ':'"`
|
|
Type string `parser:"@Ident"`
|
|
Required bool `parser:"@'required'?"`
|
|
Source string `parser:"'from' @('path' | 'query' | 'body')"`
|
|
}
|
|
|
|
// Response specification
|
|
type ResponseSpec struct {
|
|
Type string `parser:"'returns' @Ident"`
|
|
Format *string `parser:"('as' @String)?"`
|
|
Fields []string `parser:"('fields' '[' @Ident (',' @Ident)* ']')?"`
|
|
}
|
|
|
|
// Enhanced Page definitions with layout containers and composition
|
|
type Page struct {
|
|
Name string `parser:"'page' @Ident"`
|
|
Path string `parser:"'at' @String"`
|
|
Layout string `parser:"'layout' @Ident"`
|
|
Title *string `parser:"('title' @String)?"`
|
|
Description *string `parser:"('desc' @String)?"`
|
|
Auth bool `parser:"@'auth'?"`
|
|
LayoutType *string `parser:"('layout' @String)?"`
|
|
Meta []MetaTag `parser:"@@*"`
|
|
MasterDetail *MasterDetail `parser:"@@?"`
|
|
Containers []Container `parser:"@@*"`
|
|
Components []Component `parser:"@@*"`
|
|
Modals []Modal `parser:"@@*"`
|
|
}
|
|
|
|
// Meta tags for SEO
|
|
type MetaTag struct {
|
|
Name string `parser:"'meta' @Ident"`
|
|
Content string `parser:"@String"`
|
|
}
|
|
|
|
// Container types for layout organization
|
|
type Container struct {
|
|
Type string `parser:"'container' @Ident"`
|
|
Class *string `parser:"('class' @String)?"`
|
|
Sections []Section `parser:"@@*"`
|
|
Tabs []Tab `parser:"@@*"`
|
|
Components []Component `parser:"@@*"`
|
|
}
|
|
|
|
// Sections within containers
|
|
type Section struct {
|
|
Name string `parser:"'section' @Ident"`
|
|
Class *string `parser:"('class' @String)?"`
|
|
Components []Component `parser:"@@*"`
|
|
Panels []Panel `parser:"@@*"`
|
|
}
|
|
|
|
// Tab definitions for tabbed interfaces
|
|
type Tab struct {
|
|
Name string `parser:"'tab' @Ident"`
|
|
Label string `parser:"'label' @String"`
|
|
Active bool `parser:"@'active'?"`
|
|
Components []Component `parser:"@@*"`
|
|
}
|
|
|
|
// Panel definitions for slide-out or overlay interfaces
|
|
type Panel struct {
|
|
Name string `parser:"'panel' @Ident"`
|
|
Entity *string `parser:"('for' @Ident)?"`
|
|
Trigger string `parser:"'trigger' @String"`
|
|
Position *string `parser:"('position' @String)?"`
|
|
Components []Component `parser:"@@*"`
|
|
}
|
|
|
|
// Modal definitions
|
|
type Modal struct {
|
|
Name string `parser:"'modal' @Ident"`
|
|
Trigger string `parser:"'trigger' @String"`
|
|
Components []Component `parser:"@@*"`
|
|
}
|
|
|
|
// Master-detail layout components - simplified parsing
|
|
type MasterDetail struct {
|
|
Master *MasterSection `parser:"'master' @@"`
|
|
Detail *DetailSection `parser:"'detail' @@"`
|
|
}
|
|
|
|
type MasterSection struct {
|
|
Name string `parser:"@Ident"`
|
|
Components []Component `parser:"@@*"`
|
|
}
|
|
|
|
type DetailSection struct {
|
|
Name string `parser:"@Ident"`
|
|
Trigger *string `parser:"('trigger' @String)?"`
|
|
Components []Component `parser:"@@*"`
|
|
}
|
|
|
|
// Enhanced Component definitions with detailed field configurations
|
|
type Component struct {
|
|
Type string `parser:"'component' @Ident"`
|
|
Entity *string `parser:"('for' @Ident)?"`
|
|
Elements []ComponentElement `parser:"@@*"`
|
|
}
|
|
|
|
// Union type for component elements to allow flexible ordering
|
|
type ComponentElement struct {
|
|
Config *ComponentAttr `parser:"@@"`
|
|
Field *ComponentField `parser:"| @@"`
|
|
Condition *WhenCondition `parser:"| @@"`
|
|
Section *ComponentSection `parser:"| @@"`
|
|
Action *ComponentButtonAttr `parser:"| @@"`
|
|
}
|
|
|
|
// Enhanced component field with detailed configuration using flexible attributes
|
|
type ComponentField struct {
|
|
Name string `parser:"'field' @Ident"`
|
|
Type string `parser:"'type' @Ident"`
|
|
Attributes []ComponentFieldAttribute `parser:"@@*"`
|
|
}
|
|
|
|
// Flexible field attribute system
|
|
type ComponentFieldAttribute struct {
|
|
Label *string `parser:"('label' @String)"`
|
|
Placeholder *string `parser:"| ('placeholder' @String)"`
|
|
Required bool `parser:"| @'required'"`
|
|
Sortable bool `parser:"| @'sortable'"`
|
|
Searchable bool `parser:"| @'searchable'"`
|
|
Thumbnail bool `parser:"| @'thumbnail'"`
|
|
Default *string `parser:"| ('default' @String)"`
|
|
Options []string `parser:"| ('options' '[' @String (',' @String)* ']')"`
|
|
Accept *string `parser:"| ('accept' @String)"`
|
|
Rows *int `parser:"| ('rows' @Int)"`
|
|
Format *string `parser:"| ('format' @String)"`
|
|
Size *string `parser:"| ('size' @String)"`
|
|
Display *string `parser:"| ('display' @String)"`
|
|
Value *string `parser:"| ('value' @String)"`
|
|
Source *string `parser:"| ('source' @String)"`
|
|
Relates *FieldRelation `parser:"| @@"`
|
|
Validation *ComponentValidation `parser:"| @@"`
|
|
}
|
|
|
|
// Field relationship for autocomplete and select fields
|
|
type FieldRelation struct {
|
|
Type string `parser:"'relates' 'to' @Ident"`
|
|
}
|
|
|
|
// Component validation
|
|
type ComponentValidation struct {
|
|
Type string `parser:"'validate' @Ident"`
|
|
Value *string `parser:"@String?"`
|
|
}
|
|
|
|
// Conditional rendering
|
|
type WhenCondition struct {
|
|
Field string `parser:"'when' @Ident"`
|
|
Operator string `parser:"@('equals' | 'not_equals' | 'contains')"`
|
|
Value string `parser:"@String"`
|
|
Fields []ComponentField `parser:"@@*"`
|
|
Sections []ComponentSection `parser:"@@*"`
|
|
Buttons []ComponentButtonAttr `parser:"@@*"`
|
|
}
|
|
|
|
// Component sections for grouping
|
|
type ComponentSection struct {
|
|
Name string `parser:"'section' @Ident"`
|
|
Class *string `parser:"('class' @String)?"`
|
|
Fields []ComponentField `parser:"@@*"`
|
|
Buttons []ComponentButtonAttr `parser:"@@*"`
|
|
Actions []ComponentButtonAttr `parser:"@@*"`
|
|
}
|
|
|
|
// Enhanced component buttons/actions with detailed configuration
|
|
type ComponentButtonAttr struct {
|
|
Name string `parser:"'button' @Ident"`
|
|
Label string `parser:"'label' @String"`
|
|
Style *string `parser:"('style' @String)?"`
|
|
Icon *string `parser:"('icon' @String)?"`
|
|
Loading *string `parser:"('loading' @String)?"`
|
|
Disabled *string `parser:"('disabled' 'when' @Ident)?"`
|
|
Confirm *string `parser:"('confirm' @String)?"`
|
|
Target *string `parser:"('target' @Ident)?"`
|
|
Position *string `parser:"('position' @String)?"`
|
|
Via *string `parser:"('via' @String)?"`
|
|
}
|
|
|
|
// Component attributes and configurations (keeping existing for backward compatibility)
|
|
type ComponentAttr struct {
|
|
Fields *ComponentFields `parser:"@@"`
|
|
Actions *ComponentActions `parser:"| @@"`
|
|
DataSource *ComponentDataSource `parser:"| @@"`
|
|
Style *ComponentStyle `parser:"| @@"`
|
|
Pagination *ComponentPagination `parser:"| @@"`
|
|
Filters *ComponentFilters `parser:"| @@"`
|
|
Validation bool `parser:"| @'validate'"`
|
|
}
|
|
|
|
// Component field specification (simple version for backward compatibility)
|
|
type ComponentFields struct {
|
|
Fields []string `parser:"'fields' '[' @Ident (',' @Ident)* ']'"`
|
|
}
|
|
|
|
// Enhanced component actions
|
|
type ComponentActions struct {
|
|
Actions []ComponentAction `parser:"'actions' '[' @@ (',' @@)* ']'"`
|
|
}
|
|
|
|
type ComponentAction struct {
|
|
Name string `parser:"@Ident"`
|
|
Label *string `parser:"('label' @String)?"`
|
|
Icon *string `parser:"('icon' @String)?"`
|
|
Style *string `parser:"('style' @String)?"`
|
|
Endpoint *string `parser:"('via' @String)?"`
|
|
Target *string `parser:"('target' @Ident)?"`
|
|
Position *string `parser:"('position' @String)?"`
|
|
Confirm *string `parser:"('confirm' @String)?"`
|
|
}
|
|
|
|
// Data source configuration (can reference endpoints)
|
|
type ComponentDataSource struct {
|
|
Endpoint string `parser:"'data' 'from' @String"`
|
|
}
|
|
|
|
// Component styling
|
|
type ComponentStyle struct {
|
|
Theme *string `parser:"'style' @Ident"`
|
|
Classes []string `parser:"('classes' '[' @String (',' @String)* ']')?"`
|
|
}
|
|
|
|
// Pagination configuration
|
|
type ComponentPagination struct {
|
|
PageSize *int `parser:"'pagination' ('size' @Int)?"`
|
|
}
|
|
|
|
// Filter specifications
|
|
type ComponentFilters struct {
|
|
Filters []ComponentFilter `parser:"'filters' '[' @@ (',' @@)* ']'"`
|
|
}
|
|
|
|
type ComponentFilter struct {
|
|
Field string `parser:"@Ident"`
|
|
Type string `parser:"'as' @('text' | 'select' | 'date' | 'number')"`
|
|
Label *string `parser:"('label' @String)?"`
|
|
}
|
|
|
|
func ParseInput(input string) (AST, error) {
|
|
parser, err := participle.Build[AST](
|
|
participle.Unquote("String"),
|
|
)
|
|
if err != nil {
|
|
return AST{}, err
|
|
}
|
|
ast, err := parser.ParseString("", input)
|
|
if err != nil {
|
|
return AST{}, err
|
|
}
|
|
return *ast, nil
|
|
}
|