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 units #72

Merged
merged 4 commits into from
Jan 21, 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
149 changes: 149 additions & 0 deletions corpus/expr/units.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
====
units-001-duration
====

604800e+9ns
604800E6us
604800e3ms
604800sec
10080min
168hr
7day
1wk

-----

(nu_script
(pipeline
(pipe_element
(val_duration
(val_number))))
(pipeline
(pipe_element
(val_duration
(val_number))))
(pipeline
(pipe_element
(val_duration
(val_number))))
(pipeline
(pipe_element
(val_duration
(val_number))))
(pipeline
(pipe_element
(val_duration
(val_number))))
(pipeline
(pipe_element
(val_duration
(val_number))))
(pipeline
(pipe_element
(val_duration
(val_number))))
(pipeline
(pipe_element
(val_duration
(val_number))))
)

====
units-002-filesize
====

1E+9b
1e6kb
1000mb
1gb
0.001tb
1e-6pb
1E-9eb

1.048576e+6kib
1024mib
1gib
0.0009765625tib
9.536743e-7pib
9.313226E-10eib

-----

(nu_script
(pipeline
(pipe_element
(val_filesize
(val_number))))
(pipeline
(pipe_element
(val_filesize
(val_number))))
(pipeline
(pipe_element
(val_filesize
(val_number))))
(pipeline
(pipe_element
(val_filesize
(val_number))))
(pipeline
(pipe_element
(val_filesize
(val_number))))
(pipeline
(pipe_element
(val_filesize
(val_number))))
(pipeline
(pipe_element
(val_filesize
(val_number))))
(pipeline
(pipe_element
(val_filesize
(val_number))))
(pipeline
(pipe_element
(val_filesize
(val_number))))
(pipeline
(pipe_element
(val_filesize
(val_number))))
(pipeline
(pipe_element
(val_filesize
(val_number))))
(pipeline
(pipe_element
(val_filesize
(val_number))))
(pipeline
(pipe_element
(val_filesize
(val_number))))
)

====
units-003-command-args
====

sleep 1sec
echo 1kb

-----

(nu_script
(pipeline
(pipe_element
(command
(cmd_identifier)
(val_duration
(val_number)))))
(pipeline
(pipe_element
(command
(cmd_identifier)
(val_filesize
(val_number)))))
)
18 changes: 14 additions & 4 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 +982,7 @@ module.exports = grammar({
const path = choice(prec.right(2, token(/[0-9a-zA-Z_-]+/)), quoted);

return seq(
PUNC().dot,
token.immediate(PUNC().dot),
choice(
field("raw_path", path),
field("protected_path", seq(path, PUNC().question)),
Expand Down Expand Up @@ -1047,7 +1047,19 @@ module.exports = grammar({
unquoted: ($) =>
prec.left(
-69,
token(/[^-$\s\n\t\r{}()\[\]"`';][^\s\n\t\r{}()\[\]"`';]*/),
choice(
token(prec(-69, /[^-$\s\n\t\r{}()\[\]"`';][^\s\n\t\r{}()\[\]"`';]*/)),

// distinguish between unquoted and val_range in cmd_arg
seq(
choice(
OPR().range_exclusive,
OPR().range_inclusive,
OPR().range_inclusive2,
),
token.immediate(/[^\s\n\t\r{}()\[\]"`';]*/),
),
),
),

/// Comments
Expand Down Expand Up @@ -1472,14 +1484,12 @@ function FILESIZE_UNIT() {
"tb", "tB", "Tb", "TB",
"pb", "pB", "Pb", "PB",
"eb", "eB", "Eb", "EB",
"zb", "zB", "Zb", "ZB",

"kib", "kiB", "kIB", "kIb", "Kib", "KIb", "KIB",
"mib", "miB", "mIB", "mIb", "Mib", "MIb", "MIB",
"gib", "giB", "gIB", "gIb", "Gib", "GIb", "GIB",
"tib", "tiB", "tIB", "tIb", "Tib", "TIb", "TIB",
"pib", "piB", "pIB", "pIb", "Pib", "PIb", "PIB",
"eib", "eiB", "eIB", "eIb", "Eib", "EIb", "EIB",
"zib", "ziB", "zIB", "zIb", "Zib", "ZIb", "ZIB",
])
}
2 changes: 0 additions & 2 deletions queries/nu/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,13 @@
"tb" "tB" "Tb" "TB"
"pb" "pB" "Pb" "PB"
"eb" "eB" "Eb" "EB"
"zb" "zB" "Zb" "ZB"

"kib" "kiB" "kIB" "kIb" "Kib" "KIb" "KIB"
"mib" "miB" "mIB" "mIb" "Mib" "MIb" "MIB"
"gib" "giB" "gIB" "gIb" "Gib" "GIb" "GIB"
"tib" "tiB" "tIB" "tIb" "Tib" "TIb" "TIB"
"pib" "piB" "pIB" "pIb" "Pib" "PIb" "PIB"
"eib" "eiB" "eIB" "eIb" "Eib" "EIb" "EIB"
"zib" "ziB" "zIB" "zIb" "Zib" "ZIb" "ZIB"
] @variable.parameter
)
(val_binary
Expand Down
99 changes: 48 additions & 51 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -6994,22 +6994,6 @@
"type": "STRING",
"value": "EB"
},
{
"type": "STRING",
"value": "zb"
},
{
"type": "STRING",
"value": "zB"
},
{
"type": "STRING",
"value": "Zb"
},
{
"type": "STRING",
"value": "ZB"
},
{
"type": "STRING",
"value": "kib"
Expand Down Expand Up @@ -7177,34 +7161,6 @@
{
"type": "STRING",
"value": "EIB"
},
{
"type": "STRING",
"value": "zib"
},
{
"type": "STRING",
"value": "ziB"
},
{
"type": "STRING",
"value": "zIB"
},
{
"type": "STRING",
"value": "zIb"
},
{
"type": "STRING",
"value": "Zib"
},
{
"type": "STRING",
"value": "ZIb"
},
{
"type": "STRING",
"value": "ZIB"
}
]
}
Expand Down Expand Up @@ -8244,8 +8200,11 @@
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "."
"type": "IMMEDIATE_TOKEN",
"content": {
"type": "STRING",
"value": "."
}
},
{
"type": "CHOICE",
Expand Down Expand Up @@ -8689,11 +8648,49 @@
"type": "PREC_LEFT",
"value": -69,
"content": {
"type": "TOKEN",
"content": {
"type": "PATTERN",
"value": "[^-$\\s\\n\\t\\r{}()\\[\\]\"`';][^\\s\\n\\t\\r{}()\\[\\]\"`';]*"
}
"type": "CHOICE",
"members": [
{
"type": "TOKEN",
"content": {
"type": "PREC",
"value": -69,
"content": {
"type": "PATTERN",
"value": "[^-$\\s\\n\\t\\r{}()\\[\\]\"`';][^\\s\\n\\t\\r{}()\\[\\]\"`';]*"
}
}
},
{
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "..<"
},
{
"type": "STRING",
"value": ".."
},
{
"type": "STRING",
"value": "..="
}
]
},
{
"type": "IMMEDIATE_TOKEN",
"content": {
"type": "PATTERN",
"value": "[^\\s\\n\\t\\r{}()\\[\\]\"`';]*"
}
}
]
}
]
}
},
"comment": {
Expand Down
Loading