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

Update julia grammar #520

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
19 changes: 5 additions & 14 deletions lang/semgrep-grammars/src/semgrep-julia/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,12 @@ const base_grammar = require('tree-sitter-julia/grammar');
module.exports = grammar(base_grammar, {
name: 'julia',

conflicts: ($, previous) => previous.concat([
[$.typed_parameter, $._expression],
]),

/*
Support for semgrep ellipsis ('...') and metavariables ('$FOO'),
if they're not already part of the base grammar.
*/
rules: {
semgrep_ellipsis: $ => '...',
semgrep_ellipsis: _ => '...',

catch_clause: $ => prec(1, seq(
'catch',
Expand All @@ -33,8 +29,8 @@ module.exports = grammar(base_grammar, {
// metavariable usually is. This is fine, because having a slightly more
// permissive grammar is OK, we will just dispatch in the Generic
// translation.
semgrep_extended_metavariable: $ =>
/\$[A-Z_][a-zA-Z_0-9]*/,
semgrep_extended_metavariable: _ =>
/\$[A-Z_][a-zA-Z_0-9]*/,

// Metavariables
// We allow an identifier to be a simple metavariable regex, so that
Expand Down Expand Up @@ -69,23 +65,18 @@ module.exports = grammar(base_grammar, {
// tree stays the same. Otherwise, we will fail `tree-sitter` tests.
alias(prec(999,
$.semgrep_extended_metavariable
), $.identifier)
), $.identifier)
),

_expression: ($, previous) => choice(
previous,
$.semgrep_ellipsis,
$.deep_expression
$.deep_expression
),

_statement: ($, previous) => choice(
previous,
$.semgrep_ellipsis,
),

typed_parameter: ($, previous) => choice(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why removing this? We can still have ellpisis in type parameters even with this removal?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Julia doesn't have a distinction between expressions and patterns, so function signatures are parsed as function calls, and checked in the lowering phase (after macro expansion). Having these parameter rules added a lot of duplication and conflicts, so we removed them.

We can still have ellpisis in type parameters even with this removal?

Yes. Ellipsis would now be parsed as splat_expression.

previous,
$.semgrep_ellipsis,
),
}
});
19 changes: 11 additions & 8 deletions lang/semgrep-grammars/src/semgrep-julia/test/corpus/semgrep.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ end

(source_file
(function_definition
(identifier)
(parameter_list
(typed_parameter
(semgrep_ellipsis)))
(signature
(call_expression
(identifier)
(argument_list
(semgrep_ellipsis))))
(return_statement
(integer_literal))))

Expand Down Expand Up @@ -122,9 +123,11 @@ end

(source_file
(function_definition
(interpolation_expression
(identifier))
(parameter_list)
(signature
(call_expression
(interpolation_expression
(identifier))
(argument_list)))
(assignment
(interpolation_expression
(identifier))
Expand Down Expand Up @@ -219,7 +222,7 @@ end
(argument_list
(vector_expression))
(do_clause
(parameter_list
(argument_list
(identifier
(semgrep_extended_metavariable)))
(return_statement
Expand Down
Loading