more tests; add function expressions

This commit is contained in:
mhoffm
2021-06-11 23:32:22 +02:00
parent 08ce0c6653
commit 2226687dd7
8 changed files with 2413 additions and 1683 deletions

View File

@@ -58,7 +58,7 @@ module.exports = grammar({
$.literal_value,
$.collection_value,
$.variable_expr,
// $.function_call,
$.function_call,
$.for_expr,
seq($.expr_term, $.index),
seq($.expr_term, $.get_attr),
@@ -154,6 +154,14 @@ module.exports = grammar({
variable_expr: $ => $.identifier,
function_call: $ => seq($.identifier, '(', optional($.function_arguments), ')'),
function_arguments: $ => seq(
$.expression,
repeat(seq(',', $.expression)),
optional(choice(',', '...'))
),
// http://stackoverflow.com/questions/13014947/regex-to-match-a-c-style-multiline-comment/36328890#36328890
comment: $ => token(choice(
seq('#', /.*/),

View File

@@ -152,6 +152,10 @@
"type": "SYMBOL",
"name": "variable_expr"
},
{
"type": "SYMBOL",
"name": "function_call"
},
{
"type": "SYMBOL",
"name": "for_expr"
@@ -686,6 +690,81 @@
"type": "SYMBOL",
"name": "identifier"
},
"function_call": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "STRING",
"value": "("
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "function_arguments"
},
{
"type": "BLANK"
}
]
},
{
"type": "STRING",
"value": ")"
}
]
},
"function_arguments": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "expression"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "SYMBOL",
"name": "expression"
}
]
}
},
{
"type": "CHOICE",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "STRING",
"value": "..."
}
]
},
{
"type": "BLANK"
}
]
}
]
},
"comment": {
"type": "TOKEN",
"content": {

View File

@@ -156,6 +156,10 @@
"type": "for_expr",
"named": true
},
{
"type": "function_call",
"named": true
},
{
"type": "get_attr",
"named": true
@@ -312,6 +316,40 @@
]
}
},
{
"type": "function_arguments",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "expression",
"named": true
}
]
}
},
{
"type": "function_call",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "function_arguments",
"named": true
},
{
"type": "identifier",
"named": true
}
]
}
},
{
"type": "get_attr",
"named": true,

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,43 @@
==================
collection value tuple
==================
foo = [1, 2, "foo"]
---
(config_file
(body
(attribute
(identifier)
(expression
(expr_term
(collection_value
(tuple
(expression (expr_term (literal_value (numeric_lit))))
(expression (expr_term (literal_value (numeric_lit))))
(expression (expr_term (literal_value (string_lit)))))))))))
==================
collection value object
==================
foo = {1: 2, "foo"="bar"}
---
(config_file
(body
(attribute
(identifier)
(expression
(expr_term
(collection_value
(object
(object_elem
(expression (expr_term (literal_value (numeric_lit))))
(expression (expr_term (literal_value (numeric_lit)))))
(object_elem
(expression (expr_term (literal_value (string_lit))))
(expression (expr_term (literal_value (string_lit))))))))))))

View File

@@ -0,0 +1,86 @@
==================
nonary function call
==================
foo = bar()
---
(config_file
(body
(attribute
(identifier)
(expression
(expr_term
(function_call
(identifier)))))))
==================
unary function call
==================
foo = bar("foo")
---
(config_file
(body
(attribute
(identifier)
(expression
(expr_term
(function_call
(identifier)
(function_arguments
(expression
(expr_term
(literal_value
(string_lit)))))))))))
==================
variadic function call
==================
foo = bar(x...)
---
(config_file
(body
(attribute
(identifier)
(expression
(expr_term
(function_call
(identifier)
(function_arguments
(expression
(expr_term
(variable_expr
(identifier)))))))))))
==================
multiline function call
==================
foo = bar(
"a",
"b",
"c"
)
---
(config_file
(body
(attribute
(identifier)
(expression
(expr_term
(function_call
(identifier)
(function_arguments
(expression (expr_term (literal_value (string_lit))))
(expression (expr_term (literal_value (string_lit))))
(expression (expr_term (literal_value (string_lit)))))))))))

View File

@@ -118,6 +118,24 @@ foo = "bar"
(literal_value
(string_lit)))))))
==================
string literal escaped newline
==================
foo = "bar\nbaz"
---
(config_file
(body
(attribute
(identifier)
(expression
(expr_term
(literal_value
(string_lit)))))))
==================
string literal multi line error
==================
@@ -187,22 +205,3 @@ foo = null
(literal_value
(null_lit)))))))
==================
collection value tuple
==================
foo = [1, 2, "foo"]
---
(config_file
(body
(attribute
(identifier)
(expression
(expr_term
(collection_value
(tuple
(expression (expr_term (literal_value (numeric_lit))))
(expression (expr_term (literal_value (numeric_lit))))
(expression (expr_term (literal_value (string_lit)))))))))))

86
test/corpus/splat.txt Normal file
View File

@@ -0,0 +1,86 @@
==================
get attr
==================
foo = bar.baz
---
(config_file
(body
(attribute
(identifier)
(expression
(expr_term
(expr_term
(variable_expr
(identifier)))
(get_attr
(identifier)))))))
==================
get index
==================
foo = bar[1]
---
(config_file
(body
(attribute
(identifier)
(expression
(expr_term
(expr_term
(variable_expr
(identifier)))
(index
(expression
(expr_term
(literal_value
(numeric_lit))))))))))
==================
attr splat
==================
foo = bar.*.foo
---
(config_file
(body
(attribute
(identifier)
(expression
(expr_term
(expr_term
(variable_expr
(identifier)))
(splat
(attr_splat
(get_attr
(identifier)))))))))
==================
full splat
==================
foo = bar[*].foo
---
(config_file
(body
(attribute
(identifier)
(expression
(expr_term
(expr_term
(variable_expr
(identifier)))
(splat
(full_splat
(get_attr
(identifier)))))))))