Split a character into tokens
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | str | Character to split |
Resulting list of tokens
Nodes of different colours represent the following:
Solid arrows point from a procedure to one which it calls. Dashed arrows point from an interface to procedures which implement that interface. This could include the module procedures in a generic interface or the implementation in a submodule of an interface in a parent module.
function tokenize(str) result(o)
!! Split a character into tokens
character(*),intent(in)::str
!! Character to split
type(token_t),dimension(:),allocatable::o
!! Resulting list of tokens
character(64)::t
integer::s,n,k
allocate(o(0))
s = 1
do while(s<len(str))
n = scan(str(s:),ops)
if(n==0) then
exit
else if(n/=1) then
t = str(s:s+n-2)
s = s+n-1
else
t = str(s:s)
s = s+n
end if
o = [o,token_t(trim(t))]
end do
t = str(s:)
o = [o,token_t(trim(t))]
! Correct for unary (-)
if(o(1)%t==T_SUB) o(1)%t = T_NEG
do k=2,size(o)
if(o(k)%t/=T_SUB) cycle
if(o(k-1)%t>T_OPERATOR .and. o(k-1)%t<T_OPERATOR+R_SPAN) o(k)%t = T_NEG
if(o(k-1)%t==T_LPR) o(k)%t = T_NEG
end do
do k=1,size(o)
if(o(k)%t==T_NEG) o(k)%s = '_'
end do
! Support functions
do k=1,size(o)-1
if(o(k)%t/=T_VAR) cycle
if(o(k+1)%t==T_LPR) o(k)%t = T_FUNCTION
end do
end function tokenize