Created
March 15, 2014 07:39
-
-
Save PuercoPop/9563095 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defpackage regexp | |
(:use :cl :fsm)) | |
(in-package :regexp) | |
(match-sequence "Hello") | |
;; fsm | |
(deffsm regexp () | |
()) | |
(defun match-sequence (str) | |
(let* ((states (loop | |
for i from 0 upto (1+ (length str)) | |
collect (gensym))) | |
(my-fsm (make-instance 'regexp :state (car states))) | |
(last-index (1+ (length str)))) | |
(setf (elt states last-index) (gensym "FAIL")) | |
(values my-fsm | |
(append | |
(loop | |
for char across str | |
for index from 0 upto (1+ last-index) | |
collect | |
(defstate regexp (elt states index) (fsm event) | |
(if (char= char event) | |
(elt states (1+ index)) | |
(elt states last-index)))) | |
(defstate regexp (elt states last-index) (fsm event) | |
(prog1 | |
(elt states last-index) | |
(format t "FAIL WHALE~%")))) | |
(elt states (1- last-index)) | |
(elt states last-index) | |
states))) | |
;; test case | |
(multiple-value-bind | |
(fsm methods success fail states) (match-sequence "hello") | |
(declare (ignore methods)) | |
(let ((input "hello")) | |
(map 'list | |
(lambda (c) | |
(if (eql :error (fsm:state fsm)) | |
(format t "Skipping: ~S~%" c) | |
(funcall fsm c))) | |
input) | |
(values fsm (fsm:state fsm) success fail "===" states))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment