1
0

Add declaration items

This commit is contained in:
Jip J. Dekker 2021-01-13 16:22:03 +11:00
parent a5df25da33
commit c04041712d
6 changed files with 8479 additions and 6129 deletions

View File

@ -21,6 +21,19 @@ constraint true;
(source_file
(constraint (boolean_literal)))
===========
Declaration
===========
var int: simple_decl;
array[X, 1..23] of var int: simple_decl = some_call(X);
---
(source_file
(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))))
====
Goal
====

112
corpus/types.txt Normal file
View File

@ -0,0 +1,112 @@
=====
Array
=====
array[int] of int: single_index;
array[int, int] of int: two_indices;
array[X] of int: named_index;
array[1..10] of int: expr_index;
---
(source_file
(declaration (array_type (base_type (primitive_type)) (base_type (primitive_type))) (identifier))
(declaration (array_type (base_type (primitive_type)) (base_type (primitive_type)) (base_type (primitive_type))) (identifier))
(declaration (array_type (base_type (identifier)) (base_type (primitive_type))) (identifier))
(declaration (array_type (base_type (infix_operator (integer_literal) (integer_literal))) (base_type (primitive_type))) (identifier)))
==========
Identifier
==========
named: n;
mybool: b;
integer: i;
---
(source_file
(declaration (base_type (identifier)) (identifier))
(declaration (base_type (identifier)) (identifier))
(declaration (base_type (identifier)) (identifier)))
==========
Expression
==========
1..10: x;
-1..length(X): y;
---
(source_file
(declaration (base_type (infix_operator (integer_literal) (integer_literal))) (identifier))
(declaration (base_type (infix_operator (prefix_operator (integer_literal)) (call (identifier) (identifier)))) (identifier)))
========
Optional
========
opt int: i;
var opt int: voi;
par opt int: poi;
---
(source_file
(declaration (base_type (primitive_type)) (identifier))
(declaration (base_type (primitive_type)) (identifier))
(declaration (base_type (primitive_type)) (identifier)))
=======
Par/Var
=======
par int: p;
var float: v;
var X: named_domain;
par 1..10: expr_domain;
---
(source_file
(declaration (base_type (primitive_type)) (identifier))
(declaration (base_type (primitive_type)) (identifier))
(declaration (base_type (identifier)) (identifier))
(declaration (base_type (infix_operator (integer_literal) (integer_literal))) (identifier)))
=========
Primitive
=========
ann: a;
bool: b;
float: f;
int: i;
string: s;
---
(source_file
(declaration (base_type (primitive_type)) (identifier))
(declaration (base_type (primitive_type)) (identifier))
(declaration (base_type (primitive_type)) (identifier))
(declaration (base_type (primitive_type)) (identifier))
(declaration (base_type (primitive_type)) (identifier)))
===
Set
===
set of int: basic_set;
set of var opt int: qualified_type_set;
set of X: named_type_set;
set of 1..10: expr_type_set;
---
(source_file
(declaration (set_type (base_type (primitive_type))) (identifier))
(declaration (set_type (base_type (primitive_type))) (identifier))
(declaration (set_type (base_type (identifier))) (identifier))
(declaration (set_type (base_type (infix_operator (integer_literal) (integer_literal)))) (identifier)))

View File

@ -18,6 +18,8 @@ const PREC = {
equivalence: 1,
};
const primitive_types = ["ann", "bool", "float", "int", "string"];
module.exports = grammar({
name: "minizinc",
@ -25,9 +27,12 @@ module.exports = grammar({
word: ($) => $.identifier,
conflicts: ($) => [[$._expression, $.generator]],
conflicts: ($) => [
[$._expression, $.generator],
[$._expression, $.assignment],
],
supertypes: ($) => [$._expression, $._item],
supertypes: ($) => [$._expression, $._item, $._type],
rules: {
source_file: ($) => seq(sepBy(";", $._item)),
@ -35,6 +40,7 @@ module.exports = grammar({
_item: ($) =>
choice(
$.assignment,
$.declaration,
$.constraint,
$.goal,
$.include,
@ -47,6 +53,14 @@ module.exports = grammar({
constraint: ($) => seq("constraint", $._expression),
declaration: ($) =>
seq(
field("type", $._type),
":",
field("name", $.identifier),
optional(seq("=", field("expr", $._expression)))
),
goal: ($) =>
seq(
"solve",
@ -197,6 +211,18 @@ module.exports = grammar({
'"'
),
_type: ($) => choice($.array_type, $.base_type, $.set_type),
array_type: ($) =>
seq("array", "[", sepBy1(",", $.base_type), "]", "of", $.base_type),
base_type: ($) =>
seq(
optional(field("var_par", choice("var", "par"))),
optional(field("opt", "opt")),
choice($.primitive_type, $._expression)
),
primitive_type: ($) => choice(...primitive_types),
set_type: ($) => seq("set", "of", $.base_type),
_literal: ($) =>
choice(
$.absent,

241
src/grammar.json vendored
View File

@ -47,6 +47,10 @@
"type": "SYMBOL",
"name": "assignment"
},
{
"type": "SYMBOL",
"name": "declaration"
},
{
"type": "SYMBOL",
"name": "constraint"
@ -103,6 +107,56 @@
}
]
},
"declaration": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "type",
"content": {
"type": "SYMBOL",
"name": "_type"
}
},
{
"type": "STRING",
"value": ":"
},
{
"type": "FIELD",
"name": "name",
"content": {
"type": "SYMBOL",
"name": "identifier"
}
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "="
},
{
"type": "FIELD",
"name": "expr",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
}
]
},
{
"type": "BLANK"
}
]
}
]
},
"goal": {
"type": "SEQ",
"members": [
@ -1340,6 +1394,186 @@
}
]
},
"_type": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "array_type"
},
{
"type": "SYMBOL",
"name": "base_type"
},
{
"type": "SYMBOL",
"name": "set_type"
}
]
},
"array_type": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "array"
},
{
"type": "STRING",
"value": "["
},
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "base_type"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "SYMBOL",
"name": "base_type"
}
]
}
},
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "BLANK"
}
]
}
]
},
{
"type": "STRING",
"value": "]"
},
{
"type": "STRING",
"value": "of"
},
{
"type": "SYMBOL",
"name": "base_type"
}
]
},
"base_type": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "FIELD",
"name": "var_par",
"content": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "var"
},
{
"type": "STRING",
"value": "par"
}
]
}
},
{
"type": "BLANK"
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "FIELD",
"name": "opt",
"content": {
"type": "STRING",
"value": "opt"
}
},
{
"type": "BLANK"
}
]
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "primitive_type"
},
{
"type": "SYMBOL",
"name": "_expression"
}
]
}
]
},
"primitive_type": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "ann"
},
{
"type": "STRING",
"value": "bool"
},
{
"type": "STRING",
"value": "float"
},
{
"type": "STRING",
"value": "int"
},
{
"type": "STRING",
"value": "string"
}
]
},
"set_type": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "set"
},
{
"type": "STRING",
"value": "of"
},
{
"type": "SYMBOL",
"name": "base_type"
}
]
},
"_literal": {
"type": "CHOICE",
"members": [
@ -1671,13 +1905,18 @@
[
"_expression",
"generator"
],
[
"_expression",
"assignment"
]
],
"externals": [],
"inline": [],
"supertypes": [
"_expression",
"_item"
"_item",
"_type"
]
}

185
src/node-types.json vendored
View File

@ -89,6 +89,10 @@
"type": "constraint",
"named": true
},
{
"type": "declaration",
"named": true
},
{
"type": "goal",
"named": true
@ -103,6 +107,24 @@
}
]
},
{
"type": "_type",
"named": true,
"subtypes": [
{
"type": "array_type",
"named": true
},
{
"type": "base_type",
"named": true
},
{
"type": "set_type",
"named": true
}
]
},
{
"type": "array_comprehension",
"named": true,
@ -137,6 +159,21 @@
]
}
},
{
"type": "array_type",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "base_type",
"named": true
}
]
}
},
{
"type": "assignment",
"named": true,
@ -163,6 +200,50 @@
}
}
},
{
"type": "base_type",
"named": true,
"fields": {
"opt": {
"multiple": false,
"required": false,
"types": [
{
"type": "opt",
"named": false
}
]
},
"var_par": {
"multiple": false,
"required": false,
"types": [
{
"type": "par",
"named": false
},
{
"type": "var",
"named": false
}
]
}
},
"children": {
"multiple": false,
"required": true,
"types": [
{
"type": "_expression",
"named": true
},
{
"type": "primitive_type",
"named": true
}
]
}
},
{
"type": "boolean_literal",
"named": true,
@ -228,6 +309,42 @@
]
}
},
{
"type": "declaration",
"named": true,
"fields": {
"expr": {
"multiple": false,
"required": false,
"types": [
{
"type": "_expression",
"named": true
}
]
},
"name": {
"multiple": false,
"required": true,
"types": [
{
"type": "identifier",
"named": true
}
]
},
"type": {
"multiple": false,
"required": true,
"types": [
{
"type": "_type",
"named": true
}
]
}
}
},
{
"type": "generator",
"named": true,
@ -587,6 +704,11 @@
]
}
},
{
"type": "primitive_type",
"named": true,
"fields": {}
},
{
"type": "set_comprehension",
"named": true,
@ -621,6 +743,21 @@
]
}
},
{
"type": "set_type",
"named": true,
"fields": {},
"children": {
"multiple": false,
"required": true,
"types": [
{
"type": "base_type",
"named": true
}
]
}
},
{
"type": "source_file",
"named": true,
@ -727,6 +864,10 @@
"type": "/\\",
"named": false
},
{
"type": ":",
"named": false
},
{
"type": "::",
"named": false
@ -791,6 +932,18 @@
"type": "absent",
"named": true
},
{
"type": "ann",
"named": false
},
{
"type": "array",
"named": false
},
{
"type": "bool",
"named": false
},
{
"type": "constraint",
"named": false
@ -823,6 +976,10 @@
"type": "false",
"named": false
},
{
"type": "float",
"named": false
},
{
"type": "float_literal",
"named": true
@ -843,6 +1000,10 @@
"type": "include",
"named": false
},
{
"type": "int",
"named": false
},
{
"type": "integer_literal",
"named": true
@ -867,18 +1028,38 @@
"type": "not",
"named": false
},
{
"type": "of",
"named": false
},
{
"type": "opt",
"named": false
},
{
"type": "output",
"named": false
},
{
"type": "par",
"named": false
},
{
"type": "satisfy",
"named": false
},
{
"type": "set",
"named": false
},
{
"type": "solve",
"named": false
},
{
"type": "string",
"named": false
},
{
"type": "subset",
"named": false
@ -903,6 +1084,10 @@
"type": "union",
"named": false
},
{
"type": "var",
"named": false
},
{
"type": "where",
"named": false

14027
src/parser.c vendored

File diff suppressed because it is too large Load Diff