From 2020f20a29803a59c40c376ecd6680faff147101 Mon Sep 17 00:00:00 2001 From: pfitzseb Date: Thu, 1 Feb 2024 14:49:36 +0000 Subject: [PATCH] Format files using DocumentFormat --- src/CSTParser.jl | 2 +- src/components/internals.jl | 16 +-- src/components/keywords.jl | 8 +- src/components/lists.jl | 14 +- src/components/operators.jl | 262 +++++++++++++++++----------------- src/components/strings.jl | 10 +- src/conversion.jl | 22 +-- src/display.jl | 14 +- src/interface.jl | 8 +- src/iterate.jl | 81 +++++------ src/lexer.jl | 21 +-- src/packagedef.jl | 18 +-- src/recovery.jl | 4 +- src/spec.jl | 48 +++---- src/utils.jl | 190 ++++++++++++------------ test/parser/test_operators.jl | 4 +- test/parser/test_parser.jl | 2 +- test/parser/test_types.jl | 8 +- test/shared.jl | 30 ++-- test/test_display.jl | 2 +- test/test_errparse.jl | 2 +- test/test_interface.jl | 2 +- test/test_spec.jl | 4 +- 23 files changed, 388 insertions(+), 384 deletions(-) diff --git a/src/CSTParser.jl b/src/CSTParser.jl index d8792dd0..a212baff 100644 --- a/src/CSTParser.jl +++ b/src/CSTParser.jl @@ -3,7 +3,7 @@ module CSTParser using Tokenize import Tokenize.Tokens import Tokenize.Tokens: RawToken, AbstractToken, iskeyword, isliteral, isoperator, untokenize -import Tokenize.Lexers: Lexer, peekchar, iswhitespace, readchar, emit, emit_error, accept_batch, eof +import Tokenize.Lexers: Lexer, peekchar, iswhitespace, readchar, emit, emit_error, accept_batch, eof include("packagedef.jl") diff --git a/src/components/internals.jl b/src/components/internals.jl index ec178ba5..632b9f98 100644 --- a/src/components/internals.jl +++ b/src/components/internals.jl @@ -31,7 +31,7 @@ Parses an iterator, allowing for the preceding keyword `outer`. Returns an error expression if an invalid expression is parsed (anything other than `=`, `in`, `∈`). """ -function parse_iterator(ps::ParseState, outer = parse_outer(ps)) +function parse_iterator(ps::ParseState, outer=parse_outer(ps)) arg = @closer ps :range @closer ps :ws @nocloser ps :wsop parse_expression(ps) if !is_range(arg) arg = mErrorToken(ps, arg, InvalidIterator) @@ -136,7 +136,7 @@ function parse_call(ps::ParseState, ret::EXPR, ismacro=false) end # args = ismacro ? EXPR[ret, EXPR(:NOTHING, 0, 0)] : EXPR[ret] trivia = EXPR[EXPR(next(ps))] - @closeparen ps @default ps parse_comma_sep(ps, args, trivia, !ismacro, insert_params_at = ismacro ? 3 : 2) + @closeparen ps @default ps parse_comma_sep(ps, args, trivia, !ismacro, insert_params_at=ismacro ? 3 : 2) accept_rparen(ps, trivia) ret = EXPR(ismacro ? :macrocall : syntaxcall ? ret : :call, args, trivia) end @@ -147,7 +147,7 @@ end Parses a comma separated list, optionally allowing for conversion of assignment (`=`) expressions to `Kw`. """ -function parse_comma_sep(ps::ParseState, args::Vector{EXPR}, trivia::Vector{EXPR}, kw = true, block = false, istuple = false; insert_params_at = 2) +function parse_comma_sep(ps::ParseState, args::Vector{EXPR}, trivia::Vector{EXPR}, kw=true, block=false, istuple=false; insert_params_at=2) prevpos = position(ps) @nocloser ps :inwhere @nocloser ps :newline @closer ps :comma while !closer(ps) a = parse_expression(ps) @@ -206,11 +206,11 @@ end Parses parameter arguments for a function call (e.g. following a semicolon). """ -function parse_parameters(ps::ParseState, args::Vector{EXPR}, args1::Vector{EXPR} = EXPR[], insert_params_at = 2; usekw = true) +function parse_parameters(ps::ParseState, args::Vector{EXPR}, args1::Vector{EXPR}=EXPR[], insert_params_at=2; usekw=true) trivia = EXPR[] isfirst = isempty(args1) prevpos = position(ps) - @nocloser ps :inwhere @nocloser ps :newline @closer ps :comma while !isfirst || (@nocloser ps :semicolon !closer(ps)) + @nocloser ps :inwhere @nocloser ps :newline @closer ps :comma while !isfirst || (@nocloser ps :semicolon !closer(ps)) a = isfirst ? parse_expression(ps) : first(args1) if usekw && _do_kw_convert(ps, a) a = _kw_convert(a) @@ -348,7 +348,7 @@ end function get_appropriate_child_to_expand(x) if headof(x) === :generator && !(headof(x.args[1]) in (:generator, :flatten)) return x, x.args[1] - elseif headof(x) === :flatten && headof(x.args[1]) === :generator && headof(x.args[1].args[1]) === :generator + elseif headof(x) === :flatten && headof(x.args[1]) === :generator && headof(x.args[1].args[1]) === :generator x.args[1], x.args[1].args[1] else get_appropriate_child_to_expand(x.args[1]) @@ -366,7 +366,7 @@ function parse_nonstd_identifier(ps) end end -function parse_importexport_item(ps, is_colon = false) +function parse_importexport_item(ps, is_colon=false) if kindof(ps.nt) === Tokens.AT_SIGN parse_macroname(next(ps)) elseif kindof(ps.nt) === Tokens.LPAREN @@ -378,7 +378,7 @@ function parse_importexport_item(ps, is_colon = false) parse_unary(ps, INSTANCE(next(ps))) elseif !is_colon && isoperator(ps.nt) next(ps) - EXPR(:OPERATOR, ps.nt.startbyte - ps.t.startbyte, 1 + ps.t.endbyte - ps.t.startbyte, val(ps.t, ps)) + EXPR(:OPERATOR, ps.nt.startbyte - ps.t.startbyte, 1 + ps.t.endbyte - ps.t.startbyte, val(ps.t, ps)) elseif is_nonstd_identifier(ps) parse_nonstd_identifier(ps) else diff --git a/src/components/keywords.jl b/src/components/keywords.jl index 284c1dcf..bd4ecd98 100644 --- a/src/components/keywords.jl +++ b/src/components/keywords.jl @@ -94,7 +94,7 @@ function parse_const(ps::ParseState) nt = ps.nt arg = parse_expression(ps) allow_no_assignment = has_flag(ps, ParserFlags.AllowConstWithoutAssignment) || - has_flag(ps, ParserFlags.InQuote) && (kindof(nt) === Tokens.GLOBAL || kindof(lt) === Tokens.GLOBAL) + has_flag(ps, ParserFlags.InQuote) && (kindof(nt) === Tokens.GLOBAL || kindof(lt) === Tokens.GLOBAL) if !allow_no_assignment && !(isassignment(unwrapbracket(arg)) || (headof(arg) === :global && length(arg.args) > 0 && isassignment(unwrapbracket(arg.args[1])))) arg = mErrorToken(ps, arg, ExpectedAssignment) end @@ -102,7 +102,7 @@ function parse_const(ps::ParseState) return ret end -function parse_local_global(ps::ParseState, islocal = true) +function parse_local_global(ps::ParseState, islocal=true) kw = EXPR(ps) if ps.nt.kind === Tokens.CONST arg1 = parse_const(next(ps)) @@ -358,7 +358,7 @@ end Parse an `if` block. """ -function parse_if(ps::ParseState, nested = false) +function parse_if(ps::ParseState, nested=false) args = EXPR[] trivia = EXPR[EXPR(ps)] @@ -428,7 +428,7 @@ function parse_try(ps::ParseState) args[3] = EXPR(:block, 0, 0, "") end else_trivia = EXPR(next(ps)) - else_arg = EXPR(:block, parse_block(ps, EXPR[], (Tokens.FINALLY,Tokens.END))) + else_arg = EXPR(:block, parse_block(ps, EXPR[], (Tokens.FINALLY, Tokens.END))) end has_finally = false diff --git a/src/components/lists.jl b/src/components/lists.jl index dcd1741e..37140e09 100644 --- a/src/components/lists.jl +++ b/src/components/lists.jl @@ -46,7 +46,7 @@ function parse_tuple(ps::ParseState, ret::EXPR) end # XXX: Avert thine eyes. -function count_semicolons(ps, check_newline = true) +function count_semicolons(ps, check_newline=true) dims = 0 has_newline = false old_pos = position(ps.l.io) @@ -75,7 +75,7 @@ Having hit '[' return either: + A comprehension + An array (vcat of hcats) """ -function parse_array(ps::ParseState, isref = false) +function parse_array(ps::ParseState, isref=false) args = EXPR[] trivia = EXPR[EXPR(ps)] # [] or [;;;] @@ -100,7 +100,7 @@ function parse_array(ps::ParseState, isref = false) end end -binding_power(ps, nl = true) = +binding_power(ps, nl=true) = if kindof(ps.ws) == SemiColonWS -count_semicolons(ps, nl) elseif kindof(ps.ws) == NewLineWS @@ -149,7 +149,7 @@ function parse_array_outer(ps::ParseState, trivia, isref) args = EXPR[] push!(args, a) push!(trivia, accept_comma(ps)) - @closesquare ps parse_comma_sep(ps, args, trivia, isref, insert_params_at = 1) + @closesquare ps parse_comma_sep(ps, args, trivia, isref, insert_params_at=1) return EXPR(:vect, args, trivia) elseif kindof(ps.nt) === Tokens.ENDMARKER push!(args, a) @@ -309,7 +309,7 @@ function parse_barray(ps::ParseState) accept_rbrace(ps, trivia) ret = EXPR(:braces, args, trivia) else - first_arg = @nocloser ps :newline @closebrace ps @closer ps :ws @closer ps :wsop @closer ps :comma parse_expression(ps) + first_arg = @nocloser ps :newline @closebrace ps @closer ps :ws @closer ps :wsop @closer ps :comma parse_expression(ps) if kindof(ps.nt) === Tokens.RBRACE push!(args, first_arg) if kindof(ps.ws) == SemiColonWS @@ -320,7 +320,7 @@ function parse_barray(ps::ParseState) elseif iscomma(ps.nt) push!(args, first_arg) accept_comma(ps, trivia) - @closebrace ps parse_comma_sep(ps, args, trivia, true, insert_params_at = 1) + @closebrace ps parse_comma_sep(ps, args, trivia, true, insert_params_at=1) accept_rbrace(ps, trivia) return EXPR(:braces, args, trivia) elseif kindof(ps.ws) == NewLineWS @@ -328,7 +328,7 @@ function parse_barray(ps::ParseState) push!(ret, first_arg) prevpos = position(ps) while kindof(ps.nt) != Tokens.RBRACE && kindof(ps.nt) !== Tokens.ENDMARKER - a = @closebrace ps parse_expression(ps) + a = @closebrace ps parse_expression(ps) push!(ret, a) prevpos = loop_check(ps, prevpos) end diff --git a/src/components/operators.jl b/src/components/operators.jl index 4bc10506..8a6a603c 100644 --- a/src/components/operators.jl +++ b/src/components/operators.jl @@ -1,41 +1,41 @@ -precedence(op::Int) = op < Tokens.end_assignments ? AssignmentOp : - op < Tokens.end_pairarrow ? 2 : - op < Tokens.end_conditional ? ConditionalOp : - op < Tokens.end_arrow ? ArrowOp : - op < Tokens.end_lazyor ? LazyOrOp : - op < Tokens.end_lazyand ? LazyAndOp : - op < Tokens.end_comparison ? ComparisonOp : - op < Tokens.end_pipe ? PipeOp : - op < Tokens.end_colon ? ColonOp : - op < Tokens.end_plus ? PlusOp : - op < Tokens.end_bitshifts ? BitShiftOp : - op < Tokens.end_times ? TimesOp : - op < Tokens.end_rational ? RationalOp : - op < Tokens.end_power ? PowerOp : - op < Tokens.end_decl ? DeclarationOp : - op < Tokens.end_where ? WhereOp : DotOp +precedence(op::Int) = op < Tokens.end_assignments ? AssignmentOp : + op < Tokens.end_pairarrow ? 2 : + op < Tokens.end_conditional ? ConditionalOp : + op < Tokens.end_arrow ? ArrowOp : + op < Tokens.end_lazyor ? LazyOrOp : + op < Tokens.end_lazyand ? LazyAndOp : + op < Tokens.end_comparison ? ComparisonOp : + op < Tokens.end_pipe ? PipeOp : + op < Tokens.end_colon ? ColonOp : + op < Tokens.end_plus ? PlusOp : + op < Tokens.end_bitshifts ? BitShiftOp : + op < Tokens.end_times ? TimesOp : + op < Tokens.end_rational ? RationalOp : + op < Tokens.end_power ? PowerOp : + op < Tokens.end_decl ? DeclarationOp : + op < Tokens.end_where ? WhereOp : DotOp precedence(kind::Tokens.Kind) = kind === Tokens.DDDOT ? DddotOp : - kind < Tokens.begin_assignments ? 0 : - kind < Tokens.end_assignments ? AssignmentOp : - kind < Tokens.end_pairarrow ? 2 : - kind < Tokens.end_conditional ? ConditionalOp : - kind < Tokens.end_arrow ? ArrowOp : - kind < Tokens.end_lazyor ? LazyOrOp : - kind < Tokens.end_lazyand ? LazyAndOp : - kind < Tokens.end_comparison ? ComparisonOp : - kind < Tokens.end_pipe ? PipeOp : - kind < Tokens.end_colon ? ColonOp : - kind < Tokens.end_plus ? PlusOp : - kind < Tokens.end_bitshifts ? BitShiftOp : - kind < Tokens.end_times ? TimesOp : - kind < Tokens.end_rational ? RationalOp : - kind < Tokens.end_power ? PowerOp : - kind < Tokens.end_decl ? DeclarationOp : - kind < Tokens.end_where ? WhereOp : - kind < Tokens.end_dot ? DotOp : - kind === Tokens.ANON_FUNC ? AnonFuncOp : - kind === Tokens.PRIME ? PrimeOp : 20 + kind < Tokens.begin_assignments ? 0 : + kind < Tokens.end_assignments ? AssignmentOp : + kind < Tokens.end_pairarrow ? 2 : + kind < Tokens.end_conditional ? ConditionalOp : + kind < Tokens.end_arrow ? ArrowOp : + kind < Tokens.end_lazyor ? LazyOrOp : + kind < Tokens.end_lazyand ? LazyAndOp : + kind < Tokens.end_comparison ? ComparisonOp : + kind < Tokens.end_pipe ? PipeOp : + kind < Tokens.end_colon ? ColonOp : + kind < Tokens.end_plus ? PlusOp : + kind < Tokens.end_bitshifts ? BitShiftOp : + kind < Tokens.end_times ? TimesOp : + kind < Tokens.end_rational ? RationalOp : + kind < Tokens.end_power ? PowerOp : + kind < Tokens.end_decl ? DeclarationOp : + kind < Tokens.end_where ? WhereOp : + kind < Tokens.end_dot ? DotOp : + kind === Tokens.ANON_FUNC ? AnonFuncOp : + kind === Tokens.PRIME ? PrimeOp : 20 precedence(x) = 0 precedence(x::AbstractToken) = precedence(kindof(x)) @@ -55,97 +55,97 @@ isunaryop(op::EXPR) = isoperator(op) && ((valof(op) == "<:" || valof(op) == "¬" || valof(op) == "&" || valof(op) == "√" || - valof(op) == "∛" || - valof(op) == "∜" || + valof(op) == "∛" || + valof(op) == "∜" || valof(op) == "::" || valof(op) == "\$" || valof(op) == ":" || valof(op) == "⋆" || valof(op) == "±" || valof(op) == "∓") || - (length(valof(op)) == 2 && valof(op)[1] == '.' && (valof(op)[2] == '+' || - valof(op)[2] == '-' || - valof(op)[2] == '!' || - valof(op)[2] == '~' || - valof(op)[2] == '¬' || - valof(op)[2] == '√' || - valof(op)[2] == '∛' || - valof(op)[2] == '∜' || - valof(op)[2] == '⋆' || - valof(op)[2] == '±' || - valof(op)[2] == '∓'))) + (length(valof(op)) == 2 && valof(op)[1] == '.' && (valof(op)[2] == '+' || + valof(op)[2] == '-' || + valof(op)[2] == '!' || + valof(op)[2] == '~' || + valof(op)[2] == '¬' || + valof(op)[2] == '√' || + valof(op)[2] == '∛' || + valof(op)[2] == '∜' || + valof(op)[2] == '⋆' || + valof(op)[2] == '±' || + valof(op)[2] == '∓'))) isunaryop(t::AbstractToken) = isunaryop(kindof(t)) @static if VERSION < v"1.2.0" isunaryop(kind::Tokens.Kind) = kind === Tokens.ISSUBTYPE || - kind === Tokens.ISSUPERTYPE || - kind === Tokens.PLUS || - kind === Tokens.MINUS || - kind === Tokens.NOT || - kind === Tokens.APPROX || - kind === Tokens.NOT_SIGN || - kind === Tokens.AND || - kind === Tokens.SQUARE_ROOT || - kind === Tokens.CUBE_ROOT || - kind === Tokens.QUAD_ROOT || - kind === Tokens.DECLARATION || - kind === Tokens.EX_OR || - kind === Tokens.COLON + kind === Tokens.ISSUPERTYPE || + kind === Tokens.PLUS || + kind === Tokens.MINUS || + kind === Tokens.NOT || + kind === Tokens.APPROX || + kind === Tokens.NOT_SIGN || + kind === Tokens.AND || + kind === Tokens.SQUARE_ROOT || + kind === Tokens.CUBE_ROOT || + kind === Tokens.QUAD_ROOT || + kind === Tokens.DECLARATION || + kind === Tokens.EX_OR || + kind === Tokens.COLON else isunaryop(kind::Tokens.Kind) = kind === Tokens.ISSUBTYPE || - kind === Tokens.ISSUPERTYPE || - kind === Tokens.PLUS || - kind === Tokens.MINUS || - kind === Tokens.NOT || - kind === Tokens.APPROX || - kind === Tokens.NOT_SIGN || - kind === Tokens.AND || - kind === Tokens.SQUARE_ROOT || - kind === Tokens.CUBE_ROOT || - kind === Tokens.QUAD_ROOT || - kind === Tokens.DECLARATION || - kind === Tokens.EX_OR || - kind === Tokens.COLON || - kind === Tokens.STAR_OPERATOR + kind === Tokens.ISSUPERTYPE || + kind === Tokens.PLUS || + kind === Tokens.MINUS || + kind === Tokens.NOT || + kind === Tokens.APPROX || + kind === Tokens.NOT_SIGN || + kind === Tokens.AND || + kind === Tokens.SQUARE_ROOT || + kind === Tokens.CUBE_ROOT || + kind === Tokens.QUAD_ROOT || + kind === Tokens.DECLARATION || + kind === Tokens.EX_OR || + kind === Tokens.COLON || + kind === Tokens.STAR_OPERATOR end isunaryandbinaryop(t) = false isunaryandbinaryop(t::AbstractToken) = isunaryandbinaryop(kindof(t)) @static if VERSION < v"1.2.0" isunaryandbinaryop(kind::Tokens.Kind) = kind === Tokens.PLUS || - kind === Tokens.MINUS || - kind === Tokens.EX_OR || - kind === Tokens.ISSUBTYPE || - kind === Tokens.ISSUPERTYPE || - kind === Tokens.AND || - kind === Tokens.APPROX || - kind === Tokens.DECLARATION || - kind === Tokens.COLON + kind === Tokens.MINUS || + kind === Tokens.EX_OR || + kind === Tokens.ISSUBTYPE || + kind === Tokens.ISSUPERTYPE || + kind === Tokens.AND || + kind === Tokens.APPROX || + kind === Tokens.DECLARATION || + kind === Tokens.COLON else isunaryandbinaryop(kind::Tokens.Kind) = kind === Tokens.PLUS || - kind === Tokens.MINUS || - kind === Tokens.EX_OR || - kind === Tokens.ISSUBTYPE || - kind === Tokens.ISSUPERTYPE || - kind === Tokens.AND || - kind === Tokens.APPROX || - kind === Tokens.DECLARATION || - kind === Tokens.COLON || - kind === Tokens.STAR_OPERATOR + kind === Tokens.MINUS || + kind === Tokens.EX_OR || + kind === Tokens.ISSUBTYPE || + kind === Tokens.ISSUPERTYPE || + kind === Tokens.AND || + kind === Tokens.APPROX || + kind === Tokens.DECLARATION || + kind === Tokens.COLON || + kind === Tokens.STAR_OPERATOR end isbinaryop(op) = false isbinaryop(op::EXPR) = isoperator(op) && !(valof(op) == "√" || - valof(op) == "∛" || - valof(op) == "∜" || - valof(op) == "!" || - valof(op) == "¬") + valof(op) == "∛" || + valof(op) == "∜" || + valof(op) == "!" || + valof(op) == "¬") isbinaryop(t::AbstractToken) = isbinaryop(kindof(t)) isbinaryop(kind::Tokens.Kind) = isoperator(kind) && - !(kind === Tokens.SQUARE_ROOT || - kind === Tokens.CUBE_ROOT || - kind === Tokens.QUAD_ROOT || - kind === Tokens.NOT || - kind === Tokens.NOT_SIGN) + !(kind === Tokens.SQUARE_ROOT || + kind === Tokens.CUBE_ROOT || + kind === Tokens.QUAD_ROOT || + kind === Tokens.NOT || + kind === Tokens.NOT_SIGN) function non_dotted_op(t::AbstractToken) k = kindof(t) @@ -176,20 +176,20 @@ function issyntaxcall(op::EXPR) return true end assign_prec(v) && !(v == "~" || v == ".~" || v == "=>") || - v == "-->" || - v == "||" || - v == ".||" || - v == "&&" || - v == ".&&" || - v == "<:" || - v == ">:" || - v == ":" || - v == "::" || - v == "." || - v == "..." || - v == "'" || - v == "where" || - v == "->" + v == "-->" || + v == "||" || + v == ".||" || + v == "&&" || + v == ".&&" || + v == "<:" || + v == ">:" || + v == ":" || + v == "::" || + v == "." || + v == "..." || + v == "'" || + v == "where" || + v == "->" end @@ -217,8 +217,8 @@ function parse_unary(ps::ParseState, op::EXPR) ret = EXPR(literalmap(kindof(ps.t)), op.fullspan + arg.fullspan, (op.fullspan + arg.span), string(is_plus(op) ? "+" : "-", val(ps.t, ps))) else prec = valof(op) == "::" ? DeclarationOp : - valof(op) == "&" ? DeclarationOp : - valof(op) == "\$" ? 20 : PowerOp + valof(op) == "&" ? DeclarationOp : + valof(op) == "\$" ? 20 : PowerOp arg = @closer ps :unary @precedence ps prec parse_expression(ps) if issyntaxunarycall(op) ret = EXPR(op, EXPR[arg], nothing) @@ -242,7 +242,7 @@ function parse_unary_colon(ps::ParseState, op::EXPR) end ret = EXPR(:quotenode, EXPR[id], EXPR[op]) elseif Tokens.begin_literal < kindof(ps.nt) < Tokens.CHAR || - isoperator(kindof(ps.nt)) || isidentifier(ps.nt) || kindof(ps.nt) === Tokens.TRUE || kindof(ps.nt) === Tokens.FALSE + isoperator(kindof(ps.nt)) || isidentifier(ps.nt) || kindof(ps.nt) === Tokens.TRUE || kindof(ps.nt) === Tokens.FALSE ret = EXPR(:quotenode, EXPR[INSTANCE(next(ps))], EXPR[op]) elseif closer(ps) ret = op @@ -364,7 +364,7 @@ function parse_operator_where(ps::ParseState, ret::EXPR, op::EXPR, setscope=true if headof(nextarg) === :braces pushfirst!(nextarg.args, ret) pushfirst!(nextarg.trivia, op) - ret = EXPR(:where, nextarg.args,nextarg.trivia) + ret = EXPR(:where, nextarg.args, nextarg.trivia) else ret = EXPR(:where, EXPR[ret, nextarg], EXPR[op]) end @@ -448,8 +448,8 @@ function parse_operator(ps::ParseState, ret::EXPR, op::EXPR) P = RationalOp end if headof(ret) === :call && (is_plus(ret.args[1]) || is_star(ret.args[1])) && - valof(ret.args[1]) == valof(op) && ret.args[1].span > 0 && - !(hastrivia(ret) && headof(ret[end]) === :RPAREN) + valof(ret.args[1]) == valof(op) && ret.args[1].span > 0 && + !(hastrivia(ret) && headof(ret[end]) === :RPAREN) # a + b -> a + b + c nextarg = @precedence ps P - LtoR(P) parse_expression(ps) !hastrivia(ret) && (ret.trivia = EXPR[]) @@ -472,15 +472,15 @@ function parse_operator(ps::ParseState, ret::EXPR, op::EXPR) ret = EXPR(op, EXPR[ret], nothing) elseif is_prime(op) if isidentifier(ret) || isliteral(ret) || - headof(ret) in ( - :call, :tuple, :brackets, :ref, :vect, :vcat, :hcat, :ncat, :typed_vcat, - :typed_hcat, :typed_ncat, :comprehension, :typed_comprehension, :curly, - :braces, :braces_cat - ) || - headof(ret) === :do || - is_dot(headof(ret)) || - is_prime(headof(ret)) || - isinterpolant(ret) + headof(ret) in ( + :call, :tuple, :brackets, :ref, :vect, :vcat, :hcat, :ncat, :typed_vcat, + :typed_hcat, :typed_ncat, :comprehension, :typed_comprehension, :curly, + :braces, :braces_cat + ) || + headof(ret) === :do || + is_dot(headof(ret)) || + is_prime(headof(ret)) || + isinterpolant(ret) if valof(op) == "'" ret = EXPR(op, EXPR[ret], nothing) else diff --git a/src/components/strings.jl b/src/components/strings.jl index ad6fe7df..37ceb44f 100644 --- a/src/components/strings.jl +++ b/src/components/strings.jl @@ -81,13 +81,13 @@ function parse_string_or_cmd(ps::ParseState, prefixed=false) t_str = val(ps.t, ps) if istrip && length(t_str) == 6 if iscmd - return wrapwithcmdmacro(EXPR(:TRIPLESTRING , sfullspan, sspan, "")) + return wrapwithcmdmacro(EXPR(:TRIPLESTRING, sfullspan, sspan, "")) else return EXPR(:TRIPLESTRING, sfullspan, sspan, "") end elseif length(t_str) == 2 if iscmd - return wrapwithcmdmacro(EXPR(:STRING , sfullspan, sspan, "")) + return wrapwithcmdmacro(EXPR(:STRING, sfullspan, sspan, "")) else return EXPR(:STRING, sfullspan, sspan, "") end @@ -246,8 +246,8 @@ function parse_string_or_cmd(ps::ParseState, prefixed=false) end if iscmd str = istrip ? - t_str[nextind(t_str, 1, 3):prevind(t_str, sizeof(t_str), 3)] : - t_str[nextind(t_str, 1, 1):prevind(t_str, sizeof(t_str))] + t_str[nextind(t_str, 1, 3):prevind(t_str, sizeof(t_str), 3)] : + t_str[nextind(t_str, 1, 1):prevind(t_str, sizeof(t_str))] # remove common prefix: if lcp !== nothing str = replace(str, "\n$lcp" => "\n") @@ -277,7 +277,7 @@ function parse_string_or_cmd(ps::ParseState, prefixed=false) end # Drop leading newline if !isempty(ret.args) && isliteral(ret.args[1]) && headof(ret.args[1]) in single_string_T && - !isempty(valof(ret.args[1])) && valof(ret.args[1])[1] == '\n' && shoulddropleadingnewline + !isempty(valof(ret.args[1])) && valof(ret.args[1])[1] == '\n' && shoulddropleadingnewline ret.args[1] = dropleadingnewline(ret.args[1]) end end diff --git a/src/conversion.jl b/src/conversion.jl index f5d8b1e7..673142db 100644 --- a/src/conversion.jl +++ b/src/conversion.jl @@ -13,11 +13,11 @@ end function utf8proc_map_custom(str::String, options) norm_func = @cfunction julia_normalization_map Int32 (Int32, Ptr{Nothing}) nwords = ccall(:utf8proc_decompose_custom, Int, (Ptr{UInt8}, Int, Ptr{UInt8}, Int, Cint, Ptr{Nothing}, Ptr{Nothing}), - str, sizeof(str), C_NULL, 0, options, norm_func, C_NULL) + str, sizeof(str), C_NULL, 0, options, norm_func, C_NULL) nwords < 0 && Base.Unicode.utf8proc_error(nwords) buffer = Base.StringVector(nwords * 4) nwords = ccall(:utf8proc_decompose_custom, Int, (Ptr{UInt8}, Int, Ptr{UInt8}, Int, Cint, Ptr{Nothing}, Ptr{Nothing}), - str, sizeof(str), buffer, nwords, options, norm_func, C_NULL) + str, sizeof(str), buffer, nwords, options, norm_func, C_NULL) nwords < 0 && Base.Unicode.utf8proc_error(nwords) nbytes = ccall(:utf8proc_reencode, Int, (Ptr{UInt8}, Int, Cint), buffer, nwords, options) nbytes < 0 && Base.Unicode.utf8proc_error(nbytes) @@ -34,10 +34,10 @@ function sized_uint_literal(s::AbstractString, b::Integer) # We know integers are all ASCII, so we can use sizeof to compute # the length of ths string more quickly l = (sizeof(s) - 2) * b - l <= 8 && return Base.parse(UInt8, s) - l <= 16 && return Base.parse(UInt16, s) - l <= 32 && return Base.parse(UInt32, s) - l <= 64 && return Base.parse(UInt64, s) + l <= 8 && return Base.parse(UInt8, s) + l <= 16 && return Base.parse(UInt16, s) + l <= 32 && return Base.parse(UInt32, s) + l <= 64 && return Base.parse(UInt64, s) # l <= 128 && return Base.parse(UInt128, s) if l <= 128 @static if VERSION >= v"1.1" @@ -52,8 +52,8 @@ end function sized_uint_oct_literal(s::AbstractString) s[3] == 0 && return sized_uint_literal(s, 3) len = sizeof(s) - (len < 5 || (len == 5 && s <= "0o377")) && return Base.parse(UInt8, s) - (len < 8 || (len == 8 && s <= "0o177777")) && return Base.parse(UInt16, s) + (len < 5 || (len == 5 && s <= "0o377")) && return Base.parse(UInt8, s) + (len < 8 || (len == 8 && s <= "0o177777")) && return Base.parse(UInt16, s) (len < 13 || (len == 13 && s <= "0o37777777777")) && return Base.parse(UInt32, s) (len < 24 || (len == 24 && s <= "0o1777777777777777777777")) && return Base.parse(UInt64, s) # (len < 45 || (len == 45 && s <= "0o3777777777777777777777777777777777777777777")) && return Base.parse(UInt128, s) @@ -184,9 +184,9 @@ function to_codeobject(x::EXPR) elseif x.head === :quotenode # quote nodes in import/using are unwrapped by the Julia parser if x.parent isa EXPR && - x.parent.parent isa EXPR && - is_dot(x.parent.head) && - x.parent.parent.head in (:import, :using) + x.parent.parent isa EXPR && + is_dot(x.parent.head) && + x.parent.parent.head in (:import, :using) to_codeobject(x.args[1]) else QuoteNode(to_codeobject(x.args[1])) diff --git a/src/display.jl b/src/display.jl index 40c90232..952a974b 100644 --- a/src/display.jl +++ b/src/display.jl @@ -1,21 +1,21 @@ -function Base.show(io::IO, x::EXPR, offset = 0, d = 0, er = false) +function Base.show(io::IO, x::EXPR, offset=0, d=0, er=false) T = headof(x) - c = T === :errortoken || er ? :red : :normal + c = T === :errortoken || er ? :red : :normal # Print span as 1-based range of the source string. This presentation is # simple to understand when strings are presented to CSTParser.parse(). print(io, lpad(offset + 1, 3), ":", rpad(offset + x.fullspan, 3), " ") if isidentifier(x) - printstyled(io, " "^d, headof(x) == :NONSTDIDENTIFIER ? valof(x.args[2]) : valof(x), color = :yellow) + printstyled(io, " "^d, headof(x) == :NONSTDIDENTIFIER ? valof(x.args[2]) : valof(x), color=:yellow) x.meta !== nothing && show(io, x.meta) print(io) elseif isoperator(x) - printstyled(io, " "^d, "OP: ", valof(x), color = c) + printstyled(io, " "^d, "OP: ", valof(x), color=c) elseif iskeyword(x) - printstyled(io, " "^d, headof(x), color = :magenta) + printstyled(io, " "^d, headof(x), color=:magenta) elseif ispunctuation(x) - printstyled(io, " "^d, punctuationprinting[headof(x)], color = c) + printstyled(io, " "^d, punctuationprinting[headof(x)], color=c) elseif isliteral(x) - printstyled(io, " "^d, "$(headof(x)): ", valof(x) === nothing ? "nothing" : valof(x), color = c) + printstyled(io, " "^d, "$(headof(x)): ", valof(x) === nothing ? "nothing" : valof(x), color=c) else printstyled(io, " "^d, T, color=c) if x.meta !== nothing diff --git a/src/interface.jl b/src/interface.jl index 40ebe8bb..6329ed50 100644 --- a/src/interface.jl +++ b/src/interface.jl @@ -3,8 +3,8 @@ isidentifier(x::EXPR) = headof(x) === :IDENTIFIER || headof(x) === :NONSTDIDENTI isoperator(x::EXPR) = headof(x) === :OPERATOR isnonstdid(x::EXPR) = headof(x) === :NONSTDIDENTIFIER iskeyword(x::EXPR) = headof(x) in (:ABSTRACT, :BAREMODULE, :BEGIN, :BREAK, :CATCH, :CONST, :CONTINUE, :DO, :ELSE, :ELSEIF, :END, :EXPORT, :FINALLY, :FOR, :FUNCTION, :GLOBAL, :IF, :IMPORT, :importall, :LET, :LOCAL, :MACRO, :MODULE, :MUTABLE, :NEW, :OUTER, :PRIMITIVE, :QUOTE, :RETURN, :STRUCT, :TRY, :TYPE, :USING, :WHILE) -isliteral(x::EXPR) = isstringliteral(x) || iscmd(x) || ischar(x) || headof(x) in (:INTEGER, :BININT, :HEXINT, :OCTINT, :FLOAT, :NOTHING, :TRUE, :FALSE) -ispunctuation(x::EXPR) = is_comma(x) || is_lparen(x) || is_rparen(x) || is_lsquare(x) || is_rsquare(x) || is_lbrace(x) || is_rbrace(x) || headof(x) === :ATSIGN || headof(x) === :DOT +isliteral(x::EXPR) = isstringliteral(x) || iscmd(x) || ischar(x) || headof(x) in (:INTEGER, :BININT, :HEXINT, :OCTINT, :FLOAT, :NOTHING, :TRUE, :FALSE) +ispunctuation(x::EXPR) = is_comma(x) || is_lparen(x) || is_rparen(x) || is_lsquare(x) || is_rsquare(x) || is_lbrace(x) || is_rbrace(x) || headof(x) === :ATSIGN || headof(x) === :DOT isstringliteral(x) = headof(x) === :STRING || headof(x) === :TRIPLESTRING isstring(x) = headof(x) === :string || isstringliteral(x) iscmd(x) = headof(x) === :CMD || headof(x) === :TRIPLECMD @@ -19,10 +19,10 @@ is_id_or_macroname(x::EXPR) = isidentifier(x) || ismacroname(x) # expressions iscall(x::EXPR) = headof(x) === :call isunarycall(x::EXPR) = (headof(x) === :call && length(x) == 2 && (isoperator(x.args[1]) || isoperator(x.args[2]))) -isunarysyntax(x::EXPR) = (isoperator(x.head) && length(x.args) == 1) +isunarysyntax(x::EXPR) = (isoperator(x.head) && length(x.args) == 1) isbinarycall(x::EXPR) = headof(x) === :call && length(x) == 3 && isoperator(x.args[1]) isbinarycall(x::EXPR, op) = headof(x) === :call && length(x) == 3 && isoperator(x.args[1]) && valof(x.args[1]) == op -isbinarysyntax(x::EXPR) = (isoperator(x.head) && length(x.args) == 2) +isbinarysyntax(x::EXPR) = (isoperator(x.head) && length(x.args) == 2) ischainedcall(x::EXPR) = headof(x) === :call && isoperator(x.args[1]) && (valof(x.args[1]) == "+" || valof(x.args[1]) == "*") && hastrivia(x) && isoperator(first(x.trivia)) iswhere(x::EXPR) = headof(x) === :where istuple(x::EXPR) = headof(x) === :tuple diff --git a/src/iterate.jl b/src/iterate.jl index cb57a9b4..1dfa71d2 100644 --- a/src/iterate.jl +++ b/src/iterate.jl @@ -80,7 +80,7 @@ function _getindex(x::EXPR, i) elseif iseven(i) x.trivia[div(i, 2)] else - x.args[div(i + 1, 2) + 1] + x.args[div(i + 1, 2)+1] end elseif length(x.args) > 1 && headof(x.args[2]) === :parameters if i == length(x) @@ -88,7 +88,7 @@ function _getindex(x::EXPR, i) elseif i == 1 x.args[1] elseif isodd(i) - x.args[div(i + 1, 2) + 1] + x.args[div(i + 1, 2)+1] else x.trivia[div(i, 2)] end @@ -155,7 +155,7 @@ function _getindex(x::EXPR, i) else x.args[2] end - elseif length(x) == 2 && (valof(headof(x)) == "\$" || valof(headof(x)) == "::" || valof(headof(x)) == "<:" || valof(headof(x)) == ">:" || valof(headof(x)) == "&") + elseif length(x) == 2 && (valof(headof(x)) == "\$" || valof(headof(x)) == "::" || valof(headof(x)) == "<:" || valof(headof(x)) == ">:" || valof(headof(x)) == "&") if i == 1 x.head else @@ -177,7 +177,7 @@ function _getindex(x::EXPR, i) elseif i == length(x) last(x.trivia) elseif isodd(i) - x.args[div(i+1, 2) - 1] + x.args[div(i + 1, 2)-1] else x.trivia[div(i, 2)] end @@ -187,7 +187,7 @@ function _getindex(x::EXPR, i) end end Base.iterate(x::EXPR) = length(x) == 0 ? nothing : (x[1], 1) -Base.iterate(x::EXPR, s) = s < length(x) ? (x[s + 1], s + 1) : nothing +Base.iterate(x::EXPR, s) = s < length(x) ? (x[s+1], s + 1) : nothing Base.firstindex(x::EXPR) = 1 Base.lastindex(x::EXPR) = x.args === nothing ? 0 : length(x) @@ -263,12 +263,12 @@ function _global(x, i) elseif isodd(i) ai = div(i - 1, 2) if ai > length(x.args) # trailing comma - x.trivia[div(i - 1, 2) + 2] + x.trivia[div(i - 1, 2)+2] else x.args[ai] end else - x.trivia[div(i - 2, 2) + 2] + x.trivia[div(i - 2, 2)+2] end else if hastrivia(x) && (headof(first(x.trivia)) === :GLOBAL || headof(first(x.trivia)) === :LOCAL) @@ -306,7 +306,7 @@ function _struct(x, i) if i == 1 x.trivia[1] elseif 1 < i < 5 - x.args[i - 1] + x.args[i-1] elseif i == 5 x.trivia[2] end @@ -314,7 +314,7 @@ function _struct(x, i) if i < 3 x.trivia[i] elseif 2 < i < 6 - x.args[i - 2] + x.args[i-2] elseif i == 6 x.trivia[3] end @@ -327,7 +327,7 @@ function _block(x, i) if i == 1 x.trivia[1] elseif 1 < i < length(x) - x.args[i - 1] + x.args[i-1] elseif i == length(x) x.trivia[2] end @@ -370,7 +370,8 @@ end function _function(x, i) if length(x.args) == 1 tat(x, i) - else length(x.args) == 2 + else + length(x.args) == 2 taat(x, i) end end @@ -384,7 +385,7 @@ function _braces(x, i) elseif i == length(x) - 1 x.args[1] elseif iseven(i) - x.args[div(i, 2) + 1] + x.args[div(i, 2)+1] else x.trivia[div(i + 1, 2)] end @@ -406,7 +407,7 @@ function _curly(x, i) if i == length(x) - 1 x.args[2] elseif isodd(i) - x.args[div(i + 1, 2) + 1] + x.args[div(i + 1, 2)+1] else x.trivia[div(i, 2)] end @@ -431,7 +432,7 @@ function _colon_in_using(x, i) elseif isodd(i) x.args[div(i + 1, 2)] else - x.trivia[div(i, 2) - 1] + x.trivia[div(i, 2)-1] end end @@ -451,13 +452,13 @@ function _dot(x, i) x.args[i] elseif iseven(ndots) if isodd(i) - x.args[ndots + div(i - ndots + 1, 2)] + x.args[ndots+div(i - ndots + 1, 2)] else x.trivia[div(i - ndots, 2)] end else if iseven(i) - x.args[ndots + div(i - ndots + 1, 2)] + x.args[ndots+div(i - ndots + 1, 2)] else x.trivia[div(i - ndots, 2)] end @@ -504,7 +505,7 @@ function _call(x, i) if iseven(i) x.trivia[div(i - 2, 2)] else - x.args[div(i - 3, 2) + 3] + x.args[div(i - 3, 2)+3] end end end @@ -537,7 +538,7 @@ function _tuple(x, i) elseif isodd(i) x.trivia[div(i + 1, 2)] else - x.args[div(i, 2) + 1] + x.args[div(i, 2)+1] end else if isempty(x.args) @@ -579,17 +580,17 @@ function _if(x, i) if length(x) == 4 # if c expr end taat(x, i) elseif length(x) == 5 # if c expr elseif... end - if i == 1 - x.trivia[1] - elseif i == 2 - x.args[1] - elseif i == 3 - x.args[2] - elseif i == 4 - x.args[3] - elseif i == 5 - x.trivia[2] - end + if i == 1 + x.trivia[1] + elseif i == 2 + x.args[1] + elseif i == 3 + x.args[2] + elseif i == 4 + x.args[3] + elseif i == 5 + x.trivia[2] + end elseif length(x) == 6 # if c expr else expr end if i == 1 x.trivia[1] @@ -624,14 +625,14 @@ function _elseif(x, i) if i == 1 x.trivia[1] else - x.args[i - 1] + x.args[i-1] end end end function _string(x, i) # TODO: this is mega slow - ai, ti = 1,1 + ai, ti = 1, 1 arg = isstringliteral(x.args[1])# && sizeof(valof(x.args[1])) !== x.args[1].fullspan isinterpolant = !arg bracket = false @@ -665,7 +666,7 @@ function _string(x, i) end else if is_exor(x.trivia[ti]) || (x.trivia[ti].head === :errortoken && x.trivia[ti].args[1].head === :OPERATOR && valof(x.trivia[ti].args[1]) == "\$") - if ti < length(x.trivia) && is_lparen(x.trivia[ti + 1]) + if ti < length(x.trivia) && is_lparen(x.trivia[ti+1]) # else isinterpolant = true @@ -701,13 +702,13 @@ function _macrocall(x, i) elseif isodd(i) x.trivia[div(i, 2)] else - x.args[div(i + 1, 2) + 2] + x.args[div(i + 1, 2)+2] end else if isodd(i) x.trivia[div(i, 2)] else - x.args[div(i + 1, 2) + 1] + x.args[div(i + 1, 2)+1] end end end @@ -722,11 +723,11 @@ function _vcat(x, i) x.trivia[1] elseif i == length(x) x.trivia[2] - elseif (i-1) > length(x.args) + elseif (i - 1) > length(x.args) # TODO Remove once we have figured out what is causing this bug error("Illegal indexing into CSTParser. x.head: '$(x.head)', x.trivia: '$(x.trivia)', x.args: '$(x.args)'.") else - x.args[i - 1] + x.args[i-1] end end @@ -738,7 +739,7 @@ function _typed_vcat(x, i) elseif i == length(x) x.trivia[2] else - x.args[i - 1] + x.args[i-1] end end @@ -763,7 +764,7 @@ function _filter(x, i) elseif iseven(i) x.trivia[div(i, 2)] else - x.args[div(i + 1, 2) + 1] + x.args[div(i + 1, 2)+1] end end @@ -844,7 +845,7 @@ function _where(x, i) elseif isodd(i) x.trivia[div(i + 1, 2)] else - x.args[div(i, 2) + 1] + x.args[div(i, 2)+1] end else oddt_evena(x, i) @@ -857,7 +858,7 @@ function _flatten(x, i) lhs[i] end -function _flatten_lhs(x, ret = []) +function _flatten_lhs(x, ret=[]) if x.args[1].head === :generator || x.args[1].head === :flatten if headof(x) !== :flatten for i = 2:length(x) diff --git a/src/lexer.jl b/src/lexer.jl index 458bf1bf..6ca8659c 100644 --- a/src/lexer.jl +++ b/src/lexer.jl @@ -58,8 +58,8 @@ function ParseState(str::Union{IOBuffer,String}, loc::Int) end module ParserFlags - const AllowConstWithoutAssignment = 0x1 - const InQuote = 0x2 +const AllowConstWithoutAssignment = 0x1 +const InQuote = 0x2 end enable!(ps::ParseState, flag::Integer) = ps.flags |= flag @@ -73,10 +73,11 @@ function Base.show(io::IO, ps::ParseState) println(io, "next : ", kindof(ps.nt), " ($(ps.nt))", " ($(wstype(ps.nws)))") end peekchar(ps::ParseState) = peekchar(ps.l) -if !applicable(Base.peek, Tuple{IOBuffer, Char}) +if !applicable(Base.peek, Tuple{IOBuffer,Char}) function _peek(s::IO, ::Type{T}) where T mark(s) - try read(s, T)::T + try + read(s, T)::T finally reset(s) end @@ -87,8 +88,8 @@ else end wstype(t::AbstractToken) = kindof(t) == EmptyWS ? "empty" : - kindof(t) == NewLineWS ? "ws w/ newline" : - kindof(t) == SemiColonWS ? "ws w/ semicolon" : "ws" + kindof(t) == NewLineWS ? "ws w/ newline" : + kindof(t) == SemiColonWS ? "ws w/ semicolon" : "ws" function next(ps::ParseState) # shift old tokens @@ -199,16 +200,16 @@ end isemptyws(t::AbstractToken) = kindof(t) == EmptyWS isnewlinews(t::AbstractToken) = kindof(t) === NewLineWS isendoflinews(t::AbstractToken) = kindof(t) == SemiColonWS || kindof(t) == NewLineWS -@inline val(token::AbstractToken, ps::ParseState) = String(ps.l.io.data[token.startbyte + 1:token.endbyte + 1]) +@inline val(token::AbstractToken, ps::ParseState) = String(ps.l.io.data[token.startbyte+1:token.endbyte+1]) both_symbol_and_op(t::AbstractToken) = kindof(t) === Tokens.WHERE || kindof(t) === Tokens.IN || kindof(t) === Tokens.ISA isprefixableliteral(t::AbstractToken) = (kindof(t) === Tokens.STRING || kindof(t) === Tokens.TRIPLE_STRING || kindof(t) === Tokens.CMD || kindof(t) === Tokens.TRIPLE_CMD) isassignmentop(t::AbstractToken) = Tokens.begin_assignments < kindof(t) < Tokens.end_assignments isidentifier(t::AbstractToken) = kindof(t) === Tokens.IDENTIFIER isliteral(t::AbstractToken) = Tokens.begin_literal < kindof(t) < Tokens.end_literal -isbool(t::AbstractToken) = Tokens.TRUE ≤ kindof(t) ≤ Tokens.FALSE -iscomma(t::AbstractToken) = kindof(t) === Tokens.COMMA -iscolon(t::AbstractToken) = kindof(t) === Tokens.COLON +isbool(t::AbstractToken) = Tokens.TRUE ≤ kindof(t) ≤ Tokens.FALSE +iscomma(t::AbstractToken) = kindof(t) === Tokens.COMMA +iscolon(t::AbstractToken) = kindof(t) === Tokens.COLON iskeyword(t::AbstractToken) = Tokens.iskeyword(kindof(t)) isinstance(t::AbstractToken) = isidentifier(t) || isliteral(t) || isbool(t) || iskeyword(t) ispunctuation(t::AbstractToken) = iscomma(t) || kindof(t) === Tokens.END || Tokens.LSQUARE ≤ kindof(t) ≤ Tokens.RPAREN || kindof(t) === Tokens.AT_SIGN diff --git a/src/packagedef.jl b/src/packagedef.jl index c547b8ec..99858255 100644 --- a/src/packagedef.jl +++ b/src/packagedef.jl @@ -33,7 +33,7 @@ Acceptable starting tokens are: + An `@`. """ -function parse_expression(ps::ParseState, esc_on_error = false) +function parse_expression(ps::ParseState, esc_on_error=false) if kindof(ps.nt) === Tokens.ENDMARKER ret = mErrorToken(ps, UnexpectedToken) elseif (esc_on_error && ps.nt.kind == Tokens.ERROR) @@ -142,9 +142,9 @@ function parse_compound(ps::ParseState, ret::EXPR) # prime operator followed by an identifier has an implicit multiplication nextarg = @precedence ps TimesOp parse_expression(ps) ret = EXPR(:call, EXPR[EXPR(:OPERATOR, 0, 0, "*"), ret, nextarg], nothing) -# ############################################################################### -# Everything below here is an error -# ############################################################################### + # ############################################################################### + # Everything below here is an error + # ############################################################################### else ps.errored = true if kindof(ps.nt) in (Tokens.RPAREN, Tokens.RSQUARE, Tokens.RBRACE) @@ -172,7 +172,7 @@ Parses an expression starting with a `(`. function parse_paren(ps::ParseState) args = EXPR[] trivia = EXPR[EXPR(ps)] - @closeparen ps @default ps @nocloser ps :inwhere parse_comma_sep(ps, args, trivia, false, true, true, insert_params_at = 1) + @closeparen ps @default ps @nocloser ps :inwhere parse_comma_sep(ps, args, trivia, false, true, true, insert_params_at=1) if length(args) == 1 && length(trivia) == 1 && ((kindof(ps.ws) !== SemiColonWS || headof(args[1]) === :block) && headof(args[1]) !== :parameters) accept_rparen(ps, trivia) ret = EXPR(:brackets, args, trivia) @@ -291,10 +291,10 @@ end function _continue_doc_parse(ps::ParseState, x::EXPR) kindof(ps.nt) !== Tokens.ENDMARKER && - headof(x) === :macrocall && - valof(x.args[1]) == "@doc" && - length(x.args) < 4 && - ps.t.endpos[1] + 1 == ps.nt.startpos[1] + headof(x) === :macrocall && + valof(x.args[1]) == "@doc" && + length(x.args) < 4 && + ps.t.endpos[1] + 1 == ps.nt.startpos[1] end include("precompile.jl") diff --git a/src/recovery.jl b/src/recovery.jl index 42db18f9..d54e6ae9 100644 --- a/src/recovery.jl +++ b/src/recovery.jl @@ -53,8 +53,8 @@ end function requires_no_ws(x, ps) if !(kindof(ps.nt) === Tokens.RPAREN || - kindof(ps.nt) === Tokens.RBRACE || - kindof(ps.nt) === Tokens.RSQUARE || kindof(ps.nt) === Tokens.END) && x.span != x.fullspan + kindof(ps.nt) === Tokens.RBRACE || + kindof(ps.nt) === Tokens.RSQUARE || kindof(ps.nt) === Tokens.END) && x.span != x.fullspan return mErrorToken(ps, x, UnexpectedWhiteSpace) else return x diff --git a/src/spec.jl b/src/spec.jl index edef7a66..e3295710 100644 --- a/src/spec.jl +++ b/src/spec.jl @@ -1,29 +1,29 @@ # Operator hierarchy -const AssignmentOp = 1 +const AssignmentOp = 1 const ConditionalOp = 2 -const ArrowOp = 3 -const LazyOrOp = 4 -const LazyAndOp = 5 -const ComparisonOp = 6 -const PipeOp = 7 -const ColonOp = 8 -const PlusOp = 9 +const ArrowOp = 3 +const LazyOrOp = 4 +const LazyAndOp = 5 +const ComparisonOp = 6 +const PipeOp = 7 +const ColonOp = 8 +const PlusOp = 9 @static if Base.operator_precedence(:<<) == 12 - const BitShiftOp = 10 - const TimesOp = 11 - const RationalOp = 12 + const BitShiftOp = 10 + const TimesOp = 11 + const RationalOp = 12 else - const TimesOp = 10 - const RationalOp = 11 - const BitShiftOp = 12 + const TimesOp = 10 + const RationalOp = 11 + const BitShiftOp = 12 end -const PowerOp = 13 +const PowerOp = 13 const DeclarationOp = 14 -const WhereOp = 15 -const DotOp = 16 -const PrimeOp = 16 -const DddotOp = 7 -const AnonFuncOp = 14 +const WhereOp = 15 +const DotOp = 16 +const PrimeOp = 16 +const DddotOp = 7 +const AnonFuncOp = 14 @enum(ErrorKind, UnexpectedToken, @@ -118,20 +118,20 @@ function EXPR(head::Union{Symbol,EXPR}, args::Vector{EXPR}, trivia::Union{Vector ex end -function EXPR(head::Union{Symbol,EXPR}, args::Vector{EXPR}, trivia::Union{Vector{EXPR},Nothing} = EXPR[]) +function EXPR(head::Union{Symbol,EXPR}, args::Vector{EXPR}, trivia::Union{Vector{EXPR},Nothing}=EXPR[]) ret = EXPR(head, args, trivia, 0, 0) update_span!(ret) ret end # These methods are for terminal/childless expressions. -@noinline EXPR(head::Union{Symbol,EXPR}, fullspan::Int, span::Int, val = nothing) = EXPR(head, nothing, nothing, fullspan, span, val, nothing, nothing) +@noinline EXPR(head::Union{Symbol,EXPR}, fullspan::Int, span::Int, val=nothing) = EXPR(head, nothing, nothing, fullspan, span, val, nothing, nothing) @noinline EXPR(head::Union{Symbol,EXPR}, ps::ParseState) = EXPR(head, ps.nt.startbyte - ps.t.startbyte, ps.t.endbyte - ps.t.startbyte + 1, val(ps.t, ps)) @noinline EXPR(ps::ParseState) = EXPR(tokenkindtoheadmap(kindof(ps.t)), ps) @noinline function mLITERAL(ps::ParseState) if kindof(ps.t) === Tokens.STRING || kindof(ps.t) === Tokens.TRIPLE_STRING || - kindof(ps.t) === Tokens.CMD || kindof(ps.t) === Tokens.TRIPLE_CMD + kindof(ps.t) === Tokens.CMD || kindof(ps.t) === Tokens.TRIPLE_CMD return parse_string_or_cmd(ps) else v = val(ps.t, ps) @@ -163,7 +163,7 @@ function update_span!(x::EXPR) end if x.head isa EXPR && isoperator(x.head) && (is_dddot(x.head) || is_prime(x.head)) # trailing unary operator - x.span = x.fullspan - x.head.fullspan + x.head.span + x.span = x.fullspan - x.head.fullspan + x.head.span elseif hastrivia(x) && lastchildistrivia(x) x.span = x.fullspan - last(x.trivia).fullspan + last(x.trivia).span elseif !isempty(x.args) diff --git a/src/utils.jl b/src/utils.jl index efb6437a..d54e20de 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -6,49 +6,49 @@ stop. """ function closer(ps::ParseState) kindof(ps.nt) === Tokens.ENDMARKER || - (ps.closer.newline && kindof(ps.ws) == NewLineWS && !iscomma(ps.t)) || - (ps.closer.semicolon && kindof(ps.ws) == SemiColonWS) || - (isoperator(ps.nt) && precedence(ps.nt) <= ps.closer.precedence) || - (kindof(ps.nt) === Tokens.WHERE && ps.closer.precedence == LazyAndOp) || - (ps.closer.inwhere && kindof(ps.nt) === Tokens.WHERE) || - (ps.closer.inwhere && ps.closer.ws && kindof(ps.t) === Tokens.RPAREN && isoperator(ps.nt) && precedence(ps.nt) < DeclarationOp) || - (ps.closer.precedence > WhereOp && ( - (kindof(ps.nt) === Tokens.LPAREN && !(kindof(ps.t) === Tokens.EX_OR)) || - kindof(ps.nt) === Tokens.LBRACE || - kindof(ps.nt) === Tokens.LSQUARE || - (kindof(ps.nt) === Tokens.STRING && isemptyws(ps.ws)) || - ((kindof(ps.nt) === Tokens.RPAREN || kindof(ps.nt) === Tokens.RSQUARE) && isidentifier(ps.nt)) - )) || - (iscomma(ps.nt) && ps.closer.precedence > AssignmentOp) || - kindof(ps.nt) === Tokens.ENDMARKER || - (ps.closer.comma && iscomma(ps.nt)) || - (ps.closer.tuple && (iscomma(ps.nt) || isassignmentop(ps.nt))) || - (kindof(ps.nt) === Tokens.FOR && ps.closer.precedence > -1) || - (ps.closer.block && kindof(ps.nt) === Tokens.END) || - (ps.closer.paren && kindof(ps.nt) === Tokens.RPAREN) || - (ps.closer.brace && kindof(ps.nt) === Tokens.RBRACE) || - (ps.closer.square && kindof(ps.nt) === Tokens.RSQUARE) || - # tilde parsing in vect exprs needs to be special cased because `~` has assignment precedence - (@static VERSION < v"1.4" ? - false : - ((ps.closer.insquare || ps.closer.inmacro) && kindof(ps.nt) === Tokens.APPROX && !isemptyws(ps.ws) && isemptyws(ps.nws)) - ) || - kindof(ps.nt) === Tokens.ELSEIF || - kindof(ps.nt) === Tokens.ELSE || - kindof(ps.nt) === Tokens.CATCH || - kindof(ps.nt) === Tokens.FINALLY || - (ps.closer.ifop && isoperator(ps.nt) && (precedence(ps.nt) <= 0 || kindof(ps.nt) === Tokens.COLON)) || - (ps.closer.range && (kindof(ps.nt) === Tokens.FOR || iscomma(ps.nt) || kindof(ps.nt) === Tokens.IF)) || - (ps.closer.ws && !isemptyws(ps.ws) && - !iscomma(ps.nt) && - !iscomma(ps.t) && - !(!ps.closer.inmacro && kindof(ps.nt) === Tokens.FOR) && - !(kindof(ps.nt) === Tokens.DO) && - !( - (isbinaryop(ps.nt) && !(ps.closer.wsop && isemptyws(ps.nws) && isunaryop(ps.nt) && precedence(ps.nt) > 7)) || - (isunaryop(ps.t) && kindof(ps.ws) == WS && kindof(ps.lt) !== CSTParser.Tokens.COLON) + (ps.closer.newline && kindof(ps.ws) == NewLineWS && !iscomma(ps.t)) || + (ps.closer.semicolon && kindof(ps.ws) == SemiColonWS) || + (isoperator(ps.nt) && precedence(ps.nt) <= ps.closer.precedence) || + (kindof(ps.nt) === Tokens.WHERE && ps.closer.precedence == LazyAndOp) || + (ps.closer.inwhere && kindof(ps.nt) === Tokens.WHERE) || + (ps.closer.inwhere && ps.closer.ws && kindof(ps.t) === Tokens.RPAREN && isoperator(ps.nt) && precedence(ps.nt) < DeclarationOp) || + (ps.closer.precedence > WhereOp && ( + (kindof(ps.nt) === Tokens.LPAREN && !(kindof(ps.t) === Tokens.EX_OR)) || + kindof(ps.nt) === Tokens.LBRACE || + kindof(ps.nt) === Tokens.LSQUARE || + (kindof(ps.nt) === Tokens.STRING && isemptyws(ps.ws)) || + ((kindof(ps.nt) === Tokens.RPAREN || kindof(ps.nt) === Tokens.RSQUARE) && isidentifier(ps.nt)) )) || - (ps.closer.unary && (kindof(ps.t) in (Tokens.INTEGER, Tokens.FLOAT, Tokens.RPAREN, Tokens.RSQUARE, Tokens.RBRACE) && isidentifier(ps.nt))) + (iscomma(ps.nt) && ps.closer.precedence > AssignmentOp) || + kindof(ps.nt) === Tokens.ENDMARKER || + (ps.closer.comma && iscomma(ps.nt)) || + (ps.closer.tuple && (iscomma(ps.nt) || isassignmentop(ps.nt))) || + (kindof(ps.nt) === Tokens.FOR && ps.closer.precedence > -1) || + (ps.closer.block && kindof(ps.nt) === Tokens.END) || + (ps.closer.paren && kindof(ps.nt) === Tokens.RPAREN) || + (ps.closer.brace && kindof(ps.nt) === Tokens.RBRACE) || + (ps.closer.square && kindof(ps.nt) === Tokens.RSQUARE) || + # tilde parsing in vect exprs needs to be special cased because `~` has assignment precedence + (@static VERSION < v"1.4" ? + false : + ((ps.closer.insquare || ps.closer.inmacro) && kindof(ps.nt) === Tokens.APPROX && !isemptyws(ps.ws) && isemptyws(ps.nws)) + ) || + kindof(ps.nt) === Tokens.ELSEIF || + kindof(ps.nt) === Tokens.ELSE || + kindof(ps.nt) === Tokens.CATCH || + kindof(ps.nt) === Tokens.FINALLY || + (ps.closer.ifop && isoperator(ps.nt) && (precedence(ps.nt) <= 0 || kindof(ps.nt) === Tokens.COLON)) || + (ps.closer.range && (kindof(ps.nt) === Tokens.FOR || iscomma(ps.nt) || kindof(ps.nt) === Tokens.IF)) || + (ps.closer.ws && !isemptyws(ps.ws) && + !iscomma(ps.nt) && + !iscomma(ps.t) && + !(!ps.closer.inmacro && kindof(ps.nt) === Tokens.FOR) && + !(kindof(ps.nt) === Tokens.DO) && + !( + (isbinaryop(ps.nt) && !(ps.closer.wsop && isemptyws(ps.nws) && isunaryop(ps.nt) && precedence(ps.nt) > 7)) || + (isunaryop(ps.t) && kindof(ps.ws) == WS && kindof(ps.lt) !== CSTParser.Tokens.COLON) + )) || + (ps.closer.unary && (kindof(ps.t) in (Tokens.INTEGER, Tokens.FLOAT, Tokens.RPAREN, Tokens.RSQUARE, Tokens.RBRACE) && isidentifier(ps.nt))) end """ @@ -207,10 +207,10 @@ end isajuxtaposition(ps::ParseState, ret::EXPR) = ((isnumber(ret) && (isidentifier(ps.nt) || kindof(ps.nt) === Tokens.LPAREN || kindof(ps.nt) === Tokens.CMD || kindof(ps.nt) === Tokens.STRING || kindof(ps.nt) === Tokens.TRIPLE_STRING)) || - ((is_prime(ret.head) && isidentifier(ps.nt)) || - ((kindof(ps.t) === Tokens.RPAREN || kindof(ps.t) === Tokens.RSQUARE) && (isidentifier(ps.nt) || kindof(ps.nt) === Tokens.CMD)) || - ((kindof(ps.t) === Tokens.STRING || kindof(ps.t) === Tokens.TRIPLE_STRING) && (kindof(ps.nt) === Tokens.STRING || kindof(ps.nt) === Tokens.TRIPLE_STRING)))) || ((kindof(ps.t) in (Tokens.INTEGER, Tokens.FLOAT) || kindof(ps.t) in (Tokens.RPAREN, Tokens.RSQUARE, Tokens.RBRACE)) && isidentifier(ps.nt)) || - (isnumber(ret) && ps.closer.inref && (ps.nt.kind === Tokens.END || ps.nt.kind === Tokens.BEGIN)) + ((is_prime(ret.head) && isidentifier(ps.nt)) || + ((kindof(ps.t) === Tokens.RPAREN || kindof(ps.t) === Tokens.RSQUARE) && (isidentifier(ps.nt) || kindof(ps.nt) === Tokens.CMD)) || + ((kindof(ps.t) === Tokens.STRING || kindof(ps.t) === Tokens.TRIPLE_STRING) && (kindof(ps.nt) === Tokens.STRING || kindof(ps.nt) === Tokens.TRIPLE_STRING)))) || ((kindof(ps.t) in (Tokens.INTEGER, Tokens.FLOAT) || kindof(ps.t) in (Tokens.RPAREN, Tokens.RSQUARE, Tokens.RBRACE)) && isidentifier(ps.nt)) || + (isnumber(ret) && ps.closer.inref && (ps.nt.kind === Tokens.END || ps.nt.kind === Tokens.BEGIN)) """ has_error(ps::ParseState) @@ -277,8 +277,8 @@ Reversed version of firstdiff but returns two indices, one for each string. """ function revfirstdiff(s0::AbstractString, s1::AbstractString) minlength = min(sizeof(s0), sizeof(s1)) - @inbounds for i in 0:minlength - 1 - if codeunits(s0)[end - i] !== codeunits(s1)[end - i] + @inbounds for i in 0:minlength-1 + if codeunits(s0)[end-i] !== codeunits(s1)[end-i] return sizeof(s0) - i, sizeof(s1) - i# This could return a non-commencing byte of a multi-byte unicode sequence. end end @@ -305,20 +305,20 @@ end comp(x, y) = x == y function comp(x::CSTParser.EXPR, y::CSTParser.EXPR) comp(x.head, y.head) && - x.span == y.span && - x.fullspan == y.fullspan && - x.val == y.val && - length(x) == length(y) && - all(comp(x[i], y[i]) for i = 1:length(x)) + x.span == y.span && + x.fullspan == y.fullspan && + x.val == y.val && + length(x) == length(y) && + all(comp(x[i], y[i]) for i = 1:length(x)) end -function minimal_reparse(s0, s1, x0 = CSTParser.parse(s0, true), x1 = CSTParser.parse(s1, true); inds = false) +function minimal_reparse(s0, s1, x0=CSTParser.parse(s0, true), x1=CSTParser.parse(s1, true); inds=false) if has_error(x0) return inds ? (1:0, 1:length(x1.args), 1:0) : x1 # Error while re-parsing, so lets return the whole expression instead of patching end if sizeof(s0) !== x0.fullspan - error("minimal reparse - original input text length doesn't match the full span of the provided CST.") + error("minimal reparse - original input text length doesn't match the full span of the provided CST.") # return inds ? (1:0, 1:length(x1.args), 1:0) : x1 end @@ -328,7 +328,7 @@ function minimal_reparse(s0, s1, x0 = CSTParser.parse(s0, true), x1 = CSTParser. if sizeof(s1) !== x1.fullspan error("minimal reparse - new input text length doesn't match the full span of the provided CST.") - # return inds ? (1:0, 1:length(x1.args), 1:0) : x1 + # return inds ? (1:0, 1:length(x1.args), 1:0) : x1 end isempty(x0.args) && return inds ? (1:0, 1:length(x1.args), 1:0) : x1 # Original CST was empty x1.fullspan == 0 && return inds ? (1:0, 1:0, 1:0) : x1 # New CST is empty @@ -351,30 +351,30 @@ function minimal_reparse(s0, s1, x0 = CSTParser.parse(s0, true), x1 = CSTParser. # we can re-use x0.args[r1] # assume we'll just use x1.args from here on - r2 = (last(r1) + 1):length(x1.args) + r2 = (last(r1)+1):length(x1.args) r3 = 0:-1 # though we now check whether there is a sequence at the end of x0.args and # x1.args that match offset = sizeof(s1) - for i = 0:min(length(x0.args) - last(r1), length(x0.args), length(x1.args)) - 1 - if !quick_comp(x0.args[end - i], x1.args[end - i]) || - offset <= i1 || - length(x0.args) - i == last(r1) + 1 || - offset - x1.args[end-i].fullspan <= i2 <= offset - - r2 = first(r2):length(x1.args) - i - r3 = length(x0.args) .+ ((-i + 1):0) + for i = 0:min(length(x0.args) - last(r1), length(x0.args), length(x1.args))-1 + if !quick_comp(x0.args[end-i], x1.args[end-i]) || + offset <= i1 || + length(x0.args) - i == last(r1) + 1 || + offset - x1.args[end-i].fullspan <= i2 <= offset + + r2 = first(r2):length(x1.args)-i + r3 = length(x0.args) .+ ((-i+1):0) break end - offset -= x1.args[end - i].fullspan + offset -= x1.args[end-i].fullspan end inds && return r1, r2, r3 x2 = CSTParser.EXPR(x0.head, CSTParser.EXPR[ - x0.args[r1] - x1.args[r2] - x0.args[r3] - ], nothing) + x0.args[r1] + x1.args[r2] + x0.args[r3] + ], nothing) return x2 end @@ -397,7 +397,7 @@ check_span(x, neq = []) Recursively checks whether the span of an expression equals the sum of the span of its components. Returns a vector of failing expressions. """ -function check_span(x::EXPR, neq = []) +function check_span(x::EXPR, neq=[]) (ispunctuation(x) || isidentifier(x) || iskeyword(x) || isoperator(x) || isliteral(x) || headof(x) == :string) && return neq s = 0 @@ -425,8 +425,8 @@ end function speed_test() dir = dirname(Base.find_source_file("essentials.jl")) println("speed test : ", @timed(for i = 1:5 - parse(read(joinpath(dir, "essentials.jl"), String), true); - parse(read(joinpath(dir, "abstractarray.jl"), String), true); + parse(read(joinpath(dir, "essentials.jl"), String), true) + parse(read(joinpath(dir, "abstractarray.jl"), String), true) end)[2]) end @@ -481,7 +481,7 @@ function _unescape_string(io, s::AbstractString) k = 1 n = c - '0' while (k += 1) <= 3 && !isempty(a) - c = Base.peek(a) + c = Base.peek(a) n = ('0' <= c <= '7') ? n << 3 + c - '0' : break popfirst!(a) end @@ -620,11 +620,11 @@ _kw_convert(x::EXPR) = EXPR(:kw, EXPR[x.args[1], x.args[2]], EXPR[x.head], x.ful When parsing a function or macro signature, should it be converted to a tuple? """ convertsigtotuple(sig::EXPR) = isbracketed(sig) && !( - istuple(sig.args[1]) || - iscall(sig.args[1]) || - headof(sig.args[1]) === :block || - issplat(sig.args[1]) - ) + istuple(sig.args[1]) || + iscall(sig.args[1]) || + headof(sig.args[1]) === :block || + issplat(sig.args[1]) +) """ docable(head) @@ -662,22 +662,22 @@ end function issuffixableliteral(ps::ParseState, x::EXPR) # prefixed string/cmd macros can be suffixed by identifiers or numeric literals return ( - isidentifier(ps.nt) || - isnumberliteral(ps.nt) || - isbool(ps.nt) - ) && - isemptyws(ps.ws) && - ismacrocall(x) && - ( - ( - valof(x.args[1]) isa String && - (endswith(valof(x.args[1]), "_str") || endswith(valof(x.args[1]), "_cmd")) - ) || - ( - is_getfield(x.args[1]) && x.args[1].args[2] isa EXPR && x.args[1].args[2].head in (:quote, :quotenode) && - (endswith(valof(x.args[1].args[2].args[1]), "_str") || endswith(valof(x.args[1].args[2].args[1]), "_cmd")) - ) - ) + isidentifier(ps.nt) || + isnumberliteral(ps.nt) || + isbool(ps.nt) + ) && + isemptyws(ps.ws) && + ismacrocall(x) && + ( + ( + valof(x.args[1]) isa String && + (endswith(valof(x.args[1]), "_str") || endswith(valof(x.args[1]), "_cmd")) + ) || + ( + is_getfield(x.args[1]) && x.args[1].args[2] isa EXPR && x.args[1].args[2].head in (:quote, :quotenode) && + (endswith(valof(x.args[1].args[2].args[1]), "_str") || endswith(valof(x.args[1].args[2].args[1]), "_cmd")) + ) + ) end function loop_check(ps, prevpos) diff --git a/test/parser/test_operators.jl b/test/parser/test_operators.jl index 6e0c3fc9..e262c0b8 100644 --- a/test/parser/test_operators.jl +++ b/test/parser/test_operators.jl @@ -1,6 +1,6 @@ # @testitem "Binary Operators" begin - # using CSTParser: remlineinfo! - # include("../shared.jl") +# using CSTParser: remlineinfo! +# include("../shared.jl") # for iter = 1:25 # println(iter) diff --git a/test/parser/test_parser.jl b/test/parser/test_parser.jl index 18676de5..a4b0c83a 100644 --- a/test/parser/test_parser.jl +++ b/test/parser/test_parser.jl @@ -699,7 +699,7 @@ end end @testitem "nonstandard identifier (var\"blah\") parsing" begin -using CSTParser: remlineinfo! + using CSTParser: remlineinfo! include("../shared.jl") if VERSION > v"1.3.0-" diff --git a/test/parser/test_types.jl b/test/parser/test_types.jl index 34c653d4..f6104b4a 100644 --- a/test/parser/test_types.jl +++ b/test/parser/test_types.jl @@ -1,7 +1,7 @@ @testitem "Abstract" begin using CSTParser: remlineinfo! include("../shared.jl") - + @test "abstract type t end" |> test_expr @test "abstract type t{T} end" |> test_expr @test "abstract type t <: S end" |> test_expr @@ -11,7 +11,7 @@ end @testitem "primitive" begin using CSTParser: remlineinfo! include("../shared.jl") - + @test "primitive type Int 64 end" |> test_expr @test "primitive type Int 4*16 end" |> test_expr end @@ -19,7 +19,7 @@ end @testitem "Structs" begin using CSTParser: remlineinfo! include("../shared.jl") - + @test "struct a end" |> test_expr @test "struct a; end" |> test_expr @test "struct a; b;end" |> test_expr @@ -61,4 +61,4 @@ end arg2 end""" |> test_expr end -end \ No newline at end of file +end diff --git a/test/shared.jl b/test/shared.jl index 2b2c6ab8..0bdf24aa 100644 --- a/test/shared.jl +++ b/test/shared.jl @@ -28,7 +28,7 @@ function test_iter(ex) end end -function test_expr(s, head, n, endswithtrivia = false) +function test_expr(s, head, n, endswithtrivia=false) x = CSTParser.parse(s) head === nothing || @test headof(x) === head @test length(x) === n @@ -38,22 +38,22 @@ function test_expr(s, head, n, endswithtrivia = false) @test isempty(check_span(x)) check_parents(x) test_iter(x) - @test endswithtrivia ? (x.fullspan-x.span) == (last(x.trivia).fullspan - last(x.trivia).span) : (x.fullspan-x.span) == (last(x.args).fullspan - last(x.args).span) + @test endswithtrivia ? (x.fullspan - x.span) == (last(x.trivia).fullspan - last(x.trivia).span) : (x.fullspan - x.span) == (last(x.args).fullspan - last(x.args).span) end randop() = rand(["-->", "→", - "||", - "&&", - "<", "==", "<:", ">:", - "<|", "|>", - ":", - "+", "-", - ">>", "<<", - "*", "/", - "//", - "^", "↑", - "::", - ".", "->"]) + "||", + "&&", + "<", "==", "<:", ">:", + "<|", "|>", + ":", + "+", "-", + ">>", "<<", + "*", "/", + "//", + "^", "↑", + "::", + ".", "->"]) test_expr_broken(str) = test_expr(str, false) @@ -98,7 +98,7 @@ end function test_iter_spans(x) n = 0 for i = 1:length(x) - a = x[i] + a = x[i] if !(a isa EXPR) @info i, headof(x), to_codeobject(x) end diff --git a/test/test_display.jl b/test/test_display.jl index cdfbf487..15c2b280 100644 --- a/test/test_display.jl +++ b/test/test_display.jl @@ -1,5 +1,5 @@ @testitem "show" begin x = CSTParser.parse("a + (b*c) - d") @test sprint(show, x) === - " 1:13 call\n 1:2 OP: -\n 3:12 call\n 3:4 OP: +\n 5:6 a\n 7:12 brackets\n 7:9 call\n 7:7 OP: *\n 8:8 b\n 9:9 c\n 13:13 d" + " 1:13 call\n 1:2 OP: -\n 3:12 call\n 3:4 OP: +\n 5:6 a\n 7:12 brackets\n 7:9 call\n 7:7 OP: *\n 8:8 b\n 9:9 c\n 13:13 d" end diff --git a/test/test_errparse.jl b/test/test_errparse.jl index fae720c7..5f456174 100644 --- a/test/test_errparse.jl +++ b/test/test_errparse.jl @@ -5,7 +5,7 @@ using CSTParser: to_codeobject using Tokenize: tokenize import Tokenize.Tokens: untokenize - + function trav(x, f=x -> nothing) f(x) for a in x diff --git a/test/test_interface.jl b/test/test_interface.jl index e5d2e353..42b5f50f 100644 --- a/test/test_interface.jl +++ b/test/test_interface.jl @@ -28,7 +28,7 @@ end @testitem "get_name" begin using CSTParser: valof - + @test valof(CSTParser.get_name(CSTParser.parse("struct T end"))) == "T" @test valof(CSTParser.get_name(CSTParser.parse("struct T{T} end"))) == "T" @test valof(CSTParser.get_name(CSTParser.parse("struct T <: T end"))) == "T" diff --git a/test/test_spec.jl b/test/test_spec.jl index 111d461e..805f83fc 100644 --- a/test/test_spec.jl +++ b/test/test_spec.jl @@ -133,7 +133,9 @@ end test_expr("a where {b }", :where, 5, true) test_expr("a where {b,c } ", :where, 7, true) test_expr("a...", nothing, 2, false) - @test let x = cst"a... "; x.fullspan - x.span == 1 end + @test let x = cst"a... " + x.fullspan - x.span == 1 + end test_expr("a <: b", nothing, 3, false) # https://github.com/julia-vscode/CSTParser.jl/issues/278