Skip to content

Commit

Permalink
Fixes/improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
cbuttner committed Aug 2, 2024
1 parent 096e16d commit d564fcc
Show file tree
Hide file tree
Showing 6 changed files with 132,157 additions and 142,318 deletions.
9 changes: 6 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@

### Changes

- Added node `bitstruct_member_declaration`
- Added rule `bitstruct_member_declaration`
- Added field `name` to `distinct_declaration`
- Added field `name` to `const_declaration`
- Added field `type` to `bitstruct_member_declaration`
- Added field `body` to `bitstruct_body`
- Added field `return_type` to `lambda_declaration`
- Added fields `type` and `name` to `parameter`
- Removed node `multi_declaration`
- Removed node `optional_type`, it's now `type` with an optional '!' token at the end
- Removed rule `multi_declaration`
- Removed rule `optional_type`, it's now `type` with an optional '!' token at the end
- Renamed rule `suffix_expr` to `optional_expr`
- Node `multi_declaration` inlined into `global_declaration`
- Relaxed bitstruct members
- Relaxed enum body to be empty
- Relaxed optional types
- `macro_declaration` now always has a `macro_header`, previously it could have a `func_header`
- Fixed not accepting optional types where it's permitted
- Fixed/improved parameter grammar
- Fixed incorrect ternary precedence
- Fixed not permitting assignment expressions in some cases
Binary file modified docs/playground/tree-sitter-c3.wasm
Binary file not shown.
89 changes: 32 additions & 57 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const TYPE_IDENT = /_*[A-Z][_A-Z0-9]*[a-z][_a-zA-Z0-9]*/;
const CONST_IDENT = /_*[A-Z][_A-Z0-9]*/;

// https://c3lang.github.io/c3-web/references/docs/precedence/
// c3c/src/compiler/enums.h
const PREC = {
// Expressions
ASSIGNMENT: -2,
Expand Down Expand Up @@ -219,7 +220,6 @@ module.exports = grammar({
// Helpers
// -------------------------
_assign_right_expr: $ => seq('=', field('right', $._expr)),
_assign_right_constant_expr: $ => seq('=', field('right', $._constant_expr)),

_cond: $ => choice(
choice($._try_unwrap_chain, $.catch_unwrap),
Expand Down Expand Up @@ -696,7 +696,7 @@ module.exports = grammar({
// -------------------------
local_decl_after_type: $ => choice(
seq(field('name', $.ident), optional($.attributes), optional($._assign_right_expr)),
seq(field('name', $.ct_ident), optional($._assign_right_constant_expr)),
seq(field('name', $.ct_ident), optional($._assign_right_expr)),
),
_decl_statement_after_type: $ => commaSep1($.local_decl_after_type),

Expand Down Expand Up @@ -765,7 +765,7 @@ module.exports = grammar({

// If-Catch
// -------------------------
catch_unwrap_list: $ => commaSep1($._relational_expr),
catch_unwrap_list: $ => commaSep1($._expr),
// Precedence over assignment expression
catch_unwrap: $ => prec(1, seq(
'catch',
Expand All @@ -777,12 +777,11 @@ module.exports = grammar({

// If-Try
// -------------------------
_rel_or_lambda_expr: $ => choice(
$._relational_expr,
seq($.lambda_declaration, '=>', $._relational_expr),
),
_rel_or_lambda_expr: $ => prec(3, choice(
$._expr,
seq($.lambda_declaration, '=>', $._expr),
)),

// Precedence over assignment expression
try_unwrap: $ => prec(1, seq(
'try',
choice(
Expand Down Expand Up @@ -1013,7 +1012,7 @@ module.exports = grammar({
$.ternary_expr,
$.lambda_expr,
$.elvis_orelse_expr,
$.suffix_expr,
$.optional_expr,
$.binary_expr,
$.unary_expr,
$.cast_expr,
Expand All @@ -1030,7 +1029,7 @@ module.exports = grammar({
$.ternary_expr,
$.lambda_expr,
$.elvis_orelse_expr,
$.suffix_expr,
$.optional_expr,
$.binary_expr,
$.unary_expr,
$.cast_expr,
Expand All @@ -1043,31 +1042,6 @@ module.exports = grammar({
$._base_expr,
)),

// NOTE This deviates original grammar by including && and ||.
_relational_expr: $ => prec(2, choice(
$.binary_expr,
$.unary_expr,
$.cast_expr,
$.rethrow_expr,
$.trailing_generic_expr,
$.update_expr,
$.call_expr,
$.subscript_expr,
$.initializer_list,
$._base_expr,
)),

// One more level for more accurate errors
_trailing_expr: $ => prec(3, choice(
$.rethrow_expr,
$.trailing_generic_expr,
$.update_expr,
$.call_expr,
$.subscript_expr,
$.initializer_list,
$._base_expr,
)),

// Base Expression
// -------------------------
_ident_expr: $ => choice(
Expand Down Expand Up @@ -1102,7 +1076,7 @@ module.exports = grammar({
bytes_expr: $ => repeat1($.bytes_literal),
paren_expr: $ => seq('(', $._expr, ')'),

_base_expr: $ => prec(5, choice(
_base_expr: $ => prec(2, choice(
'true',
'false',
'null',
Expand All @@ -1116,12 +1090,12 @@ module.exports = grammar({
$.bytes_expr,

$._ident_expr,
$.module_ident_expr,
$._local_ident_expr,

$.initializer_list,
seq($.type, $.initializer_list),

$.module_ident_expr,
$.field_expr,
$.type_access_expr,
$.paren_expr,
Expand Down Expand Up @@ -1208,12 +1182,11 @@ module.exports = grammar({
// -------------------------
ternary_expr: $ => prec.right(PREC.TERNARY, choice(
seq(
// field('condition', $._constant_expr),
field('condition', $._relational_expr), // TODO
field('condition', $._expr), // TODO
'?',
field('consequence', $._expr),
':',
field('alternative', $._constant_expr),
field('alternative', $._expr),
),
)),

Expand All @@ -1224,13 +1197,15 @@ module.exports = grammar({
// Elvis/or-else (?:, ??) Expression
// -------------------------
elvis_orelse_expr: $ => prec.right(PREC.TERNARY, seq(
$._constant_expr, choice('?:', '??'), $._constant_expr,
field('condition', $._expr),
field('operator', choice('?:', '??')),
field('alternative', $._expr),
)),

// Suffix Expression
// -------------------------
suffix_expr: $ => prec.right(PREC.TERNARY, seq(
field('argument', $._relational_expr),
optional_expr: $ => prec.right(PREC.TERNARY, seq(
field('argument', $._expr),
field('operator', choice(
'?',
seq('?', '!'),
Expand Down Expand Up @@ -1293,8 +1268,8 @@ module.exports = grammar({

// Arguments
// -------------------------
// Precedence over _trailing_expr
param_path_element: $ => prec(4, choice(
// Precedence over _expr
param_path_element: $ => prec(1, choice(
seq('[', $._expr, ']'),
seq('[', $._expr, '..', $._expr, ']'),
seq('.', $._base_expr),
Expand Down Expand Up @@ -1328,29 +1303,29 @@ module.exports = grammar({
optional($.call_inline_attributes),
),
call_expr: $ => prec.right(PREC.TRAILING, seq(
field('function', $._trailing_expr),
field('function', $._expr),
field('arguments', $.call_invocation),
field('trailing', optional($.compound_stmt)),
)),

// Postfix Update Expression (--/++)
// -------------------------
update_expr: $ => prec.right(PREC.TRAILING, seq(
field('argument', $._trailing_expr),
field('argument', $._expr),
field('operator', choice('--', '++')),
)),

// Rethrow Expression
// -------------------------
rethrow_expr: $ => prec.right(PREC.TRAILING, seq(
field('argument', $._trailing_expr),
field('argument', $._expr),
field('operator', choice('!', '!!')),
)),

// Trailing Generic Expression
// -------------------------
trailing_generic_expr: $ => prec.right(PREC.TRAILING, seq(
field('argument', $._trailing_expr),
field('argument', $._expr),
field('operator', $.generic_arguments),
)),

Expand All @@ -1370,7 +1345,7 @@ module.exports = grammar({
// Subscript Expression
// -------------------------
subscript_expr: $ => prec.right(PREC.SUBSCRIPT, seq(
field('argument', $._trailing_expr),
field('argument', $._expr),
'[',
choice(
field('index', $._range_loc),
Expand Down Expand Up @@ -1445,23 +1420,23 @@ module.exports = grammar({
'typeid',
),

base_type: $ => choice(
base_type: $ => prec.right(choice(
$.base_type_name,
seq($.type_ident, optional($.generic_arguments)),
seq($.module_type_ident, optional($.generic_arguments)),
$.ct_type_ident,
seq('$typeof', '(', $._expr, ')'),
seq('$typefrom', '(', $._constant_expr, ')'),
seq('$vatype', '(', $._constant_expr, ')'),
seq('$evaltype', '(', $._constant_expr, ')'),
),
seq('$typefrom', '(', $._expr, ')'),
seq('$vatype', '(', $._expr, ')'),
seq('$evaltype', '(', $._expr, ')'),
)),

type_suffix: $ => choice(
'*',
seq('[', $._constant_expr, ']'),
seq('[', $._expr, ']'),
seq('[', ']'),
seq('[', '*', ']'),
seq('[<', $._constant_expr, '>]'),
seq('[<', $._expr, '>]'),
seq('[<', '*', '>]'),
),
type: $ => prec.right(seq(
Expand Down
Loading

0 comments on commit d564fcc

Please sign in to comment.