Advertisement
Guest User

Untitled

a guest
Apr 13th, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns cmm.errors
  2.   "This namespace encapsulates access to the global error flag in the parser system. If an error
  3.   occurs during parsing, then the raise-error! function should be called, along with appropriate
  4.   printf arguments.")
  5.  
  6. (def error-occured?
  7.   "This represents the singular piece of global state within the parser. For the sake of
  8.   simplifying return schematics for the parser functions, this piece of state was extracted and
  9.   moved here. This boolean represents whether or not an unrecoverable error was encountered during
  10.   the parsing process.
  11.  
  12.   It can be modified solely through raise-error!, which sets it to true, and
  13.   reset-error! which sets it to false. The reset-error! MUST be called inbetween independent file
  14.   parses to maintain the contract assumed in this sytem."
  15.   (atom false))
  16.  
  17. (defn error?
  18.   "Wrapper function around access to the global error flag. This will return true if raise-error!
  19.   has been called since the last call to reset-error!, and will return false otherwise."
  20.   []
  21.   (deref error-occured?))
  22.  
  23. (defn raise-error!
  24.   "Sets the global error flag to true and then sends the supplied arguments to printf,
  25.   destined for stderr."
  26.   [& args]
  27.   (reset! error-occured? true)
  28.   (binding [*out* *err*] (print "ERROR:" (apply format args))
  29.     (flush)))
  30.  
  31. (defn reset-error!
  32.   "Resets the global error flag to false. This function should be called in-between independent
  33.   parses. This will return true if resetting the flag was successful, false otherwise."
  34.   []
  35.   (not (reset! error-occured? false)))
  36.  
  37. (comment
  38.   (defn parse-filename [filename]
  39.     (let [parse-result (parse (slurp filename))
  40.           failure (error?)]
  41.       {:result parse-result
  42.        :failure failure})))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement