Created
November 25, 2016 13:19
-
-
Save pfitzseb/f30d024ec95a8991a5b88d93f91917bf to your computer and use it in GitHub Desktop.
codemodule
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Tokenize | |
SCOPE_STARTERS = [Tokenize.Tokens.BEGIN, | |
Tokenize.Tokens.WHILE, | |
Tokenize.Tokens.IF, | |
Tokenize.Tokens.FOR, | |
Tokenize.Tokens.TRY, | |
Tokenize.Tokens.FUNCTION, | |
Tokenize.Tokens.MACRO, | |
Tokenize.Tokens.LET, | |
Tokenize.Tokens.ABSTRACT, | |
Tokenize.Tokens.TYPE, | |
Tokenize.Tokens.BITSTYPE, | |
Tokenize.Tokens.IMMUTABLE, | |
Tokenize.Tokens.DO, | |
Tokenize.Tokens.QUOTE, | |
Tokenize.Tokens.MODULE, | |
Tokenize.Tokens.BAREMODULE | |
] | |
function codemodule(code, line) | |
stack = String[] | |
# count all unterminated block openers | |
n_openers = 0 | |
ts = tokenize(code) | |
for t in ts | |
Tokenize.Tokens.startpos(t)[1] >= line && return join(stack, ".") | |
if Tokenize.Tokens.exactkind(t) in SCOPE_STARTERS | |
if Tokenize.Tokens.exactkind(t) == Tokenize.Tokens.MODULE || | |
Tokenize.Tokens.exactkind(t) == Tokenize.Tokens.BAREMODULE | |
pos = Tokenize.Lexers.position(ts) | |
# not sure what happens when changing the iterator state while iterating, | |
# but in theory nothing bad should happen, right? | |
t, e = Tokenize.Tokens.next(ts, false) | |
t, _ = Tokenize.Tokens.next(ts, e) | |
m = Tokenize.Tokens.untokenize(t) | |
push!(stack, m) | |
else | |
n_openers += 1 | |
end | |
elseif Tokenize.Tokens.exactkind(t) == Tokenize.Tokens.END | |
if n_openers == 0 | |
!isempty(stack) && pop!(stack) | |
else | |
n_openers -= 1 | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment