parsing.lisp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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 (.prog1 (.simple-date default-year) (.eol)) 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 parse-account (text)
  109. (parse (.prog1 (.account) (.eol)) text))
  110. (defun .money-number (thousands-sep decimal-sep)
  111. (.let* ((first-part (.while (.is 'digit-char-p) :result-type 'string))
  112. (rest-parts (.mapcar* (.progn (.char= thousands-sep)
  113. (.while (.is 'digit-char-p) :result-type 'string))))
  114. (decimals (.optional (.progn (.char= decimal-sep)
  115. (.while (.is 'digit-char-p) :result-type 'list :at-least 0)))))
  116. ;; One thousands separator and no decimals - is other way around, fail for other to succeceed
  117. (if (and (= 1 (length rest-parts))
  118. (null decimals))
  119. (.fail)
  120. (.identity
  121. (+
  122. (parse-integer (apply 'concatenate 'string (cons first-part rest-parts)))
  123. (if decimals
  124. (* (parse-integer (coerce decimals 'string))
  125. (expt 10 (- (length decimals))))
  126. 0))))))
  127. (defun .money ()
  128. (.plus (.money-number #\, #\.)
  129. (.money-number #\. #\,)))
  130. (defun .commodity ()
  131. (.or (.until (.or (.chars #\. #\, #\Space #\" #\; #\Newline #\- #\+ #\@)
  132. (.is 'digit-char-p))
  133. :result-type 'string)
  134. (.prog2 (.char= #\")
  135. (.until (.char= #\") :result-type 'string)
  136. (.char= #\"))))
  137. (defun .amount ()
  138. (.or
  139. (.let* ((sign1 (.optional (.chars #\- #\+)))
  140. (commodity (.prog1 (.commodity) (.optional (.whitespace))))
  141. (sign2 (.optional (.chars #\- #\+)))
  142. (quantity (.money)))
  143. (if (and sign1 sign2)
  144. (.fail)
  145. (.identity
  146. (make-amount :quantity (if (or (equal sign1 #\-)
  147. (equal sign2 #\-))
  148. (- quantity)
  149. quantity)
  150. :commodity commodity))))
  151. (.let* ((sign (.optional (.chars #\- #\+)))
  152. (quantity (.money))
  153. (commodity (.optional (.progn (.optional (.whitespace))
  154. (.commodity)))))
  155. (.identity
  156. (make-amount :quantity (if (equal sign #\-)
  157. (- quantity)
  158. quantity)
  159. :commodity commodity)))))
  160. (defun parse-amount (text)
  161. (parse (.prog1 (.amount) (.eol)) text))
  162. (defun .posting ()
  163. (.let* ((_ (.whitespace))
  164. (status (.optional (.prog1 (.status) (.optional (.spaces)))))
  165. (account (.account))
  166. (amount (.optional (.prog2 (.whitespace :at-least 2) (.amount) (.optional (.whitespace)))))
  167. (unit-price (.optional (.progn (.char= #\@)
  168. (.not (.char= #\@))
  169. (.optional (.whitespace))
  170. (.amount))))
  171. (_ (.optional (.whitespace)))
  172. (total-price (.optional (.progn (.string= "@@")
  173. (.optional (.whitespace))
  174. (.amount))))
  175. (_ (.optional (.whitespace)))
  176. (comment (.optional (.comment)))
  177. (_ (.eol))
  178. (comments (.mapcar* (.prog2 (.whitespace) (.comment) (.eol)))))
  179. (if (and unit-price total-price) (.fail)
  180. (.identity (make-posting
  181. :status status
  182. :account account
  183. :amount amount
  184. :unit-price unit-price
  185. :total-price total-price
  186. :comment (when (or comment comments)
  187. (format nil "~{~A~^~%~}" (remove nil (list* comment comments)))))))))
  188. (defun .entry ()
  189. (.let* ((date (.simple-date))
  190. (secondary-date (.optional (.progn (.char= #\=)
  191. (.simple-date (car (get-date date))))))
  192. (status (.optional (.progn (.spaces) (.status))))
  193. (code (.optional (.progn (.spaces) (.code))))
  194. (_ (.optional (.whitespace)))
  195. (description (.optional (.description)))
  196. (_ (.optional (.whitespace)))
  197. (comment (.optional (.comment)))
  198. (_ (.eol))
  199. (comments (.mapcar* (.prog2 (.whitespace) (.comment) (.eol))))
  200. (postings (.mapcar* (.posting))))
  201. (.identity (make-entry :date date
  202. :secondary-date secondary-date
  203. :status status
  204. :code code
  205. :description description
  206. :comment (when (or comment comments)
  207. (format nil "~{~A~^~%~}" (remove nil (list* comment comments))))
  208. :postings postings))))
  209. (defun .wrap (prefix parser)
  210. (.let* ((value parser))
  211. (.identity (cons prefix value))))
  212. (defun .journal ()
  213. (.prog1
  214. (.mapcar*
  215. (.progn
  216. (.empty-lines)
  217. (.or
  218. (.wrap :entry (.entry))
  219. (.prog1
  220. (.or
  221. (.wrap :market-price
  222. (.progn (.char= #\P)
  223. (.optional (.whitespace))
  224. (.let* ((date (.simple-date))
  225. (_ (.whitespace))
  226. (commodity (.commodity))
  227. (_ (.whitespace))
  228. (unit-price (.amount)))
  229. (.identity (list date commodity unit-price)))))
  230. (.wrap :commodity
  231. (.progn (.string= "commodity")
  232. (.whitespace)
  233. (.amount)))
  234. (.wrap :default-commodity
  235. (.progn (.char= #\D)
  236. (.optional (.whitespace))
  237. (.amount)))
  238. (.wrap :default-year
  239. (.progn (.char= #\Y)
  240. (.optional (.whitespace))
  241. (.number))))
  242. (.optional (.whitespace))
  243. (.eol)))))
  244. (.empty-lines)))
  245. (defun .query-coloned (type key-parser value-parser)
  246. (.let* ((key key-parser)
  247. (_ (.char= #\:))
  248. (value value-parser))
  249. (.identity (cons type
  250. #'(lambda (entry posting)
  251. (let ((key-value (funcall key entry posting)))
  252. (or (eql key-value :t)
  253. (funcall value key-value))))))))
  254. (defun .query-entryp (prefix key)
  255. (.progn (.string= prefix) (.identity #'(lambda (e p) (declare (ignore p)) (funcall key e)))))
  256. (defun .query-postingp (prefix key)
  257. (.progn (.string= prefix) (.identity #'(lambda (e p) (declare (ignore e)) (funcall key p)))))
  258. (defun .query-value-regex ()
  259. (.let* ((regex (.until (.eol) :result-type 'string)))
  260. (handler-case
  261. (let ((matcher (cl-ppcre:create-scanner regex :case-insensitive-mode t)))
  262. (.identity #'(lambda (value)
  263. (cl-ppcre:scan matcher value))))
  264. (error ()
  265. (.fail)))))
  266. (defun .query-value-amount ()
  267. (.let* ((op (.or (.string= ">=")
  268. (.string= "<=")
  269. (.char= #\=)
  270. (.char= #\<)
  271. (.char= #\>)
  272. (.identity #\=)))
  273. (value (.number)))
  274. (.identity #'(lambda (v)
  275. (funcall (find-symbol (string op)) v value)))))
  276. (defun .month ()
  277. (.or (.progn (.or (.string= "january") (.string= "jan")) (.identity 1))
  278. (.progn (.or (.string= "february") (.string= "feb")) (.identity 2))
  279. (.progn (.or (.string= "march") (.string= "mar")) (.identity 3))
  280. (.progn (.or (.string= "april") (.string= "apr")) (.identity 4))
  281. (.progn (.string= "may") (.identity 5))
  282. (.progn (.or (.string= "june") (.string= "jun")) (.identity 6))
  283. (.progn (.or (.string= "july") (.string= "jul")) (.identity 7))
  284. (.progn (.or (.string= "august") (.string= "aug")) (.identity 8))
  285. (.progn (.or (.string= "september") (.string= "sep")) (.identity 9))
  286. (.progn (.or (.string= "october") (.string= "oct")) (.identity 10))
  287. (.progn (.or (.string= "november") (.string= "nov")) (.identity 11))
  288. (.progn (.or (.string= "december") (.string= "dec")) (.identity 12))))
  289. (defun week-start-date (universal-time &optional offset)
  290. (get-date (- universal-time (* +day+ (+ (nth 6 (multiple-value-list (decode-universal-time universal-time)))
  291. (- (* 7 (or offset 0))))))))
  292. (defun month-start-date (year month &optional offset)
  293. (multiple-value-bind (year-offset month) (floor (+ month (or offset 0) -1) 12)
  294. (list (+ year year-offset) (1+ month) 1)))
  295. (defun .smart-range (parser &optional duration)
  296. (.let* ((date parser))
  297. (destructuring-bind (&optional year month day) date
  298. (handler-case
  299. (let* ((ut (get-ut (or year (car (get-date (get-universal-time))))
  300. (or month 1) (or day 1)))
  301. (date (get-date ut)))
  302. (.identity (cons ut
  303. (if duration (+ ut duration)
  304. (if day (+ ut +day+)
  305. (if month (apply #'get-ut (month-start-date (car date) (cadr date) 1))
  306. (get-ut (1+ year) 1 1)))))))
  307. (error () (.fail))))))
  308. (defun .smart-date ()
  309. (let* ((now (get-universal-time))
  310. (date-now (get-date now)))
  311. (.or (.smart-range (.list (.prog1 (.number 4) (.date-delimiter))
  312. (.prog1 (.number) (.date-delimiter))
  313. (.number)))
  314. (.smart-range (.list (.prog1 (.number 4) (.date-delimiter))
  315. (.number)
  316. (.identity nil)))
  317. (.smart-range (.list (.number 4) (.identity nil) (.identity nil)))
  318. (.smart-range (.list (.identity nil)
  319. (.prog1 (.number) (.date-delimiter))
  320. (.number)))
  321. (.smart-range (.list (.identity nil) (.month) (.identity nil)))
  322. (.smart-range (.progn (.string= "today") (.identity date-now)))
  323. (.smart-range (.progn (.string= "yesterday") (.identity (get-date (- now +day+)))))
  324. (.smart-range (.progn (.string= "tomorrow") (.identity (get-date (+ now +day+)))))
  325. (.let* ((offset (.or (.progn (.string= "this") (.identity 0))
  326. (.progn (.string= "last") (.identity -1))
  327. (.progn (.string= "next") (.identity 1))
  328. (.identity 0)))
  329. (_ (.optional (.whitespace))))
  330. (.or (.smart-range (.progn (.string= "year")
  331. (.identity (list (+ (car date-now) offset) nil nil))))
  332. (.smart-range (.progn (.string= "month")
  333. (.identity (subseq (month-start-date (car date-now) (cadr date-now) offset)
  334. 0 2))))
  335. (.smart-range (.progn (.string= "week")
  336. (.identity (week-start-date now offset)))
  337. (* +day+ 7)))))))
  338. (defun .query-value-period ()
  339. (.let* ((period (.or (.let* ((start (.optional (.smart-date)))
  340. (_ (.optional (.whitespace)))
  341. (_ (.or (.char= #\-) (.string= "to")))
  342. (_ (.optional (.whitespace)))
  343. (end (.optional (.smart-date))))
  344. (if (or start end)
  345. (.identity (cons (car start) (car end)))
  346. (.fail)))
  347. (.smart-date))))
  348. (destructuring-bind (start . end) period
  349. (.identity #'(lambda (value)
  350. (and
  351. (or (not start) (>= value start))
  352. (or (not end) (< value end))))))))
  353. (defun .query-term ()
  354. (.or (.query-coloned :acct (.query-postingp "acct" #'posting-account)
  355. (.query-value-regex))
  356. (.query-coloned :amt (.progn (.string= "amt")
  357. (.identity
  358. #'(lambda (e p)
  359. (let ((amounts (get-amounts p (entry-postings e))))
  360. (if (> (length amounts) 1) :t
  361. (amount-quantity (car amounts)))))))
  362. (.query-value-amount))
  363. (.query-coloned :code (.query-entryp "code" #'entry-code)
  364. (.query-value-regex))
  365. (.query-coloned :cur (.progn (.string= "cur")
  366. (.identity
  367. #'(lambda (e p)
  368. (get-amounts p (entry-postings e)))))
  369. (.let* ((regexp (.query-value-regex)))
  370. (.identity #'(lambda (amounts)
  371. (find-if regexp amounts :key #'amount-commodity)))))
  372. (.query-coloned :desc (.query-entryp "desc" #'entry-description)
  373. (.query-value-regex))
  374. (.query-coloned :date (.query-entryp "date" #'entry-date)
  375. (.query-value-period))
  376. (.let* ((value (.query-value-regex)))
  377. (.identity (cons :acct
  378. #'(lambda (entry posting)
  379. (declare (ignore entry))
  380. (let ((account (posting-account posting)))
  381. (funcall value account))))))))
  382. (defun .arg ()
  383. (.let* ((word (.until (.chars #\' #\" #\Space #\Tab) :result-type 'string :at-least 0))
  384. (quoted (.optional (.let* ((quote (.chars #\' #\"))
  385. (text (.until (.char= quote) :result-type 'string :at-least 0))
  386. (_ (.char= quote)))
  387. (.identity text)))))
  388. (.identity (if quoted (concatenate 'string word quoted)
  389. word))))
  390. (defun make-query-predicate (terms)
  391. (let (desc acct status other)
  392. (loop for (type . f) in terms
  393. do (case type
  394. (:desc (push f desc))
  395. (:acct (push f acct))
  396. (:status (push f status))
  397. (otherwise (push f other))))
  398. #'(lambda (entry posting)
  399. (labels ((any (predicates)
  400. (or (not predicates)
  401. (find-if #'(lambda (p) (funcall p entry posting)) predicates)))
  402. (all (predicates)
  403. (not (find-if-not #'(lambda (p) (funcall p entry posting)) predicates))))
  404. (and (any desc) (any acct) (any status) (all other))))))
  405. (defun .query ()
  406. (.let* ((args (.mapcar* (.prog1 (.arg) (.optional (.whitespace))))))
  407. (loop for arg in args with terms
  408. do (multiple-value-bind (pred left)
  409. (parse (.query-term) arg)
  410. (if (and pred (input-empty-p left))
  411. (push pred terms)
  412. (return (.fail))))
  413. finally (return (.identity (make-query-predicate terms))))))