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
|
name: Tree Sitter CI
|
||||||
|
|
||||||
on:
|
on: [push, pull_request]
|
||||||
push:
|
|
||||||
branches: [ master ]
|
|
||||||
pull_request:
|
|
||||||
branches: [ master ]
|
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v2
|
||||||
- name: Setup Node.js
|
- name: Setup Node.js
|
||||||
uses: actions/setup-node@v1
|
uses: actions/setup-node@v1
|
||||||
- name: Install Dependencies
|
- name: Install Dependencies
|
||||||
run: npm install
|
run: npm install
|
||||||
- name: Check if generated file are up-to-date
|
- name: Check if files are formatted correctly
|
||||||
run: npm run build --if-present && git diff --exit-code
|
run: npx prettier --check .
|
||||||
- name: Run parser tests
|
- name: Check if generated file are up-to-date
|
||||||
run: npm test
|
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,
|
or: 1,
|
||||||
implication: 2,
|
implication: 2,
|
||||||
equivalence: 1,
|
equivalence: 1,
|
||||||
}
|
};
|
||||||
|
|
||||||
module.exports = grammar({
|
module.exports = grammar({
|
||||||
name: 'minizinc',
|
name: "minizinc",
|
||||||
|
|
||||||
extras: $ => [/\s/, $.line_comment, $.block_comment],
|
extras: ($) => [/\s/, $.line_comment, $.block_comment],
|
||||||
|
|
||||||
word: $ => $.identifier,
|
word: ($) => $.identifier,
|
||||||
|
|
||||||
conflicts: $ => [
|
conflicts: ($) => [[$._expression, $.generator]],
|
||||||
[$._expression, $.generator],
|
|
||||||
],
|
|
||||||
|
|
||||||
supertypes: $ => [
|
supertypes: ($) => [$._expression, $._item],
|
||||||
$._expression,
|
|
||||||
$._item,
|
|
||||||
],
|
|
||||||
|
|
||||||
rules: {
|
rules: {
|
||||||
source_file: $ => seq(sepBy(';', $._item)),
|
source_file: ($) => seq(sepBy(";", $._item)),
|
||||||
|
|
||||||
_item: $ => choice(
|
_item: ($) =>
|
||||||
$.assignment,
|
choice(
|
||||||
$.constraint,
|
$.assignment,
|
||||||
$.goal,
|
$.constraint,
|
||||||
$.include,
|
$.goal,
|
||||||
$.output,
|
$.include,
|
||||||
// TODO: Other statements types
|
$.output
|
||||||
),
|
// TODO: Other statements types
|
||||||
|
),
|
||||||
|
|
||||||
assignment: $ => seq(
|
assignment: ($) =>
|
||||||
field('name', $.identifier),
|
seq(field("name", $.identifier), "=", field("expr", $._expression)),
|
||||||
'=',
|
|
||||||
field('expr', $._expression)
|
|
||||||
),
|
|
||||||
|
|
||||||
constraint: $ => seq('constraint', $._expression),
|
constraint: ($) => seq("constraint", $._expression),
|
||||||
|
|
||||||
goal: $ => seq(
|
goal: ($) =>
|
||||||
'solve', field('strategy', choice(
|
seq(
|
||||||
'satisfy',
|
"solve",
|
||||||
seq('maximize', $._expression),
|
field(
|
||||||
seq('minimize', $._expression),
|
"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(
|
_expression: ($) =>
|
||||||
$.identifier,
|
choice(
|
||||||
$._literal,
|
$.identifier,
|
||||||
|
$._literal,
|
||||||
|
|
||||||
$.array_comprehension,
|
$.array_comprehension,
|
||||||
$.call,
|
$.call,
|
||||||
$.generator_call,
|
$.generator_call,
|
||||||
$.if_then_else,
|
$.if_then_else,
|
||||||
$.indexed_access,
|
$.indexed_access,
|
||||||
$.infix_operator,
|
$.infix_operator,
|
||||||
$.prefix_operator,
|
$.prefix_operator,
|
||||||
$.set_comprehension,
|
$.set_comprehension,
|
||||||
$.string_interpolation,
|
$.string_interpolation,
|
||||||
$.parenthesised_expression,
|
$.parenthesised_expression
|
||||||
),
|
),
|
||||||
|
|
||||||
parenthesised_expression: $ => seq('(', $._expression, ')'),
|
parenthesised_expression: ($) => seq("(", $._expression, ")"),
|
||||||
|
|
||||||
array_comprehension: $ => seq(
|
array_comprehension: ($) =>
|
||||||
'[', $._expression, '|', sepBy1(',', $.generator), ']',
|
seq("[", $._expression, "|", sepBy1(",", $.generator), "]"),
|
||||||
),
|
|
||||||
|
|
||||||
call: $ => prec(PREC.call, seq(
|
call: ($) =>
|
||||||
field('name', $.identifier),
|
prec(
|
||||||
'(',
|
PREC.call,
|
||||||
field('arguments', sepBy(',', $._expression)),
|
seq(
|
||||||
')',
|
field("name", $.identifier),
|
||||||
)),
|
"(",
|
||||||
|
field("arguments", sepBy(",", $._expression)),
|
||||||
|
")"
|
||||||
|
)
|
||||||
|
),
|
||||||
|
|
||||||
generator_call: $ => prec(PREC.call, seq(
|
generator_call: ($) =>
|
||||||
field('name', $.identifier),
|
prec(
|
||||||
'(',
|
PREC.call,
|
||||||
field('generators', sepBy1(',', $.generator)),
|
seq(
|
||||||
')', '(',
|
field("name", $.identifier),
|
||||||
field('template', $._expression),
|
"(",
|
||||||
')',
|
field("generators", sepBy1(",", $.generator)),
|
||||||
)),
|
")",
|
||||||
|
"(",
|
||||||
|
field("template", $._expression),
|
||||||
|
")"
|
||||||
|
)
|
||||||
|
),
|
||||||
|
|
||||||
generator: $ => seq(
|
generator: ($) =>
|
||||||
$.identifier, 'in', $._expression,
|
seq(
|
||||||
optional(seq('where', $._expression))
|
$.identifier,
|
||||||
),
|
"in",
|
||||||
|
$._expression,
|
||||||
|
optional(seq("where", $._expression))
|
||||||
|
),
|
||||||
|
|
||||||
if_then_else: $ => seq(
|
if_then_else: ($) =>
|
||||||
"if", $._expression,
|
seq(
|
||||||
"then", $._expression,
|
"if",
|
||||||
repeat(seq("elseif", $._expression, "then", $._expression)),
|
$._expression,
|
||||||
optional(seq("else", $._expression)),
|
"then",
|
||||||
"endif",
|
$._expression,
|
||||||
),
|
repeat(seq("elseif", $._expression, "then", $._expression)),
|
||||||
|
optional(seq("else", $._expression)),
|
||||||
|
"endif"
|
||||||
|
),
|
||||||
|
|
||||||
indexed_access: $ => prec(PREC.call, seq(
|
indexed_access: ($) =>
|
||||||
field('collection', $._expression),
|
prec(
|
||||||
'[',
|
PREC.call,
|
||||||
field('indices', seq($._expression, repeat(seq(',', $._expression)))),
|
seq(
|
||||||
']',
|
field("collection", $._expression),
|
||||||
)),
|
"[",
|
||||||
|
field("indices", seq($._expression, repeat(seq(",", $._expression)))),
|
||||||
|
"]"
|
||||||
|
)
|
||||||
|
),
|
||||||
|
|
||||||
infix_operator: $ => {
|
infix_operator: ($) => {
|
||||||
const table = [
|
const table = [
|
||||||
[prec.left, PREC.equivalence, '<->'],
|
[prec.left, PREC.equivalence, "<->"],
|
||||||
[prec.left, PREC.implication, choice('->', '<-')],
|
[prec.left, PREC.implication, choice("->", "<-")],
|
||||||
[prec.left, PREC.or, '\\/'],
|
[prec.left, PREC.or, "\\/"],
|
||||||
[prec.left, PREC.xor, 'xor'],
|
[prec.left, PREC.xor, "xor"],
|
||||||
[prec.left, PREC.and, '/\\'],
|
[prec.left, PREC.and, "/\\"],
|
||||||
// TODO: Should really be nonassoc
|
// TODO: Should really be nonassoc
|
||||||
[prec.left, PREC.comparative, choice('=', '==', '!=', '<', '<=', '>', '>=', 'in', 'subset', 'superset')],
|
// prettier-ignore
|
||||||
[prec.left, PREC.union, 'union'],
|
[prec.left, PREC.comparative, choice(
|
||||||
[prec.left, PREC.diff, 'diff'],
|
"=", "==", "!=", "<", "<=", ">", ">=", "in", "subset", "superset"
|
||||||
[prec.left, PREC.symdiff, 'symdiff'],
|
)],
|
||||||
[prec.left, PREC.intersect, 'intersect'],
|
[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
|
// TODO: Could be nonassoc, will always give type error
|
||||||
[prec.left, PREC.dotdot, '..'],
|
[prec.left, PREC.dotdot, ".."],
|
||||||
[prec.left, PREC.additive, choice('+', '-', '++')],
|
[prec.left, PREC.additive, choice("+", "-", "++")],
|
||||||
[prec.left, PREC.multiplicative, choice('*', '/', 'div', 'mod')],
|
[prec.left, PREC.multiplicative, choice("*", "/", "div", "mod")],
|
||||||
[prec.left, PREC.exponent, '^'],
|
[prec.left, PREC.exponent, "^"],
|
||||||
[prec.left, PREC.annotation, '::'],
|
[prec.left, PREC.annotation, "::"],
|
||||||
];
|
];
|
||||||
|
|
||||||
return choice(...table.map(([assoc, precedence, operator]) => assoc(precedence, seq(
|
return choice(
|
||||||
field('left', $._expression),
|
...table.map(([assoc, precedence, operator]) =>
|
||||||
field('operator', operator),
|
assoc(
|
||||||
field('right', $._expression),
|
precedence,
|
||||||
))));
|
seq(
|
||||||
|
field("left", $._expression),
|
||||||
|
field("operator", operator),
|
||||||
|
field("right", $._expression)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
},
|
},
|
||||||
|
|
||||||
prefix_operator: $ => prec(PREC.unary, seq(
|
prefix_operator: ($) =>
|
||||||
field('operator', choice('-', 'not', '¬')),
|
prec(
|
||||||
$._expression
|
PREC.unary,
|
||||||
)),
|
seq(field("operator", choice("-", "not", "¬")), $._expression)
|
||||||
|
),
|
||||||
|
|
||||||
set_comprehension: $ => seq(
|
set_comprehension: ($) =>
|
||||||
'{', $._expression, '|', sepBy1(',', $.generator), '}',
|
seq("{", $._expression, "|", sepBy1(",", $.generator), "}"),
|
||||||
),
|
|
||||||
|
|
||||||
// TODO: Decide if string_literal and string_interpolation should be combined
|
// TODO: Decide if string_literal and string_interpolation should be combined
|
||||||
string_interpolation: $ => seq(
|
string_interpolation: ($) =>
|
||||||
'"', optional($.string_content), repeat1(seq('\\(', $._expression, ')', optional($.string_content))), '"',
|
seq(
|
||||||
),
|
'"',
|
||||||
|
optional($.string_content),
|
||||||
|
repeat1(seq("\\(", $._expression, ")", optional($.string_content))),
|
||||||
|
'"'
|
||||||
|
),
|
||||||
|
|
||||||
_literal: $ => choice(
|
_literal: ($) =>
|
||||||
$.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(
|
|
||||||
'\\',
|
|
||||||
choice(
|
choice(
|
||||||
/[^xuU]/,
|
$.absent,
|
||||||
/\d{2,3}/,
|
$.array_literal,
|
||||||
/x[0-9a-fA-F]{2,}/,
|
$.boolean_literal,
|
||||||
/u[0-9a-fA-F]{4}/,
|
$.float_literal,
|
||||||
/U[0-9a-fA-F]{8}/
|
$.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('%', /.*/)),
|
string_literal: ($) =>
|
||||||
block_comment: $ => token(seq('/*', /([^*]|\*[^\/]|\n)*?\*?/, '*/')),
|
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) {
|
function sepBy(sep, rule) {
|
||||||
return seq(repeat(seq(rule, sep)), optional(rule))
|
return seq(repeat(seq(rule, sep)), optional(rule));
|
||||||
}
|
}
|
||||||
|
|
||||||
function sepBy1(sep, 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 {
|
try {
|
||||||
module.exports = require("./build/Debug/tree_sitter_minizinc_binding");
|
module.exports = require("./build/Debug/tree_sitter_minizinc_binding");
|
||||||
} catch (_) {
|
} catch (_) {
|
||||||
throw error
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,6 +25,7 @@
|
|||||||
"nan": "^2.14.1"
|
"nan": "^2.14.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"prettier": "2.2.1",
|
||||||
"tree-sitter-cli": "^0.16.9"
|
"tree-sitter-cli": "^0.16.9"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user