1
0

Add operator expressions

This commit is contained in:
Jip J. Dekker 2020-10-09 18:19:53 +11:00
parent 1eab2c23fc
commit a2088f0636
5 changed files with 3200 additions and 371 deletions

74
corpus/expressions.txt Normal file
View File

@ -0,0 +1,74 @@
===============
Binary Operator
===============
exponent = x ^ y;
mult = x * y;
div = x / y;
idiv = x div y;
add = x + y;
minus = x - y;
set_inter = x intersect y;
set_dotdot = x..y;
set_symdiff = x symdiff y;
set_diff = x diff y;
set_union = x union y;
same = x = y;
samedouble = x == y;
different = x != y;
smaller = x < y;
bigger = x > y;
smalleq = x <= y;
bigeq = x >= y;
set_in = x in y;
set_sub = x subset y;
set_super = x superset y;
and = x /\ y;
bool_xor = x xor y;
or = x \/ y;
---
(source_file
(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)))
(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)))
(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)))
(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)))
(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)))
(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))))
==============
Unary Operator
==============
negatitve_number = -5;
negative_ident = -num;
negated_bool = not true;
unicode_negation = ¬ b;
---
(source_file
(assignment_item (identifier) (unary_operation (integer_literal)))
(assignment_item (identifier) (unary_operation (identifier)))
(assignment_item (identifier) (unary_operation (boolean_literal)))
(assignment_item (identifier) (unary_operation (identifier))))

View File

@ -1,3 +1,21 @@
const PREC = {
annotation: 13,
exponent: 12,
multiplicative: 11,
additive: 10,
intersect: 9,
dotdot: 8,
symdiff: 7,
diff: 6,
union: 5,
comparative: 4,
and: 3,
xor: 2,
or: 1,
implication: 2,
equivalence: 1,
}
module.exports = grammar({
name: 'minizinc',
@ -20,11 +38,45 @@ module.exports = grammar({
),
_expression: $ => choice(
$._literal,
$.identifier,
$._literal,
$.unary_operation,
$.binary_operation,
// TODO: Other expression types
),
binary_operation: $ => {
const table = [
[prec.left, PREC.equivalence, '<->'],
[prec.left, PREC.implication, choice('->', '<-')],
[prec.left, PREC.or, '\\/'],
[prec.left, PREC.xor, 'xor'],
[prec.left, PREC.and, '/\\'],
// TODO: Should really be nonassoc
[prec.left, PREC.comparative, choice('=', '==', '!=', '<', '<=', '>', '>=', 'in', 'subset', 'superset')],
[prec.left, PREC.union, 'union'],
[prec.left, PREC.diff, 'diff'],
[prec.left, PREC.symdiff, 'symdiff'],
[prec.left, PREC.intersect, 'intersect'],
// TODO: Could be nonassoc, will always give type error
[prec.left, PREC.dotdot, '..'],
[prec.left, PREC.additive, choice('+', '-', '++')],
[prec.left, PREC.multiplicative, choice('*', '/', 'div', 'mod')],
[prec.left, PREC.exponent, '^'],
[prec.left, PREC.annotation, '::'],
];
return choice(...table.map(([assoc, precedence, operator]) => assoc(precedence, seq(
field('left', $._expression),
field('operator', operator),
field('right', $._expression),
))));
},
unary_operation: $ => seq(
field('operator', choice('-', 'not', '¬')),
$._expression
),
_literal: $ => choice(
$.absent,

620
src/grammar.json vendored
View File

@ -89,13 +89,631 @@
"_expression": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "SYMBOL",
"name": "_literal"
},
{
"type": "SYMBOL",
"name": "identifier"
"name": "unary_operation"
},
{
"type": "SYMBOL",
"name": "binary_operation"
}
]
},
"binary_operation": {
"type": "CHOICE",
"members": [
{
"type": "PREC_LEFT",
"value": 1,
"content": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "left",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
},
{
"type": "FIELD",
"name": "operator",
"content": {
"type": "STRING",
"value": "<->"
}
},
{
"type": "FIELD",
"name": "right",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
}
]
}
},
{
"type": "PREC_LEFT",
"value": 2,
"content": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "left",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
},
{
"type": "FIELD",
"name": "operator",
"content": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "->"
},
{
"type": "STRING",
"value": "<-"
}
]
}
},
{
"type": "FIELD",
"name": "right",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
}
]
}
},
{
"type": "PREC_LEFT",
"value": 1,
"content": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "left",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
},
{
"type": "FIELD",
"name": "operator",
"content": {
"type": "STRING",
"value": "\\/"
}
},
{
"type": "FIELD",
"name": "right",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
}
]
}
},
{
"type": "PREC_LEFT",
"value": 2,
"content": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "left",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
},
{
"type": "FIELD",
"name": "operator",
"content": {
"type": "STRING",
"value": "xor"
}
},
{
"type": "FIELD",
"name": "right",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
}
]
}
},
{
"type": "PREC_LEFT",
"value": 3,
"content": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "left",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
},
{
"type": "FIELD",
"name": "operator",
"content": {
"type": "STRING",
"value": "/\\"
}
},
{
"type": "FIELD",
"name": "right",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
}
]
}
},
{
"type": "PREC_LEFT",
"value": 4,
"content": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "left",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
},
{
"type": "FIELD",
"name": "operator",
"content": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "="
},
{
"type": "STRING",
"value": "=="
},
{
"type": "STRING",
"value": "!="
},
{
"type": "STRING",
"value": "<"
},
{
"type": "STRING",
"value": "<="
},
{
"type": "STRING",
"value": ">"
},
{
"type": "STRING",
"value": ">="
},
{
"type": "STRING",
"value": "in"
},
{
"type": "STRING",
"value": "subset"
},
{
"type": "STRING",
"value": "superset"
}
]
}
},
{
"type": "FIELD",
"name": "right",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
}
]
}
},
{
"type": "PREC_LEFT",
"value": 5,
"content": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "left",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
},
{
"type": "FIELD",
"name": "operator",
"content": {
"type": "STRING",
"value": "union"
}
},
{
"type": "FIELD",
"name": "right",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
}
]
}
},
{
"type": "PREC_LEFT",
"value": 6,
"content": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "left",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
},
{
"type": "FIELD",
"name": "operator",
"content": {
"type": "STRING",
"value": "diff"
}
},
{
"type": "FIELD",
"name": "right",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
}
]
}
},
{
"type": "PREC_LEFT",
"value": 7,
"content": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "left",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
},
{
"type": "FIELD",
"name": "operator",
"content": {
"type": "STRING",
"value": "symdiff"
}
},
{
"type": "FIELD",
"name": "right",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
}
]
}
},
{
"type": "PREC_LEFT",
"value": 9,
"content": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "left",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
},
{
"type": "FIELD",
"name": "operator",
"content": {
"type": "STRING",
"value": "intersect"
}
},
{
"type": "FIELD",
"name": "right",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
}
]
}
},
{
"type": "PREC_LEFT",
"value": 8,
"content": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "left",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
},
{
"type": "FIELD",
"name": "operator",
"content": {
"type": "STRING",
"value": ".."
}
},
{
"type": "FIELD",
"name": "right",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
}
]
}
},
{
"type": "PREC_LEFT",
"value": 10,
"content": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "left",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
},
{
"type": "FIELD",
"name": "operator",
"content": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "+"
},
{
"type": "STRING",
"value": "-"
},
{
"type": "STRING",
"value": "++"
}
]
}
},
{
"type": "FIELD",
"name": "right",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
}
]
}
},
{
"type": "PREC_LEFT",
"value": 11,
"content": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "left",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
},
{
"type": "FIELD",
"name": "operator",
"content": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "*"
},
{
"type": "STRING",
"value": "/"
},
{
"type": "STRING",
"value": "div"
},
{
"type": "STRING",
"value": "mod"
}
]
}
},
{
"type": "FIELD",
"name": "right",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
}
]
}
},
{
"type": "PREC_LEFT",
"value": 12,
"content": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "left",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
},
{
"type": "FIELD",
"name": "operator",
"content": {
"type": "STRING",
"value": "^"
}
},
{
"type": "FIELD",
"name": "right",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
}
]
}
},
{
"type": "PREC_LEFT",
"value": 13,
"content": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "left",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
},
{
"type": "FIELD",
"name": "operator",
"content": {
"type": "STRING",
"value": "::"
}
},
{
"type": "FIELD",
"name": "right",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
}
]
}
}
]
},
"unary_operation": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "operator",
"content": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "-"
},
{
"type": "STRING",
"value": "not"
},
{
"type": "STRING",
"value": "¬"
}
]
}
},
{
"type": "SYMBOL",
"name": "_expression"
}
]
},

402
src/node-types.json vendored
View File

@ -11,6 +11,10 @@
"type": "absent",
"named": true
},
{
"type": "binary_operation",
"named": true
},
{
"type": "boolean_literal",
"named": true
@ -30,6 +34,10 @@
{
"type": "string_literal",
"named": true
},
{
"type": "unary_operation",
"named": true
}
]
},
@ -45,6 +53,214 @@
}
}
},
{
"type": "binary_operation",
"named": true,
"fields": {
"left": {
"multiple": false,
"required": true,
"types": [
{
"type": "absent",
"named": true
},
{
"type": "binary_operation",
"named": true
},
{
"type": "boolean_literal",
"named": true
},
{
"type": "float_literal",
"named": true
},
{
"type": "identifier",
"named": true
},
{
"type": "integer_literal",
"named": true
},
{
"type": "string_literal",
"named": true
},
{
"type": "unary_operation",
"named": true
}
]
},
"operator": {
"multiple": false,
"required": true,
"types": [
{
"type": "!=",
"named": false
},
{
"type": "*",
"named": false
},
{
"type": "+",
"named": false
},
{
"type": "++",
"named": false
},
{
"type": "-",
"named": false
},
{
"type": "->",
"named": false
},
{
"type": "..",
"named": false
},
{
"type": "/",
"named": false
},
{
"type": "/\\",
"named": false
},
{
"type": "::",
"named": false
},
{
"type": "<",
"named": false
},
{
"type": "<-",
"named": false
},
{
"type": "<->",
"named": false
},
{
"type": "<=",
"named": false
},
{
"type": "=",
"named": false
},
{
"type": "==",
"named": false
},
{
"type": ">",
"named": false
},
{
"type": ">=",
"named": false
},
{
"type": "\\/",
"named": false
},
{
"type": "^",
"named": false
},
{
"type": "diff",
"named": false
},
{
"type": "div",
"named": false
},
{
"type": "in",
"named": false
},
{
"type": "intersect",
"named": false
},
{
"type": "mod",
"named": false
},
{
"type": "subset",
"named": false
},
{
"type": "superset",
"named": false
},
{
"type": "symdiff",
"named": false
},
{
"type": "union",
"named": false
},
{
"type": "xor",
"named": false
}
]
},
"right": {
"multiple": false,
"required": true,
"types": [
{
"type": "absent",
"named": true
},
{
"type": "binary_operation",
"named": true
},
{
"type": "boolean_literal",
"named": true
},
{
"type": "float_literal",
"named": true
},
{
"type": "identifier",
"named": true
},
{
"type": "integer_literal",
"named": true
},
{
"type": "string_literal",
"named": true
},
{
"type": "unary_operation",
"named": true
}
]
}
}
},
{
"type": "boolean_literal",
"named": true,
@ -80,22 +296,168 @@
]
}
},
{
"type": "unary_operation",
"named": true,
"fields": {
"operator": {
"multiple": false,
"required": true,
"types": [
{
"type": "-",
"named": false
},
{
"type": "not",
"named": false
},
{
"type": "¬",
"named": false
}
]
}
},
"children": {
"multiple": false,
"required": true,
"types": [
{
"type": "absent",
"named": true
},
{
"type": "binary_operation",
"named": true
},
{
"type": "boolean_literal",
"named": true
},
{
"type": "float_literal",
"named": true
},
{
"type": "identifier",
"named": true
},
{
"type": "integer_literal",
"named": true
},
{
"type": "string_literal",
"named": true
},
{
"type": "unary_operation",
"named": true
}
]
}
},
{
"type": "!=",
"named": false
},
{
"type": "\"",
"named": false
},
{
"type": "*",
"named": false
},
{
"type": "+",
"named": false
},
{
"type": "++",
"named": false
},
{
"type": "-",
"named": false
},
{
"type": "->",
"named": false
},
{
"type": "..",
"named": false
},
{
"type": "/",
"named": false
},
{
"type": "/\\",
"named": false
},
{
"type": "::",
"named": false
},
{
"type": ";",
"named": false
},
{
"type": "<",
"named": false
},
{
"type": "<-",
"named": false
},
{
"type": "<->",
"named": false
},
{
"type": "<=",
"named": false
},
{
"type": "=",
"named": false
},
{
"type": "==",
"named": false
},
{
"type": ">",
"named": false
},
{
"type": ">=",
"named": false
},
{
"type": "\\/",
"named": false
},
{
"type": "^",
"named": false
},
{
"type": "absent",
"named": true
},
{
"type": "diff",
"named": false
},
{
"type": "div",
"named": false
},
{
"type": "escape_sequence",
"named": true
@ -112,12 +474,52 @@
"type": "identifier",
"named": true
},
{
"type": "in",
"named": false
},
{
"type": "integer_literal",
"named": true
},
{
"type": "intersect",
"named": false
},
{
"type": "mod",
"named": false
},
{
"type": "not",
"named": false
},
{
"type": "subset",
"named": false
},
{
"type": "superset",
"named": false
},
{
"type": "symdiff",
"named": false
},
{
"type": "true",
"named": false
},
{
"type": "union",
"named": false
},
{
"type": "xor",
"named": false
},
{
"type": "¬",
"named": false
}
]

2421
src/parser.c vendored

File diff suppressed because it is too large Load Diff