1
0

Add calls

This commit is contained in:
Jip J. Dekker 2021-01-07 17:28:33 +11:00
parent ff43112b5f
commit 5004b7fc38
5 changed files with 3146 additions and 2487 deletions

View File

@ -55,6 +55,21 @@ or = x \/ y;
(assignment_item (identifier) (binary_operation (identifier) (identifier))) (assignment_item (identifier) (binary_operation (identifier) (identifier)))
(assignment_item (identifier) (binary_operation (identifier) (identifier)))) (assignment_item (identifier) (binary_operation (identifier) (identifier))))
====
Call
====
no_args = my_fn();
single_arg = my_fn(1);
mult_args = my_fn(2, "test");
---
(source_file
(assignment_item (identifier) (call (identifier)))
(assignment_item (identifier) (call (identifier) (integer_literal)))
(assignment_item (identifier) (call (identifier) (integer_literal) (string_literal))))
================ ================
Index Expression Index Expression
================ ================

View File

@ -26,7 +26,7 @@ module.exports = grammar({
word: $ => $.identifier, word: $ => $.identifier,
rules: { rules: {
source_file: $ => seq(sepBy(';', $._items), optional(';')), source_file: $ => seq(sepBy(';', $._items)),
_items: $ => choice( _items: $ => choice(
$.assignment_item, $.assignment_item,
@ -44,6 +44,7 @@ module.exports = grammar({
$._literal, $._literal,
$.binary_operation, $.binary_operation,
$.call,
$.index_expression, $.index_expression,
$.unary_operation, $.unary_operation,
// TODO: Other expression types // TODO: Other expression types
@ -77,6 +78,13 @@ module.exports = grammar({
)))); ))));
}, },
call: $ => prec(PREC.call, seq(
field('name', $.identifier),
'(',
field('arguments', sepBy(',', $._expression)),
')',
)),
index_expression: $ => prec(PREC.call, seq( index_expression: $ => prec(PREC.call, seq(
field('collection', $._expression), field('collection', $._expression),
'[', '[',
@ -100,7 +108,7 @@ module.exports = grammar({
), ),
absent: $ => '<>', absent: $ => '<>',
array_literal: $ => seq('[', repeat(seq($._expression, ',')), optional($._expression), ']'), array_literal: $ => seq('[', sepBy(',', $._expression), ']'),
boolean_literal: $ => choice('true', 'false'), boolean_literal: $ => choice('true', 'false'),
float_literal: $ => token(choice( float_literal: $ => token(choice(
/\d+\.\d+/, /\d+\.\d+/,
@ -113,7 +121,7 @@ module.exports = grammar({
/0b[01]+/, /0b[01]+/,
/0o[0-7]+/ /0o[0-7]+/
)), )),
set_literal: $ => seq('{', repeat(seq($._expression, ',')), optional($._expression), '}'), set_literal: $ => seq('{', sepBy(',', $._expression), '}'),
string_literal: $ => seq( string_literal: $ => seq(
'"', '"',
@ -142,10 +150,6 @@ module.exports = grammar({
} }
}); });
function sepBy1(sep, rule) {
return seq(rule, repeat(seq(sep, rule)))
}
function sepBy(sep, rule) { function sepBy(sep, rule) {
return optional(sepBy1(sep, rule)) return seq(repeat(seq(rule, sep)), optional(rule))
} }

210
src/grammar.json vendored
View File

@ -4,46 +4,32 @@
"rules": { "rules": {
"source_file": { "source_file": {
"type": "SEQ", "type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [ "members": [
{ {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{
"type": "SYMBOL",
"name": "_items"
},
{ {
"type": "REPEAT", "type": "REPEAT",
"content": { "content": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{
"type": "STRING",
"value": ";"
},
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_items" "name": "_items"
}
]
}
}
]
}, },
{ {
"type": "BLANK" "type": "STRING",
"value": ";"
} }
] ]
}
}, },
{ {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{ {
"type": "STRING", "type": "SYMBOL",
"value": ";" "name": "_items"
}, },
{ {
"type": "BLANK" "type": "BLANK"
@ -51,6 +37,8 @@
] ]
} }
] ]
}
]
}, },
"_items": { "_items": {
"type": "CHOICE", "type": "CHOICE",
@ -101,6 +89,10 @@
"type": "SYMBOL", "type": "SYMBOL",
"name": "binary_operation" "name": "binary_operation"
}, },
{
"type": "SYMBOL",
"name": "call"
},
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "index_expression" "name": "index_expression"
@ -111,60 +103,6 @@
} }
] ]
}, },
"index_expression": {
"type": "PREC",
"value": 15,
"content": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "collection",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
},
{
"type": "STRING",
"value": "["
},
{
"type": "FIELD",
"name": "indices",
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_expression"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "SYMBOL",
"name": "_expression"
}
]
}
}
]
}
},
{
"type": "STRING",
"value": "]"
}
]
}
},
"binary_operation": { "binary_operation": {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
@ -745,6 +683,122 @@
} }
] ]
}, },
"call": {
"type": "PREC",
"value": 15,
"content": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "name",
"content": {
"type": "SYMBOL",
"name": "identifier"
}
},
{
"type": "STRING",
"value": "("
},
{
"type": "FIELD",
"name": "arguments",
"content": {
"type": "SEQ",
"members": [
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_expression"
},
{
"type": "STRING",
"value": ","
}
]
}
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_expression"
},
{
"type": "BLANK"
}
]
}
]
}
},
{
"type": "STRING",
"value": ")"
}
]
}
},
"index_expression": {
"type": "PREC",
"value": 15,
"content": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "collection",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
},
{
"type": "STRING",
"value": "["
},
{
"type": "FIELD",
"name": "indices",
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_expression"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "SYMBOL",
"name": "_expression"
}
]
}
}
]
}
},
{
"type": "STRING",
"value": "]"
}
]
}
},
"unary_operation": { "unary_operation": {
"type": "PREC", "type": "PREC",
"value": 13, "value": 13,
@ -823,6 +877,9 @@
"type": "STRING", "type": "STRING",
"value": "[" "value": "["
}, },
{
"type": "SEQ",
"members": [
{ {
"type": "REPEAT", "type": "REPEAT",
"content": { "content": {
@ -850,6 +907,8 @@
"type": "BLANK" "type": "BLANK"
} }
] ]
}
]
}, },
{ {
"type": "STRING", "type": "STRING",
@ -917,6 +976,9 @@
"type": "STRING", "type": "STRING",
"value": "{" "value": "{"
}, },
{
"type": "SEQ",
"members": [
{ {
"type": "REPEAT", "type": "REPEAT",
"content": { "content": {
@ -944,6 +1006,8 @@
"type": "BLANK" "type": "BLANK"
} }
] ]
}
]
}, },
{ {
"type": "STRING", "type": "STRING",

114
src/node-types.json vendored
View File

@ -23,6 +23,10 @@
"type": "boolean_literal", "type": "boolean_literal",
"named": true "named": true
}, },
{
"type": "call",
"named": true
},
{ {
"type": "float_literal", "type": "float_literal",
"named": true "named": true
@ -78,6 +82,10 @@
"type": "boolean_literal", "type": "boolean_literal",
"named": true "named": true
}, },
{
"type": "call",
"named": true
},
{ {
"type": "float_literal", "type": "float_literal",
"named": true "named": true
@ -144,6 +152,10 @@
"type": "boolean_literal", "type": "boolean_literal",
"named": true "named": true
}, },
{
"type": "call",
"named": true
},
{ {
"type": "float_literal", "type": "float_literal",
"named": true "named": true
@ -320,6 +332,10 @@
"type": "boolean_literal", "type": "boolean_literal",
"named": true "named": true
}, },
{
"type": "call",
"named": true
},
{ {
"type": "float_literal", "type": "float_literal",
"named": true "named": true
@ -357,6 +373,80 @@
"named": true, "named": true,
"fields": {} "fields": {}
}, },
{
"type": "call",
"named": true,
"fields": {
"arguments": {
"multiple": true,
"required": false,
"types": [
{
"type": ",",
"named": false
},
{
"type": "absent",
"named": true
},
{
"type": "array_literal",
"named": true
},
{
"type": "binary_operation",
"named": true
},
{
"type": "boolean_literal",
"named": true
},
{
"type": "call",
"named": true
},
{
"type": "float_literal",
"named": true
},
{
"type": "identifier",
"named": true
},
{
"type": "index_expression",
"named": true
},
{
"type": "integer_literal",
"named": true
},
{
"type": "set_literal",
"named": true
},
{
"type": "string_literal",
"named": true
},
{
"type": "unary_operation",
"named": true
}
]
},
"name": {
"multiple": false,
"required": true,
"types": [
{
"type": "identifier",
"named": true
}
]
}
}
},
{ {
"type": "index_expression", "type": "index_expression",
"named": true, "named": true,
@ -381,6 +471,10 @@
"type": "boolean_literal", "type": "boolean_literal",
"named": true "named": true
}, },
{
"type": "call",
"named": true
},
{ {
"type": "float_literal", "type": "float_literal",
"named": true "named": true
@ -435,6 +529,10 @@
"type": "boolean_literal", "type": "boolean_literal",
"named": true "named": true
}, },
{
"type": "call",
"named": true
},
{ {
"type": "float_literal", "type": "float_literal",
"named": true "named": true
@ -491,6 +589,10 @@
"type": "boolean_literal", "type": "boolean_literal",
"named": true "named": true
}, },
{
"type": "call",
"named": true
},
{ {
"type": "float_literal", "type": "float_literal",
"named": true "named": true
@ -595,6 +697,10 @@
"type": "boolean_literal", "type": "boolean_literal",
"named": true "named": true
}, },
{
"type": "call",
"named": true
},
{ {
"type": "float_literal", "type": "float_literal",
"named": true "named": true
@ -634,6 +740,14 @@
"type": "\"", "type": "\"",
"named": false "named": false
}, },
{
"type": "(",
"named": false
},
{
"type": ")",
"named": false
},
{ {
"type": "*", "type": "*",
"named": false "named": false

5170
src/parser.c vendored

File diff suppressed because it is too large Load Diff