Skip to content

Commit

Permalink
feat: support for expression
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyfettes committed Apr 1, 2024
1 parent b034ab9 commit 12962c1
Show file tree
Hide file tree
Showing 5 changed files with 331 additions and 0 deletions.
40 changes: 40 additions & 0 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,24 @@ module.exports = grammar({
'}',
),

for_binder: $ => seq(
$.lowercase_identifier,
'=',
$.expression
),

for_expression: $ => seq(
'for',
commaStrictList($.for_binder),
optional(seq(
';',
optional($.simple_expression),
';',
commaStrictList($.for_binder)
)),
$.block_expression
),

return_expression: $ => seq('return', optional($.expression)),

// Patterns
Expand Down Expand Up @@ -799,6 +817,28 @@ function commaList1(rule) {
);
}


/**
* @param {Rule} rule
*
* @return {ChoiceRule}
*/
function commaStrictList(rule) {
return optional(commaStrictList1(rule))
}

/**
* @param {Rule} rule
*
* @return {SeqRule}
*/
function commaStrictList1(rule) {
return seq(
rule,
repeat(seq(',', rule)),
);
}

/**
*
* @param {Rule} rule
Expand Down
129 changes: 129 additions & 0 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -3652,6 +3652,135 @@
}
]
},
"for_binder": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "lowercase_identifier"
},
{
"type": "STRING",
"value": "="
},
{
"type": "SYMBOL",
"name": "expression"
}
]
},
"for_expression": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "for"
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "for_binder"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "SYMBOL",
"name": "for_binder"
}
]
}
}
]
},
{
"type": "BLANK"
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ";"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "simple_expression"
},
{
"type": "BLANK"
}
]
},
{
"type": "STRING",
"value": ";"
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "for_binder"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "SYMBOL",
"name": "for_binder"
}
]
}
}
]
},
{
"type": "BLANK"
}
]
}
]
},
{
"type": "BLANK"
}
]
},
{
"type": "SYMBOL",
"name": "block_expression"
}
]
},
"return_expression": {
"type": "SEQ",
"members": [
Expand Down
23 changes: 23 additions & 0 deletions src/node-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,25 @@
]
}
},
{
"type": "for_binder",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "expression",
"named": true
},
{
"type": "lowercase_identifier",
"named": true
}
]
}
},
{
"type": "function_definition",
"named": true,
Expand Down Expand Up @@ -2433,6 +2452,10 @@
"type": "fn",
"named": false
},
{
"type": "for",
"named": false
},
{
"type": "if",
"named": false
Expand Down
Binary file modified src/parser.c
Binary file not shown.
139 changes: 139 additions & 0 deletions test/corpus/for.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
================================================================================
for
================================================================================
fn init {
for i = 0; i < 10; i = i + 1 {
}
}
--------------------------------------------------------------------------------

(structure
(structure_item
(function_definition
(function_identifier
(lowercase_identifier))
(block_expression
(statement_expression
(assign_expression
(left_value
(qualified_identifier
(lowercase_identifier)))
(ERROR
(lowercase_identifier))
(expression
(simple_expression
(atomic_expression
(literal
(integer_literal)))))))
(statement_expression
(expression
(simple_expression
(binary_expression
(expression
(simple_expression
(qualified_identifier
(lowercase_identifier))))
(expression
(simple_expression
(atomic_expression
(literal
(integer_literal)))))))))
(statement_expression
(assign_expression
(left_value
(qualified_identifier
(lowercase_identifier)))
(expression
(simple_expression
(binary_expression
(expression
(simple_expression
(qualified_identifier
(lowercase_identifier))))
(ERROR
(simple_expression
(atomic_expression
(literal
(integer_literal)))))
(expression
(simple_expression
(struct_expression
(struct_brace_expression)))))))))))))

================================================================================
forever
================================================================================
fn init {
for i = 0; i < 10; {
}
}
--------------------------------------------------------------------------------

(structure
(structure_item
(function_definition
(function_identifier
(lowercase_identifier))
(block_expression
(statement_expression
(assign_expression
(left_value
(qualified_identifier
(lowercase_identifier)))
(ERROR
(lowercase_identifier))
(expression
(simple_expression
(atomic_expression
(literal
(integer_literal)))))))
(statement_expression
(expression
(simple_expression
(binary_expression
(expression
(simple_expression
(qualified_identifier
(lowercase_identifier))))
(expression
(simple_expression
(atomic_expression
(literal
(integer_literal)))))))))
(statement_expression
(expression
(simple_expression
(struct_expression
(struct_brace_expression)))))))))

================================================================================
simple for
================================================================================
fn init {
for i = 0 {
}
}
--------------------------------------------------------------------------------

(structure
(structure_item
(function_definition
(function_identifier
(lowercase_identifier))
(block_expression
(statement_expression
(assign_expression
(left_value
(qualified_identifier
(lowercase_identifier)))
(ERROR
(lowercase_identifier))
(ERROR
(simple_expression
(atomic_expression
(literal
(integer_literal)))))
(expression
(simple_expression
(struct_expression
(struct_brace_expression))))))))))

0 comments on commit 12962c1

Please sign in to comment.