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

Fix parsing of def using flags #74

Merged
merged 2 commits into from
Jan 25, 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
184 changes: 175 additions & 9 deletions corpus/decl/def.nu
Original file line number Diff line number Diff line change
Expand Up @@ -41,48 +41,163 @@ export def test [] {}
(block)))

======
def-env-001-smoke-test
def-001-flags
======

def-env test [] {}
def --env foo [] {}
def --env --wrapped bar [...args] {}
def --wrapped foo [...args] {}
def --wrapped --env bar [...args] {}

-----

(nu_script
(decl_def
(long_flag)
(cmd_identifier)
(parameter_bracks)
(block))
(decl_def
(long_flag)
(long_flag)
(cmd_identifier)
(parameter_bracks
(parameter
(param_rest
(identifier))))
(block))
(decl_def
(long_flag)
(cmd_identifier)
(parameter_bracks
(parameter
(param_rest
(identifier))))
(block))
(decl_def
(long_flag)
(long_flag)
(cmd_identifier)
(parameter_bracks
(parameter
(param_rest
(identifier))))
(block)))

======
def-env-004-with-one-return-type
def-004-flag-with-one-return-type
======

def-env test []: nothing -> string {}
def --env test []: nothing -> string {}
def --env --wrapped test [...args]: nothing -> string {}
def --wrapped test [...args]: nothing -> string {}
def --wrapped --env test [...args]: nothing -> string {}

-----

(nu_script
(decl_def
(long_flag)
(cmd_identifier)
(parameter_bracks)
(returns
(flat_type)
(flat_type))
(block)))
(block))
(decl_def
(long_flag)
(long_flag)
(cmd_identifier)
(parameter_bracks
(parameter
(param_rest
(identifier))))
(returns
(flat_type)
(flat_type))
(block))
(decl_def
(long_flag)
(cmd_identifier)
(parameter_bracks
(parameter
(param_rest
(identifier))))
(returns
(flat_type)
(flat_type))
(block))
(decl_def
(long_flag)
(long_flag)
(cmd_identifier)
(parameter_bracks
(parameter
(param_rest
(identifier))))
(returns
(flat_type)
(flat_type))
(block))
)

======
def-env-005-with-multiple-return-types
def-005-env-with-multiple-return-types
======

def-env test []: [nothing -> string, nothing -> int] {}
def --env test []: [nothing -> string, nothing -> int] {}
def --env --wrapped test [...args]: [nothing -> string, nothing -> int] {}
def --wrapped --env test [...args]: [nothing -> string, nothing -> int] {}
def --wrapped test [...args]: [nothing -> string, nothing -> int] {}

-----

(nu_script
(decl_def
(long_flag)
(cmd_identifier)
(parameter_bracks)
(returns
(flat_type)
(flat_type)
(flat_type)
(flat_type))
(block))
(decl_def
(long_flag)
(long_flag)
(cmd_identifier)
(parameter_bracks
(parameter
(param_rest
(identifier))))
(returns
(flat_type)
(flat_type)
(flat_type)
(flat_type))
(block))
(decl_def
(long_flag)
(long_flag)
(cmd_identifier)
(parameter_bracks
(parameter
(param_rest
(identifier))))
(returns
(flat_type)
(flat_type)
(flat_type)
(flat_type))
(block))
(decl_def
(long_flag)
(cmd_identifier)
(parameter_bracks
(parameter
(param_rest
(identifier))))
(returns
(flat_type)
(flat_type)
Expand All @@ -91,17 +206,68 @@ def-env test []: [nothing -> string, nothing -> int] {}
(block)))

======
def-env-006-with-return-type-complex
def-006-flag-with-return-type-complex
======

def-env test []: nothing -> record<a: string, b: int> {}
def --env test []: nothing -> record<a: string, b: int> {}
def --env --wrapped test [...args]: nothing -> record<a: string, b: int> {}
def --wrapped --env test [...args]: nothing -> record<a: string, b: int> {}
def --wrapped test [...args]: nothing -> record<a: string, b: int> {}

-----

(nu_script
(decl_def
(long_flag)
(cmd_identifier)
(parameter_bracks)
(returns
(flat_type)
(collection_type
(identifier)
(flat_type)
(identifier)
(flat_type)))
(block))
(decl_def
(long_flag)
(long_flag)
(cmd_identifier)
(parameter_bracks
(parameter
(param_rest
(identifier))))
(returns
(flat_type)
(collection_type
(identifier)
(flat_type)
(identifier)
(flat_type)))
(block))
(decl_def
(long_flag)
(long_flag)
(cmd_identifier)
(parameter_bracks
(parameter
(param_rest
(identifier))))
(returns
(flat_type)
(collection_type
(identifier)
(flat_type)
(identifier)
(flat_type)))
(block))
(decl_def
(long_flag)
(cmd_identifier)
(parameter_bracks
(parameter
(param_rest
(identifier))))
(returns
(flat_type)
(collection_type
Expand Down
3 changes: 2 additions & 1 deletion grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ module.exports = grammar({
decl_def: ($) =>
seq(
optional(MODIFIER().visibility),
choice(KEYWORD().def, KEYWORD().def_env),
KEYWORD().def,
repeat($.long_flag),
field("name", $._command_name),
field("parameters", choice($.parameter_parens, $.parameter_bracks)),
field("return_type", optional($.returns)),
Expand Down
1 change: 0 additions & 1 deletion queries/nu/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
;;; keywords
[
"def"
"def-env"
"alias"
"export-env"
"export"
Expand Down
20 changes: 9 additions & 11 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -1386,17 +1386,15 @@
]
},
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "def"
},
{
"type": "STRING",
"value": "def-env"
}
]
"type": "STRING",
"value": "def"
},
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "long_flag"
}
},
{
"type": "FIELD",
Expand Down
14 changes: 10 additions & 4 deletions src/node-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -1288,6 +1288,16 @@
}
]
}
},
"children": {
"multiple": true,
"required": false,
"types": [
{
"type": "long_flag",
"named": true
}
]
}
},
{
Expand Down Expand Up @@ -5657,10 +5667,6 @@
"type": "def",
"named": false
},
{
"type": "def-env",
"named": false
},
{
"type": "directory",
"named": false
Expand Down
Loading