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 Assignment
========== ==========
@ -34,6 +51,23 @@ array[X, 1..23] of var int: simple_decl = some_call(X);
(declaration (base_type (primitive_type)) (identifier)) (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)))) (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 Goal
==== ====
@ -70,3 +104,18 @@ output ["something"];
(source_file (source_file
(output (array_literal (string_literal)))) (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: ($) => _item: ($) =>
choice( choice(
$.annotation,
$.assignment, $.assignment,
$.declaration,
$.constraint, $.constraint,
$.declaration,
$.function_item,
$.goal, $.goal,
$.include, $.include,
$.output $.output,
$.predicate
// TODO: Other statements types // TODO: Other statements types
), ),
annotation: ($) =>
seq(
"annotation",
field("name", $.identifier),
optional(field("parameters", $._parameters)),
optional(seq("=", field("expr", $._expression)))
),
assignment: ($) => assignment: ($) =>
seq(field("name", $.identifier), "=", field("expr", $._expression)), seq(field("name", $.identifier), "=", field("expr", $._expression)),
@ -61,6 +72,17 @@ module.exports = grammar({
optional(seq("=", field("expr", $._expression))) 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: ($) => goal: ($) =>
seq( seq(
"solve", "solve",
@ -78,6 +100,19 @@ module.exports = grammar({
output: ($) => seq("output", $._expression), 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: ($) => _expression: ($) =>
choice( choice(
$.identifier, $.identifier,

343
src/grammar.json vendored
View File

@ -43,17 +43,25 @@
"_item": { "_item": {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{
"type": "SYMBOL",
"name": "annotation"
},
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "assignment" "name": "assignment"
}, },
{
"type": "SYMBOL",
"name": "constraint"
},
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "declaration" "name": "declaration"
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "constraint" "name": "function_item"
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
@ -66,6 +74,68 @@
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "output" "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": { "goal": {
"type": "SEQ", "type": "SEQ",
"members": [ "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": { "_expression": {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [

248
src/node-types.json vendored
View File

@ -85,6 +85,10 @@
"type": "_item", "type": "_item",
"named": true, "named": true,
"subtypes": [ "subtypes": [
{
"type": "annotation",
"named": true
},
{ {
"type": "assignment", "type": "assignment",
"named": true "named": true
@ -97,6 +101,10 @@
"type": "declaration", "type": "declaration",
"named": true "named": true
}, },
{
"type": "function_item",
"named": true
},
{ {
"type": "goal", "type": "goal",
"named": true "named": true
@ -108,6 +116,10 @@
{ {
"type": "output", "type": "output",
"named": true "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", "type": "array_comprehension",
"named": true, "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", "type": "generator",
"named": true, "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", "type": "prefix_operator",
"named": true, "named": true,
@ -978,6 +1210,10 @@
"type": "ann", "type": "ann",
"named": false "named": false
}, },
{
"type": "annotation",
"named": false
},
{ {
"type": "array", "type": "array",
"named": false "named": false
@ -1026,6 +1262,10 @@
"type": "float_literal", "type": "float_literal",
"named": true "named": true
}, },
{
"type": "function",
"named": false
},
{ {
"type": "identifier", "type": "identifier",
"named": true "named": true
@ -1090,6 +1330,10 @@
"type": "par", "type": "par",
"named": false "named": false
}, },
{
"type": "predicate",
"named": false
},
{ {
"type": "satisfy", "type": "satisfy",
"named": false "named": false
@ -1118,6 +1362,10 @@
"type": "symdiff", "type": "symdiff",
"named": false "named": false
}, },
{
"type": "test",
"named": false
},
{ {
"type": "then", "type": "then",
"named": false "named": false

20122
src/parser.c vendored

File diff suppressed because it is too large Load Diff