branch: externals/parser-generator
commit afa7cb9b05112f2defb44a87fea6be9f7bcac2f5
Author: Christian Johansson <[email protected]>
Commit: Christian Johansson <[email protected]>
Added unit tests for retrieving grammar RHS
---
parser.el | 12 ++++++------
test/parser-test.el | 27 +++++++++++++++++++++++++++
2 files changed, 33 insertions(+), 6 deletions(-)
diff --git a/parser.el b/parser.el
index a9a4315..f773a02 100644
--- a/parser.el
+++ b/parser.el
@@ -116,12 +116,12 @@
(dolist (p productions)
(let ((lhs (car p))
(rhs (cdr p)))
- (dolist (rhs-element rhs)
- (unless (listp rhs-element)
- (setq rhs-element (list rhs-element)))
- (let ((new-value (gethash lhs parser--table-productions)))
- (setq new-value (append new-value rhs))
- (puthash lhs new-value parser--table-productions)))))))
+ (let ((new-value (gethash lhs parser--table-productions)))
+ (dolist (rhs-element rhs)
+ (unless (listp rhs-element)
+ (setq rhs-element (list rhs-element)))
+ (push rhs-element new-value))
+ (puthash lhs (nreverse new-value) parser--table-productions))))))
(defun parser--set-look-ahead-number (k)
"Set look-ahead number K."
diff --git a/test/parser-test.el b/test/parser-test.el
index 24de34b..0b12126 100644
--- a/test/parser-test.el
+++ b/test/parser-test.el
@@ -337,15 +337,42 @@
(message "Passed tests for (parser--valid-production-p)"))
+(defun parser-test--get-grammar-rhs ()
+ "Test `parser--get-grammar-rhs'."
+ (message "Started tests for (parser--get-grammar-rhs)")
+
+ (parser--set-grammar '((S A) ("a" "b") ((S A) (A ("b" "a"))) S))
+ (should (equal
+ '((A))
+ (parser--get-grammar-rhs 'S)))
+ (should (equal
+ '(("b" "a"))
+ (parser--get-grammar-rhs 'A)))
+
+ (parser--set-grammar '((S A B) ("a" "b") ((S A) (S (B)) (B "a") (A "a") (A
("b" "a"))) S))
+ (should (equal
+ '((A) (B))
+ (parser--get-grammar-rhs 'S)))
+ (should (equal
+ '(("a") ("b" "a"))
+ (parser--get-grammar-rhs 'A)))
+
+ (message "Passed tests for (parser--get-grammar-rhs)"))
+
(defun parser-test ()
"Run test."
;; (setq debug-on-error t)
+
+ ;; Helpers
(parser-test--valid-look-ahead-number-p)
(parser-test--valid-production-p)
(parser-test--valid-grammar-p)
(parser-test--valid-sentential-form-p)
(parser-test--distinct)
(parser-test--sort-list)
+ (parser-test--get-grammar-rhs)
+
+ ;; Algorithms
(parser-test--first)
(parser-test--e-free-first)
(parser-test--follow)