-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
parse-it-util.el
83 lines (66 loc) · 2.7 KB
/
parse-it-util.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
;;; parse-it-util.el --- Util define -*- lexical-binding: t; -*-
;; Copyright (C) 2019-2024 Shen, Jen-Chieh <jcs090218@gmail.com>
;; This file is NOT part of GNU Emacs.
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; Util define.
;;
;;; Code:
(require 'cl-lib)
(require 'rect)
(require 'subr-x)
(require 's)
(defun parse-it-util--get-string-from-file (path)
"Return PATH file content."
(with-temp-buffer
(insert-file-contents path)
(buffer-string)))
(defun parse-it-util--get-string-from-buffer (buf-name)
"Return BUF-NAME file content."
(with-current-buffer buf-name
(buffer-string)))
(defun parse-it-util--add-to-list (lst elm)
"Append ELM to LST."
(append lst (list elm)))
(defun parse-it-util--is-contain-list-string (in-list in-str)
"Check if a string IN-STR contain in any string in the string list IN-LIST."
(cl-some #'(lambda (lb-sub-str) (string-match-p (regexp-quote lb-sub-str) in-str)) in-list))
(defun parse-it-util--remove-nth-element (nth lst)
"Remove NTH element from the LST and return the list."
(if (zerop nth)
(cdr lst)
(let ((last (nthcdr (1- nth) lst)))
(setcdr last (cddr last))
lst)))
(defun parse-it-util--print-token-list (token-list)
"Print out the TOKEN-LIST."
(dolist (token token-list)
(message "%s" token)))
(defun parse-it-util--print-ast-tree (ast-tree &optional ind st-ind)
"Print out the AST-TREE with indentation (IND) and started-indentation (ST-IND)."
(unless st-ind (setq st-ind 0))
(unless ind (setq ind 2))
(dolist (node ast-tree)
(let ((node-type (car node)) (node-val (cdr node)))
(if (listp node-val)
(progn
(if (= 0 (length node-val))
(message "%s(%s)" (spaces-string st-ind) node-type)
(message "%s(%s" (spaces-string st-ind) node-type)
(setq st-ind (+ st-ind ind))
(parse-it-util--print-ast-tree node-val ind st-ind)
(setq st-ind (- st-ind ind))
(message "%s)" (spaces-string st-ind))))
(message "%s%s" (spaces-string st-ind) node)))))
(provide 'parse-it-util)
;;; parse-it-util.el ends here