imlach

parser.py

Feb 12th, 2012
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.94 KB | None | 0 0
  1. #!/usr/bin/python
  2. # author: imlach
  3. # file: parser.py
  4. import drawFunctions, sys
  5. from string import *
  6.  
  7.  
  8. fns = drawFunctions.draw()
  9.  
  10. ##
  11. # Prompt the user for a filename, if not supplied as the first
  12. # command line arg.
  13. def filePrompt(args=[]):
  14.     if len(args) > 1:
  15.         fName = args[1]
  16.     else:
  17.         fName = raw_input("Please enter your filename: ")
  18.        
  19.        
  20.     try:
  21.         lstFile = open(fName,"r")
  22.         return lstFile,fName
  23.     except IOError:
  24.         print "Error opening file "+fName+"..."
  25.         return filePrompt(),fName
  26.        
  27.  
  28. ##
  29. # Defines what commands are recognised, and how to handle them.
  30. # returns a pair of vals, used for different things
  31. def execFn(fnStr, args, defs):
  32.    
  33.     ## Little 'hack' to check if parameters are numerical.
  34.     for arg in args[1:]:
  35.         try:
  36.             float(arg)
  37.         except ValueError:
  38.             return "valErr",arg
  39.    
  40.     ## Begin working things out
  41.     if fnStr == "position":
  42.         fns.pos(args[1],args[2])
  43.         return fnStr,True
  44.     elif fnStr == "move":
  45.         fns.mov(args[1],args[2])
  46.         return fnStr,True
  47.     elif fnStr == "line":
  48.         fns.line(args[1],args[2])
  49.         return fnStr,True
  50.     elif fnStr == "circle":
  51.         fns.circ(args[1])
  52.         return fnStr,True
  53.     elif fnStr == "define":
  54.         defs[args[1]] = []
  55.         curDef = args[1]
  56.         return fnStr,args[1]
  57.     elif fnStr == "end":
  58.         return fnStr,""
  59.     elif fnStr in defs.keys():
  60.         ## Execute function call
  61.         for line in defs[fnStr]:
  62.             execFn(line[0],line,defs)
  63.         return fnStr,True
  64.     elif fnStr == "" or fnStr[0] == "#":
  65.         #Ignore comments or blank lines
  66.         pass
  67.         return "",""
  68.     else:
  69.         return "error",False
  70.  
  71.  
  72. ##
  73. # Runs through the file returned by filePrompt, and goes through
  74. # each line.
  75. def parse(listing,fName):
  76.    
  77.     ## Set up vars and objects.
  78.    
  79.     defs = {}
  80.     lines = []
  81.     curDef = ""
  82.     i = 0
  83.     loops = 0
  84.     backTo = 0
  85.    
  86.     for line in listing:
  87.         line = split(line[:-1]," ")
  88.         lines += [line]
  89.    
  90.     while i < len(lines):
  91.         line = lines[i]
  92.        
  93.        
  94.         if line[0] == "loop":
  95.             ## If in loop mode, store the number of loops and which line
  96.             #  to return to.
  97.             loops = int(line[1])-1
  98.             backTo = i
  99.         elif curDef != "" and line[0] != "end":
  100.             ## Add line to def if in define mode
  101.             defs[curDef] += [line]
  102.         else:
  103.             ## Execute current line
  104.             rtVal = execFn(line[0],line,defs)  
  105.             if rtVal[0] == "define":
  106.                 # Enter define mode
  107.                 curDef = rtVal[1]
  108.             elif rtVal[0] == "end":
  109.                 if loops == 0:
  110.                     # If not in loop mode, exit define mode
  111.                     curDef = rtVal[1]
  112.                 else:
  113.                     # Decrement loop counter, and return to the line i,
  114.                     # so it gets incremented so to be on the correct
  115.                     # line in the next loop.
  116.                     loops -= 1
  117.                     i = backTo
  118.  
  119.             ## Handle errors nicely
  120.             elif rtVal[0] == "error":
  121.                 print "Syntax error on line "+str(i+1)+" of "+fName+": '"+line[0]+"'"
  122.                 return False
  123.             elif rtVal[0] == "valErr":
  124.                 print "Value error on line "+str(i+1)+" of "+fName+": '"+rtVal[1]+"'"
  125.                 return False
  126.        
  127.            
  128.         i += 1
  129.            
  130.     fns.complete()
  131.     return True
  132.    
  133.    
  134. lst = filePrompt(sys.argv)
  135. parse(lst[0],lst[1])
Advertisement
Add Comment
Please, Sign In to add comment