YREA-SLS/docs/grammar_summary.md

106 lines
2.9 KiB
Markdown

## 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**:
```
line_comment ::= "//" <any characters until newline>
```
**Identifiers**:
```
identifier ::= [a-zA-Z_][a-zA-Z0-9_]*
identifier_literal ::= "::" identifier
```
**Integer Literals**:
```
integer ::= decimal | hexadecimal | binary
decimal ::= [0-9]+ (":" type_name)?
hexadecimal ::= "0x" [0-9a-fA-F]+
binary ::= "0b" [01]+
```
**Floating Point Literals**:
```
float ::= [0-9]+ "." [0-9]+ (":" type_name)?
```
**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)* "]"
```
**Token Strings**:
```
token_string ::= "{" (token)* "}"
```
### D.2 Expression Grammar
```
expression ::= literal
| identifier
| identifier_literal
| expression operator
| control_flow
| definition
literal ::= integer | float | string | boolean | 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**:
- 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.
---