1
0

Add function/predicate/annotation definitions

This commit is contained in:
Jip J. Dekker 2021-01-14 13:32:56 +11:00
parent 44ea60a191
commit 2983b86b9e
5 changed files with 11625 additions and 9176 deletions

View File

@ -1,3 +1,20 @@
==========
Annotation
==========
annotation simple;
annotation redirect = simple;
annotation funclike(int, int);
annotation funcdef(int: x, int: y) = other(x, y);
---
(source_file
(annotation (identifier))
(annotation (identifier) (identifier))
(annotation (identifier) (base_type (primitive_type)) (base_type (primitive_type)))
(annotation (identifier) (base_type (primitive_type)) (identifier) (base_type (primitive_type)) (identifier) (call (identifier) (identifier) (identifier))))
==========
Assignment
==========
@ -34,6 +51,23 @@ array[X, 1..23] of var int: simple_decl = some_call(X);
(declaration (base_type (primitive_type)) (identifier))
(declaration (array_type (base_type (identifier)) (base_type (infix_operator (integer_literal) (integer_literal))) (base_type (primitive_type))) (identifier) (call (identifier) (identifier))))
========
Function
========
function int: this();
function var int: that() = this();
function X: with_args(int, float);
function X: with_named_args(X: x, bool: b) = something(b, x);
---
(source_file
(function_item (base_type (primitive_type)) (identifier))
(function_item (base_type (primitive_type)) (identifier) (call (identifier)))
(function_item (base_type (identifier)) (identifier) (base_type (primitive_type)) (base_type (primitive_type)))
(function_item (base_type (identifier)) (identifier) (base_type (identifier)) (identifier) (base_type (primitive_type)) (identifier) (call (identifier) (identifier) (identifier))))
====
Goal
====
@ -70,3 +104,18 @@ output ["something"];
(source_file
(output (array_literal (string_literal))))
=========
Predicate
=========
test pred();
predicate redirecht() = pred();
predicate with_args(1..10: x, var bool: b) = pred();
---
(source_file
(predicate (identifier))
(predicate (identifier) (call (identifier)))
(predicate (identifier) (base_type (infix_operator (integer_literal) (integer_literal))) (identifier) (base_type (primitive_type)) (identifier) (call (identifier))))

View File

@ -39,15 +39,26 @@ module.exports = grammar({
_item: ($) =>
choice(
$.annotation,
$.assignment,
$.declaration,
$.constraint,
$.declaration,
$.function_item,
$.goal,
$.include,
$.output
$.output,
$.predicate
// TODO: Other statements types
),
annotation: ($) =>
seq(
"annotation",
field("name", $.identifier),
optional(field("parameters", $._parameters)),
optional(seq("=", field("expr", $._expression)))
),
assignment: ($) =>
seq(field("name", $.identifier), "=", field("expr", $._expression)),
@ -61,6 +72,17 @@ module.exports = grammar({
optional(seq("=", field("expr", $._expression)))
),
function_item: ($) =>
seq(
"function",
field("type", $._type),
":",
field("name", $.identifier),
field("parameters", $._parameters),
optional(field("annotations", $._annotations)),
optional(seq("=", field("expr", $._expression)))
),
goal: ($) =>
seq(
"solve",
@ -78,6 +100,19 @@ module.exports = grammar({
output: ($) => seq("output", $._expression),
predicate: ($) =>
seq(
field("type", choice("predicate", "test")),
field("name", $.identifier),
field("parameters", $._parameters),
optional(field("annotations", $._annotations)),
optional(seq("=", field("expr", $._expression)))
),
_annotations: ($) => repeat1(seq("::", $._expression)),
_parameters: ($) =>
seq("(", sepBy(",", seq($._type, optional(seq(":", $.identifier)))), ")"),
_expression: ($) =>
choice(
$.identifier,

343
src/grammar.json vendored
View File

@ -43,17 +43,25 @@
"_item": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "annotation"
},
{
"type": "SYMBOL",
"name": "assignment"
},
{
"type": "SYMBOL",
"name": "constraint"
},
{
"type": "SYMBOL",
"name": "declaration"
},
{
"type": "SYMBOL",
"name": "constraint"
"name": "function_item"
},
{
"type": "SYMBOL",
@ -66,6 +74,68 @@
{
"type": "SYMBOL",
"name": "output"
},
{
"type": "SYMBOL",
"name": "predicate"
}
]
},
"annotation": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "annotation"
},
{
"type": "FIELD",
"name": "name",
"content": {
"type": "SYMBOL",
"name": "identifier"
}
},
{
"type": "CHOICE",
"members": [
{
"type": "FIELD",
"name": "parameters",
"content": {
"type": "SYMBOL",
"name": "_parameters"
}
},
{
"type": "BLANK"
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "="
},
{
"type": "FIELD",
"name": "expr",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
}
]
},
{
"type": "BLANK"
}
]
}
]
},
@ -157,6 +227,84 @@
}
]
},
"function_item": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "function"
},
{
"type": "FIELD",
"name": "type",
"content": {
"type": "SYMBOL",
"name": "_type"
}
},
{
"type": "STRING",
"value": ":"
},
{
"type": "FIELD",
"name": "name",
"content": {
"type": "SYMBOL",
"name": "identifier"
}
},
{
"type": "FIELD",
"name": "parameters",
"content": {
"type": "SYMBOL",
"name": "_parameters"
}
},
{
"type": "CHOICE",
"members": [
{
"type": "FIELD",
"name": "annotations",
"content": {
"type": "SYMBOL",
"name": "_annotations"
}
},
{
"type": "BLANK"
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "="
},
{
"type": "FIELD",
"name": "expr",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
}
]
},
{
"type": "BLANK"
}
]
}
]
},
"goal": {
"type": "SEQ",
"members": [
@ -231,6 +379,199 @@
}
]
},
"predicate": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "type",
"content": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "predicate"
},
{
"type": "STRING",
"value": "test"
}
]
}
},
{
"type": "FIELD",
"name": "name",
"content": {
"type": "SYMBOL",
"name": "identifier"
}
},
{
"type": "FIELD",
"name": "parameters",
"content": {
"type": "SYMBOL",
"name": "_parameters"
}
},
{
"type": "CHOICE",
"members": [
{
"type": "FIELD",
"name": "annotations",
"content": {
"type": "SYMBOL",
"name": "_annotations"
}
},
{
"type": "BLANK"
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "="
},
{
"type": "FIELD",
"name": "expr",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
}
]
},
{
"type": "BLANK"
}
]
}
]
},
"_annotations": {
"type": "REPEAT1",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "::"
},
{
"type": "SYMBOL",
"name": "_expression"
}
]
}
},
"_parameters": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "("
},
{
"type": "SEQ",
"members": [
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_type"
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ":"
},
{
"type": "SYMBOL",
"name": "identifier"
}
]
},
{
"type": "BLANK"
}
]
}
]
},
{
"type": "STRING",
"value": ","
}
]
}
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_type"
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ":"
},
{
"type": "SYMBOL",
"name": "identifier"
}
]
},
{
"type": "BLANK"
}
]
}
]
},
{
"type": "BLANK"
}
]
}
]
},
{
"type": "STRING",
"value": ")"
}
]
},
"_expression": {
"type": "CHOICE",
"members": [

248
src/node-types.json vendored
View File

@ -85,6 +85,10 @@
"type": "_item",
"named": true,
"subtypes": [
{
"type": "annotation",
"named": true
},
{
"type": "assignment",
"named": true
@ -97,6 +101,10 @@
"type": "declaration",
"named": true
},
{
"type": "function_item",
"named": true
},
{
"type": "goal",
"named": true
@ -108,6 +116,10 @@
{
"type": "output",
"named": true
},
{
"type": "predicate",
"named": true
}
]
},
@ -129,6 +141,62 @@
}
]
},
{
"type": "annotation",
"named": true,
"fields": {
"expr": {
"multiple": false,
"required": false,
"types": [
{
"type": "_expression",
"named": true
}
]
},
"name": {
"multiple": false,
"required": true,
"types": [
{
"type": "identifier",
"named": true
}
]
},
"parameters": {
"multiple": true,
"required": false,
"types": [
{
"type": "(",
"named": false
},
{
"type": ")",
"named": false
},
{
"type": ",",
"named": false
},
{
"type": ":",
"named": false
},
{
"type": "_type",
"named": true
},
{
"type": "identifier",
"named": true
}
]
}
}
},
{
"type": "array_comprehension",
"named": true,
@ -349,6 +417,86 @@
}
}
},
{
"type": "function_item",
"named": true,
"fields": {
"annotations": {
"multiple": true,
"required": false,
"types": [
{
"type": "::",
"named": false
},
{
"type": "_expression",
"named": true
}
]
},
"expr": {
"multiple": false,
"required": false,
"types": [
{
"type": "_expression",
"named": true
}
]
},
"name": {
"multiple": false,
"required": true,
"types": [
{
"type": "identifier",
"named": true
}
]
},
"parameters": {
"multiple": true,
"required": true,
"types": [
{
"type": "(",
"named": false
},
{
"type": ")",
"named": false
},
{
"type": ",",
"named": false
},
{
"type": ":",
"named": false
},
{
"type": "_type",
"named": true
},
{
"type": "identifier",
"named": true
}
]
},
"type": {
"multiple": false,
"required": true,
"types": [
{
"type": "_type",
"named": true
}
]
}
}
},
{
"type": "generator",
"named": true,
@ -712,6 +860,90 @@
]
}
},
{
"type": "predicate",
"named": true,
"fields": {
"annotations": {
"multiple": true,
"required": false,
"types": [
{
"type": "::",
"named": false
},
{
"type": "_expression",
"named": true
}
]
},
"expr": {
"multiple": false,
"required": false,
"types": [
{
"type": "_expression",
"named": true
}
]
},
"name": {
"multiple": false,
"required": true,
"types": [
{
"type": "identifier",
"named": true
}
]
},
"parameters": {
"multiple": true,
"required": true,
"types": [
{
"type": "(",
"named": false
},
{
"type": ")",
"named": false
},
{
"type": ",",
"named": false
},
{
"type": ":",
"named": false
},
{
"type": "_type",
"named": true
},
{
"type": "identifier",
"named": true
}
]
},
"type": {
"multiple": false,
"required": true,
"types": [
{
"type": "predicate",
"named": false
},
{
"type": "test",
"named": false
}
]
}
}
},
{
"type": "prefix_operator",
"named": true,
@ -978,6 +1210,10 @@
"type": "ann",
"named": false
},
{
"type": "annotation",
"named": false
},
{
"type": "array",
"named": false
@ -1026,6 +1262,10 @@
"type": "float_literal",
"named": true
},
{
"type": "function",
"named": false
},
{
"type": "identifier",
"named": true
@ -1090,6 +1330,10 @@
"type": "par",
"named": false
},
{
"type": "predicate",
"named": false
},
{
"type": "satisfy",
"named": false
@ -1118,6 +1362,10 @@
"type": "symdiff",
"named": false
},
{
"type": "test",
"named": false
},
{
"type": "then",
"named": false

20122
src/parser.c vendored

File diff suppressed because it is too large Load Diff