forked from nim-lang/Nim
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix bug nim-lang#14343, parseCmdLine
- Loading branch information
1 parent
d0dd923
commit 1ee9221
Showing
3 changed files
with
118 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
#[ | ||
KEY shlex | ||
parseCmdLine D20200513T195153 | ||
]# | ||
|
||
type State = enum | ||
sInStart | ||
sInRegular | ||
sInSpace | ||
sInSingleQuote | ||
sInDoubleQuote | ||
sFinished | ||
|
||
type ShlexError = enum | ||
seOk | ||
seMissingDoubleQuote | ||
seMissingSingleQuote | ||
|
||
iterator shlex*(a: openArray[char], error: var ShlexError): string = | ||
var i = 0 | ||
var buf: string | ||
var state = sInStart | ||
var ready = false | ||
error = seOk | ||
while true: | ||
# echo (i, state, buf) | ||
if i >= a.len: | ||
case state | ||
of sInSingleQuote: | ||
error = seMissingSingleQuote | ||
of sInDoubleQuote: | ||
error = seMissingDoubleQuote | ||
else: | ||
ready = true | ||
state = sFinished | ||
var c: char | ||
if i < a.len: | ||
c = a[i] | ||
i.inc | ||
case state | ||
of sFinished: discard | ||
of sInStart: | ||
case c | ||
of ' ': discard | ||
of '\'': state = sInSingleQuote | ||
of '\"': state = sInDoubleQuote | ||
else: | ||
state = sInRegular | ||
buf.add c | ||
of sInRegular: | ||
case c | ||
of ' ': ready = true | ||
of '\'': state = sInSingleQuote | ||
of '\"': state = sInDoubleQuote | ||
else: buf.add c | ||
of sInSingleQuote: | ||
case c | ||
of '\'': state = sInRegular | ||
else: buf.add c | ||
of sInDoubleQuote: | ||
case c | ||
of '\"': state = sInRegular | ||
# of '\'': state = sInRegular | ||
else: buf.add c | ||
of sInSpace: | ||
case c | ||
of ' ': discard | ||
of '\'': state = sInSingleQuote | ||
of '\"': state = sInDoubleQuote | ||
else: | ||
state = sInRegular | ||
buf.add c | ||
if ready: | ||
ready = false | ||
# echo (buf,) | ||
yield buf | ||
buf.setLen 0 | ||
if state != sFinished: | ||
state = sInStart | ||
if state == sFinished: | ||
break | ||
|
||
proc parseCmdLineImpl*(a: string): seq[string] {.inline.} = | ||
var err: ShlexError | ||
for val in shlex(a, err): | ||
if err == seOk: | ||
result.add val | ||
else: | ||
raise newException(ValueError, $(a, err)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters