parsing.lisp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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. (.chars #\) #\] #\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. (virtual (.optional (.chars #\[ #\()))
  166. (account (.account))
  167. (_ (.optional (.chars #\] #\))))
  168. (amount (.optional (.prog2 (.whitespace :at-least 2) (.amount) (.optional (.whitespace)))))
  169. (unit-price (.optional (.progn (.char= #\@)
  170. (.not (.char= #\@))
  171. (.optional (.whitespace))
  172. (.amount))))
  173. (_ (.optional (.whitespace)))
  174. (total-price (.optional (.progn (.string= "@@")
  175. (.optional (.whitespace))
  176. (.amount))))
  177. (_ (.optional (.whitespace)))
  178. (comment (.optional (.comment)))
  179. (_ (.eol))
  180. (comments (.mapcar* (.prog2 (.whitespace) (.comment) (.eol)))))
  181. (if (and unit-price total-price) (.fail)
  182. (.identity (make-posting
  183. :status status
  184. :virtual virtual
  185. :account account
  186. :amount amount
  187. :unit-price unit-price
  188. :total-price total-price
  189. :comment (when (or comment comments)
  190. (format nil "~{~A~^~%~}" (remove nil (list* comment comments)))))))))
  191. (defun .entry ()
  192. (.let* ((date (.simple-date))
  193. (secondary-date (.optional (.progn (.char= #\=)
  194. (.simple-date (car (get-date date))))))
  195. (status (.optional (.progn (.spaces) (.status))))
  196. (code (.optional (.progn (.spaces) (.code))))
  197. (_ (.optional (.whitespace)))
  198. (description (.optional (.description)))
  199. (_ (.optional (.whitespace)))
  200. (comment (.optional (.comment)))
  201. (_ (.eol))
  202. (comments (.mapcar* (.prog2 (.whitespace) (.comment) (.eol))))
  203. (postings (.mapcar* (.posting))))
  204. (.identity (make-entry :date date
  205. :secondary-date secondary-date
  206. :status status
  207. :code code
  208. :description description
  209. :comment (when (or comment comments)
  210. (format nil "~{~A~^~%~}" (remove nil (list* comment comments))))
  211. :postings postings))))
  212. (defun .wrap (prefix parser)
  213. (.let* ((value parser))
  214. (.identity (cons prefix value))))
  215. (defun .journal ()
  216. (.prog1
  217. (.mapcar*
  218. (.progn
  219. (.empty-lines)
  220. (.or
  221. (.wrap :entry (.entry))
  222. (.prog1
  223. (.or
  224. (.wrap :market-price
  225. (.progn (.char= #\P)
  226. (.optional (.whitespace))
  227. (.let* ((date (.simple-date))
  228. (_ (.whitespace))
  229. (commodity (.commodity))
  230. (_ (.whitespace))
  231. (unit-price (.amount)))
  232. (.identity (list date commodity unit-price)))))
  233. (.wrap :commodity
  234. (.progn (.string= "commodity")
  235. (.whitespace)
  236. (.amount)))
  237. (.wrap :default-commodity
  238. (.progn (.char= #\D)
  239. (.optional (.whitespace))
  240. (.amount)))
  241. (.wrap :default-year
  242. (.progn (.char= #\Y)
  243. (.optional (.whitespace))
  244. (.number))))
  245. (.optional (.whitespace))
  246. (.eol)))))
  247. (.empty-lines)))
  248. (defun .query-coloned (type key-parser value-parser)
  249. (.let* ((key key-parser)
  250. (_ (.char= #\:))
  251. (value value-parser))
  252. (.identity (cons type
  253. #'(lambda (entry posting)
  254. (let ((key-value (funcall key entry posting)))
  255. (or (eql key-value :t)
  256. (funcall value key-value))))))))
  257. (defun .query-entryp (prefix key)
  258. (.progn (.string= prefix) (.identity #'(lambda (e p) (declare (ignore p)) (funcall key e)))))
  259. (defun .query-postingp (prefix key)
  260. (.progn (.string= prefix) (.identity #'(lambda (e p) (declare (ignore e)) (funcall key p)))))
  261. (defun .query-value-regex ()
  262. (.let* ((regex (.until (.eol) :result-type 'string)))
  263. (handler-case
  264. (let ((matcher (cl-ppcre:create-scanner regex :case-insensitive-mode t)))
  265. (.identity #'(lambda (value)
  266. (cl-ppcre:scan matcher value))))
  267. (error ()
  268. (.fail)))))
  269. (defun .query-value-amount ()
  270. (.let* ((op (.or (.string= ">=")
  271. (.string= "<=")
  272. (.char= #\=)
  273. (.char= #\<)
  274. (.char= #\>)
  275. (.identity #\=)))
  276. (value (.number)))
  277. (.identity #'(lambda (v)
  278. (funcall (find-symbol (string op)) v value)))))
  279. (defun .month ()
  280. (.or (.progn (.or (.string= "january") (.string= "jan")) (.identity 1))
  281. (.progn (.or (.string= "february") (.string= "feb")) (.identity 2))
  282. (.progn (.or (.string= "march") (.string= "mar")) (.identity 3))
  283. (.progn (.or (.string= "april") (.string= "apr")) (.identity 4))
  284. (.progn (.string= "may") (.identity 5))
  285. (.progn (.or (.string= "june") (.string= "jun")) (.identity 6))
  286. (.progn (.or (.string= "july") (.string= "jul")) (.identity 7))
  287. (.progn (.or (.string= "august") (.string= "aug")) (.identity 8))
  288. (.progn (.or (.string= "september") (.string= "sep")) (.identity 9))
  289. (.progn (.or (.string= "october") (.string= "oct")) (.identity 10))
  290. (.progn (.or (.string= "november") (.string= "nov")) (.identity 11))
  291. (.progn (.or (.string= "december") (.string= "dec")) (.identity 12))))
  292. (defun week-start-date (universal-time &optional offset)
  293. (get-date (- universal-time (* +day+ (+ (nth 6 (multiple-value-list (decode-universal-time universal-time)))
  294. (- (* 7 (or offset 0))))))))
  295. (defun month-start-date (year month &optional offset)
  296. (multiple-value-bind (year-offset month) (floor (+ month (or offset 0) -1) 12)
  297. (list (+ year year-offset) (1+ month) 1)))
  298. (defun .smart-range (parser &optional duration)
  299. (.let* ((date parser))
  300. (destructuring-bind (&optional year month day) date
  301. (handler-case
  302. (let* ((ut (get-ut (or year (car (get-date (get-universal-time))))
  303. (or month 1) (or day 1)))
  304. (date (get-date ut)))
  305. (.identity (cons ut
  306. (if duration (+ ut duration)
  307. (if day (+ ut +day+)
  308. (if month (apply #'get-ut (month-start-date (car date) (cadr date) 1))
  309. (get-ut (1+ year) 1 1)))))))
  310. (error () (.fail))))))
  311. (defun .smart-date ()
  312. (let* ((now (get-universal-time))
  313. (date-now (get-date now)))
  314. (.or (.smart-range (.list (.prog1 (.number 4) (.date-delimiter))
  315. (.prog1 (.number) (.date-delimiter))
  316. (.number)))
  317. (.smart-range (.list (.prog1 (.number 4) (.date-delimiter))
  318. (.number)
  319. (.identity nil)))
  320. (.smart-range (.list (.number 4) (.identity nil) (.identity nil)))
  321. (.smart-range (.list (.identity nil)
  322. (.prog1 (.number) (.date-delimiter))
  323. (.number)))
  324. (.smart-range (.list (.identity nil) (.month) (.identity nil)))
  325. (.smart-range (.progn (.string= "today") (.identity date-now)))
  326. (.smart-range (.progn (.string= "yesterday") (.identity (get-date (- now +day+)))))
  327. (.smart-range (.progn (.string= "tomorrow") (.identity (get-date (+ now +day+)))))
  328. (.let* ((offset (.or (.progn (.string= "this") (.identity 0))
  329. (.progn (.string= "last") (.identity -1))
  330. (.progn (.string= "next") (.identity 1))
  331. (.identity 0)))
  332. (_ (.optional (.whitespace))))
  333. (.or (.smart-range (.progn (.string= "year")
  334. (.identity (list (+ (car date-now) offset) nil nil))))
  335. (.smart-range (.progn (.string= "month")
  336. (.identity (subseq (month-start-date (car date-now) (cadr date-now) offset)
  337. 0 2))))
  338. (.smart-range (.progn (.string= "week")
  339. (.identity (week-start-date now offset)))
  340. (* +day+ 7)))))))
  341. (defun .query-value-period ()
  342. (.let* ((period (.or (.let* ((start (.optional (.smart-date)))
  343. (_ (.optional (.whitespace)))
  344. (_ (.or (.char= #\-) (.string= "to")))
  345. (_ (.optional (.whitespace)))
  346. (end (.optional (.smart-date))))
  347. (if (or start end)
  348. (.identity (cons (car start) (car end)))
  349. (.fail)))
  350. (.smart-date))))
  351. (destructuring-bind (start . end) period
  352. (.identity #'(lambda (value)
  353. (and
  354. (or (not start) (>= value start))
  355. (or (not end) (< value end))))))))
  356. (defun .query-coloned-option (option-parser value-parser)
  357. (.let* ((option option-parser)
  358. (_ (.char= #\:))
  359. (value value-parser))
  360. (.identity (cons option value))))
  361. (defun .query-option (option)
  362. (.progn (.string= (string-downcase (symbol-name option)))
  363. (.identity option)))
  364. (defun .query-value-boolean ()
  365. (.let* ((value (.optional (.chars #\t #\f))))
  366. (.identity (ecase value
  367. ((nil #\t) :t)
  368. (#\f :f)))))
  369. (defun .query-term ()
  370. (.or (.query-coloned-option (.query-option :cost) (.query-value-boolean))
  371. (.query-coloned-option (.query-option :tree) (.query-value-boolean))
  372. (.query-coloned-option (.query-option :real) (.query-value-boolean))
  373. (.query-coloned :acct (.query-postingp "acct" #'posting-account)
  374. (.query-value-regex))
  375. (.query-coloned :amt (.progn (.string= "amt")
  376. (.identity
  377. #'(lambda (e p)
  378. (let ((amounts (get-amounts p (entry-postings e))))
  379. (if (> (length amounts) 1) :t
  380. (amount-quantity (car amounts)))))))
  381. (.query-value-amount))
  382. (.query-coloned :code (.query-entryp "code" #'entry-code)
  383. (.query-value-regex))
  384. (.query-coloned :cur (.progn (.string= "cur")
  385. (.identity
  386. #'(lambda (e p)
  387. (get-amounts p (entry-postings e)))))
  388. (.let* ((regexp (.query-value-regex)))
  389. (.identity #'(lambda (amounts)
  390. (find-if regexp amounts :key #'amount-commodity)))))
  391. (.query-coloned :desc (.query-entryp "desc" #'entry-description)
  392. (.query-value-regex))
  393. (.query-coloned :date (.query-entryp "date" #'entry-date)
  394. (.query-value-period))
  395. (.query-coloned :comment (.query-entryp "comment" #'entry-comment)
  396. (.query-value-regex))
  397. (.let* ((value (.query-value-regex)))
  398. (.identity (cons :acct
  399. #'(lambda (entry posting)
  400. (declare (ignore entry))
  401. (let ((account (posting-account posting)))
  402. (funcall value account))))))))
  403. (defun .arg ()
  404. (.let* ((word (.until (.chars #\' #\" #\Space #\Tab) :result-type 'string :at-least 0))
  405. (quoted (.optional (.let* ((quote (.chars #\' #\"))
  406. (text (.until (.char= quote) :result-type 'string :at-least 0))
  407. (_ (.char= quote)))
  408. (.identity text)))))
  409. (.identity (if quoted (concatenate 'string word quoted)
  410. word))))
  411. (defun make-options (terms)
  412. (let (options desc acct status other)
  413. (loop for (type . f) in terms
  414. do (case type
  415. (:desc (push f desc))
  416. (:comment (push f desc))
  417. (:acct (push f acct))
  418. (:status (push f status))
  419. ((:cost :real :tree) (setf (getf options type) f))
  420. (otherwise (push f other))))
  421. (append options
  422. (list :predicate
  423. #'(lambda (entry posting)
  424. (labels ((any (predicates)
  425. (or (not predicates)
  426. (find-if #'(lambda (p) (funcall p entry posting)) predicates)))
  427. (all (predicates)
  428. (not (find-if-not #'(lambda (p) (funcall p entry posting)) predicates))))
  429. (and (any desc) (any acct) (any status) (all other))))))))
  430. (defun .query ()
  431. (.let* ((args (.mapcar* (.prog1 (.arg) (.optional (.whitespace))))))
  432. (loop for arg in args with terms
  433. do (multiple-value-bind (pred left)
  434. (parse (.query-term) arg)
  435. (if (and pred (input-empty-p left))
  436. (push pred terms)
  437. (return (.fail))))
  438. finally (return (.identity (make-options terms))))))