Format files using prettier
This commit is contained in:
parent
ffaa229697
commit
a5df25da33
26
.github/workflows/tree-sitter.yml
vendored
26
.github/workflows/tree-sitter.yml
vendored
@ -1,22 +1,20 @@
|
||||
name: Tree Sitter CI
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ master ]
|
||||
pull_request:
|
||||
branches: [ master ]
|
||||
on: [push, pull_request]
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Setup Node.js
|
||||
uses: actions/setup-node@v1
|
||||
- name: Install Dependencies
|
||||
run: npm install
|
||||
- name: Check if generated file are up-to-date
|
||||
run: npm run build --if-present && git diff --exit-code
|
||||
- name: Run parser tests
|
||||
run: npm test
|
||||
- uses: actions/checkout@v2
|
||||
- name: Setup Node.js
|
||||
uses: actions/setup-node@v1
|
||||
- name: Install Dependencies
|
||||
run: npm install
|
||||
- name: Check if files are formatted correctly
|
||||
run: npx prettier --check .
|
||||
- name: Check if generated file are up-to-date
|
||||
run: npm run build --if-present && git diff --exit-code
|
||||
- name: Run parser tests
|
||||
run: npm test
|
||||
|
3
.prettierignore
Normal file
3
.prettierignore
Normal file
@ -0,0 +1,3 @@
|
||||
build/
|
||||
node_modules/
|
||||
src/
|
1
.prettierrc.json
Normal file
1
.prettierrc.json
Normal file
@ -0,0 +1 @@
|
||||
{}
|
354
grammar.js
354
grammar.js
@ -16,210 +16,242 @@ const PREC = {
|
||||
or: 1,
|
||||
implication: 2,
|
||||
equivalence: 1,
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = grammar({
|
||||
name: 'minizinc',
|
||||
name: "minizinc",
|
||||
|
||||
extras: $ => [/\s/, $.line_comment, $.block_comment],
|
||||
extras: ($) => [/\s/, $.line_comment, $.block_comment],
|
||||
|
||||
word: $ => $.identifier,
|
||||
word: ($) => $.identifier,
|
||||
|
||||
conflicts: $ => [
|
||||
[$._expression, $.generator],
|
||||
],
|
||||
conflicts: ($) => [[$._expression, $.generator]],
|
||||
|
||||
supertypes: $ => [
|
||||
$._expression,
|
||||
$._item,
|
||||
],
|
||||
supertypes: ($) => [$._expression, $._item],
|
||||
|
||||
rules: {
|
||||
source_file: $ => seq(sepBy(';', $._item)),
|
||||
source_file: ($) => seq(sepBy(";", $._item)),
|
||||
|
||||
_item: $ => choice(
|
||||
$.assignment,
|
||||
$.constraint,
|
||||
$.goal,
|
||||
$.include,
|
||||
$.output,
|
||||
// TODO: Other statements types
|
||||
),
|
||||
_item: ($) =>
|
||||
choice(
|
||||
$.assignment,
|
||||
$.constraint,
|
||||
$.goal,
|
||||
$.include,
|
||||
$.output
|
||||
// TODO: Other statements types
|
||||
),
|
||||
|
||||
assignment: $ => seq(
|
||||
field('name', $.identifier),
|
||||
'=',
|
||||
field('expr', $._expression)
|
||||
),
|
||||
assignment: ($) =>
|
||||
seq(field("name", $.identifier), "=", field("expr", $._expression)),
|
||||
|
||||
constraint: $ => seq('constraint', $._expression),
|
||||
constraint: ($) => seq("constraint", $._expression),
|
||||
|
||||
goal: $ => seq(
|
||||
'solve', field('strategy', choice(
|
||||
'satisfy',
|
||||
seq('maximize', $._expression),
|
||||
seq('minimize', $._expression),
|
||||
)),
|
||||
),
|
||||
goal: ($) =>
|
||||
seq(
|
||||
"solve",
|
||||
field(
|
||||
"strategy",
|
||||
choice(
|
||||
"satisfy",
|
||||
seq("maximize", $._expression),
|
||||
seq("minimize", $._expression)
|
||||
)
|
||||
)
|
||||
),
|
||||
|
||||
include: $ => seq('include', $.string_literal),
|
||||
include: ($) => seq("include", $.string_literal),
|
||||
|
||||
output: $ => seq('output', $._expression),
|
||||
output: ($) => seq("output", $._expression),
|
||||
|
||||
_expression: $ => choice(
|
||||
$.identifier,
|
||||
$._literal,
|
||||
_expression: ($) =>
|
||||
choice(
|
||||
$.identifier,
|
||||
$._literal,
|
||||
|
||||
$.array_comprehension,
|
||||
$.call,
|
||||
$.generator_call,
|
||||
$.if_then_else,
|
||||
$.indexed_access,
|
||||
$.infix_operator,
|
||||
$.prefix_operator,
|
||||
$.set_comprehension,
|
||||
$.string_interpolation,
|
||||
$.parenthesised_expression,
|
||||
),
|
||||
$.array_comprehension,
|
||||
$.call,
|
||||
$.generator_call,
|
||||
$.if_then_else,
|
||||
$.indexed_access,
|
||||
$.infix_operator,
|
||||
$.prefix_operator,
|
||||
$.set_comprehension,
|
||||
$.string_interpolation,
|
||||
$.parenthesised_expression
|
||||
),
|
||||
|
||||
parenthesised_expression: $ => seq('(', $._expression, ')'),
|
||||
parenthesised_expression: ($) => seq("(", $._expression, ")"),
|
||||
|
||||
array_comprehension: $ => seq(
|
||||
'[', $._expression, '|', sepBy1(',', $.generator), ']',
|
||||
),
|
||||
array_comprehension: ($) =>
|
||||
seq("[", $._expression, "|", sepBy1(",", $.generator), "]"),
|
||||
|
||||
call: $ => prec(PREC.call, seq(
|
||||
field('name', $.identifier),
|
||||
'(',
|
||||
field('arguments', sepBy(',', $._expression)),
|
||||
')',
|
||||
)),
|
||||
call: ($) =>
|
||||
prec(
|
||||
PREC.call,
|
||||
seq(
|
||||
field("name", $.identifier),
|
||||
"(",
|
||||
field("arguments", sepBy(",", $._expression)),
|
||||
")"
|
||||
)
|
||||
),
|
||||
|
||||
generator_call: $ => prec(PREC.call, seq(
|
||||
field('name', $.identifier),
|
||||
'(',
|
||||
field('generators', sepBy1(',', $.generator)),
|
||||
')', '(',
|
||||
field('template', $._expression),
|
||||
')',
|
||||
)),
|
||||
generator_call: ($) =>
|
||||
prec(
|
||||
PREC.call,
|
||||
seq(
|
||||
field("name", $.identifier),
|
||||
"(",
|
||||
field("generators", sepBy1(",", $.generator)),
|
||||
")",
|
||||
"(",
|
||||
field("template", $._expression),
|
||||
")"
|
||||
)
|
||||
),
|
||||
|
||||
generator: $ => seq(
|
||||
$.identifier, 'in', $._expression,
|
||||
optional(seq('where', $._expression))
|
||||
),
|
||||
generator: ($) =>
|
||||
seq(
|
||||
$.identifier,
|
||||
"in",
|
||||
$._expression,
|
||||
optional(seq("where", $._expression))
|
||||
),
|
||||
|
||||
if_then_else: $ => seq(
|
||||
"if", $._expression,
|
||||
"then", $._expression,
|
||||
repeat(seq("elseif", $._expression, "then", $._expression)),
|
||||
optional(seq("else", $._expression)),
|
||||
"endif",
|
||||
),
|
||||
if_then_else: ($) =>
|
||||
seq(
|
||||
"if",
|
||||
$._expression,
|
||||
"then",
|
||||
$._expression,
|
||||
repeat(seq("elseif", $._expression, "then", $._expression)),
|
||||
optional(seq("else", $._expression)),
|
||||
"endif"
|
||||
),
|
||||
|
||||
indexed_access: $ => prec(PREC.call, seq(
|
||||
field('collection', $._expression),
|
||||
'[',
|
||||
field('indices', seq($._expression, repeat(seq(',', $._expression)))),
|
||||
']',
|
||||
)),
|
||||
indexed_access: ($) =>
|
||||
prec(
|
||||
PREC.call,
|
||||
seq(
|
||||
field("collection", $._expression),
|
||||
"[",
|
||||
field("indices", seq($._expression, repeat(seq(",", $._expression)))),
|
||||
"]"
|
||||
)
|
||||
),
|
||||
|
||||
infix_operator: $ => {
|
||||
infix_operator: ($) => {
|
||||
const table = [
|
||||
[prec.left, PREC.equivalence, '<->'],
|
||||
[prec.left, PREC.implication, choice('->', '<-')],
|
||||
[prec.left, PREC.or, '\\/'],
|
||||
[prec.left, PREC.xor, 'xor'],
|
||||
[prec.left, PREC.and, '/\\'],
|
||||
[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'],
|
||||
// prettier-ignore
|
||||
[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, '::'],
|
||||
[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),
|
||||
))));
|
||||
return choice(
|
||||
...table.map(([assoc, precedence, operator]) =>
|
||||
assoc(
|
||||
precedence,
|
||||
seq(
|
||||
field("left", $._expression),
|
||||
field("operator", operator),
|
||||
field("right", $._expression)
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
},
|
||||
|
||||
prefix_operator: $ => prec(PREC.unary, seq(
|
||||
field('operator', choice('-', 'not', '¬')),
|
||||
$._expression
|
||||
)),
|
||||
prefix_operator: ($) =>
|
||||
prec(
|
||||
PREC.unary,
|
||||
seq(field("operator", choice("-", "not", "¬")), $._expression)
|
||||
),
|
||||
|
||||
set_comprehension: $ => seq(
|
||||
'{', $._expression, '|', sepBy1(',', $.generator), '}',
|
||||
),
|
||||
set_comprehension: ($) =>
|
||||
seq("{", $._expression, "|", sepBy1(",", $.generator), "}"),
|
||||
|
||||
// TODO: Decide if string_literal and string_interpolation should be combined
|
||||
string_interpolation: $ => seq(
|
||||
'"', optional($.string_content), repeat1(seq('\\(', $._expression, ')', optional($.string_content))), '"',
|
||||
),
|
||||
string_interpolation: ($) =>
|
||||
seq(
|
||||
'"',
|
||||
optional($.string_content),
|
||||
repeat1(seq("\\(", $._expression, ")", optional($.string_content))),
|
||||
'"'
|
||||
),
|
||||
|
||||
_literal: $ => choice(
|
||||
$.absent,
|
||||
$.array_literal,
|
||||
$.boolean_literal,
|
||||
$.float_literal,
|
||||
$.integer_literal,
|
||||
$.set_literal,
|
||||
$.string_literal,
|
||||
),
|
||||
|
||||
absent: $ => '<>',
|
||||
array_literal: $ => seq('[', sepBy(',', $._expression), ']'),
|
||||
boolean_literal: $ => choice('true', 'false'),
|
||||
float_literal: $ => token(choice(
|
||||
/\d+\.\d+/,
|
||||
/\d+(\.\d+)?[Ee][+-]?\d+/,
|
||||
// TODO: Hexadecimal floating point numbers
|
||||
)),
|
||||
integer_literal: $ => token(choice(
|
||||
/[0-9]+/,
|
||||
/0x[0-9a-fA-F]+/,
|
||||
/0b[01]+/,
|
||||
/0o[0-7]+/
|
||||
)),
|
||||
set_literal: $ => seq('{', sepBy(',', $._expression), '}'),
|
||||
|
||||
string_literal: $ => seq('"', alias(optional($.string_content), 'content'), '"'),
|
||||
string_content: $ => repeat1(choice(
|
||||
token.immediate(prec(1, /[^"\n\\]+/)),
|
||||
$.escape_sequence
|
||||
)),
|
||||
escape_sequence: $ => token.immediate(seq(
|
||||
'\\',
|
||||
_literal: ($) =>
|
||||
choice(
|
||||
/[^xuU]/,
|
||||
/\d{2,3}/,
|
||||
/x[0-9a-fA-F]{2,}/,
|
||||
/u[0-9a-fA-F]{4}/,
|
||||
/U[0-9a-fA-F]{8}/
|
||||
)
|
||||
)),
|
||||
$.absent,
|
||||
$.array_literal,
|
||||
$.boolean_literal,
|
||||
$.float_literal,
|
||||
$.integer_literal,
|
||||
$.set_literal,
|
||||
$.string_literal
|
||||
),
|
||||
|
||||
identifier: $ => /[A-Za-z][A-Za-z0-9_]*/,
|
||||
absent: ($) => "<>",
|
||||
array_literal: ($) => seq("[", sepBy(",", $._expression), "]"),
|
||||
boolean_literal: ($) => choice("true", "false"),
|
||||
float_literal: ($) =>
|
||||
token(
|
||||
choice(
|
||||
/\d+\.\d+/,
|
||||
/\d+(\.\d+)?[Ee][+-]?\d+/
|
||||
// TODO: Hexadecimal floating point numbers
|
||||
)
|
||||
),
|
||||
integer_literal: ($) =>
|
||||
token(choice(/[0-9]+/, /0x[0-9a-fA-F]+/, /0b[01]+/, /0o[0-7]+/)),
|
||||
set_literal: ($) => seq("{", sepBy(",", $._expression), "}"),
|
||||
|
||||
line_comment: $ => token(seq('%', /.*/)),
|
||||
block_comment: $ => token(seq('/*', /([^*]|\*[^\/]|\n)*?\*?/, '*/')),
|
||||
string_literal: ($) =>
|
||||
seq('"', alias(optional($.string_content), "content"), '"'),
|
||||
string_content: ($) =>
|
||||
repeat1(choice(token.immediate(prec(1, /[^"\n\\]+/)), $.escape_sequence)),
|
||||
escape_sequence: ($) =>
|
||||
token.immediate(
|
||||
seq(
|
||||
"\\",
|
||||
choice(
|
||||
/[^xuU]/,
|
||||
/\d{2,3}/,
|
||||
/x[0-9a-fA-F]{2,}/,
|
||||
/u[0-9a-fA-F]{4}/,
|
||||
/U[0-9a-fA-F]{8}/
|
||||
)
|
||||
)
|
||||
),
|
||||
|
||||
}
|
||||
identifier: ($) => /[A-Za-z][A-Za-z0-9_]*/,
|
||||
|
||||
line_comment: ($) => token(seq("%", /.*/)),
|
||||
block_comment: ($) => token(seq("/*", /([^*]|\*[^\/]|\n)*?\*?/, "*/")),
|
||||
},
|
||||
});
|
||||
|
||||
function sepBy(sep, rule) {
|
||||
return seq(repeat(seq(rule, sep)), optional(rule))
|
||||
return seq(repeat(seq(rule, sep)), optional(rule));
|
||||
}
|
||||
|
||||
function sepBy1(sep, rule) {
|
||||
return seq(rule, repeat(seq(sep, rule)), optional(sep))
|
||||
return seq(rule, repeat(seq(sep, rule)), optional(sep));
|
||||
}
|
||||
|
2
index.js
2
index.js
@ -4,7 +4,7 @@ try {
|
||||
try {
|
||||
module.exports = require("./build/Debug/tree_sitter_minizinc_binding");
|
||||
} catch (_) {
|
||||
throw error
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -25,6 +25,7 @@
|
||||
"nan": "^2.14.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"prettier": "2.2.1",
|
||||
"tree-sitter-cli": "^0.16.9"
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user