Skip to content

Commit

Permalink
parse: refactor to make greedy and raise keyword arguments.
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanKarpinski committed Oct 19, 2013
1 parent 5920706 commit af33ea7
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
3 changes: 3 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,9 @@ export ComplexPair
@deprecate sortcols(v::AbstractMatrix,a::Algorithm,o::Ordering) sortcols(v,alg=a,order=o)
@deprecate sortcols(v::AbstractMatrix,o::Ordering,a::Algorithm) sortcols(v,alg=a,order=o)

@deprecate parse(str::String, pos::Int, greedy::Bool, raise::Bool) parse(str,pos,greedy=greedy,raise=raise)
@deprecate parse(str::String, pos::Int, greedy::Bool) parse(str,pos,greedy=greedy)

function amap(f::Function, A::AbstractArray, axis::Integer)
depwarn("amap is deprecated, use mapslices(f, A, dims) instead", :amap)
dimsA = size(A)
Expand Down
12 changes: 6 additions & 6 deletions base/string.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1089,7 +1089,7 @@ function shell_parse(raw::String, interp::Bool)
error("space not allowed right after \$")
end
stpos = j
ex, j = parse(s,j,false)
ex, j = parse(s,j,greedy=false)
last_parse = stpos:j
update_arg(esc(ex)); i = j
else
Expand Down Expand Up @@ -1191,16 +1191,16 @@ shell_escape(args::String...) = sprint(print_shell_escaped, args...)

## interface to parser ##

function parse(str::String, pos::Int, greedy::Bool=true, err::Bool=true)
function parse(str::String, pos::Int; greedy::Bool=true, raise::Bool=true)
# returns (expr, end_pos). expr is () in case of parse error.
ex, pos = ccall(:jl_parse_string, Any,
(Ptr{Uint8}, Int32, Int32),
str, pos-1, greedy ? 1:0)
if err && isa(ex,Expr) && is(ex.head,:error)
if raise && isa(ex,Expr) && is(ex.head,:error)
throw(ParseError(ex.args[1]))
end
if ex == ()
if err
if raise
throw(ParseError("end of input"))
else
ex = Expr(:error, "end of input")
Expand All @@ -1209,8 +1209,8 @@ function parse(str::String, pos::Int, greedy::Bool=true, err::Bool=true)
ex, pos+1 # C is zero-based, Julia is 1-based
end

function parse(str::String)
ex, pos = parse(str, start(str))
function parse(str::String; greedy::Bool=true, raise::Bool=true)
ex, pos = parse(str, start(str), greedy=greedy, raise=raise)
done(str, pos) || error("syntax: extra token after end of expression")
return ex
end
Expand Down
4 changes: 2 additions & 2 deletions doc/stdlib/base.rst
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,9 @@ Syntax

Generates a gensym symbol for a variable. For example, `@gensym x y` is transformed into `x = gensym("x"); y = gensym("y")`.

.. function:: parse(str, [start, [greedy, [err]]])
.. function:: parse(str, [start]; greedy=true, raise=false)

Parse the expression string and return an expression (which could later be passed to eval for execution). Start is the index of the first character to start parsing (default is 1). If greedy is true (default), parse will try to consume as much input as it can; otherwise, it will stop as soon as it has parsed a valid token. If err is true (default), parse errors will raise an error; otherwise, it will return the error as a normal expression.
Parse the expression string and return an expression (which could later be passed to eval for execution). Start is the index of the first character to start parsing (default is 1). If greedy is true (default), parse will try to consume as much input as it can; otherwise, it will stop as soon as it has parsed a valid token. If raise is true (default), parse errors will raise an error; otherwise, parse will return the error as an expression object.

Iteration
---------
Expand Down

0 comments on commit af33ea7

Please sign in to comment.