Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correcting exisiting grammar and adding test cases #20

Merged
merged 5 commits into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 115 additions & 18 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,13 +330,28 @@ module.exports = grammar({
'in',
field('quant_target', $.quant_target),
'{',
field('expr1', $.expression),
choice(
field('expr1', $.expression),
seq(
field('dotted_name', $.dotted_name),
field('string', $.string)
)
),
optional(seq(
'if',
field('expr2', $.expression)
)),
'}',
)),

quant_target: $ => choice(
quant_target: $ => prec(1, choice(
field('dictionary_or_list', $.identifier),
$.dictionary,
$.string,
$.list,
$.list_comprehension,
$.config_expr,
$.dictionary_comprehension,
seq(
'[',
field('integer', $.integer),
Expand All @@ -346,7 +361,7 @@ module.exports = grammar({
)),
']'
)
),
)),

quant_op: $ => choice(
'all',
Expand Down Expand Up @@ -460,7 +475,7 @@ module.exports = grammar({
$._dedent,
),

dotted_name: $ => prec(1, sep1($.identifier, choice('?.','.',))),
dotted_name: $ => prec.left(2, sep1($.identifier, choice('?.','.',))),

// Patterns

Expand Down Expand Up @@ -507,7 +522,7 @@ module.exports = grammar({
field('alias', $.expression),
)),

primary_expression: $ => choice(
primary_expression: $ => prec(2, choice(
$.binary_operator,
$.identifier,
$.string,
Expand Down Expand Up @@ -535,7 +550,7 @@ module.exports = grammar({
$.null_coalesce,
$.string_literal_expr,
$.config_expr,
),
)),

paren_expression: $ => seq(
'(', $.expression, ')'
Expand Down Expand Up @@ -569,16 +584,70 @@ module.exports = grammar({
'"'
),

config_expr: $ => prec(1, seq(
config_expr: $ => seq(
'{',
sep1(',', seq(
field('key', $.identifier),
'=',
field('value', $.expression)
optional(choice(
$.config_entries,
seq(
'\n',
optional($.config_entries)
)
)),
'}'
),

config_entries: $ => seq(
$.config_entry,
repeat(seq(
choice(
',',
seq(
optional(','),
'\n'
)
),
$.config_entry
)),
optional(','),
optional('\n')
),

config_entry: $ => choice(
seq(
$.test,
choice(':', '=', '+='),
$.test
),
$.dictionary_splat,
$.if_entry
),

test: $ => prec(1, choice(
$.dotted_name,
$.identifier,
$.string,
$.integer,
$.float,
$.paren_expression
)),

dotted_identifier: $ => prec(4, seq(
$.identifier,
repeat(seq('.', $.identifier))
)),

double_star_expr: $ => seq(
'**',
$.expression
),

if_entry: $ => seq(
'if',
$.expression,
'then',
$.expression
),

binary_operator: $ => {
const table = [
[prec.left, '+', PREC.plus],
Expand Down Expand Up @@ -648,17 +717,22 @@ module.exports = grammar({
),

unification: $ => seq(
field('left', $.identifier),
field('left', $.dotted_name),
':',
field('right', $.schema_expr),
),

attribute: $ => prec(PREC.call, seq(
field('object', $.primary_expression),
'.',
field('attribute', $.identifier),
attribute: $ => prec.right(11, seq(
field('name', $.identifier),
optional(seq(
field('is_mutable', $.isMutableFlag),
field('type', choice($.type, $.union_type)),
optional(seq('=', field('value', $.expression)))
))
)),


isMutableFlag: $ => '_',

optional_attribute: $ => prec(PREC.call, seq(
field('object', $.primary_expression),
'?.',
Expand Down Expand Up @@ -811,6 +885,25 @@ module.exports = grammar({
$.expression,
)),

raw_string: $ => prec(64, seq(
$.raw_string_start,
repeat($.string_content),
$.string_end,
)),

raw_string_content: $ => prec.right(repeat1(
choice(
$._not_escape_sequence,
$._raw_string_content,
))),

raw_string_start: $ => token(seq(
choice('r', 'R'),
'"'
)),

_raw_string_content: $ => /[^"]/, // matches any character except "

string: $ => seq(
$.string_start,
repeat($.string_content),
Expand All @@ -825,6 +918,8 @@ module.exports = grammar({
$._string_content,
))),

escape_interpolation: _ => token.immediate(/\$\{[^}]*\}/),

escape_sequence: _ => token.immediate(prec(1, seq(
'\\',
choice(
Expand All @@ -840,6 +935,8 @@ module.exports = grammar({

_not_escape_sequence: _ => token.immediate('\\'),

_string_content: _ => token.immediate(/[^\\{}]+/),

integer: $ => token(choice(
seq(
choice('0x', '0X'),
Expand Down Expand Up @@ -872,7 +969,7 @@ module.exports = grammar({
));
},

identifier: _ => /\$?[_\p{XID_Start}][_\p{XID_Continue}]*/,
identifier: _ => /[_$a-zA-Z][_$\p{XID_Continue}]*/,

true: _ => 'True',
false: _ => 'False',
Expand Down
Loading
Loading