CodeSearch.Match
— Typestruct Match <: AbstractMatch
syntax_node::JuliaSyntax.SyntaxNode
captures::Vector{JuliaSyntax.SyntaxNode}
end
Represents a single match to a Pattern
, typically created from the eachmatch
or match
function.
The syntax_node
field stores the SyntaxNode
that matched the Pattern
and the captures
field stores the SyntaxNode
s that fill match each wildcard in the Pattern
, indexed in the order they appear.
Methods that accept Match
objects are defined for Expr
, SyntaxNode
, AbstractString
, indices
, and getindex
.
Examples
julia> m = match(j"√*", "2 + √ x")
CodeSearch.Match((call-pre √ x), captures=[x])
julia> m.captures
1-element Vector{JuliaSyntax.SyntaxNode}:
x
julia> m[1]
line:col│ tree │ file_name
1:7 │x
julia> Expr(m)
:(√x)
julia> AbstractString(m)
" √ x"
julia> CodeSearch.indices(m)
4:9
CodeSearch.Pattern
— TypePattern <: AbstractPattern
A struct that represents a Julia expression with wildcards. When matching Pattern
s, it is possilbe for multiple matches to nest within one another.
The fields and constructor of this struct are not part of the public API. See @j_str
and pattern
for the public API for creating Pattern
s.
Methods accepting Pattern
objects are defined for eachmatch
, match
, findall
, findfirst
, findlast
, occursin
, and count
.
Extended Help
The following are implmenetation details:
The expression is stored as an ordinary SyntaxNode
in the internal syntax_node
field. Wildcards in that expression are represented by the symbol stored in the internal wildcard_symbol
field. For example, the expression a + (b + *)
might be stored as Pattern((call-i a + (call-i b + wildcard)), :wildcard)
.
CodeSearch.gen_wildcard
— Functiongen_wildcard(str, prefix="wildcard")
return a string starting with prefix
that is not in str
CodeSearch.indices
— Functionindices(m)
Return the indices into a source datastructure that a view is derived from.
Examples
julia> m = match(j"x/*", "4 + x/2")
CodeSearch.Match((call-i x / 2), captures=[2])
julia> indices(m)
4:7
julia> c = m[1]
line:col│ tree │ file_name
1:7 │2
julia> indices(c)
7:7
CodeSearch.pattern
— Methodpattern(str::AbstractString) -> Pattern
Function version of the j"str"
macro. See @j_str
for documentation.
Examples
julia> using CodeSearch: pattern
julia> pattern("a + (b + *)")
j"a + (b + *)"
julia> match(pattern("(b + *)"), "(b + 6)")
CodeSearch.Match((call-i b + 6), captures=[6])
julia> findall(pattern("* + *"), "(a+b)+(d+e)")
3-element Vector{UnitRange{Int64}}:
1:11
2:4
8:10
julia> match(pattern("(* + *) \\* *"), "(a-b)*(d+e)") # no match -> returns nothing
julia> occursin(pattern("(* + *) \\* *"), "(a-b)*(d+e)")
false
julia> eachmatch(pattern("*(\"hello world\")"), "print(\"hello world\"), display(\"hello world\")")
2-element Vector{CodeSearch.Match}:
Match((call print (string "hello world")), captures=[print])
Match((call display (string "hello world")), captures=[display])
julia> count(pattern("*(*)"), "a(b(c))")
2
julia> match(pattern("(* + *) \\* *"), "(a+b)*(d+e)")
CodeSearch.Match((call-i (call-i a + b) * (call-i d + e)), captures=[a, b, (call-i d + e)])
CodeSearch.prepare_wildcards
— Methodprepare_wildcards(str) -> (new_str, wildcard_str)
Replace *
with an identifier that does not occur in str
(preferrably "wildcard"
) and return the new string and the identifier. *
may be escaped, and the new identifier is padded with spaces only when necessary to prevent it from parsing together with characters before or after it.
CodeSearch.@j_str
— Macroj"str" -> Pattern
Construct a Pattern
, such as j"a + (b + *)"
that matches Julia code.
The *
character is a wildcard that matches any expression, and matching is performed insensitive of whitespace and comments. Only the characters "
and *
must be escaped, and interpolation is not supported.
See pattern
for the function version of this macro if you need interpolation.
Examples
julia> j"a + (b + *)"
j"a + (b + *)"
julia> match(j"(b + *)", "(b + 6)")
CodeSearch.Match((call-i b + 6), captures=[6])
julia> findall(j"* + *", "(a+b)+(d+e)")
3-element Vector{UnitRange{Int64}}:
1:11
2:4
8:10
julia> match(j"(* + *) \* *", "(a-b)*(d+e)") # no match -> returns nothing
julia> occursin(j"(* + *) \* *", "(a-b)*(d+e)")
false
julia> eachmatch(j"*(\"hello world\")", "print(\"hello world\"), display(\"hello world\")")
2-element Vector{CodeSearch.Match}:
Match((call print (string "hello world")), captures=[print])
Match((call display (string "hello world")), captures=[display])
julia> count(j"*(*)", "a(b(c))")
2
julia> match(j"(* + *) \* *", "(a+b)*(d+e)")
CodeSearch.Match((call-i (call-i a + b) * (call-i d + e)), captures=[a, b, (call-i d + e)])