parsing.lisp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. (in-package #:pta-ledger)
  2. (defvar *default-year* nil "Initialized to current year on parsing start. Could be set with directive")
  3. (defvar *default-commodity* "RUB" "Could be set with directive")
  4. (defparameter +day+ (* 60 60 24) "day in seconds")
  5. (defun get-date (universal-time)
  6. (nreverse (subseq (multiple-value-list (decode-universal-time universal-time)) 3 6)))
  7. (defun get-ut (year month day)
  8. (encode-universal-time 0 0 0 day month year))
  9. (defun strip-space (text)
  10. (subseq text (or (position #\Space text :test-not 'eql) 0)
  11. (1+ (or (position #\Space text :test-not 'eql :from-end t) -1))))
  12. (defun .date-delimiter ()
  13. (.is #'member '(#\/ #\- #\.)))
  14. (defun .chars (&rest chars)
  15. (.is #'member chars))
  16. ;; optimized (.first (.map 'list parser at-least))
  17. (defun .while (parser &key result-type (at-least 1))
  18. (lambda (input)
  19. (loop
  20. for inp = input then (input-rest inp)
  21. for count from 0
  22. until (input-empty-p inp)
  23. while (funcall parser inp)
  24. finally (return (when (>= count at-least)
  25. (list (cons (when result-type
  26. (coerce (subseq input 0 count) result-type))
  27. inp)))))))
  28. (defun .until (parser &key result-type (at-least 1))
  29. (.while (.not parser) :result-type result-type :at-least at-least))
  30. (defun .mapcar* (parser &optional skip)
  31. (lambda (input)
  32. (loop
  33. with results
  34. until (input-empty-p input)
  35. for result = (run parser input)
  36. while result
  37. do
  38. (unless skip (push (caar result) results))
  39. (setf input (cdar result))
  40. finally (return (list (cons (unless skip (nreverse results)) input))))))
  41. (defun .list (&rest parsers)
  42. (if (not parsers)
  43. (.fail)
  44. (.let* ((first (first parsers))
  45. (rest (if (rest parsers)
  46. (apply #'.list (rest parsers))
  47. (.identity nil))))
  48. (.identity (cons first rest)))))
  49. (defun .number (&optional (at-least 1))
  50. (.let* ((digits (.while (.is #'digit-char-p) :result-type 'string :at-least at-least)))
  51. (.identity (parse-integer digits))))
  52. (defun .spaces (&key (at-least 1) result-type)
  53. (.while (.char= #\Space) :result-type result-type :at-least at-least))
  54. (defun .whitespace (&key (at-least 1) result-type)
  55. (.while (.chars #\Space #\Tab) :result-type result-type :at-least at-least))
  56. (defun .eol ()
  57. (.or (.char= #\Newline)
  58. (.and (.not (.item))
  59. (.identity '()))))
  60. (defun .line-comment ()
  61. (.let* ((_ (.whitespace :at-least 0))
  62. (_ (.chars #\; #\# #\*))
  63. (text (.until (.char= #\Newline) :at-least 0 :result-type 'string))
  64. (_ (.eol)))
  65. (.identity (strip-space text))))
  66. (defun .multi-line-comment ()
  67. (.let* ((_ (.string= "comment"))
  68. (text (.until (.progn (.eol) (.string= "end comment")) :result-type 'string))
  69. (_ (.optional (.progn (.eol) (.string= "end comment"))))
  70. (_ (.eol)))
  71. (.identity (strip-space text))))
  72. (defun .comment ()
  73. (.let* ((_ (.char= #\;))
  74. (text (.until (.char= #\Newline) :at-least 0 :result-type 'string)))
  75. (.identity (strip-space text))))
  76. (defun .empty-lines ()
  77. (.mapcar*
  78. (.or (.progn (.whitespace :at-least 0) (.eol))
  79. (.line-comment)
  80. (.multi-line-comment))
  81. t))
  82. (defun split (text &optional (delimiter (.char= #\Newline)))
  83. (when text
  84. (parse (.mapcar* (.prog1 (.until delimiter :result-type 'string :at-least 0)
  85. (.optional delimiter))) text)))
  86. (defun .simple-date (&optional default-year)
  87. (.let* ((year (.optional (.prog1 (.number 4) (.date-delimiter))))
  88. (month (.prog1 (.number) (.date-delimiter)))
  89. (day (.number)))
  90. (handler-case
  91. (.identity (get-ut (or year default-year *default-year*) month day))
  92. (error () (.fail)))))
  93. (defun parse-date (text &optional default-year)
  94. (parse (.simple-date default-year) text))
  95. (defun .status ()
  96. (.is 'member '(#\! #\*)))
  97. (defun .code ()
  98. (.prog2 (.char= #\()
  99. (.until (.char= #\)) :result-type 'string :at-least 0)
  100. (.char= #\))))
  101. (defun .description ()
  102. (.let* ((text (.until (.chars #\Newline #\;) :result-type 'string)))
  103. (.identity (strip-space text))))
  104. (defun .account ()
  105. (.let* ((account (.until (.or (.string= " ")
  106. (.char= #\Newline)) :result-type 'string)))
  107. (.identity (strip-space account))))
  108. (defun .money-number (thousands-sep decimal-sep)
  109. (.let* ((first-part (.while (.is 'digit-char-p) :result-type 'string))
  110. (rest-parts (.mapcar* (.progn (.char= thousands-sep)
  111. (.while (.is 'digit-char-p) :result-type 'string))))
  112. (decimals (.optional (.progn (.char= decimal-sep)
  113. (.while (.is 'digit-char-p) :result-type 'list :at-least 0)))))
  114. ;; One thousands separator and no decimals - is other way around, fail for other to succeceed
  115. (if (and (= 1 (length rest-parts))
  116. (null decimals))
  117. (.fail)
  118. (.identity
  119. (+
  120. (parse-integer (apply 'concatenate 'string (cons first-part rest-parts)))
  121. (if decimals
  122. (* (parse-integer (coerce decimals 'string))
  123. (expt 10 (- (length decimals))))
  124. 0))))))
  125. (defun .money ()
  126. (.plus (.money-number #\, #\.)
  127. (.money-number #\. #\,)))
  128. (defun .commodity ()
  129. (.or (.until (.or (.chars #\. #\, #\Space #\" #\; #\Newline #\- #\+ #\@)
  130. (.is 'digit-char-p))
  131. :result-type 'string)
  132. (.prog2 (.char= #\")
  133. (.until (.char= #\") :result-type 'string)
  134. (.char= #\"))))
  135. (defun .amount ()
  136. (.or
  137. (.let* ((sign1 (.optional (.chars #\- #\+)))
  138. (commodity (.prog1 (.commodity) (.optional (.whitespace))))
  139. (sign2 (.optional (.chars #\- #\+)))
  140. (quantity (.money)))
  141. (if (and sign1 sign2)
  142. (.fail)
  143. (.identity
  144. (make-amount :quantity (if (or (equal sign1 #\-)
  145. (equal sign2 #\-))
  146. (- quantity)
  147. quantity)
  148. :commodity commodity))))
  149. (.let* ((sign (.optional (.chars #\- #\+)))
  150. (quantity (.money))
  151. (commodity (.optional (.progn (.optional (.whitespace))
  152. (.commodity)))))
  153. (.identity
  154. (make-amount :quantity (if (equal sign #\-)
  155. (- quantity)
  156. quantity)
  157. :commodity commodity)))))
  158. (defun .posting ()
  159. (.let* ((_ (.whitespace))
  160. (status (.optional (.prog1 (.status) (.optional (.spaces)))))
  161. (account (.account))
  162. (amount (.optional (.prog2 (.whitespace :at-least 2) (.amount) (.optional (.whitespace)))))
  163. (unit-price (.optional (.progn (.char= #\@)
  164. (.not (.char= #\@))
  165. (.optional (.whitespace))
  166. (.amount))))
  167. (_ (.optional (.whitespace)))
  168. (total-price (.optional (.progn (.string= "@@")
  169. (.optional (.whitespace))
  170. (.amount))))
  171. (_ (.optional (.whitespace)))
  172. (comment (.optional (.comment)))
  173. (_ (.eol))
  174. (comments (.mapcar* (.prog2 (.whitespace) (.comment) (.eol)))))
  175. (if (and unit-price total-price) (.fail)
  176. (.identity (make-posting
  177. :status status
  178. :account account
  179. :amount amount
  180. :unit-price unit-price
  181. :total-price total-price
  182. :comment (when (or comment comments)
  183. (format nil "~{~A~^~%~}" (remove nil (list* comment comments)))))))))
  184. (defun .entry ()
  185. (.let* ((date (.simple-date))
  186. (secondary-date (.optional (.progn (.char= #\=)
  187. (.simple-date (car (get-date date))))))
  188. (status (.optional (.progn (.spaces) (.status))))
  189. (code (.optional (.progn (.spaces) (.code))))
  190. (_ (.optional (.whitespace)))
  191. (description (.optional (.description)))
  192. (_ (.optional (.whitespace)))
  193. (comment (.optional (.comment)))
  194. (_ (.eol))
  195. (comments (.mapcar* (.prog2 (.whitespace) (.comment) (.eol))))
  196. (postings (.mapcar* (.posting))))
  197. (.identity (make-entry :date date
  198. :secondary-date secondary-date
  199. :status status
  200. :code code
  201. :description description
  202. :comment (when (or comment comments)
  203. (format nil "~{~A~^~%~}" (remove nil (list* comment comments))))
  204. :postings postings))))
  205. (defun .wrap (prefix parser)
  206. (.let* ((value parser))
  207. (.identity (cons prefix value))))
  208. (defun .journal ()
  209. (.prog1
  210. (.mapcar*
  211. (.progn
  212. (.empty-lines)
  213. (.or
  214. (.wrap :entry (.entry))
  215. (.prog1
  216. (.or
  217. (.wrap :market-price
  218. (.progn (.char= #\P)
  219. (.optional (.whitespace))
  220. (.let* ((date (.simple-date))
  221. (_ (.whitespace))
  222. (commodity (.commodity))
  223. (_ (.whitespace))
  224. (unit-price (.amount)))
  225. (.identity (list date commodity unit-price)))))
  226. (.wrap :commodity
  227. (.progn (.string= "commodity")
  228. (.whitespace)
  229. (.amount)))
  230. (.wrap :default-commodity
  231. (.progn (.char= #\D)
  232. (.optional (.whitespace))
  233. (.amount)))
  234. (.wrap :default-year
  235. (.progn (.char= #\Y)
  236. (.optional (.whitespace))
  237. (.number))))
  238. (.optional (.whitespace))
  239. (.eol)))))
  240. (.empty-lines)))
  241. (defun .query-coloned (type key-parser value-parser)
  242. (.let* ((key key-parser)
  243. (_ (.char= #\:))
  244. (value value-parser))
  245. (.identity (cons type
  246. #'(lambda (entry posting)
  247. (let ((key-value (funcall key entry posting)))
  248. (or (eql key-value :t)
  249. (funcall value key-value))))))))
  250. (defun .query-entryp (prefix key)
  251. (.progn (.string= prefix) (.identity #'(lambda (e p) (declare (ignore p)) (funcall key e)))))
  252. (defun .query-postingp (prefix key)
  253. (.progn (.string= prefix) (.identity #'(lambda (e p) (declare (ignore e)) (funcall key p)))))
  254. (defun .query-value-regex ()
  255. (.let* ((regex (.until (.eol) :result-type 'string)))
  256. (handler-case
  257. (let ((matcher (cl-ppcre:create-scanner regex :case-insensitive-mode t)))
  258. (.identity #'(lambda (value)
  259. (cl-ppcre:scan matcher value))))
  260. (error ()
  261. (.fail)))))
  262. (defun .query-value-amount ()
  263. (.let* ((op (.or (.string= ">=")
  264. (.string= "<=")
  265. (.char= #\=)
  266. (.char= #\<)
  267. (.char= #\>)
  268. (.identity #\=)))
  269. (value (.number)))
  270. (.identity #'(lambda (v)
  271. (funcall (find-symbol (string op)) v value)))))
  272. (defun .month ()
  273. (.or (.progn (.or (.string= "january") (.string= "jan")) (.identity 1))
  274. (.progn (.or (.string= "february") (.string= "feb")) (.identity 2))
  275. (.progn (.or (.string= "march") (.string= "mar")) (.identity 3))
  276. (.progn (.or (.string= "april") (.string= "apr")) (.identity 4))
  277. (.progn (.string= "may") (.identity 5))
  278. (.progn (.or (.string= "june") (.string= "jun")) (.identity 6))
  279. (.progn (.or (.string= "july") (.string= "jul")) (.identity 7))
  280. (.progn (.or (.string= "august") (.string= "aug")) (.identity 8))
  281. (.progn (.or (.string= "september") (.string= "sep")) (.identity 9))
  282. (.progn (.or (.string= "october") (.string= "oct")) (.identity 10))
  283. (.progn (.or (.string= "november") (.string= "nov")) (.identity 11))
  284. (.progn (.or (.string= "december") (.string= "dec")) (.identity 12))))
  285. (defun week-start-date (universal-time &optional offset)
  286. (get-date (- universal-time (* +day+ (+ (nth 6 (multiple-value-list (decode-universal-time universal-time)))
  287. (- (* 7 (or offset 0))))))))
  288. (defun month-start-date (year month &optional offset)
  289. (multiple-value-bind (year-offset month) (floor (+ month (or offset 0) -1) 12)
  290. (list (+ year year-offset) (1+ month) 1)))
  291. (defun .smart-range (parser &optional duration)
  292. (.let* ((date parser))
  293. (destructuring-bind (&optional year month day) date
  294. (handler-case
  295. (let* ((ut (get-ut (or year (car (get-date (get-universal-time))))
  296. (or month 1) (or day 1)))
  297. (date (get-date ut)))
  298. (.identity (cons ut
  299. (if duration (+ ut duration)
  300. (if day (+ ut +day+)
  301. (if month (apply #'get-ut (month-start-date (car date) (cadr date) 1))
  302. (get-ut (1+ year) 1 1)))))))
  303. (error () (.fail))))))
  304. (defun .smart-date ()
  305. (let* ((now (get-universal-time))
  306. (date-now (get-date now)))
  307. (.or (.smart-range (.list (.prog1 (.number 4) (.date-delimiter))
  308. (.prog1 (.number) (.date-delimiter))
  309. (.number)))
  310. (.smart-range (.list (.prog1 (.number 4) (.date-delimiter))
  311. (.number)
  312. (.identity nil)))
  313. (.smart-range (.list (.number 4) (.identity nil) (.identity nil)))
  314. (.smart-range (.list (.identity nil)
  315. (.prog1 (.number) (.date-delimiter))
  316. (.number)))
  317. (.smart-range (.list (.identity nil) (.month) (.identity nil)))
  318. (.smart-range (.progn (.string= "today") (.identity date-now)))
  319. (.smart-range (.progn (.string= "yesterday") (.identity (get-date (- now +day+)))))
  320. (.smart-range (.progn (.string= "tomorrow") (.identity (get-date (+ now +day+)))))
  321. (.let* ((offset (.or (.progn (.string= "this") (.identity 0))
  322. (.progn (.string= "last") (.identity -1))
  323. (.progn (.string= "next") (.identity 1))
  324. (.identity 0)))
  325. (_ (.optional (.whitespace))))
  326. (.or (.smart-range (.progn (.string= "year")
  327. (.identity (list (+ (car date-now) offset) nil nil))))
  328. (.smart-range (.progn (.string= "month")
  329. (.identity (subseq (month-start-date (car date-now) (cadr date-now) offset)
  330. 0 2))))
  331. (.smart-range (.progn (.string= "week")
  332. (.identity (week-start-date now offset)))
  333. (* +day+ 7)))))))
  334. (defun .query-value-period ()
  335. (.let* ((period (.or (.let* ((start (.optional (.smart-date)))
  336. (_ (.optional (.whitespace)))
  337. (_ (.or (.char= #\-) (.string= "to")))
  338. (_ (.optional (.whitespace)))
  339. (end (.optional (.smart-date))))
  340. (if (or start end)
  341. (.identity (cons (car start) (car end)))
  342. (.fail)))
  343. (.smart-date))))
  344. (destructuring-bind (start . end) period
  345. (.identity #'(lambda (value)
  346. (and
  347. (or (not start) (>= value start))
  348. (or (not end) (< value end))))))))
  349. (defun .query-term ()
  350. (.or (.query-coloned :acct (.query-postingp "acct" #'posting-account)
  351. (.query-value-regex))
  352. (.query-coloned :amt (.progn (.string= "amt")
  353. (.identity
  354. #'(lambda (e p)
  355. (let ((amounts (get-amounts p (entry-postings e))))
  356. (if (> (length amounts) 1) :t
  357. (amount-quantity (car amounts)))))))
  358. (.query-value-amount))
  359. (.query-coloned :code (.query-entryp "code" #'entry-code)
  360. (.query-value-regex))
  361. (.query-coloned :cur (.progn (.string= "cur")
  362. (.identity
  363. #'(lambda (e p)
  364. (get-amounts p (entry-postings e)))))
  365. (.let* ((regexp (.query-value-regex)))
  366. (.identity #'(lambda (amounts)
  367. (find-if regexp amounts :key #'amount-commodity)))))
  368. (.query-coloned :desc (.query-entryp "desc" #'entry-description)
  369. (.query-value-regex))
  370. (.query-coloned :date (.query-entryp "date" #'entry-date)
  371. (.query-value-period))
  372. (.let* ((value (.query-value-regex)))
  373. (.identity (cons :acct
  374. #'(lambda (entry posting)
  375. (declare (ignore entry))
  376. (let ((account (posting-account posting)))
  377. (funcall value account))))))))
  378. (defun .arg ()
  379. (.let* ((word (.until (.chars #\' #\" #\Space #\Tab) :result-type 'string :at-least 0))
  380. (quoted (.optional (.let* ((quote (.chars #\' #\"))
  381. (text (.until (.char= quote) :result-type 'string :at-least 0))
  382. (_ (.char= quote)))
  383. (.identity text)))))
  384. (.identity (if quoted (concatenate 'string word quoted)
  385. word))))
  386. (defun make-query-predicate (terms)
  387. (let (desc acct status other)
  388. (loop for (type . f) in terms
  389. do (case type
  390. (:desc (push f desc))
  391. (:acct (push f acct))
  392. (:status (push f status))
  393. (otherwise (push f other))))
  394. #'(lambda (entry posting)
  395. (labels ((any (predicates)
  396. (or (not predicates)
  397. (find-if #'(lambda (p) (funcall p entry posting)) predicates)))
  398. (all (predicates)
  399. (not (find-if-not #'(lambda (p) (funcall p entry posting)) predicates))))
  400. (and (any desc) (any acct) (any status) (all other))))))
  401. (defun .query ()
  402. (.let* ((args (.mapcar* (.prog1 (.arg) (.optional (.whitespace))))))
  403. (loop for arg in args with terms
  404. do (multiple-value-bind (pred left)
  405. (parse (.query-term) arg)
  406. (if (and pred (input-empty-p left))
  407. (push pred terms)
  408. (return (.fail))))
  409. finally (return (.identity (make-query-predicate terms))))))