-
Notifications
You must be signed in to change notification settings - Fork 0
/
process-text.jl
80 lines (71 loc) · 2.86 KB
/
process-text.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#######################################################################
# Analysing Julia file for lower bounds
###############################
#
# The goal is to find all occurrences of lower bounds
# on type variables in the text of a Julia program.
#
# This can happen in 2 cases:
# 1) where T >: Int
# 2) where Int <: T <: Number
#
# So first, we need to look for both `>:` and `<:` textually
#
# If at least one of those is present, parsing Julia code
# can give more precise results.
#######################################################################
# For Julia < 1.3 compatibility, we need `count` on strings
@static if VERSION < v"1.3"
Base.count(pattern :: String, string :: String) :: Int =
sum(map(_ -> 1, eachmatch(Regex(pattern), string)))
end
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Simple search of text for `<:` and `>:`
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Returns the number of occurences of constraint `constr` in `text`
countTextualConstr(constr :: ConstrKind, text :: String) :: Int =
count(CONSTRAINT_PATTERN[constr], text)
# Returns textual constraints info for `text`
countTextualConstr(text :: String) :: TxtConstrStat =
TxtConstrStat(
countTextualConstr(subtc, text),
countTextualConstr(suptc, text))
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Analysing the text of a Julia file
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Returns `true` if there is at least one sub/sup constraint in `txtConstr`
nonVacuous(txtConstr :: TxtConstrStat) :: Bool =
txtConstr.subConsr + txtConstr.supConsr > 0
# Returns `true` if there is at least one lower bound in `stat`
nonVacuous(stat :: LBValsFreq) :: Bool = length(stat) > 0
# LBStat|Nothing → Bool
# Returns `true` if there is at least one lower bound in `stat`
nonVacuous(stat :: LBStat) :: Bool = stat.lbs > 0
nonVacuous(stat :: Nothing) :: Bool = false
# Computes lower-bound statistics for Julia file `file`
# - if `isPath`, `file` means file path; otherwise, file text
# NOTE: parsing might file, in which case it records the error
lbFileInfo(file :: String; isPath :: Bool = false) :: FileLBInfo = begin
text = file
if isPath
text = read(file, String)
end
txtConstr = countTextualConstr(text)
if nonVacuous(txtConstr)
try
FileLBInfo(txtConstr, lbStatInfo(text))
catch e
fname = isPath ? file : "<no file>"
info = "lbFileInfo ERR"
isa(e, Base.Meta.ParseError) ?
@debugonly(@warn info fname e) :
@error info fname e
FileLBInfo(txtConstr, e)
end
else
FileLBInfo(txtConstr)
end
end
# Computes lower-bounds statistics for the code represented by `text`
lbStatInfo(text :: String) :: LBStat =
LBStat(extractLowerBounds(parseJuliaCode(text)))