YREA-SLS/docs/grammar_summary.md

126 lines
3.4 KiB
Markdown

---
Title: D Grammar Summary
Prev: Complete Operator Reference
Next: Module System
---
## Appendix D: Grammar Summary
This appendix provides a concise grammar reference. For complete specifications of language constructs (fn, struct, trait, impl, etc.), see the `::Implementable` trait in Appendix B.
### D.1 Lexical Grammar
**Comments and Directives**:
```
line_comment ::= "//" <any characters until newline>
directive ::= "#" <implementation-specific>
shebang ::= "#!/" <path to interpreter>
```
**Identifiers**:
```
identifier ::= [a-zA-Z_][a-zA-Z0-9_]*
identifier_literal ::= "::" identifier
```
**Integer Literals**:
```
integer ::= decimal | hexadecimal | binary | octal
decimal ::= [0-9](_?[0-9])* (":" type_name)?
hexadecimal ::= "0x" [0-9a-fA-F](_?[0-9a-fA-F])*
binary ::= "0b" [01](_?[01])*
octal ::= "0o" [0-7](_?[0-7])*
```
**Floating Point Literals**:
```
float ::= [0-9](_?[0-9])* "." [0-9](_?[0-9])* (":" type_name)?
```
**Character Literals**:
```
char ::= "'" (character | escape_sequence) "'"
```
**String Literals**:
```
string ::= '"' (character | escape_sequence)* '"'
escape_sequence ::= "\n" | "\r" | "\t" | "\\" | '\"' | "\'" | "\0"
| "\x" hex_digit hex_digit
| "\u{" hex_digit+ "}"
```
**Boolean Literals**:
```
boolean ::= "true" | "false"
```
**Array Literals**:
```
array ::= "[" (expression)* (":"|type_annotation)? "]"
typed_array ::= "[" (expression)* ":" type_name "]"
```
**Constants**:
```
constant ::= literal "::" identifier "const"
```
**Token Strings**:
```
token_string ::= "{" (token)* "}"
```
### D.2 Expression Grammar
```
expression ::= literal
| identifier
| identifier_literal
| expression operator
| control_flow
| definition
literal ::= integer | float | string | boolean | char | array
operator ::= built_in_operator | user_defined_operator
control_flow ::= if_expr | while_expr | for_expr | match_expr
```
**Note on Operator Precedence**: Stack-based postfix notation eliminates the need for operator precedence. Operations are performed in the order they appear, operating on values already on the stack.
### D.3 Type Grammar
**Type Tuples (Stack Signatures)**:
```
type_tuple ::= "(" type_list "--" type_list ")"
| "(---)"
type_list ::= (type)*
type ::= identifier
| generic_type
| trait_constraint
generic_type ::= identifier "<" type_param_list ">"
type_param_list ::= type_param ("," type_param)*
type_param ::= identifier (":" trait_constraint)?
```
### D.4 Construct Grammar
Language constructs (fn, struct, trait, impl, enum, union, inher) are defined by operators in the `::Implementable` trait. These operators parse TokenStrings to create language-level definitions.
**High-Level Patterns**:
- Constants: `value ::name const`
- Functions: `(inputs -- outputs) { body } ::name fn`
- Structs: `(field_types --) { field_names: } ::name<type_params>? struct`
- Unions: `(variant_types --) { Variant(T) ... } ::name<type_params>? union`
- Enums: `{ Variant value?: ... } ::name enum`
- Traits: `{ (sig) method: ... } ::identifier<type_params>? trait`
- Trait Impl: `::trait_id { (sig) { body } method: ... } ::type_id<type_params>? impl`
- Trait Inheritance: `[ identifier_list ] ::identifier<type_params>? inher { } ::identifier<type_params>? trait`
> **Complete Specification**: See Appendix B (`::Implementable` trait) for precise definitions of these construct operators.
---