@@ -15,6 +15,74 @@ fn translation_rules() -> Vec<yeast::Rule> {
1515 // Declarations may be wrapped in local/global wrapper nodes.
1616 rule!( ( global_declaration _ @inner) => { inner} ) ,
1717 rule!( ( local_declaration _ @inner) => { inner} ) ,
18+ // ---- Literals ----
19+ rule!( ( integer_literal) => ( int_literal) ) ,
20+ rule!( ( hex_literal) => ( int_literal) ) ,
21+ rule!( ( bin_literal) => ( int_literal) ) ,
22+ rule!( ( oct_literal) => ( int_literal) ) ,
23+ rule!( ( real_literal) => ( float_literal) ) ,
24+ rule!( ( boolean_literal) => ( boolean_literal) ) ,
25+ rule!( "nil" => ( builtin_expr) ) ,
26+ rule!( ( special_literal) => ( builtin_expr) ) ,
27+ rule!( ( line_string_literal) => ( string_literal) ) ,
28+ rule!( ( multi_line_string_literal) => ( string_literal) ) ,
29+ rule!( ( raw_string_literal) => ( string_literal) ) ,
30+ rule!( ( regex_literal) => ( regex_literal) ) ,
31+ // ---- Names ----
32+ rule!( ( simple_identifier) @id => ( name_expr identifier: ( identifier #{ id} ) ) ) ,
33+ // A referenceable_operator (e.g. `+` used as a value, as in `reduce(0, +)`)
34+ // is treated as a name reference to the operator symbol.
35+ rule!( ( referenceable_operator) @op => ( name_expr identifier: ( identifier #{ op} ) ) ) ,
36+ // ---- Operators ----
37+ // All binary operators share the lhs/op/rhs shape.
38+ rule!( ( additive_expression lhs: @l op: @op rhs: @r) => ( binary_expr left: { l} operator: ( infix_operator #{ op} ) right: { r} ) ) ,
39+ rule!( ( multiplicative_expression lhs: @l op: @op rhs: @r) => ( binary_expr left: { l} operator: ( infix_operator #{ op} ) right: { r} ) ) ,
40+ rule!( ( comparison_expression lhs: @l op: @op rhs: @r) => ( binary_expr left: { l} operator: ( infix_operator #{ op} ) right: { r} ) ) ,
41+ rule!( ( equality_expression lhs: @l op: @op rhs: @r) => ( binary_expr left: { l} operator: ( infix_operator #{ op} ) right: { r} ) ) ,
42+ rule!( ( conjunction_expression lhs: @l op: @op rhs: @r) => ( binary_expr left: { l} operator: ( infix_operator #{ op} ) right: { r} ) ) ,
43+ rule!( ( disjunction_expression lhs: @l op: @op rhs: @r) => ( binary_expr left: { l} operator: ( infix_operator #{ op} ) right: { r} ) ) ,
44+ rule!( ( infix_expression lhs: @l op: @op rhs: @r) => ( binary_expr left: { l} operator: ( infix_operator #{ op} ) right: { r} ) ) ,
45+ // Range expression `a..<b` / `a...b`
46+ rule!( ( range_expression start: @l op: @op end: @r) => ( binary_expr left: { l} operator: ( infix_operator #{ op} ) right: { r} ) ) ,
47+ // Open-ended ranges `a...` / `...b`
48+ rule!( ( open_end_range_expression start: @l) => ( unary_expr operator: ( postfix_operator "..." ) operand: { l} ) ) ,
49+ rule!( ( open_start_range_expression end: @r) => ( unary_expr operator: ( prefix_operator "..." ) operand: { r} ) ) ,
50+ // Custom operator declaration: `[prefix|infix|postfix] operator OP [: PrecedenceGroup]`.
51+ // The fixity keyword is an anonymous child of `operator_declaration`, so we
52+ // dispatch on it with one rule per keyword.
53+ rule!(
54+ ( operator_declaration "prefix" ( referenceable_operator _ @op) ( simple_identifier) ? @prec)
55+ =>
56+ ( operator_syntax_declaration name: ( identifier #{ op} ) fixity: ( fixity "prefix" ) precedence: { ..prec} )
57+ ) ,
58+ rule!(
59+ ( operator_declaration "postfix" ( referenceable_operator _ @op) ( simple_identifier) ? @prec)
60+ =>
61+ ( operator_syntax_declaration name: ( identifier #{ op} ) fixity: ( fixity "postfix" ) precedence: { ..prec} )
62+ ) ,
63+ rule!(
64+ ( operator_declaration "infix" ( referenceable_operator _ @op) ( simple_identifier) ? @prec)
65+ =>
66+ ( operator_syntax_declaration
67+ name: ( identifier #{ op} )
68+ fixity: ( fixity "infix" )
69+ precedence: { ..prec} )
70+ ) ,
71+ rule!( ( bitwise_operation lhs: @l op: @op rhs: @r) => ( binary_expr left: { l} operator: ( infix_operator #{ op} ) right: { r} ) ) ,
72+ rule!( ( nil_coalescing_expression value: @l if_nil: @r) => ( binary_expr left: { l} operator: ( infix_operator "??" ) right: { r} ) ) ,
73+ // Leading-dot member shorthand (e.g. `.some`, `.foo`) means member access
74+ // on a contextually inferred type.
75+ rule!( ( prefix_expression operation: "." target: @member) => ( member_access_expr base: ( inferred_type_expr) member: ( identifier #{ member} ) ) ) ,
76+ // Prefix unary operators
77+ rule!( ( prefix_expression operation: @op target: @operand) => ( unary_expr operator: ( prefix_operator #{ op} ) operand: { operand} ) ) ,
78+ // Postfix unary operators
79+ rule!( ( postfix_expression operation: @op target: @operand) => ( unary_expr operator: ( postfix_operator #{ op} ) operand: { operand} ) ) ,
80+ // Parenthesised single-value tuple is a grouping expression; pass through.
81+ // Multi-value tuples become tuple_expr.
82+ rule!( ( tuple_expression value: _* @v) => ( tuple_expr element: { ..v} ) ) ,
83+ // Blocks contain statement* directly.
84+ rule!( ( block statement: _+ @stmts) => ( block stmt: { ..stmts} ) ) ,
85+ rule!( ( block) => ( block) ) ,
1886 // ---- Fallbacks ----
1987 rule!(
2088 ( _)
0 commit comments