Skip to content

Commit

Permalink
Merge pull request #56 from kit494way/fix-parsing-long-flag
Browse files Browse the repository at this point in the history
Fix parsing long flag
  • Loading branch information
fdncred authored Jan 3, 2024
2 parents 2e074e4 + d0b0adc commit 958ccfb
Show file tree
Hide file tree
Showing 5 changed files with 6,927 additions and 12,334 deletions.
240 changes: 240 additions & 0 deletions corpus/decl/def.nu
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,243 @@ def-env test []: nothing -> record<a: string, b: int> {}
(identifier)
(flat_type)))
(block)))

======
def-007-long-flag
======

def test [
--long-flag
] {}

-----

(nu_script
(decl_def
(cmd_identifier)
(parameter_bracks
(parameter
(param_long_flag
(identifier))))
(block)))

======
def-008-long-flag-with-short
======

def test [
--long-flag (-l)
] {}

-----

(nu_script
(decl_def
(cmd_identifier)
(parameter_bracks
(parameter
(param_long_flag
(identifier))
(flag_capsule
(param_short_flag))))
(block)))

======
def-009-long-flag-with-type
======

def test [
--long-flag: string
] {}

-----

(nu_script
(decl_def
(cmd_identifier)
(parameter_bracks
(parameter
(param_long_flag
(identifier))
(param_type
(flat_type))))
(block)))

======
def-010-short-flag
======

def test [
-s
] {}

-----

(nu_script
(decl_def
(cmd_identifier)
(parameter_bracks
(parameter
(param_short_flag)))
(block)))

======
def-011-short-flag-with-type
======

def test [
-s: int
] {}

-----

(nu_script
(decl_def
(cmd_identifier)
(parameter_bracks
(parameter
(param_short_flag)
(param_type
(flat_type))))
(block)))

======
def-012-parameter
======

def test [
foo
] {}

-----

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

======
def-013-parameter-with-type
======

def test [
foo: string
] {}

-----

(nu_script
(decl_def
(cmd_identifier)
(parameter_bracks
(parameter
(identifier)
(param_type
(flat_type))))
(block)))

======
def-014-optional-parameter
======

def test [
foo?
] {}

-----

(nu_script
(decl_def
(cmd_identifier)
(parameter_bracks
(parameter
(param_opt
(identifier))))
(block)))

======
def-015-optional-parameter-with-type
======

def test [
foo?: string
] {}

-----

(nu_script
(decl_def
(cmd_identifier)
(parameter_bracks
(parameter
(param_opt
(identifier))
(param_type
(flat_type))))
(block)))

======
def-016-rest-parameter
======

def test [
...rest
] {}

-----

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

======
def-017-long-flag-with-short-without-space
======

def test [
--long-flag(-l)
] {}

-----

(nu_script
(decl_def
(cmd_identifier)
(parameter_bracks
(parameter
(param_long_flag
(identifier))
(flag_capsule
(param_short_flag))))
(block)))

======
def-018-long-flag-with-short-and-type
======

def test [
--long-flag(-l): int
] {}

-----

(nu_script
(decl_def
(cmd_identifier)
(parameter_bracks
(parameter
(param_long_flag
(identifier))
(flag_capsule
(param_short_flag))
(param_type
(flat_type))))
(block)))
15 changes: 15 additions & 0 deletions corpus/pipe/commands.nu
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,18 @@ let x = 42
(pipeline
(pipe_element
(val_number)))))

======
cmd-010-long-flag
======

cmd --long-flag

------

(nu_script
(pipeline
(pipe_element
(command
(cmd_identifier)
(long_flag)))))
11 changes: 6 additions & 5 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ module.exports = grammar({

identifier: ($) => token(/[_\p{XID_Start}][_\p{XID_Continue}]*/),

_long_flag_identifier: ($) =>
token.immediate(/[\p{XID_Start}_][\p{XID_Continue}_-]*/),

_command_name: ($) =>
choice(
field("unquoted_name", $.cmd_identifier),
Expand Down Expand Up @@ -213,7 +216,8 @@ module.exports = grammar({
param_opt: ($) =>
seq(field("name", $.identifier), token.immediate(PUNC().question)),

param_long_flag: ($) => seq("--", $.identifier),
param_long_flag: ($) =>
seq("--", alias($._long_flag_identifier, $.identifier)),

flag_capsule: ($) =>
seq(BRACK().open_paren, $.param_short_flag, BRACK().close_paren),
Expand Down Expand Up @@ -965,10 +969,7 @@ module.exports = grammar({
short_flag: ($) => token(/-[_\p{XID_Continue}]+/),

long_flag: ($) =>
prec.right(
10,
choice("--", seq("--", token.immediate(/[_\p{XID_Continue}]+/))),
),
prec.right(10, choice("--", seq("--", $._long_flag_identifier))),

// because this catches almost anything, we want to ensure it is
// picked as the a last resort after everything else has failed.
Expand Down
23 changes: 16 additions & 7 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,13 @@
"value": "[_\\p{XID_Start}][_\\p{XID_Continue}]*"
}
},
"_long_flag_identifier": {
"type": "IMMEDIATE_TOKEN",
"content": {
"type": "PATTERN",
"value": "[\\p{XID_Start}_][\\p{XID_Continue}_-]*"
}
},
"_command_name": {
"type": "CHOICE",
"members": [
Expand Down Expand Up @@ -2337,8 +2344,13 @@
"value": "--"
},
{
"type": "SYMBOL",
"name": "identifier"
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_long_flag_identifier"
},
"named": true,
"value": "identifier"
}
]
},
Expand Down Expand Up @@ -8029,11 +8041,8 @@
"value": "--"
},
{
"type": "IMMEDIATE_TOKEN",
"content": {
"type": "PATTERN",
"value": "[_\\p{XID_Continue}]+"
}
"type": "SYMBOL",
"name": "_long_flag_identifier"
}
]
}
Expand Down
Loading

0 comments on commit 958ccfb

Please sign in to comment.