Refactor of ast yaml and schema.

This commit is contained in:
Jesse Brault 2025-09-15 11:44:16 -05:00
parent 4c2ee8f929
commit 2aee2cdd4e
3 changed files with 242 additions and 324 deletions

View File

@ -5,23 +5,43 @@ description: Top level is a map of node names in Pascal case (e.g., CompilationU
additionalProperties: additionalProperties:
$ref: "#/$defs/NodeDefinition" $ref: "#/$defs/NodeDefinition"
$defs: $defs:
# Top level
NodeDefinition: NodeDefinition:
type: object type: object
additionalProperties: false additionalProperties: false
description: A definition of a node type. description: |
A definition of a node type. The main key in this hash determines the type of node: `children` for Struct node,
`members` for Leaf-Struct node, `rules` for Enum node, `leaf_rules` for Leaf-Enum node, and `produce` for a
translate rule.
A Struct node has child nodes and other properties, and is built by looping through all the inner pairs of the
given Parser Pair.
A Leaf-Struct node does not have child nodes, but does have members. The members are built by some kind of parsing
of the string value of the the given Parser Pair (i.e., no looping through inner pairs).
An Enum node maps Parser Rules to an enum of node types. Each enum member may have a child, or not. By default, a
Rule name maps to the node type-name of a single child.
A Leaf-Enum node is like a regular enum node, but no children are allowed. Rather, the Parser Rule maps to a bare
enum member.
A translate rule simply translates the Parser rule into some kind of arbitrary type, such as a string, int, etc.
oneOf: oneOf:
- $ref: "#/$defs/StructNodeDefinition" - $ref: "#/$defs/StructNodeDefinition"
- $ref: "#/$defs/LeafStructNodeDefinition" - $ref: "#/$defs/LeafStructNodeDefinition"
- $ref: "#/$defs/EnumNodeDefinition" - $ref: "#/$defs/EnumNodeDefinition"
- $ref: "#/$defs/LeafEnumNodeDefinition" - $ref: "#/$defs/LeafEnumNodeDefinition"
- $ref: "#/$defs/ProductionDefinition"
# Four main types of nodes
StructNodeDefinition: StructNodeDefinition:
type: object type: object
additionalProperties: false additionalProperties: false
description: A description of a Struct node to be built. description: A description of a Struct node to be built.
properties: properties:
type:
const: struct
children: children:
type: array
description: Ordered child fields for this node. description: Ordered child fields for this node.
items: items:
$ref: "#/$defs/StructChildDefinition" $ref: "#/$defs/StructChildDefinition"
@ -32,22 +52,18 @@ $defs:
additionalProperties: false additionalProperties: false
description: A description of a Leaf-Struct node to be built. description: A description of a Leaf-Struct node to be built.
properties: properties:
type: members:
const: leaf_struct type: array
children: description: Ordered members for this node.
description: Ordered child fields for this node.
items: items:
$ref: "#/$defs/LeafStructChildDefinition" $ref: "#/$defs/LeafStructMemberDefinition"
required: required:
- type - members
- children
EnumNodeDefinition: EnumNodeDefinition:
type: object type: object
additionalProperties: false additionalProperties: false
description: A description of an Enum node to be built. description: A description of an Enum node to be built.
properties: properties:
type:
const: enum
rules: rules:
type: array type: array
description: Alternative parse rules that build this node. description: Alternative parse rules that build this node.
@ -60,39 +76,40 @@ $defs:
additionalProperties: false additionalProperties: false
description: A description of a leaf-enum node to be built. description: A description of a leaf-enum node to be built.
properties: properties:
type: leaf_rules:
const: leaf_enum
rules:
type: array type: array
description: Alternative parse rules that build this node. description: Alternative parse rules that build this node.
items: items:
$ref: "#/$defs/LeafEnumChildDefinition" type: string
required: required:
- type - leaf_rules
- rules
# Struct node children
StructChildDefinition: StructChildDefinition:
description: A definition of a node's child. Either a bare child name (string) in snake case, or an object. description: |
A definition of a Struct node's child. Either a bare child name (string) in snake case, or an object. The former
is a shorthand where the child name and built type are the same; casing is automatically done. The latter allows
further customization of the built child.
oneOf: oneOf:
- type: string - type: string
description: Shorthand where child name, var, build, and with are inferred from the given snake-case child name. - $ref: "#/$defs/StructChildDefinitionWrapper"
- $ref: "#/$defs/ChildDefinitionWrapper" StructChildDefinitionWrapper:
ChildDefinitionWrapper:
type: object type: object
description: Single-key object mapping the child-name to its spec. description: Single-key object mapping the child-name to its advanced definition.
minProperties: 1 minProperties: 1
maxProperties: 1 maxProperties: 1
additionalProperties: false additionalProperties: false
patternProperties: patternProperties:
"^[a-z][a-z0-9_]*$": "^[a-z][a-z0-9_]*$":
$ref: "#/$defs/ChildDefinition" $ref: "#/$defs/StructChildAdvancedDefinition"
ChildDefinition: StructChildAdvancedDefinition:
type: object type: object
description: One of skip/vec/single child specs. description: One of skip/vec/single child specs.
oneOf: oneOf:
- $ref: "#/$defs/SkipChildDefinition" - $ref: "#/$defs/StructChildSkipChildDefinition"
- $ref: "#/$defs/VecChildDefinition" - $ref: "#/$defs/StructChildVecChildDefinition"
- $ref: "#/$defs/SingleChildDefinition" - $ref: "#/$defs/StructChildMemberDefinition"
SkipChildDefinition: StructChildSkipChildDefinition:
type: object type: object
additionalProperties: false additionalProperties: false
description: A definition for a child rule that does nothing, i.e., is skipped. description: A definition for a child rule that does nothing, i.e., is skipped.
@ -100,49 +117,130 @@ $defs:
rule: rule:
type: string type: string
skip: skip:
type: boolean # note: must be true type: boolean
const: true
required: required:
- rule - rule
- skip - skip
VecChildDefinition: StructChildVecChildDefinition:
type: object type: object
additionalProperties: false additionalProperties: false
description: A definition for a child rule that can be matched multiple times. description: A definition for a child rule that can be matched multiple times.
properties: properties:
rule: rule:
type: string type: string
kind:
type: string
enum:
- string
vec: vec:
type: boolean type: boolean
const: true
required: required:
- rule - rule
- vec - vec
SingleChildDefinition: StructChildMemberDefinition:
type: object type: object
additionalProperties: false additionalProperties: false
description: A definition for a child rule that builds one item. description: |
A definition for a child rule that builds one member. If a bare string, it is assumed to be the name/build-type
for a node. An object allows different types (i.e., things additional to nodes) to be built.
properties: properties:
rule: rule:
type: string type: string
description: The type to build, in Pascal case. description: The rule to match.
optional: optional:
type: boolean type: boolean
description: If true, this child will be stored as an Option. description: If true, this child will be stored as an Option.
build: build:
oneOf: oneOf:
- type: string - type: string
- $ref: "#/$defs/SingleChildBuildDefinition" - $ref: "#/$defs/StructChildMemberBuildDefinition"
SingleChildBuildDefinition: StructChildMemberBuildDefinition:
type: object type: object
additionalProperties: false additionalProperties: false
description: A definition of what exactly to build for a given child rule. description: A definition of what exactly to build for a given child rule.
oneOf: oneOf:
- $ref: "#/$defs/BuildSingleTypeChild" - $ref: "#/$defs/BuildNode"
- $ref: "#/$defs/BuildBooleanChild" - $ref: "#/$defs/BuildBoolean"
- $ref: "#/$defs/BuildStringChild"
- $ref: "#/$defs/BuildDoubleChild" # Leaf Struct children
- $ref: "#/$defs/BuildIntChild" LeafStructMemberDefinition:
- $ref: "#/$defs/BuildLongChild" type: object
BuildSingleTypeChild: description: Single-key object mapping the member-name to what is to be built by parsing the Parser Pair.
minProperties: 1
maxProperties: 1
additionalProperties: false
patternProperties:
"^[a-z][a-z0-9_]*$":
oneOf:
- $ref: "#/$defs/BuildMember"
BuildMember:
type: object
description: A specification for a member to build.
additionalProperties: false
properties:
kind:
enum:
- string
from:
enum:
- whole_pair
required:
- kind
- from
# Enum children
EnumChildDefinition:
oneOf:
- type: string
description: Shorthand where child name, var, build, and with are inferred from the given Pascal-case rule name.
- $ref: "#/$defs/LongEnumChildDefinition"
LongEnumChildDefinition:
type: object
additionalProperties: false
description: A format for an advanced enum child.
properties:
child:
type: boolean
kind:
enum:
- int
- long
- double
- usize
- string
- boolean
from:
enum:
- translate
required:
- kind
- from
# Production definition
ProductionDefinition:
type: object
properties:
produce:
type: object
properties:
kind:
enum:
- int
- long
- double
- string
- boolean
from:
enum:
- translate_and_parse
- string_inner
- whole_pair
- parse_whole_pair
# Common things to build
BuildNode:
type: object type: object
additionalProperties: false additionalProperties: false
description: A definition of a single-type child to build. description: A definition of a single-type child to build.
@ -153,109 +251,15 @@ $defs:
or_else_default: or_else_default:
type: boolean type: boolean
description: Whether to call the default method on the built-type if the rule is not found. description: Whether to call the default method on the built-type if the rule is not found.
BuildBooleanChild: BuildBoolean:
type: object type: object
additionalProperties: false additionalProperties: false
description: A definition for building a boolean child. description: A boolean member to be built.
properties: properties:
type: kind:
type: string type: string
enum: const: boolean
- boolean
on: on:
type: string type: string
enum: enum:
- rule_present - rule_present
from:
type: string
enum:
- parse_whole_pair
BuildStringChild:
type: object
additionalProperties: false
description: A definition for building a string child.
properties:
type:
const: string
from:
type: string
enum:
- whole_pair
BuildDoubleChild:
type: object
additionalProperties: false
description: A definition for building a Double child.
properties:
type:
const: f64
from:
type: string
enum:
- parse_whole_pair
BuildIntChild:
type: object
additionalProperties: false
description: A definition for building an Int child.
properties:
type:
const: i32
from:
type: string
enum:
- parse_number_base
BuildLongChild:
type: object
additionalProperties: false
description: A definition for building a Long child.
properties:
type:
const: i64
from:
type: string
enum:
- parse_number_base
LeafStructChildDefinition:
# TODO
EnumChildDefinition:
description: A definition of an enum node's child. Either a bare rule (string) in Pascal case, or an object.
oneOf:
- type: string
description: Shorthand where child name, var, build, and with are inferred from the given Pascal-case rule name.
- $ref: "#/$defs/LongEnumChildDefinition"
LongEnumChildDefinition:
type: object
additionalProperties: false
description: A format for specifying more specific information for an enum child.
properties:
rule:
type: string
build:
type: string
required:
- rule
- build
LeafEnumChildDefinition:
description: A definition of a leaf-enum node's child. Either a bare rule-string in Pascal case, or an object.
oneOf:
- type: string
description: Shorthand where the rule name maps onto an empty enum rule.
- $ref: "#/$defs/LongLeafEnumChildDefinitionWrapper"
LongLeafEnumChildDefinitionWrapper:
type: object
description: Single-key object mapping the child-name to its spec.
minProperties: 1
maxProperties: 1
additionalProperties: false
patternProperties:
"^([A-Z][a-z0-9]*)*$":
$ref: "#/$defs/LongLeafEnumChildDefinition"
LongLeafEnumChildDefinition:
type: object
additionalProperties: false
description: A format for specifying more specific information about a leaf-enum child.
properties:
child:
type: boolean
description: If true, a node of the same name is built as the lone member of the enum child.
required:
- child

View File

@ -1,8 +1,7 @@
# $schema: ./ast.schema.yaml # $schema: ./ast.schema.yaml
# Operators # Operators
Operator: Operator:
type: leaf_enum leaf_rules:
rules:
- Or - Or
- And - And
- EqualTo - EqualTo
@ -29,11 +28,10 @@ Operator:
# Names # Names
Identifier: Identifier:
type: leaf_struct members:
children:
- name: - name:
build: kind: string
type: string from: whole_pair
FullyQualifiedName: FullyQualifiedName:
children: children:
- identifiers: - identifiers:
@ -64,8 +62,7 @@ TypeUse:
- TupleTypeUse - TupleTypeUse
- FunctionTypeUse - FunctionTypeUse
PrimitiveType: PrimitiveType:
type: leaf_enum leaf_rules:
rules:
- Byte - Byte
- Short - Short
- Char - Char
@ -174,13 +171,11 @@ UseStatementPrefix:
children: children:
- identifier - identifier
UseStatementSuffix: UseStatementSuffix:
type: leaf_enum
rules: rules:
- Identifier: - Identifier
child: true - Star:
- Star child: false
- UseList: - UseList
child: true
UseList: UseList:
children: children:
- identifiers: - identifiers:
@ -219,7 +214,7 @@ Module:
- is_public: - is_public:
rule: Pub rule: Pub
build: build:
type: boolean kind: boolean
on: rule_present on: rule_present
- mod_kw: - mod_kw:
rule: Mod rule: Mod
@ -251,7 +246,7 @@ Interface:
- is_public: - is_public:
rule: Pub rule: Pub
build: build:
type: boolean kind: boolean
on: rule_present on: rule_present
- int_kw: - int_kw:
rule: IntKw rule: IntKw
@ -274,7 +269,7 @@ Class:
- is_public: - is_public:
rule: Pub rule: Pub
build: build:
type: boolean kind: boolean
on: rule_present on: rule_present
- class_kw: - class_kw:
rule: ClassKw rule: ClassKw
@ -299,7 +294,7 @@ Function:
- is_public: - is_public:
rule: Pub rule: Pub
build: build:
type: boolean kind: boolean
on: rule_present on: rule_present
- fn_kw: - fn_kw:
rule: Fn rule: Fn
@ -317,7 +312,7 @@ OperatorFunction:
- is_public: - is_public:
rule: Pub rule: Pub
build: build:
type: boolean kind: boolean
on: rule_present on: rule_present
- op_kw: - op_kw:
rule: Op rule: Op
@ -337,7 +332,7 @@ PlatformFunction:
- is_public: - is_public:
rule: Pub rule: Pub
build: build:
type: boolean kind: boolean
on: rule_present on: rule_present
- platform_kw: - platform_kw:
rule: Platform rule: Platform
@ -415,14 +410,10 @@ InterfaceDefaultOperatorFunction:
# Function Bodies # Function Bodies
FunctionBody: FunctionBody:
type: leaf_enum
rules: rules:
- FunctionAliasBody: - FunctionAliasBody
child: true - FunctionEqualsBody
- FunctionEqualsBody: - FunctionBlockBody
child: true
- FunctionBlockBody:
child: true
FunctionEqualsBody: FunctionEqualsBody:
children: children:
- expression - expression
@ -452,34 +443,26 @@ Member:
- is_public: - is_public:
rule: Pub rule: Pub
build: build:
type: boolean kind: boolean
on: rule_present on: rule_present
- is_mut: - is_mut:
rule: Mut rule: Mut
build: build:
type: boolean kind: boolean
on: rule_present on: rule_present
- identifier - identifier
- type_use - type_use
# Statements # Statements
Statement: Statement:
type: leaf_enum
rules: rules:
- VariableDeclaration: - VariableDeclaration
child: true - AssignmentStatement
- AssignmentStatement: - ExpressionStatement
child: true - UseStatement
- ExpressionStatement: - IfStatement
child: true - WhileStatement
- UseStatement: - ForStatement
child: true
- IfStatement:
child: true
- WhileStatement:
child: true
- ForStatement:
child: true
VariableDeclaration: VariableDeclaration:
children: children:
- let_kw: - let_kw:
@ -488,7 +471,7 @@ VariableDeclaration:
- is_mut: - is_mut:
rule: Mut rule: Mut
build: build:
type: boolean kind: boolean
on: rule_present on: rule_present
- identifier - identifier
- type_use: - type_use:
@ -625,8 +608,7 @@ ComparisonExpression:
rule: Expression rule: Expression
optional: true optional: true
ComparisonOperator: ComparisonOperator:
type: leaf_enum leaf_rules:
rules:
- Greater - Greater
- Less - Less
- GreaterEqual - GreaterEqual
@ -643,8 +625,7 @@ ShiftExpression:
- right: - right:
rule: Expression rule: Expression
ShiftOperator: ShiftOperator:
type: leaf_enum leaf_rules:
rules:
- LeftShift - LeftShift
- RightShift - RightShift
AdditiveExpression: AdditiveExpression:
@ -658,8 +639,7 @@ AdditiveExpression:
rule: Expression rule: Expression
optional: true optional: true
AdditiveOperator: AdditiveOperator:
type: leaf_enum leaf_rules:
rules:
- Add - Add
- Subtract - Subtract
MultiplicativeExpression: MultiplicativeExpression:
@ -672,8 +652,7 @@ MultiplicativeExpression:
- right: - right:
rule: Expression rule: Expression
MultiplicativeOperator: MultiplicativeOperator:
type: leaf_enum leaf_rules:
rules:
- Multiply - Multiply
- Divide - Divide
- Modulo - Modulo
@ -683,8 +662,7 @@ PrefixExpression:
rule: PrefixOperator rule: PrefixOperator
vec: true vec: true
PrefixOperator: PrefixOperator:
type: leaf_enum leaf_rules:
rules:
- Spread - Spread
- Not - Not
- Negative - Negative
@ -696,16 +674,14 @@ SuffixExpression:
rule: SuffixOperator rule: SuffixOperator
vec: true vec: true
SuffixOperator: SuffixOperator:
type: leaf_enum
rules: rules:
- PlusPlus - PlusPlus:
- MinusMinus child: false
- ObjectProperty: - MinusMinus:
child: true child: false
- ObjectIndex: - ObjectProperty
child: true - ObjectIndex
- Call: - Call
child: true
ObjectProperty: ObjectProperty:
children: children:
- identifier - identifier
@ -713,23 +689,17 @@ ObjectIndex:
children: children:
- expression - expression
PrimaryExpression: PrimaryExpression:
type: leaf_enum
rules: rules:
- Literal: - Literal
child: true - FullyQualifiedName
- FullyQualifiedName: - Closure
child: true - ParenthesizedExpression
- Closure:
child: true
- ParenthesizedExpression:
child: true
ParenthesizedExpression: ParenthesizedExpression:
children: children:
- expression - expression
# Calls # Calls
Call: Call:
type: leaf_enum
rules: rules:
- ParenthesesCall - ParenthesesCall
- NonParenthesesCall - NonParenthesesCall
@ -780,101 +750,57 @@ ClosureParameter:
# Literals # Literals
Literal: Literal:
type: leaf_enum
rules: rules:
- NumberLiteral:
child: true
- StringLiteral:
child: true
- BooleanLiteral:
child: true
NumberLiteral:
type: leaf_enum
rules:
- DoubleLiteral:
child: true
- LongLiteral:
child: true
- IntLiteral: - IntLiteral:
child: true kind: int
IntLiteral: from: translate
children: - LongLiteral:
- number_base kind: long
LongLiteral: from: translate
children: - DoubleLiteral:
- number_base kind: double
DoubleLiteral: from: translate
type: leaf_struct
children:
- literal:
build:
type: f64
NumberBase:
type: leaf_enum
rules:
- BinaryBase:
child: true
- HexadecimalBase:
child: true
- DecimalBase:
child: true
DecimalBase:
type: leaf_struct
children:
- literal:
build:
type: string
BinaryBase:
children:
- binary_digits
BinaryDigits:
type: leaf_struct
children:
- literal:
build:
type: string
HexadecimalBase:
children:
- hexadecimal_digits
HexadecimalDigits:
type: leaf_struct
children:
- literal:
build:
type: string
StringLiteral:
type: leaf_enum
rules:
- SingleQuoteString: - SingleQuoteString:
child: true kind: string
- DoubleQuoteString: from: translate
child: true - DString
- BacktickString: - BacktickString
child: true - BooleanLiteral:
kind: boolean
from: translate
# Numbers
IntLiteral:
produce:
kind: int
from: translate_and_parse
LongLiteral:
produce:
kind: long
from: translate_and_parse
DoubleLiteral:
produce:
kind: double
from: translate_and_parse
# Strings
SingleQuoteString: SingleQuoteString:
children: produce:
- string_inner: kind: string
optional: true from: string_inner
DoubleQuoteString: DString:
children: children:
- inners: - inners:
rule: DStringInner rule: DStringInner
kind: string
vec: true vec: true
- expressions: - expressions:
rule: DStringExpression rule: DStringExpression
vec: true vec: true
StringInner:
type: leaf_struct
children:
- literal:
build:
type: string
DStringInner: DStringInner:
type: leaf_struct produce:
children: kind: string
- literal: from: whole_pair
build:
type: string
DStringExpression: DStringExpression:
children: children:
- expression - expression
@ -882,20 +808,16 @@ BacktickString:
children: children:
- inners: - inners:
rule: BacktickInner rule: BacktickInner
kind: string
vec: true vec: true
- expressions: - expressions:
rule: DStringExpression rule: DStringExpression
vec: true vec: true
BacktickInner: BacktickInner:
type: leaf_struct produce:
children: kind: string
- literal: from: string_inner
build:
type: string
BooleanLiteral: BooleanLiteral:
type: leaf_struct produce:
children: kind: boolean
- literal: from: parse_whole_pair
build:
type: boolean
from: parse_whole_pair

View File

@ -785,22 +785,20 @@ ClosureParameter = {
// Literals // Literals
Literal = { Literal = {
NumberLiteral
| StringLiteral
| BooleanLiteral
}
NumberLiteral = {
DoubleLiteral DoubleLiteral
| LongLiteral | LongLiteral
| IntLiteral | IntLiteral
| SingleQuoteString
| DString
| BacktickString
| BooleanLiteral
} }
IntLiteral = { NumberBase } IntLiteral = { NumberBase }
LongLiteral = ${ NumberBase ~ "L" } LongLiteral = ${ NumberBase ~ "L" }
DoubleLiteral = @{ DecimalBase ~ "." ~ DecimalBase} DoubleLiteral = @{ DecimalBase ~ "." ~ DecimalBase }
NumberBase = { NumberBase = {
BinaryBase BinaryBase
@ -822,15 +820,9 @@ HexadecimalDigits = @{ HexadecimalDigit+ }
HexadecimalDigit = { '0'..'9' | 'a'..'f' } HexadecimalDigit = { '0'..'9' | 'a'..'f' }
StringLiteral = {
SingleQuoteString
| DoubleQuoteString
| BacktickString
}
SingleQuoteString = { "'" ~ StringInner? ~ "'" } SingleQuoteString = { "'" ~ StringInner? ~ "'" }
DoubleQuoteString = { DString = {
"\"" "\""
~ ( DStringInner? ~ DStringExpression )* ~ ( DStringInner? ~ DStringExpression )*
~ DStringInner? ~ DStringInner?