Add 'sp' parser to skip whitespace
parent
e6b6bb8a5c
commit
055276ca25
|
@ -120,3 +120,11 @@ HELP: action
|
|||
"the default AST." }
|
||||
{ $example "CHAR: 0 CHAR: 9 range [ to-digit ] action" } ;
|
||||
|
||||
HELP: sp
|
||||
{ $values
|
||||
{ "p1" "a parser" }
|
||||
{ "parser" "a parser" }
|
||||
}
|
||||
{ $description
|
||||
"Returns a parser that calls the original parser 'p1' after stripping any whitespace "
|
||||
" from the left of the input string." } ;
|
||||
|
|
|
@ -156,6 +156,18 @@ M: action-parser parse ( state parser -- result )
|
|||
nip
|
||||
] if ;
|
||||
|
||||
: left-trim-slice ( string -- string )
|
||||
#! Return a new string without any leading whitespace
|
||||
#! from the original string.
|
||||
dup empty? [
|
||||
dup first blank? [ 1 tail-slice left-trim-slice ] when
|
||||
] unless ;
|
||||
|
||||
TUPLE: sp-parser p1 ;
|
||||
|
||||
M: sp-parser parse ( state parser -- result )
|
||||
[ left-trim-slice ] dip sp-parser-p1 parse ;
|
||||
|
||||
PRIVATE>
|
||||
|
||||
: token ( string -- parser )
|
||||
|
@ -190,3 +202,6 @@ PRIVATE>
|
|||
|
||||
: action ( parser quot -- parser )
|
||||
action-parser construct-boa init-parser ;
|
||||
|
||||
: sp ( parser -- parser )
|
||||
sp-parser construct-boa init-parser ;
|
||||
|
|
Loading…
Reference in New Issue