set display mode 1024,768,16
 
rem text file to display
filename$ = "c:testParser.dba"
outputfile$ = left$(filename$,len(filename$)-4) + "_organized.dba"
rem keep track of number of tabs
Global _indents = 0
Global _line_count = 0
Global tabs = 0
 
#CONSTANT TRUE = 1
#CONSTANT FALSE = 0
 
type command
   keyword$ as string
   keyType as integer
endtype
 
 
read count
dim commands(count) as command
for t = 1 to count
   read commands(t).keyword$
   read commands(t).keyType
   print commands(t).keyType
next t
 
 
rem check to see if file specified is a text file
if lower$(right$(filename$, 4)) = ".dba"
  rem check if file exists
  if file exist(filename$)
      rem open file for reading
      open to read 1, filename$
      rem open file for writing parsed file
      if file exist(outputfile$) then delete file outputfile$
      open to write 2,outputfile$
      rem add text from file to array list
      while file end(1) = 0
         read string 1, line$
         parseLine(2, line$)
      endwhile
  endif
endif
 
rem close the file
close file 1
close file 2
 
 
 
set cursor 0,0
print "File broken down into ",_line_count," lines."
 
suspend for key
 
 
 
function parseLine(file as integer, s as string)
   s = trim$(s)
   length = len(s)
   parsed = 0
   outsideQuotes = 1
   for t = 1 to length
      if mid$(s,t) = chr$(34)
         flag = 0
         if outsideQuotes = 1 and flag = 0 then outsideQuotes = 0 : flag=1
         if outsideQuotes = 0 and flag = 0 then outsideQuotes = 1 : flag=1
      endif
      if mid$(s,t) = ":" and outsideQuotes = 1
         text$ = trim$(left$(s, t-1))
         writeStringWithTabs(file, text$)
         parseLine(file, right$(s, length-t))
         parsed = 1
         exit
      endif
   next t
   if parsed = 0
      write string file, space$(_indents*4) + s
      inc _line_count
   endif
 
endfunction
 
 
 
function writeStringWithTabs(file as integer, s as string)
   match = FALSE
   s = trim$(s)
 
   for i = 1 to array count(commands())
      strg$ = lower$(s)
      com$ = lower$(commands(i).keyword$)
      bool = startsWith(strg$, com$)
      if bool = TRUE
         tabs = 0
         if commands(i).keyType = 1
            tabs = _indents
            inc _indents, 1
         else
            if commands(i).keyType = 2
               dec _indents, 1
               if _indents < 0 then _indents = 0
               tabs = indents
            else
               if commands(i).keyType = 3
                  if _indents > 0
                     tabs = _indents - 1
                  else
                     tabs = 0
                  endif
               endif
            endif
         endif
 
         match = TRUE
         exit
      endif
   next i
 
   if match = TRUE
      write string file, space$(tabs*4) + s
      `print space$(tabs*4),tabs
   else
      write string file, space$(_indents*4) + s
      `print space$(_indents*4),_indents
   endif
   inc _line_count, 1
 
endfunction
 
 
 
function trim$(s as string)
   for t = 1 to len(s)
      if mid$(s, t) <> " "
         l = len(s) - (t-1)
         exitfunction right$(s, l)
      endif
   next t
endfunction ""
 
 
 
function startsWith(s as string, c as string)
   sLength = len(s)
   cLength = len(c)
 
   for t = 1 to cLength
      if t <= sLength
         if mid$(c,t) <> mid$(s,t) then exitfunction FALSE
      endif
   next t
endfunction TRUE
 
DATA 11,"FOR",1, "IF",1, "DO",1, "WHILE",1, "REPEAT",1, "NEXT",2, "ENDIF",2, "LOOP",2, "ENDWHILE",2, "UNTIL",2, "ELSE", 3